M
M
Masteron2016-02-23 18:06:30
Java
Masteron, 2016-02-23 18:06:30

android and toolbar?

I know that the question is not quite right, but anyway:
There is a toolbar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar_actionbar"
    android:layout_width="match_parent"
    app:theme="@style/AppTheme"
    android:minHeight="?attr/actionBarSize"
    android:layout_height="wrap_content"
    android:background="@color/primary">

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

Well, there is a menu (that is, where the item's are located)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/search_bar"
        android:title="@string/search"
        android:orderInCategory="200"
        android:showAsAction="always"
        android:icon="@drawable/ic_search">
    </item>
    <item
        android:id="@+id/navigation_bar"
        android:title="@string/navigation"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/ic_nv_back">
    </item>

</menu>

And here is the activity code:
public class EnterActivity extends AppCompatActivity {
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.inflateMenu(R.menu.menu);
    }
}

As a result, when the application starts, there are no icons or text, except that if we click on the setting (which appears by default), a list will appear in which there is the text that I specified in item.
What did I do wrong and how can I fix it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Tiberal, 2016-02-24
@Tiberal

setActionBar(toolbar);

K
Konstantin Dovnar, 2016-02-24
@SolidlSnake

I would recommend inflating the menu in the Activity in the standard way, through onCreateOptionsMenu and onOptionsItemSelected , and then set your Toolbar as an ActionBar when creating the activity.

//Activity
setSupportActionBar((Toolbar) view.findViewById(R.id.toolbar));
//Fragment
((AppCompatActivity) getActivity()).setSupportActionBar((Toolbar) view.findViewById(R.id.toolbar));

E
Elysey, 2016-02-23
@Elysey

Try like this:

<?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/search_bar"
        android:title="@string/search"
        android:orderInCategory="200"
        app:showAsAction="always"
        android:icon="@drawable/ic_search">
    </item>
    <item
        android:id="@+id/navigation_bar"
        android:title="@string/navigation"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:icon="@drawable/ic_nv_back">
    </item>
</menu>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question