Custom support.v7.widget.Toolbar not showing on device

If I understand correctly, the purpose of this widget is to make the toolbar compatible with older versions of android (for example, 4.x), but for some reason, when I launch my application on the device itself using android 4.x, I can’t see my toolbar. And in the emulator with Android 6 everything is in order.

I use @ style / Theme.AppCompat.Light.NoActionBar in my topic, and my activity extends AppCompatActivity .

This is what my toolbar looks like:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> 

Included in my xml activity:

 <include android:id="@+id/tool_bar" layout="@layout/actionbar_main"></include> 

And added to the operation code to create:

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

Any hint on why this is not showing up on android 4.x?

+5
source share
2 answers

Please try this.

Change your xml layout as.

 <?xml version="1.0" encoding="utf-8"?> <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=".HomeActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 
+2
source

You select a nonexistent toolbar identifier. Placed:

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

instead.

+2
source

Source: https://habr.com/ru/post/1235260/


All Articles