Posts

Showing posts from July, 2013

ActionBarCompat and NavigationDrawer

Image
In previous posts I talked about new ActionBarCompat.
ActionBarSherlock vs ActionBarCompatHow to add ActionBarCompat to your project
In this post we'll try to use ActionBarCompat with the NavigationDrawer.

If you have already implemented a NavigationDrawer with ActionBar, it is very easy to migrate to ActionBarCompat.
I will use code I wrote with post Creating a Navigation Drawer.
Here you can see steps:

First of all you have to add ActionBarCompat to your project.
Then:
In your Activity you have to extend ActionBarActivity.
Change getActionBar() with get getSupportActionBar().

Pay attention with invalidateOptionsMenu().
You have to change it with supportInvalidateOptionsMenu();

It is very easy!
Some last tips:
Be careful not to use attributes that work only with API 14+, like ?android:attr/listPreferredItemHeightSmall or ?android:attr/textAppearanceListItemSmall.
In this case you can use different styles using folder values-v14.

Be careful with menu width. Older devices can have very…

How to add ActionBarCompat to your project.

Image
In my previous post I've talked about a comparison between ActionBarSherlock and ActionBarCompat.

In this post we'll try to add ActionBarCompat to our project.

It is pretty simple to use and implement ActionBarCompat, but we have to pay attention when we add it to our project.
In Android Studio it is very very simple. Open build.gradle and add this:
dependencies { compile 'com.android.support:appcompat-v7:18.0.+' }
In ADT (Eclipse) you can add it as a library project based on code that you can find in sdk\extras\android\support\v7\appcompat.
It is described here. Pay attention.
In your project you have to add this library project.

Now it is very simple to review our code.
In Activity we have to extend ActionBarActivity.
If you see source code it extends a FragmentActivity. import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.support.v7.app.ActionBar; import an…

ActionBarSherlock vs ActionBarCompat

Image
Now ActionBarCompat is published and is available in the Support Library for compatibility with Android 2.1 (API level 7) and above.
Here a very little comparison between ActionBarSherlock and ActionBarCompat.

Some screenshots:

One last tip.
There is a great similarity:

Toast and duration : LENGTH_LONG and LENGTH_SHORT are FLAGS!

Image
How many times I've seen this code:
Surely I wrote it myself.
Toast.makeText (context, "My Text", 5000).show(); Pay attention!! Here you can see the doc:

This is the source:

LENGTH_LONG and LENGTH_SHORT ARE FLAGS!
Then duration is not expressed in milliseconds!
If you debug this code you can see:

And this is the central point,the NotificationManagerService:

This is the main row:
long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY); where: private static final int LONG_DELAY = 3500; // 3.5 seconds private static final int SHORT_DELAY = 2000; // 2 seconds Then, the code written in the beginning IS WRONG Toast.makeText (context, "My Text", 5000).show();< // IT IS WRONG! Toast.makeText expects duration Toast.LENGTH_SHORT or Toast.LENGTH_LONG, a custom duration value is not supported.
However I have a little doubt: in the source we can find:
This time could be user-definable.
Honestly I can't find way to do th…

Quick tips for ListView: build a layout like a chat with TranscriptMode and StackFromBottom

Image
Our purpose is to create a very simple layout with a ListView that looks like a chat.
Something like this:

It is very easy.
Let's take a common ListView in a RelativeLayout. Let's populate a simple Adapter, and run it.
public class MainActivity extends ListActivity { private ViewHolderAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAdapter = ListHelper.buildViewHolderAdapter(this, R.layout.list_item); setListAdapter(mAdapter); } }
This is still not what we wuold like to achieve.

Now we'll add an xml attribute to ListView:android:stackFromBottom="true".
In this way the ListView stacks its content from the bottom.


We'll optimize the scroll with another xml attribute:
android:transcriptMode="alwaysScroll"
In this way the list will automatically scroll to the bottom.
Ok, with t…