Wednesday, March 27, 2013

Android: How to compare Drawables


Answer:

  drawable1.getConstantState().equals(drawable2.getConstantState());

It doesn't compare actual data (bytes of images) so it is fast and memory efficient.

Monday, March 25, 2013

Android: How to use Android default font size example

Small:
<style name="DefaultMedium">

<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>

</style>


Medium:

<style name="DefaultMedium">

<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>

</style>

Large:

<style name="DefaultMedium">

<item name="android:textAppearance">?android:attr/textAppearanceLarge</item>

</style>

Reference: Android source code

Android Styles.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml

Android Res files
https://github.com/android/platform_frameworks_base/tree/master/core/res/res

Friday, March 22, 2013

Android: Full Screen & No title/action bar example

No Action bar:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Caution: this code has to be written BEFORE setContentView(...); or it will crash!

Full Screen:
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.layout_main);

}

Android: Question mark only in strings.xml resource example

Use backslash:

    <string name="quick_question">\?</string>

Thursday, March 21, 2013

Android: Get screen width programmatically example

It returns number in pixel





      @SuppressLint("NewApi")
      private int getScreenWidth() {
            int iMeasuredwidth = 0;
            Point size = new Point();
            WindowManager w = getWindowManager();
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
                  w.getDefaultDisplay().getSize(size);
                  iMeasuredwidth = size.x;
            }else{
                  Display d = w.getDefaultDisplay();
                  iMeasuredwidth = d.getWidth();
            }
            return iMeasuredwidth;
      }


Android: How to convert from dp to px? (example)

From dp to pixel:
 
private static int fromDp2Px(int dp, Context context) {
final Resources res = context.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
return (int) px;
}
 
or
 
private static int fromDp2Px(int dp, Context context) {
final Resources res = context.getResources();
DisplayMetrics metrics = res.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
}
 
 
 
 
From pixel to dp:
 
private static int fromPx2Dp(int px, Context context) {
final Resources res = context.getResources();
DisplayMetrics metrics = res.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return (int) dp;
}