Reputation: 3367
I have an action bar that has a custom view but for some reason, when I add the custom view onto the action bar, my actionBar
label that I declared in my AndroidManifest.xml
is not showing. Here is my attempt:
AndroidManifest.xml:
<activity android:name=".com.tabs.activity.CreatePost"
android:label="Create Post"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateVisible">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".com.tabs.activity.news_feed"/>
</activity>
Action bar layout: create_post_menu.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".CreatePost">
<item
android:id="@+id/privacy_toggle"
android:title=""
app:showAsAction="always"
android:orderInCategory="1"
android:actionLayout="@layout/privacy_toggle_layout" />
<item
android:id="@+id/send_post"
android:orderInCategory="2"
android:title="Send"
app:showAsAction="always" />
</menu>
privacy_toggle_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioGroup
android:checkedButton="@+id/offer"
android:id="@+id/privacy_toggle"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:background="@drawable/pick_out_line"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<RadioButton
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:id="@+id/public_toggle"
android:background="@drawable/toggle_widget_background"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="Public"
android:textColor="@color/white" />
<RadioButton
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:id="@+id/private_toggle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/toggle_widget_background"
android:button="@null"
android:gravity="center"
android:text="Private"
android:checked="true"
android:textColor="@color/white" />
</RadioGroup>
</RelativeLayout>
CreatePost.java (I am just showing how I instantiate the action bar)
private void setupActionBar(){
Toolbar toolbar = (Toolbar) findViewById(R.id.create_post_toolbar);
setSupportActionBar(toolbar);
//Back bar enabled
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//Toggle bar enabled
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.privacy_toggle_layout);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
final RadioGroup privacyToggle = (RadioGroup) findViewById(R.id.privacy_toggle);
final RadioButton publicToggle = (RadioButton) findViewById(R.id.public_toggle);
final RadioButton privateToggle = (RadioButton) findViewById(R.id.private_toggle);
privateToggle.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
//Set listener for clicking on toggle
privacyToggle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.public_toggle){
System.out.println("Toggled public");
}
else {
System.out.println("Toggled private");
}
}
});
}
Even if I have ActionBar.DISPLAY_SHOW_TITLE
, the label
declared in the AndroidManifest.xml is not showing, and I also don't have any padding/margins added anywhere that might deter the label from showing. Here is a photo to show what is happening. Any help is appreciated, thanks]1
Upvotes: 2
Views: 1208
Reputation: 2591
As noted in comments above, this link -
Toolbar title with custom view
discusses ways to retain your title and add a custom view to your existing toolbar declaration.
The main thing to note in order to retain the title is that you must make sure the custom view is not in the way / overwrites your existing title.
For example a toolbar.axml of -
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/White"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_gravity="right"
android:elevation="4dp">
<RelativeLayout
android:id="@+id/toolbar_customview"
android:layout_width="50dp"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>
Would give your a relativelayout on the RHS of your toolbar, leaving the title to show. You can then FindViewById and then insert any customview you like in your Activity / Fragment as required.
Obviously you would have to adjust the width to retain the title and allow room to add custom details.
If you just want a design which is really custom (as your is, with little room for a standard title), then you are better off adding a textview for title yourself and ensuring it is set instead of SetTitle for that Activity.
Upvotes: 1