Monday, 31 August 2015

Material Design Library

source: https://github.com/DenisMondon/material-design-library

Material Design Library

Description

This library helps developers creating their Android Application with Material Design.
It offers a lot of Material Design classes easy to use like a CardView or a NavigationDrawerActivity that creates an Activity with a Material Design NavigationDrawer.

What's new

  • SearchBar in the ActionBar (can be used with any Activity)
  • SearchBar Auto Completion
  • NavigationDrawer Small Accounts Layout (to show only the current account)

Demo

Material Design Library on Google Play Store
NavigationDrawer with Accounts NavigationDrawer with Small Accounts Layout CardView with Image on the Top SearchBar with Auto Completion ListView with Pull To Refresh ViewPager with Tabs

Wiki

Usage

1. Add the dependency in your build.gradle.
dependencies {
    compile 'com.blunderer:materialdesignlibrary:2.0.4'
}
2. In your values/styles.xml file, change the parent style and add your owncolorPrimary/colorPrimaryDark colors:
<style name="AppTheme" parent="@style/MaterialDesignLibraryTheme">
    <item name="colorPrimary">#3f51b5</item>
    <item name="colorPrimaryDark">#303f9f</item>
</style>
Or if you want the Light Theme:
<style name="AppTheme" parent="@style/MaterialDesignLibraryTheme.Light">
    <item name="colorPrimary">#3f51b5</item>
    <item name="colorPrimaryDark">#303f9f</item>
</style>
3. Then you can:
- Extend your activity by one of mine (each activity will be in Material Design):
  • Activity
    Your Activity will be a basic Activity with Material Design.
  • ListViewActivity
    Your Activity will contain a ListView (with or not the Pull To Refresh).
  • ScrollViewActivity
    Your Activity will contain a ScrollView (with or not the Pull To Refresh).
  • NavigationDrawerActivity
    Your Activity will contain a NavigationDrawer (with or not Accounts).
  • ViewPagerActivity
    Your Activity will contain a ViewPager (with or not the indicator).
  • ViewPagerWithTabsActivity
    Your Activity will contain a ViewPager with tabs (expanded or not).
Example:
import com.blunderer.materialdesignlibrary.activities.NavigationDrawerActivity;

public class MyActivity extends NavigationDrawerActivity {

    @Override
    public NavigationDrawerStyleHandler getNavigationDrawerStyleHandler() {
        return new NavigationDrawerStyleHandler();
    }

    @Override
    public NavigationDrawerAccountsHandler getNavigationDrawerAccountsHandler() {
        return new NavigationDrawerAccountsHandler()
                .addAccount("Profile 1", "profile1@gmail.com",
                        R.drawable.profile1, R.drawable.profile1_background)
                .addAccount("Profile 2", "profile2@gmail.com",
                        R.drawable.profile2, R.drawable.profile2_background);
    }

    @Override
    public NavigationDrawerAccountsMenuHandler getNavigationDrawerAccountsMenuHandler() {
        return new NavigationDrawerAccountsMenuHandler(this)
                .addAddAccount(new Intent(getApplicationContext(), AddAccountActivity.class))
                .addManageAccounts(new Intent(getApplicationContext(), ManageAccountsActivity.class));
    }

    @Override
    public void onNavigationDrawerAccountChange(Account account) {
        Toast.makeText(getApplicationContext(), "Account changed!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public NavigationDrawerTopHandler getNavigationDrawerTopHandler() {
        return new NavigationDrawerTopHandler()
                .addItem(R.string.title_item1, new MainFragment())
                .addItem(R.string.title_item2, new MainFragment())
                .addSection(R.string.title_section2)
                .addItem(R.string.title_item3, new MainFragment())
                .addItem(R.string.title_item4, new Intent(this, MyActivity.class));
    }

    @Override
    public NavigationDrawerBottomHandler getNavigationDrawerBottomHandler() {
        return new NavigationDrawerBottomHandler()
                .addSettings(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                        startActivity(intent);
                    }

                })
                .addHelpAndFeedback(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getApplicationContext(), HelpAndFeedbackActivity.class);
                        startActivity(intent);
                    }

                });
    }

    @Override
    public boolean overlayActionBar() {
        return true;
    }

    @Override
    public boolean replaceActionBarTitleByNavigationDrawerItemTitle() {
        return true;
    }

    @Override
    public int defaultNavigationDrawerItemSelectedPosition() {
        return 0;
    }

    @Override
    protected boolean enableActionBarShadow() {
        return true;
    }

    @Override
    protected ActionBarHandler getActionBarHandler() {
        return new ActionBarDefaultHandler(this);
    }

}
- Or use my CardView:
  • Normal CardView
    Your CardView will be a basic CardView.
  • Left Image CardView
    Your CardView will contain an image on the left.
  • Top Image CardView
    Your CardView will contain an image on the top.
Example:
<com.blunderer.materialdesignlibrary.views.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:mdl_title="CardView"
        app:mdl_description="A Left Image CardView"
        app:mdl_normalButton="Normal"
        app:mdl_highlightButton="Highlight"
        app:mdl_imagePosition="left"
        app:mdl_image="@drawable/image" />

Developed by

How to make app auto run in background daily to fetch data from internet?

How to make app auto run in background daily to fetch data from internet?


I am developing a web crawler using Android. Currently my crawler crawls the web using async task every time the app starts, which takes a long time.
I want my app to update its database daily in the background, without needing to launch the MainActivity interface. After doing some research, I found the following classes can help me:
  1. Service
  2. AlarmManager
Can someone shed some light for me, I'm new to Android programming. Thanks



source: http://stackoverflow.com/questions/4459058/alarm-manager-example

This is working code. It wakes CPU every 10 minutes until the phone turns off.
Add to Manifest.xml:
...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver android:process=":remote" android:name=".Alarm"></receiver>
...
Code in your class:
package YourPackage; 
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;
 
public class Alarm extends BroadcastReceiver 
{     
    @Override 
    public void onReceive(Context context, Intent intent) 
    {    
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();
 
        // Put here YOUR code. 
        Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
 
        wl.release();
    } 
 
    public void SetAlarm(Context context)
    { 
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
    } 
 
    public void CancelAlarm(Context context)
    { 
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    } 
} 
Set Alarm from Service:
package YourPackage; 
 
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
 
public class YourService extends Service
{ 
    Alarm alarm = new Alarm();
    public void onCreate() 
    { 
        super.onCreate();        
    } 
 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
        alarm.SetAlarm(this);
        return START_STICKY;
    } 
 
   @Override         
   public void onStart(Intent intent, int startId)
    { 
        alarm.SetAlarm(this);
    } 
 
    @Override 
    public IBinder onBind(Intent intent) 
    { 
        return null; 
    } 
} 
If you want set alarm repeating at phone boot time:
Add permission to Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
...
And create new class:
package YourPackage; 
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
 
public class AutoStart extends BroadcastReceiver
{    
    Alarm alarm = new Alarm();
    @Override 
    public void onReceive(Context context, Intent intent)
    {    
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
        { 
            alarm.SetAlarm(context);
        } 
    } 
} 
shareedit
1 
How this setAlarm method will be called. If i would like to call it from service class ? Is it automatically called? –  Srizan Sagar Jan 20 '12 at 4:26
14 
This is very helpful, but a few things: 1. It might be better to use am.setInexactRepeating(...) so the phone isn't needlessly woken up because of the service. Other programmers should take note of this fact. 2. Instead of creating a new Alarm in AutoStart upon receiving theRECEIVE_BOOT_COMPLETED intent, it might make more sense to start YourService fromAutoStart, as shown here: stackoverflow.com/a/5439320/198348 –  Ehtesh Choudhury Jun 6 '12 at 17:31 
4 
I think acquiring the lock on the onReceive method is not mandatory since android will do it for you. See here: developer.android.com/reference/android/app/AlarmManager.html –  Ran Jun 13 '13 at 6:08
7 
WakeLock not needed in a BroadcastReceiver. Android uses its own one util broadcastReceiver finishes. – Jorge Fuentes Gonz├ílez Jul 2 '13 at 10:26
3 
Great answer, but is better extend from WakefulBroadcastReceiver instead of BroadcastReceiver, WakefulBroadcastReceiver manages the wake lock itself. See link for more info. –  icastell Mar 27 '14 at 8:28 

I tried the solution from XXX and while it did initially work, at some point it stopped working. The onReceive never got called again. I spent hours trying to figure out what it could be. What I came to realize is that the Intent for whatever mysterious reason was no longer being called. To get around this, I discovered that you really do need to specify an action for the receiver in the manifest. Example:
<receiver android:name=".Alarm" android:exported="true">
    <intent-filter>
        <action android:name="mypackage.START_ALARM" >
        </action>
    </intent-filter>
</receiver> 
Note that the name is ".Alarm" with the period. In XXX's SetAlarm method, create the Intent as follows:
Intent i = new Intent("mypackage.START_ALARM");
The START_ALARM message can be whatever you want it to be. I just gave it that name for demonstration purposes.
I have not seen receivers defined in the manifest without an intent filter that specifies the action. Creating them the way XXX has specified it seems kind of bogus. By specifying the action name, Android will be forced to create an instance of the BroadcastReceiver using the class that corresponds to the action. If you rely upon context, be aware that Android has several different objects that are ALL called context and may not result in getting your BroadcastReceiver created. Forcing Android to create an instance of your class using only the action message is far better than relying upon some iffy context that may never work.
shareedit