4

I want to change the app I have created so far, in order to implement a ListView. I followed this example and this example. The examples alone work, but not together with the changes I had to make to my so-far existing app.

I have a avticity_main.xml defined as follows:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_grid"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        >

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

</android.support.design.widget.CoordinatorLayout>

and I have the following code for my main activity:

public class MainActivity extends AppCompatActivity  {
    static final String[] MOBILE_OS =
        new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};

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

        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ListView list = (ListView) findViewById(R.id.list);
        list.setAdapter(new ListAdapter(this, MOBILE_OS));

The code compiles fine, but no list is shown.

Alex
  • 41,580
  • 88
  • 260
  • 469
  • 1
    `Any ideas?` Yes: don't use ListActivity. Use ActionBarActivity (or AppCompatActivity). Simply put your ListView in a normal layout. – Phantômaxx Nov 22 '15 at 17:19
  • Can you then please provide a WORKING example on how to implement a ListView then? Maybe I looked at the wrong example! I am trying to display several items dynamically in rows. I have trying this for almost 4 hours now... – Alex Nov 22 '15 at 17:21
  • Yes, you looked at the wrong example. Or... well, you're mixing some old technology (ListActivity/ListFragment) with some new one (the ActionBars). What a ListActivity does is only to avoid the `findViewById()` part in the `onCreate()` method. But you're very limited (i.e.: only 1 ListView per Activity) – Phantômaxx Nov 22 '15 at 17:22
  • Yes probably! It is really hard to see if an example is 'good' or 'not so good'. Now, the code compiles but crashes... – Alex Nov 22 '15 at 17:24
  • Did you forget the `findViewById()` part? or did you leave the "standard" id (`"@android:id/list"`)? – Phantômaxx Nov 22 '15 at 17:25

5 Answers5

7

ListActivity is a "convenience" class to implement a simple ListView. And that's pretty much all you can do. So, there's no setSupportActionBar(Toolbar) there.

To achieve what you want, use AppCompatActivity or default Activity.

This link provides a nice tutorial on how to use the new ToolBar instead of the old ActionBar.

You want something like this:

Layout sample

This layout example could be used:

P.S.: "@color/primary" could be any color like #FF009688.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"
        android:background="@color/primary"
        android:elevation="4dp">

    </android.support.v7.widget.Toolbar>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:id="@+id/lv"></ListView>

</RelativeLayout>

And on the Java side:

YourActivity.java

public class MainActivity extends AppCompatActivity {


    private Toolbar toolbar;
    private ListView lv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        lv = (ListView) findViewById(R.id.lv);
        setSupportActionBar(toolbar);

        final ActionBar actionBar = getSupportActionBar();

        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        // Populate the ListView here...

    }

}

EDIT: This other answer of mine can show you how to create a Custom Adapter to display the data you want on the ListView.

Second edit: Use this layout

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_grid"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        android:orientation="vertical">

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

</android.support.design.widget.CoordinatorLayout>

What did I do?

Your LinearLayout was lacking the orientation parameter on the XML, and your TextView had a android:layout_height set to match_parent, so it was filling the entire layout. I've changed it to wrap_content and you're good to go.

Community
  • 1
  • 1
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • But how to include a ListView in that activity and a toolbar? – Alex Nov 22 '15 at 17:25
  • I want something like you have posted, but actually each row consisting of multiple items: a name, a number, a number again, a small clickable icon... – Alex Nov 22 '15 at 17:39
  • Maybe then I again need something completly different? – Alex Nov 22 '15 at 17:40
  • I have my code in principle like your example. Please see my activity xml above... – Alex Nov 22 '15 at 17:42
  • You'll have to create a Custom Adapter for your `ListView` to display such a thing. Check the linked answer on how to do that, and don't use ListViewActivity ;) – Mauker Nov 22 '15 at 17:45
  • I am using an adapter, as shown in this example: http://www.mkyong.com/android/android-listview-example/ – Alex Nov 22 '15 at 17:47
  • So what problem are you still having? – Mauker Nov 22 '15 at 17:48
  • So far, things are good again (well, like they have been 4 hours ago). What is not working right now is that the ListView is empty! The text "MainTitle" is shown, but nothing else. This is the problem I have... – Alex Nov 22 '15 at 17:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95858/discussion-between-mauker-and-alex). – Mauker Nov 22 '15 at 17:49
4

setSupportActionBar(toolbar) is not available in ListActivity. You can use AppCompatActivity instead and just get a reference to the listview: ListView list = findViewById(R.id.list); and use list.setAdapter(adapter) instead of setListAdapter(adapter);

jomartigcal
  • 813
  • 1
  • 6
  • 11
  • I used AppCompatActivity before! But how to have a ListView then? – Alex Nov 22 '15 at 17:22
  • ListView list = findViewById(R.id.list); and use list.setAdapter(adapter) instead of setListAdapter(adapter); – jomartigcal Nov 22 '15 at 17:26
  • Hmm, code compiles and does not crash, but I do not see a list of items. Maybe a issue with the layout xml file? – Alex Nov 22 '15 at 17:37
1

You can't do that in ListActivity.

If you want to access getSupportActionBar(), you need to extent your class by AppCompatActivity.

My Suggestion : Don`t use ListActivty is you want to use ToolBar. Create an Activity and then only have ListView within that Activity. It'll work just fine.

0

You can use the new AppCompatDelegate component provided by the Support Library to easily add a Toolbar to your ListActivity.

Follow this link,

How to add Toolbar to an Activity which doesn't extend AppCompatActivity

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
0

OR

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);

// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // Handle the menu item
        return true;
    }
});

// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);

}`