I am trying to set logo of action-bar at run-time as shown in below image and hide back arrow but getting space in left side of action-bar(worked fine in 2.3 but not in 4.0).
How to remove this space? And also I want to remove logo and action-bar menu click glow effect?
private void actionbarsetUp() {
Drawable d = getResources().getDrawable(R.drawable.header_background);
getSupportActionBar().setBackgroundDrawable(d);
getSupportActionBar().setLogo(R.drawable.header_sidebar_btn);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar_title, null);
ImageView imgName = (ImageView) v.findViewById(R.id.action_txt_name);
getSupportActionBar().setCustomView(v);
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
replace this line :
getSupportActionBar().setDisplayShowHomeEnabled(true);
with
getSupportActionBar().setDisplayShowHomeEnabled(false);
Hope it works for u.
just change getSupportActionBar().setDisplayShowHomeEnabled(false);
and remove space.
Related
I have an Actionbar from android.support.v7.widget.Toolbar. It has hamburger image with animation to back arrow, I want to change back arrow from <- to ->. how can I do this in Android Studio?
I read somewhere to change it with setHomeAsUpIndicator() method, but it change hamburger button and it has no animation to back arrow.
There are at least half a dozen ways to do this, but probably the simplest and shortest is to use reflection to grab the ActionBarDrawerToggle's Drawable, and flip its direction.
This example wraps that functionality in a subclass, and should work no matter your ActionBar/Activity setup (provided the original class worked there in the first place).
public class FlippedDrawerToggle extends ActionBarDrawerToggle {
public FlippedDrawerToggle(Activity activity, DrawerLayout drawerLayout,
int openDrawerContentDescRes, int closeDrawerContentDescRes) {
this(activity, drawerLayout, null,
openDrawerContentDescRes, closeDrawerContentDescRes);
}
public FlippedDrawerToggle(Activity activity, DrawerLayout drawerLayout,
Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, toolbar,
openDrawerContentDescRes, closeDrawerContentDescRes);
try {
Field sliderField = ActionBarDrawerToggle.class.getDeclaredField("mSlider");
sliderField.setAccessible(true);
DrawerArrowDrawable arrow = (DrawerArrowDrawable) sliderField.get(this);
arrow.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_RIGHT);
}
catch (NoSuchFieldException | IllegalAccessException e) {
// Fail silently
}
}
}
This will only change the direction of the toggle's image. If you actually want the whole ActionBar/Toolbar to be flipped, you should instead change the layout's direction accordingly.
Try this:
//Find the action bar for customizations
final ActionBar ab = getSupportActionBar();
assert ab != null;
//Make the action bar display an up arrow and set its drawable and color
ab.setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = ResourcesCompat.getDrawable(
getResources(),
R.drawable.abc_ic_ab_back_mtrl_am_alpha, //this is the <- arrow from android resources. Change this to the thing you want.
null);
assert upArrow != null;
upArrow.setColorFilter(
ContextCompat.getColor(
getApplicationContext(),
R.color.white // change this to the color you want (or remove the setColorFilter call)
),
PorterDuff.Mode.SRC_ATOP);
ab.setHomeAsUpIndicator(upArrow);
Add this condition between mDrawerToggle = new ActionBarDrawerToggle... and mDrawerToggle.syncState(); like this :
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
{
toolbar.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
mDrawerToggle.syncState();
I am using AppCompat action bar and i want to change the back icon of the searchview I use in the action bar. I searched but i couldn't find a solution.
I have tried setting the icon in the style :
<item name="homeAsUpIndicator">#drawable/myBackButton</item>
but I want to set another one programmatically when the user selects the search view.
Any ideas on how I can do it?
To customize SearchView "up" icon, you can use style attribute:
collapseIcon="#drawable/ic_discard_icon"
in your Toolbar layout:
<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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapseIcon="#drawable/ic_discard_icon"/>
Or you can move this attribute into Toolbar style. As you wish.
Try this code snippet:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// get the parent view of home (app icon) imageview
ViewGroup home = (ViewGroup) findViewById(android.R.id.home).getParent();
// get the first child (up imageview)
( (ImageView) home.getChildAt(0) )
// change the icon according to your needs
.setImageDrawable(getResources().getDrawable(R.drawable.custom_icon_up));
} else {
// get the up imageview directly with R.id.up
( (ImageView) findViewById(R.id.up) )
.setImageDrawable(getResources().getDrawable(R.drawable.custom_icon_up));
}
You can change the background of the back icon like this
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolbar();
}
private void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.tbMaster);
setSupportActionBar(toolbar);
if (toolbar != null) {
toolbar.setBackgroundColor(getResources().getColor(R.color.base_color));
getSupportActionBar().setTitle("Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.image);
}
}
I have 2 questions which can be strange but anyway...
I have toolbar with a title of application. How to change it to picture which is not the logo?
The next question: Is it possible to set, change the size of hamburger icon in toolbar? I made classic navigation drawer with the help of next code below (I used ActionBarDrawerToggle also) but the size is too small if you will compare it with another items(icons) of my toolbar.
toolbar = (Toolbar) findViewById(R.id.application_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)getFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_layout), toolbar );
Thanks for any help! =)
I had solved this problem by this way:
for (int i = 0; i < mToolbar.getChildCount(); i++) {
if(mToolbar.getChildAt(i) instanceof ImageButton){
mToolbar.getChildAt(i).setScaleX(1.5f);
mToolbar.getChildAt(i).setScaleY(1.5f);
}
}
and my hamburger icon size had been increased.
I wanted to increase the size of the Hamburger button and found other answers to not work. Code from this answer worked perfectly and is pasted below.
"Create a custom class which extends the Drawable class to create the
hamburger and navigation icons. There are various methods to change
the size of either but below are the methods which control the
hamburger icon."
public class HamburgerDrawable extends DrawerArrowDrawable {
public HamburgerDrawable(Context context){
super(context);
setColor(context.getResources().getColor(R.color.white));
}
#Override
public void draw(Canvas canvas){
super.draw(canvas);
setBarLength(30.0f);
setBarThickness(5.0f);
setGapSize(5.0f);
}
}
"Then call it from your class:"
private void increaseHamburgerSize(){
mDrawerToggle.setDrawerArrowDrawable(new HamburgerDrawable(this));
}
How do I change the color of the split actionbar in code, and not in xml? My user can pick the color of the actionbar, and I would like it so they could also change the color of the split action bar (the actionbar that appears at the bottom of the screen).
I'm implementing splitActionBar using android:uiOptions="splitActionBarWhenNarrow" in android manifest
so far I'm trying this, but it's not working
final int splitBarId = getResources().getIdentifier("split_action_bar", "id", "android");
final View splitActionBar = findViewById(splitBarId);
if (splitActionBar != null) {
splitActionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(actionbar_colors)));
}
}
Use this to change the split actionbar color
getActionBar().setSplitBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
Or use this if you're using support actionbar
getSupportActionBar().setSplitBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
Source: http://scriptedpapers.com/2014/09/25/android-implement-spilit-action-bar-change-its-background-color/
The framework doesn't provide a way to change it programmatically; however, you can use Resources.getIdentifier to find the View and adjust the background Drawable from there.
The internal id is split_action_bar.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
final int splitBarId = getResources().getIdentifier("split_action_bar", "id", "android");
final View splitActionBar = findViewById(splitBarId);
if (splitActionBar != null) {
// Adjust the background drawable
}
}
Update
Evidently there's ActionBar.setSplitBackgroundDrawable. Definitely use that callback rather than Resources.getIdentifier.
Here's a screenshot of the results:
This should work
ActionBar mActionBar = getActionBar();
mActionBar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
I can disable system clicking sound on a button if I use android:soundEffectsEnabled="false" in its layout.
I want to disable the sound effect on an Action Bar item. This attribute seems to make no difference.
How can I disable clicking sounds when user selects a menu item?
I use this:
final Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
final View child = toolbar.getChildAt(2);
if (child instanceof ActionMenuView)
{
final ActionMenuView actionMenuView = ((ActionMenuView) child);
actionMenuView.getChildAt(actionMenuView.getChildCount() - 1).setSoundEffectsEnabled(false);
}
If have navigation drawer use toolbar.getChildAt(2); if not use toolbar.getChildAt(1);
Was able to get this working in Android 11 by disabling sound effects for the view (in this case, an ImageView) associated with the action bar item. Hopefully this helps someone...
View view = menuItem.getActionView();
if (view != null) {
view.setSoundEffectsEnabled(false);
}