change back arrow image in Actionbar with appcompat-v7 - android

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();

Related

Different text colors in navigation view for each items?

I am using Navigation View in android studio to create a navigation drawer in my app. In the navigation drawer, I want one item to be colored in green, one in red and others in black (just like the one in the screenshot below). However, I can't seem to find the solution to this. I know I can change the color of all the items using 'itemTextColor' in XML but that's not what I want to do.
So it seems it not as easy to change specific items colours, its either all or nothing (via app:itemIconTint and app:itemTextColor on the com.google.android.material.navigation.NavigationView), but it seems you can do something like this (I did it in Java as you never mentioned if you were using Kotlin or Java):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// .. other code
NavigationView navigationView = findViewById(R.id.nav_view);
// .. other code
int menuItemPosition = 0; // the position of the menu item in NavigationView you want to change the color of
MenuItem menuItem = navigationView.getMenu().getItem(menuItemPosition);
this.changeMenuItemColor(menuItem, Color.RED);
}
private void changeMenuItemColor(MenuItem menuItem, #ColorInt int color) {
SpannableString coloredMenuItemTitle = new SpannableString(menuItem.getTitle());
coloredMenuItemTitle.setSpan(new ForegroundColorSpan(Color.RED), 0, coloredMenuItemTitle.length(), 0);
menuItem.setTitle(coloredMenuItemTitle);
}

Change the size of hamburger icon in toolbar?

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));
}

Is it possible to slide open/closed a navigation drawer in uiautomator

Has anyone been able to pull this off. UiScrollable, swipeLeft and swipeRight do not appear to have any affect on it. I am using a Nexus 5 emulator with the latest api. Has anyone been able to pull it off?
TL;DR: Use the content descriptions set in the ActionBarDrawerToggle constructor
When you set up your Navigation Drawer, set an ActionBarDrawerToggle with content descriptions for opening and closing.
// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";
// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";
private void setUpNavigationDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, CONTENT_DESCRIPTION_OPEN_DRAWER, CONTENT_DESCRIPTION_CLOSE_DRAWER);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
In your UiAutomatorTestCase, to open/close the drawer, find the UI object by the open/close content description defined in step 1 and perform a click on it.
// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";
// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";
private void openNavDrawer() throws UiObjectNotFoundException {
findViewByContentDescription(CONTENT_DESCRIPTION_OPEN_DRAWER).click();
}
private void closeNavDrawer() throws UiObjectNotFoundException {
findViewByContentDescription(CONTENT_DESCRIPTION_CLOSE_DRAWER).click();
}
private UiObject findViewByContentDescription(String description) {
return new UiObject(new UiSelector().description(description));
}
Warning: If you use the Material design approach for the navigation drawer (where the drawer opens on top of the Topbar and behind the status bar), the "hamburger" icon will be drawn behind the drawer, making the closeDrawer() method not to work. As a workaround, you can just select the section that is open in the drawer menu; this closes the drawer and shows the same section that was there before opening it.
You could try this:
UiObject view = new UiObject(new UiSelector()."use what you want to identify the view");
Rect bounds = view.getBounds(); // Returns the bounds of the view as (left, top, right, bottom)
int center_x = bounds.centerX();
int center_y = bounds.centerY();
// Now you have the center of the view. You can just slide by using drag()
getUiDevice().drag(center_x, center_y, center_x + "amount of pixels", center_y)
// Where amount of pixels can be a variable set to a fraction of the screen width
I used this idea on a drawer that slides from left to right on a file browser app.
I did not use the exact same code as above because I'm using a python wrapper for uiautomator. (https://github.com/xiaocong/uiautomator)

Android Custom ActionBar doesn't show NavigationDrawer's icon in some devices

For my app I'm using a menu that is a NavigationDrawer and it's working perfectly. At this moment I have created my custom layout of action bar, just with the propose to have an icon centered in actionbar.
The problem is that apparently in some devices the NavigationDrawer's icon isn't showing with my custom ActionBar layout, but it's showing if I don't apply my custom ActionBar layout.
On my android 4.4.2 it's working fine. I've tried on 4.1.2 and on 4.3 and the icon not showing.
Here is my ActionBar layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ActionBarWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/actionbar_centered_icon"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</RelativeLayout>
Here you have the lines on my Activity that set my custom layout for the action bar and also the code that implements the navigationDrawer:
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actionbar_centered_icon);
ImageView actionBarCenteredIcon = (ImageView) findViewById(R.id.actionbar_centered_icon);
actionBarCenteredIcon.setImageResource(R.drawable.vidytape_text);
// Hide App icon
// getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Enable user to open menu from the button on action bar
getActionBar().setHomeButtonEnabled(true);
// Remove app title from action bar
getActionBar().setDisplayShowTitleEnabled(false);
// Set menu ic drawer as icon
/*
* if (lang.equalsIgnoreCase("deutsch")){
* getActionBar().setIcon(R.drawable.label_home_de); } else {
*/
getActionBar().setIcon(R.drawable.vidytape_text);
// }
ColorDrawable actionBarColor = new ColorDrawable(
Color.parseColor("#3498db"));
getActionBar().setBackgroundDrawable(actionBarColor);
// Menu behavior -- Toggle
mDrawerToggle = new ActionBarDrawerToggle((Activity) this,
mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
//Close keyboard
viewsSearchField = (EditText) findViewById(R.id.views_search);
nicesSearchField = (EditText) findViewById(R.id.nices_search);
newestSearchField = (EditText) findViewById(R.id.newest_search);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewsSearchField.getWindowToken(), 0);
imm.hideSoftInputFromWindow(nicesSearchField.getWindowToken(), 0);
imm.hideSoftInputFromWindow(newestSearchField.getWindowToken(), 0);
}
}; // End of menu behavior
mDrawerLayout.setDrawerListener(mDrawerToggle);
Anyone know what can be the problem here?
I FOUND THE SOLUTION
To make it work on the SDKs < 19 and on 19 too, I added the following two lines of code:
getActionBar().setDisplayShowHomeEnabled(true);
ColorDrawable actionBarIconColor = new ColorDrawable(Color.TRANSPARENT);
getActionBar().setIcon(actionBarIconColor);

SherlockActionBar getting space from left side of logo

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.

Categories

Resources