Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width
Looks like this is caused by the latest changes to the ActionBar
in the recent appcompat-v7
update.
It seems like that there are significant changes to how you should handle action bars.
I faced the same issue and after reading the ActionBar
documentation, and especially the following quote I found a solution.
Beginning with Android L (API level 21), the action bar may be represented by any Toolbar widget within the application layout. The application may signal to the Activity which Toolbar should be treated as the Activity's action bar. Activities that use this feature should use one of the supplied .NoActionBar themes, set the windowActionBar attribute to false or otherwise not request the window feature.
The way I see it, the AppCompat
themes were changed and on one hand seemed to break a few things but provide much more flexibility on the other.
I recommend following these steps:
- Use
.NoActionBar
style in your activity as described in the above quote - Add a
android.support.v7.widget.Toolbar
to your Activity layout - Set the
app:contentInsetStart="0dp"
attribute. This is the main issue with the margin that you describe in your question
<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/actionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp" >
</android.support.v7.widget.Toolbar>
It's usually recommended that you do that in a separate layout file and use include
in your activity layout so you will only need to customize the Toolbar in one place if used in multiple activities
<include layout="@layout/view_action_bar" />
- Use
findViewById
andsetSupportActionBar
in your ActivityonCreate
tosignal to the Activity which Toolbar should be treated as the Activity's action bar
Toolbar actionBar = (Toolbar) findViewById(R.id.actionBar);
setSupportActionBar(actionBar);
- Once you do that, all actions added in
onCreateOptionsMenu
will be added to the toolbar and it will be treated as the activity action bar. - Further customize the Toolbar as desired (Add child views etc.)
ActionBar with custom layout does not occupy full screen width on Android 4.4.2 (KitKat)
It's not a gap, it's a menu overflow button to the right. If you look closely you can see three dots button.
You've chosen the wrong theme because you can hardly see it.
Use Theme.Compat.Light.DarkActionBar
if your ActionBar is dark and you need the light theme.
On devices that don't have physical menu button (mostly new ones) the menu overflow button is shown.
You can't remove it unless you disable menus.
You can use splitActionBarWhenNarrow
uiOptions
which will split the ActionBar so that the menu part will be at the bottom one, but that will be only for vertical orientation of medium screens and probably not what you need.
Take a look at the ActionBar Patterns doc and Menus UI doc
Android ActionBar with custom layout does not occupy whole width of parent
If you want to get the height of native action bar then use ?android:attr/actionBarSize
in XML.
Now your XML will look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#fff556ff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="@+id/imgMenu"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.4"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textSize="20sp" />
<ImageView
android:id="@+id/imgUser"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imgMessage"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imgInfo"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>
Android: Custom action bar,How to use entire width?
Read this link1, link2 and set the contentInsets to 0dp like done below:
View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(mCustomView);
Toolbar toolbar=(Toolbar)mCustomView.getParent();
toolbar.setContentInsetsAbsolute(0,0);
If you are using custom Toolbar instead of ActionBar then use below code:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/action_bar_height"
android:background="?attr/colorPrimary"
android:contentInsetStart="0dp"//see this
android:contentInsetLeft="0dp"//see this
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
I Hope this might help you.
Android 4.4 Kitkat custom view actionbar not filling the whole width
The solution is adding :
actionBar.setDisplayShowTitleEnabled(false);
The code should look something like:
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.actionbar);//set the custom view
Related Topics
In Android App Toolbar.Settitle Method Has No Effect - Application Name Is Shown as Title
Adb Root Is Not Working on Emulator (Cannot Run as Root in Production Builds)
How to Set Internet Options for Android Emulator
Android: How to Overlay a Bitmap and Draw Over a Bitmap
Installation Failed Due To: 'Null' - Android Studio 3.5
How to Integrate Eclipse with Gradle in Android Project
Android Gettext from Edittext Field
Android Webview with an Embedded Youtube Video, Full Screen Button Freezes Video
How to Implement Filterable in Realmrecyclerviewadapter
How to Start/ Launch Application at Boot Time Android
Removing Log Call Using Proguard
How to Split Comma-Separated Value in SQLite
Android; Check If File Exists Without Creating a New One
Changing Background Color of Selected Item in Recyclerview
How to Securely Store Credentials (Password) in Android Application