2

enter image description here

I am having difficulty with showing my Action bar that is suppose to be at the top. Everytime I use a ListView, the ActionBar with all my menu options on top disappears on me. Can someone shed some light on why this happening with the ListView.

MainActivity Class:

public class MainActivity extends Activity {

    ListView listView;
    ArrayAdapter<String> adapter;

    String[] android_versions = {"Cupcake",
            "Donut",
            "Eclair",
            "Froyo",
            "Gingerbread",
            "Honeycomb",
            "Ice Cream Sandwich",
            "Jelly Bean",
            "KitKat",
            "Lollipop"};

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

        listView = (ListView) findViewById(R.id.listView);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android_versions);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.farrellequipment.www.listview" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Layout activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_gravity="left|top" />
</RelativeLayout>

Menu menu_main.xml file

<menu 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" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

Styles.xml file

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

It is a simple straight forward app but I am missing the ActionBar and Menu Options I need for this app. Again can someone please shed some light on the problem. I have been searching this problem for about 4 hours now and I need help. Thank you in advance.

Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60

3 Answers3

1

You just need to replace Activity to AppCompatActivity

public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayAdapter<String> adapter;

String[] android_versions = {"Cupcake",
        "Donut",
        "Eclair",
        "Froyo",
        "Gingerbread",
        "Honeycomb",
        "Ice Cream Sandwich",
        "Jelly Bean",
        "KitKat",
        "Lollipop"};

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

    listView = (ListView) findViewById(R.id.listView);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android_versions);
    listView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Deepak S. Gavkar
  • 447
  • 8
  • 21
  • I extended the java class with AppCompactActivity instead of the plain Activity. It worked like a charm. Thank you for your help and everyone else that answered my question. – Lostinspirit3000 Sep 01 '15 at 18:40
0

Your @style/AppTheme probably extends a .NoActionBar theme, make sure that's not the case.

maciekjanusz
  • 4,702
  • 25
  • 36
0

You should inherit your Activity from AppCompatActivity (https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html) from the Support Library instead from the plain Acitivty class.

You should also inherit your app theme from Theme.AppCompat.

See also Difference between extending LifecycleActivity,Activity,ActionbarActivity & AppCompactActivity?

Community
  • 1
  • 1
Steffen H
  • 116
  • 5