Action Bar Back Button is not showing in Android - android

Hi I have created an activity which extends ActionBarActivity & using material theme in my application. In the Action Bar, Back button is not showing.
I didn't find why it is not showing. Any help ?
public class RegistrationActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_background_light));
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!--Support Library compatibility-->
<item name="actionBarStyle">#style/MyTheme.ActionBarStyle</item>
</style>
<!-- ActionBar styles -->
<style name="MyTheme.ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar">
<!--Support Library compatibility-->
<item name="titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
AndroidManifest.xml
<activity
android:name=".RegistrationActivity"
android:label="#string/title_activity_registration" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeScreenActivity" />
</activity>
Thanks in advance.

add the property
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
to show the "back button"

If the Jorgesys's solution not worked for you. Try overriding the onOptionsItemSelected method.
public class MyActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home)
{
onBackPressed();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
}

there might be a problem with your toolbar theme:
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Light"

Related

Hide title and action bar for splash screen and remove White screen in start

I want to remove title and ActionBar on top of my splash activity. I tried the below code,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mSplashThread.start();
but its not working
anyone can help , why this does not work?
// try this way
//add to AndroidManifest for SplashScreen
<activity
android:name="<YOUR_PACKAGENAME.ACTIVITY>"
android:theme="#style/AppTheme.NoActionBar"
.........../>
//add this styles to styles.xml
<style name="AppTheme.NoActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#null</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
//add this code to Activity
public class SplashScreen extends AppCompatActivity {
.
.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
.
.
.
}
}

Contextual Overlay stacking on top of toolbar

I have read about every answer on this and still can't seem to make it work. When I call startSupportActionMode, the action mode menu is stacked on top of the toolbar instead of replacing it. This graphic is from another user that was having the same issue.
Most of the other questions were resolved by using windowActionModeOverlay in the styles. This doesn't seem to be working for me. I'm also sure I'm importing the correction action mode.
(import android.support.v7.view.ActionMode;
)
My situation is a little different. My main activity is a AppCompatActivity. I load different fragments based on choices made from a navigation drawer. The actionbar is changed based on the navigation item selected.
This code all worked before I changed over to appCompat-v7. I am trying to convert to the new material design and am therefor replacing the base actionbar with a toolbar. Here are some code snippets:
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.acme.common.MINApplication"
android:largeHeap="true">
<activity android:name="com.acme.common.MINMainActivity"
android:theme="#style/AppTheme"
android:configChanges="orientation|screenSize|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/ColorPrimary</item>
<!-- Support library compatibility -->
<item name="colorPrimary">#color/ColorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Support library compatibility -->
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
Main Activity:
public class MINMainActivity extends AppCompatActivity
{
private final String TAG = ((Object) this).getClass().getSimpleName();
private View mMainView;
// New Toolbar and Navigation View
public Toolbar toolbar;
public RecyclerView recyclerView;
private MINPageAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
public DrawerLayout drawerLayout;
public android.support.v7.app.ActionBarDrawerToggle drawerToggle;
volatile public boolean isDrawerOpen = false;
private Menu mMenu;
#Override
public void onCreate(Bundle savedInstanceState)
{
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS | Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
// Inflate main view
mMainView = getLayoutInflater().inflate(R.layout.material_design_drawer_layout, null);
setContentView(mMainView);
sharedInstance = this;
// Setup New Toolbar implementation
setupToolbar();
// Setup Recycler Adapter
setupAdapter();
// Setup Navigation View
initNavigationDrawer();
// Show startup screen/fragment
showStartupScreen();
// Color components based on json settings
setCustomAttributes();
init();
}
Fragment:
public class MINPageTypeGridFragment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate view
gridView = (StickyGridHeadersGridView)inflater.inflate(R.layout.page_gridview_fragment, container, false);
// Set up specific controls/views
initializeGridView(inflater, container);
...
return gridView;
}
#Override
public void onResume()
{
ActionBar actionBar = MINMainActivity.getSharedInstance().getSupportActionBar();
if(actionBar != null)
{
actionBar.show();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_USE_LOGO |
ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setTitle(pageDefinition.pageName);
}
...
super.onResume();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int count = 0;
...
freezeGridView();
mActionMode = MINMainActivity.getSharedInstance().startSupportActionMode(mActionModeCallback);
...
return super.onOptionsItemSelected(item);
}
public ActionMode.Callback mActionModeCallback = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
MenuItem shareMenuItem;
...
actionMode.getMenuInflater().inflate(R.menu.gridview_edit_menu, menu);
shareMenuItem = menu.findItem(R.id.MenuItemShare);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
if(mShareActionProvider != null)
{
mShareActionProvider.setShareIntent(Share(null));
mShareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener()
{
#Override
public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent)
{
if(mActionMode != null)
{
currentMode = MODE_STANDARD;
clearSelectedItems();
mActionMode.finish();
mActionMode = null;
}
return false;
}
});
...
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
switch(menuItem.getItemId())
{
case R.id.MenuItemEdit:
launchAlbumItemDetails(MINPageTypeGridFragment.this, currentAlbumItem, pageDefinition.pageConfigFileName);
return true;
case R.id.MenuItemDelete:
deleteItem(MINPageTypeGridFragment.this, currentAlbum, currentAlbumItem);
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode)
{
clearSelectedItems();
}
}
I'm sure that I'm missing something obvious but I'm out of ideas.
You should remove 'duplicate' definition: android:windowActionModeOverlay -> windowActionModeOverlay; android:colorPrimary -> colorPrimary and so on. This is an example that should work:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionModeOverlay">true</item>
<item name="colorControlNormal">#color/appColorControlNormal</item>
<item name="colorControlHighlight">#color/appColorControlHighlight</item>
<item name="colorControlActivated">#color/appColorControlActivated</item>
<item name="colorPrimary">#color/appColorPrimary</item>
<item name="colorPrimaryDark">#color/appColorPrimaryDark</item>
<item name="colorAccent">#color/appColorAccent</item>
</style>
Be sure to set the toolbar as current actionbar (maybe your method setupToolbar already do this):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
After that, change your code and start the action mode directly from the toolbar reference:
toolbar.startActionMode(mActionModeCallback)
Let us know if this fix your problem.

How to prevent the actionMode and toolbar to "jump" when toggling between them?

Background
I have an activity in my app that has a toolbar as the actionBar, and it also has an actionMode, for multi-selection of items.
The problem
Every time I close the actionMode, there is a "jump" between the two modes, so I can see both the toolbar and the actionMode.
Maybe I'm just doing it wrong, but I remember it worked fine in the past.
Here's how it looks like using the snippet code I've made:
What I've tried
This is a snippet of the code I've used. To test it, run the app, wait a moment for the actionMode to appear, and then either press the back button, or press the button on the actionMode. Do note that all classes that I use are of the support library (when available).
MainActivity.java
public class MainActivity extends AppCompatActivity {
protected ActionMode.Callback _actionModeCallback;
protected ActionMode _actionMode;
Toolbar _toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_toolbar = (Toolbar) findViewById(R.id.activity_app_list__toolbar);
setSupportActionBar(_toolbar);
_actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(final ActionMode mode) {
_toolbar.setVisibility(View.VISIBLE);
_actionMode = null;
}
#Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
_toolbar.setVisibility(View.GONE);
return true;
}
#Override
public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
mode.finish();
return true;
}
};
//
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
_actionMode = startSupportActionMode(_actionModeCallback);
}
}, 2000);
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/activity_app_list__toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:colorControlNormal="?attr/colorControlNormal"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:ignore="UnusedAttribute"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"/>
</FrameLayout>
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->.
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
<item name="colorPrimary">#FF0288D1</item>
</style>
</resources>
The question
Why is this happenning? How can I fix it?
Is this a known bug, perhaps?
What you're looking for is the ACTION_MODE_OVERLAY flag. In your Activity.onCreate() method, add the following before the call to super.onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
super.onCreate(savedInstanceState);
// other stuff...
}
The same as described by #Kevin Coppock can also be achieved by adding <item name="windowActionModeOverlay">true</item> in your AppTheme.
The theme could look like this:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->.
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
<item name="colorPrimary">#FF0288D1</item>
<item name="windowActionModeOverlay">true</item>
</style>

Android Material Design navigation drawer menu icon chage

I implemented a Material Design nav drawer using this example. I customize it according to my theme. Everything is working perfectly. Just the last thing I want to customize it's menu icon. I googled it, but have not find any good solution. I want to change its color if possible , or add a custom drawable. Any solution on idea. Thanks in advance
This is my Application theme which i set in menifest
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
And this is my toolbar style
<
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>
My Main activity code
public class Activity_Home extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = Activity_Home.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity__main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//
// if(id == R.id.action_search){
// Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
// return true;
// }
return super.onOptionsItemSelected(item);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
}
}
Add:
getSupportActionBar().setHomeAsUpIndicator(resId);
or
getSupportActionBar().setHomeAsUpIndicator(drawable);
to your Activity's onCreate
Found a sloution myself. It was pretty easy indeed. All you have to do is set a toolbar style. In your style primary color act as a toolbar title and textScondary color act as a menu icon color.
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>

getActionBar() returning null

I need my actionbar to be ready before setContentView because it is used by the navDrawerFragment but at this point:
public class BaseActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
Log.d("", getActionBar().toString());
setContentView(R.layout.activity_base);
}
It is returning null
My theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/light_blue</item>
<item name="colorPrimaryDark">#color/dark_blue</item>
<item name="colorAccent">#color/dark_blue</item>
<item name="android:windowBackground">#color/white</item>
<item name="android:positiveButtonText">#color/white</item>
<item name="android:windowActionBar">true</item>
</style>
and the declaration at manifest:
<activity
android:name=".controller.activity.BaseActivity"
android:label="#string/app_name" >
</activity>
Your BaseActivity must extends from ActionBarActivity and not Activity
public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
Use getSupportActionBar(); to get the ActionBar
Your problem is either one of these two things, or both. At least they should be, unless I'm insane..
Either the problem is that you are calling
getActionBar()
before you set the contentView
So change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_base);
Log.d("", getActionBar().toString());
}
OR
It is that you need to call
getSupportActionBar()
Try both and tell me which works!
You're using the support library (AppCompat), so you have to call getSupportActionBar()

Categories

Resources