rage
rage

Reputation: 61

Adding action bar in ListActivity

I'm very newbie in Android. I need some help and your suggestion here.
I'm work in Android starting last january, I have a some trouble here. I'm still working on app for Android 2.3. Very very simple app. I've read about the action bar, and I've suggest to the library such as greendroid, sherlock actionbar and the last Johan Nilson actionbar. I'm still have difficulties to integrate it into my app. So I try to create my own action bar and the trouble came one by one. One of: it's hard to combine the action bar with my ListView to still stay on the top of screen even the list view scroll down. At last the layout of action bar generate into header of ListView:

View header = getLayoutInflater().inflate(R.layout.header, null);
ListView listView = getListView();
listView.addHeaderView(header);

and the button & imagebutton not working for click event..

//try action button in actionbar
Menu1 = (Button) this.findViewById(id.button1);
Menu1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v)
    {
        Toast.makeText(List_direktoriJson.this, "action bar..", Toast.LENGTH_LONG).show();          
    }
});

How the way, if I want the action bar still on top in list view layout? And why the button and imagebutton not working when clicking?

Upvotes: 5

Views: 4803

Answers (2)

kmb64
kmb64

Reputation: 1513

If you can manage to include one of the Action Bar libraries such as Sherlock, it will be much simpler :

    private class YourListActivity extends SherlockListActivity {

}

Upvotes: 2

Sergey Glotov
Sergey Glotov

Reputation: 20356

Why are you adding action bar to the ListView header? If you do this, action bar will always scroll with the ListView. Add action bar to the layout of your ListActivity. Something like:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <include 
        android:id="@+id/action_bar"
        layout="@layout/header" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent" 
        android:layout_height="match_parent" />

    <!-- May be some other views -->

</merge>

Then in onCreate() set content view of the activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_of_the_activity);
    setListAdapter(...);

    // get action bar view
    View header = findViewById(R.id.action_bar);

    // and then set click listeners. Note that findViewById called on header, not on this
    Menu1 = (Button) header.findViewById(R.id.button1);
    Menu1.setOnClickListener(...);
}

Article about <merge> and <include> tags.

Upvotes: 0

Related Questions