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

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

Related

change back arrow image in Actionbar with appcompat-v7

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

Home/Drawer button in Actionbar cannot be focused with keyboard/controller

I'm implementing some controller/keyboard navigation in my application for accessibility, and I can't seem to make my drawer button gain keyboard focus.
Setting up the actionBarDrawerToggle (in onCreate):
this.drawerLayout = (DrawerLayout) this.findViewById(R.id.drawer_layout);
this.drawerLayout.setScrimColor(0x99000000);
this.actionBarDrawerToggle = new ActionBarDrawerToggle(this, this.drawerLayout, R.string.Root_Drawer, R.string.Root_Drawer);
this.actionBarDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_utilitybar_menu);
this.actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
this.drawerLayout.setDrawerListener(this.actionBarDrawerToggle);
Setting up the action bar (also in onCreate):
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(ACTIONBAR_BACKGROUND_COLOR)));
actionBar.setElevation(0);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
The other buttons on the action bar are focusable, but I can't go left (dpad left or keyboard left arrow) from the leftmost of those buttons onto the home button itself. For those buttons, I needed to mark them as focusable in order to get the keyboard to navigate to them, eg (from onCreateOptionsMenu):
MenuItem alertItem = menu.add(0, R.id.menu_alert, 0, null);
alertItem.setActionView(this.alertIconActionView);
alertItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
this.alertIconActionView.setFocusable(true);
this.alertIconActionView.setFocusableInTouchMode(true);
But there doesn't seem to be a view I can obtain for the home button that will allow me to set it to be focusable elements.

Lollipop make statusbar transparent on content frame of navigation drawer

i am trying to set status bar transparent on my content frame inside navigation drawer. I create the app using the template contain on android studio. I need make status bar transparent when navigation drawer is not open so my toolbar back ground is draw behind it.
This one:
This One What I mean
Not this one, because it already transparent:
Not this one
I actually had an issue related to this due to some custom rendering with parallax effects... Anyway, you can just set the status bar background color in onDrawerClosed(View drawerView) and then set it back to transparent when opening the drawer:
public class WindowHelper {
public static void setColoredStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(R.color.secondary_color));
}
}
public static void setTranslucentStatusBar(Activity activity){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
So for you it'd something like:
#Override
public void onDrawerClosed(View drawerView) {
WindowHelper.setColoredStatusBar(activity);
}

customize actionbar android who new playstore style

I would like to help me customize my actionbar, I am using a navigation drawer and would like to have the option when you open the action bar is dark.
I also would like to know how to customize a imageview to take a photo and move the name to the right and playstore.
Thank you for your help.
EDIT
I solved
actionBar = getActionBar();
toggle = new ActionBarDrawerToggle(this, drawerMenu, R.drawable.ic_drawer, R.string.texto_menu_opciones, R.string.texto_lineas_transporte){
#Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle(listaOpciones.get(position).getNombre());
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_solid_cvbus));
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
//getActionBar().setTitle("Opciones");
getActionBar().setTitle(listaOpciones.get(position).getNombre());
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_transparent_cvbus));
invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset)
{
super.onDrawerSlide(drawerView, slideOffset);
}
};
you can put this code for changing the actionbar color
ActionBar ac= getActionBar();
ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000"))); // #000000 - for black
put this code in the drawer opening method..

Changing color of action bar doesn't work in ICS

I am changing the colour of action bar using the following code.
actionBar= getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(backgroundColor));
This works perfectly fine in onCreate() method(i.e when my activity starts).
But when I am using the same code in Android 4.1 to change color of action bar in onClick() method(i.e when user clicks say a button) then it changes the color of action bar to 'white' ONLY whatsoever be the value of backgroundColor.
In Android 2.3 it works perfectly fine in both cases, whether called from onCreate or onClick().
It seems to be dead end for me.
It seems like for Android 4.0 up to Android 4.1 (4.2 is ok) you cannot change the action bar background once the activity has started.
So I guess a solution in those cases is to restart the activity with a different background color.
Not very elegant but thats the best I can come up with.
Something like this perhaps:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
if (getIntent().hasExtra("color")) {
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(getIntent().getIntExtra("color", 0)));
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent it = getIntent();
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it.putExtra("color", Color.BLUE);
startActivity(it);
}
});
}
A workaround I used to solve this problem was to maintain a reference to the ColorDrawable that was used to set the background initially. Then, to change its colour, modify the colour of the Drawable, and then tell the activity to invalidate its options, which causes the ActionBar to be redrawn. Here's my code to set the action bar color:
ColorDrawable sBackgroundDrawable;
...
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void setActionBarColor(int color){
if(getSherlockActivity() != null && !mCallback.isNavDrawerShowing()){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN){
if(sBackgroundDrawable == null){
sBackgroundDrawable = new ColorDrawable(color);
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(sBackgroundDrawable);
}else{
sBackgroundDrawable.setColor(color);
getSherlockActivity().invalidateOptionsMenu();
}
}else{
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
}
}
Based off of what steve_the_kid mentioned I was able to solve this by saving the color drawable.
ColorDrawable actionBarBackground = new ColorDrawable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setBackgroundDrawable(actionBarBackground());
actionBarBackground.setColor(someColor);
}
void onSomethingClick() {
actionBarBackground.setColor(someOtherColor);
}
Sorry for such a late reply,But to help others I am replying to this question after so long.
Just set the Title of the action bar or make change in the icons or menu along with changing the colorDrawables.A simple change will recreate the action bar with new color.Even if you need same title or icon every time set same title or same icon.
I hope this will help others

Categories

Resources