1

I have created an activity which extends ListActivity I want to setup Toolbar into my activity which requires to extends AppCompatActivity. Is there any way like fragments which imports AppCompatActivity like this

((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
Savita
  • 747
  • 1
  • 7
  • 16

3 Answers3

2

Use a ListView in your onCreate inside your AppCompatActivity. You cannot use the Toolbar with ListActivity.

In your onCreate

listView = (ListView) findViewById(R.id.list_view); 

mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, mListItems);
Quinn Turner
  • 232
  • 1
  • 2
  • 11
  • Thanks for your answer! Can you please tell me how can I change the Text color in ListFragment or ListActivity ? – Savita Mar 18 '16 at 13:36
  • The easiest way is to set it up in the XML. I believe you can use a TextView element: android:textColor="@color/abc_primary_text_material_dark". More info please check this link: http://stackoverflow.com/questions/4533440/android-listview-text-color – Quinn Turner Mar 18 '16 at 13:40
2

ListeActivity extends Activity, so you can't cast to AppCompatActivity.

Two alternatives would be to use ListFragment instead and put that into an AppCompatActivity. Or define your own ListAppCompatActivity class that includes a layout with a ListView with the id android:id/list

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

ListActivity has a default ListView in it which can be referenced by getListView() method.

You can only have a ToolBar in Android by extending AppCompatActivity. ActionBarActivity has a ActionBar which is deprecated in since version 22.1.0.

So I insist you to go with AppCompatActivity for setting ToolBar amd inside that Activity you can have a listview.

kevz
  • 2,727
  • 14
  • 39