Tuesday, December 8, 2015

onBackPressed add double tap exit

  • Android not provided double tap supported events so i have generated manually using handler.
  • handler provided using postDelayed() method  to delay post to time set by developer.
  • delay time set in milliseconds and 1000 milliseconds == 1 second. 


public class MainActivity extends Acitivity{

int count =0;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

   }

  @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if (count == 0)
            count = 1;
        else
            count = 2;
        android.os.Handler h = new android.os.Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (count == 2) {

                    this.finish();
                   // Just Put Your Code for Exit
                    Log.d("DoubleTap", "Exit");
                }
                count= 0;
            }
        }, 500);//You can set double tap interval, currently set half second

        //   super.onBackPressed();
    }

}

Thursday, December 3, 2015

Change Status bar color for your application

 




Note:- this feature is supported on api level 19 or 21 and above api level.

Step 1:- First make foloder in res and give name values-v19 or values-v21 and create style.xml in this folder and put following code is style.xml.

<style name="MyMaterialThemeWithMargin" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimaryDark">#0973AE</item>
        <item name="android:navigationMode">tabMode</item>
        <item name="android:layout_marginTop">30dp</item>
</style>

Hear "colorPrimaryDark" is set status bar color.
Step 2:- Now this style is set in Your layout by theme attributes like following code and Run Your Project.

 <RelativeLayout
        android:id="@+id/ll_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:theme="@style/MyMaterialThemeWithMargin"
        android:background="@color/homeTitleColor">
</RelativeLayout