Reputation: 6166
I'm trying to align back arrow icon of toolbar with toolbar title but didn't find any useful method do this,
By default if you will set subtitle and title text, then back arrow icon will vertical align e.g set in middle by default,
I know I can do it by giving custom layout in toolbar , but I want to know is there any default method or behaviour to this ?
/**
* For setting up actionbar/Toolbar
*/
private void setUpToolbar(){
// Set a toolbar to replace the action bar.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//Enabling up back-up arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//Callback of up button pressed
//mToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//What to do on back clicked
}
});
//setting up toolbar text
getSupportActionBar().setTitle(getToolbarTitle());
//mToolbar.setTitleTextColor(getResources().getColor(R.color.textColorPrimary));
getSupportActionBar().setSubtitle(getToolbarSubTitle());
//mToolbar.setSubtitleTextColor(getResources().getColor(R.color.textColorPrimary));
//set back arrow icon in middle
View navButtonView = getNavButtonView(mToolbar);
if(navButtonView != null) {
Toolbar.LayoutParams lp = (Toolbar.LayoutParams) navButtonView.getLayoutParams();
lp.gravity = Gravity.TOP;
navButtonView.setLayoutParams(lp);
}
}
Upvotes: 0
Views: 1764
Reputation: 2496
If you do not get direct option for setting position you can also have another option, this option is for different customization. For customizing toolbar buttons, you should make your custom toolbar. I mean instead of using
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
You can add your own button in toolbar and align the position as you want.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay">
<Button
android:id="@+id/back_button"
....
/>
<TextView
android:id="@+id/title"
....
/>
</android.support.v7.widget.Toolbar>
Upvotes: 0
Reputation: 39191
The Toolbar
class doesn't directly expose its child View
s or their properties, but you can get a reference to that View
using reflection.
private View getNavButtonView(Toolbar toolbar) {
try {
Class<?> toolbarClass = Toolbar.class;
Field navButtonField = toolbarClass.getDeclaredField("mNavButtonView");
navButtonField.setAccessible(true);
View navButtonView = (View) navButtonField.get(toolbar);
return navButtonView;
}
catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
After the View
has been created, you can get a reference to it, and adjust its LayoutParams
as desired.
View navButtonView = getNavButtonView(toolbar);
if(navButtonView != null) {
Toolbar.LayoutParams lp = (Toolbar.LayoutParams) navButtonView.getLayoutParams();
lp.gravity = Gravity.TOP;
navButtonView.setLayoutParams(lp);
}
Upvotes: 1