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

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.

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

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

expand background of action bar menuItem to height of actionbar

In my app, I highlight a check button in the action bar when the user makes a change. I changed the background of the menuitem drawable, but it appears that the button does not expand the entire height of the actionbar (in the image below, what is currently being displayed is on the left; on the right is what I would like the button to look like)
How do I adjust the menuItem button so that its background expands the height of the action bar (and extends to the right edge of the screen)?
This is what I have:
if (myHorizontalLayout.getItemList().size() == 0) {
MenuItem check = menu.findItem(R.id.save);
check.setIcon(R.drawable.check);
} else{
MenuItem check = menu.findItem(R.id.save);
check.setIcon(R.drawable.check_highlight);
}

Hide only bottom action bar Android

Currently, I have a split action bar, and there is an edit text near the bottom of the screen.
When I click on the edit text, the keyboard shows up, but the bottom action bar ALSO shows up on top of the keyboard, making it cluttered. Here is what it looks like:
How can I hide only the bottom action bar when I click on edit text?
Here is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
//bottom action bar
getMenuInflater().inflate(R.menu.defaultbottom_tabs, menu);
setActionBar();
return true;
}
// custom layout top action bar
private void setActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
// displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.home_top,null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
You can add the android:windowSoftInputMode="adjustPan" attribute to the Activity that shows your EditText. Alternatively, if you really want to do this in code, you can find the split action bar View by using Resources.getIdentifier.
final int id = getResources().getIdentifier("split_action_bar", "id", "android");
final View splitActionBar = findViewById(id);
if (splitActionBar != null) {
// Toggle the visibility
}

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