No visible icons in action bar (Android programming)

I searched the forums and found that using the app: showAsAction is a recommendation for my question. However, I am using app: showAsAction and still cannot display my icons in the action bar. ALSO, I have to set the title of the actionbar manually in my Main.java because when using the xml, the title is not shown in the actionbar. QUESTION: How do I display my icons in the action bar and why can't I set the title of the action bar in xml? Are these questions related? Here is my code:

Basic XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000"

    tools:context="example.com.listview.MainActivity">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.v7.widget.Toolbar
            android:id="@+id/myToolbar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="#8380"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:titleTextColor="@android:color/holo_green_light"
            app:titleTextColor="@android:color/holo_green_light"
            />
    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:id="@+id/my_recycler_view"/>




</LinearLayout>

      

Basic JAVA:

package example.com.listview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;

public class MainActivity extends Activity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    Repository myDataset;


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

        Toolbar myToolbar = (Toolbar) findViewById(R.id.myToolbar);
        myToolbar.setTitle("ListView");

        myDataset = new Repository();
        myDataset.addCause("Happy");
        myDataset.addCause("Sad");
        myDataset.addCause("Love");

        myDataset.causes.get(0).addOccurrence();
        myDataset.causes.get(0).addOccurrence();
        myDataset.causes.get(1).addOccurrence();
        myDataset.causes.get(2).addOccurrence();

       recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
       recyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter(myDataset);
        recyclerView.setAdapter(mAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

      

Main menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/addCause"
        android:title="Add Cause"
        android:orderInCategory="0"
        android:icon="@drawable/plussign"
        app:showAsAction="never"/>

    <item android:id="@+id/removeCause"
        android:orderInCategory="1"
        android:title="Remove Cause"
        app:showAsAction="never"
        android:icon="@drawable/minussign"/>

</menu>

      

manifest:

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

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

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

</manifest>

      

+3


source to share


1 answer


How to make my icons appear in the action bar

  • Expand with AppCompatActivity

    , notActivity

  • Bring the toolbar up to Activity by calling setSupportActionBar(myToolbar);



why can't i set title of action bar in xml

Adding app:title="My Title"

to your XML should work.

+1


source







All Articles