Adding actionbar to listactivity
Asked Answered
C

4

11

Hello so I created a list and I want to add action bar. I am quite new to android so I would like to know how to add action bar while using ListActivity. Any help will be appreciated. Thanks My code:

     public class MainActivity extends ListActivity {

     ArrayList<Item> items = new ArrayList<Item>();

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


        items.add(new SectionItem("2x2 Matrices"));
        items.add(new EntryItem("Adding 2 Matrices"));
        items.add(new EntryItem("Subtracting 2 Matrices"));
        items.add(new EntryItem("Multiplying 2 Matrices"));
        items.add(new EntryItem("Multiplying by a constant"));
        items.add(new EntryItem("Dividing 2 Matrices"));
        items.add(new EntryItem("Negative of a Matrix"));
        items.add(new EntryItem("Inverse of a Matrix"));
        items.add(new EntryItem("Determinant of a Matrix"));

        /*Section2*/
        items.add(new SectionItem("3x3 Matrices"));
        items.add(new EntryItem("Item 4"));
        items.add(new EntryItem("Item 5"));
        items.add(new EntryItem("Item 6"));
        items.add(new EntryItem("Item 7"));
        /*Section3*/
        items.add(new SectionItem("Category 3"));
        items.add(new EntryItem("Item 8"));
        items.add(new EntryItem("Item 9"));
        items.add(new EntryItem("Item 10"));
        items.add(new EntryItem("Item 11"));
        items.add(new EntryItem("Item 12"));

        EntryAdapter adapter = new EntryAdapter(this, items);

        setListAdapter(adapter);
    }

}
Countershading answered 30/8, 2013 at 11:55 Comment(0)
J
5

Then in your activity's onCreateOptionsMenu() method, inflate the menu resource into the given Menu to add each item to the action bar:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

More info for action bar

Jeanene answered 30/8, 2013 at 12:15 Comment(1)
This is not working for me on Android 4.4.2. I am using a ListActivity with style android:Theme.Holo.Light.DarkActionBar.Delossantos
T
14

First- Make sure your Android minimum API-14 or later.

Then, add android:theme="@android:style/Theme.Holo.Light.DarkActionBar" under your ListView_Activity in AndroidManifest.xml class.

Example

        <activity android:name=".Your_ListView_Activity"
                  android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
                  android:label="ListView_Activity_Label">
Thelma answered 19/4, 2015 at 14:23 Comment(1)
This worked for me, but I had a menu item that would open a dialog window, which no longer works due to the theme does not extend from appcompat, is there a way around this?Formalize
T
9

You can use Holo Themes, you need only in this screen?

In Android manifest:

For only one screen, put atribute theme, like this:

<activity
android:theme="@android:style/Theme.Holo.Light.DarkActionBar">
</activity>

For all screen, put atribute theme in application tag.

<application
android:theme="@style/My_Theme" >

Also you can make a custom theme based on Holo Light Theme.

Ex:

android:theme="@style/My_Theme" >

In styles.xml

<style name="My_Theme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
Tiresome answered 30/8, 2013 at 12:19 Comment(0)
K
7

Here is a good way:

In your layout file: activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

Now for your activity:

public class MainActivity extends ActionBarActivity
{
    private ListView listView;
    private ListAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        myAdapter = new ListAdapter(getApplicationContext());
        listView.setAdapter(myAdapter);

Good Luck!

Karlee answered 2/7, 2015 at 21:17 Comment(2)
I don't think myAdapter = new ListAdapter(getApplicationContext()); works because you can't instantiate the abstract ListAdapter class.Reborn
Actually ListAdapter is a customized class that implements BaseAdapter. Here is the signature "public class ListAdapter extends BaseAdapter" So you can create an object from ListAdapter class and this object has the functionalities of BaseAdapter + your custom methodsKarlee
J
5

Then in your activity's onCreateOptionsMenu() method, inflate the menu resource into the given Menu to add each item to the action bar:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

More info for action bar

Jeanene answered 30/8, 2013 at 12:15 Comment(1)
This is not working for me on Android 4.4.2. I am using a ListActivity with style android:Theme.Holo.Light.DarkActionBar.Delossantos

© 2022 - 2024 — McMap. All rights reserved.