miércoles, 26 de noviembre de 2014

Android: Text sizes (TextAppearance)


<style name="TextAppearance.Large">
    <item name="android:textSize">22sp</item>
</style>

<style name="TextAppearance.Medium">
    <item name="android:textSize">18sp</item>
</style>

<style name="TextAppearance.Small">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">?textColorSecondary</item>
</style>

Android: Android 5

If you set the build target of your project to 21 (Android 5) your application might stop compiling that is because Android 5 uses Java 1.7 whereas the rest uses 1.6.

martes, 25 de noviembre de 2014

Android: Menu

On a fragment (menu from scratch)
public class MyFragment extends Fragment {
    
@Override
 public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
        
     setHasOptionsMenu(true);
}
...

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        
        inflater.inflate(R.menu.menu_main, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Handle the clicked menu option here
        }
    }
On an Activity (menu from scratch)
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        this.getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Handle the clicked menu option here

        }
    }

lunes, 17 de noviembre de 2014

Skype: Account is logged permanently

When you notice you never log out from your account, try the following:

In any chat window type:  /showplaces

This command will display all the devices where you are logged in.

If you type: /remotelogout

It will log you out from all those devices but the current one.

jueves, 13 de noviembre de 2014

miércoles, 12 de noviembre de 2014

Android: Gotcha ListView

1) If you have a focusable view in your Layout of your list item the onItemClickListener won't get called. You can fix this issue by adding a "android:descendantFocusability="blocksDescendants"" to your list item layout container.

jueves, 6 de noviembre de 2014

Android: Activity orientation

Create a base class from which your activities will inherit
public class AbstractActivity extends Activity {
 
 ...
 
 @Override
 protected void onCreate(Bundle arg0) {

     super.onCreate(arg0);
     
     setOrientationAccordingToDevice();
 }

 ...

 protected void setOrientationAccordingToDevice() {

        if (ScreenHelper.isDeviceATablet(this)) {
            // Since it is a tablet it will have an api > 8. So this is safe.
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }
        else {
            // How do I implement the SCREEN_ORIENTATION_SENSOR_PORTRAIT functionality previous to api 9?
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}
ScreenHelper
public class ScreenHelper {

 public static boolean isDeviceATablet(Activity activity) {

  DisplayMetrics metrics = new DisplayMetrics();
  activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

  int widthPixels = metrics.widthPixels;
  int heightPixels = metrics.heightPixels;

  float scaleFactor = metrics.density;

  float widthDp = widthPixels / scaleFactor;
  float heightDp = heightPixels / scaleFactor;

  float smallestWidth = Math.min(widthDp, heightDp);

  return (smallestWidth >= 600);
 }
 
 public static void hideSoftKeyboard(Activity activity, View view) {

  InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
 }
}