How to hide the actionbar before the Activity is loaded?
try this in manifest file
<activity
android:name="yourActivityName"
android:label="your label"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
</activity>
ActionBarCompat: Hide ActionBar before activity is created (bug?)
I got stuck with the same problem and, it seems to me, found a reason of this strange behavior. I looked through source of support library and got this:
Appcompat checks a value of mHasActionBar
variable before creating new action bar in ActionBarActivityDelegate
final ActionBar getSupportActionBar() {
// The Action Bar should be lazily created as mHasActionBar or mOverlayActionBar
// could change after onCreate
if (mHasActionBar || mOverlayActionBar) {
if (mActionBar == null) {
mActionBar = createSupportActionBar();
...
And we can change its value by calling supportRequestWindowFeature(int featureId)
which is delegated by ActionBarActivity
to a ActionBarActivityDelegate.
There are base delegate class ActionBarDelegateBase
and its descendants ActionBarDelegateHC
, ActionBarActivityDelegateICS
, ActionBarActivityJB
, one of which is chosen according to a version of running android. And method supportRequestWindowFeature
is actually works fine almost in all of them, but it's overridden in ActionBarActivityDelegateICS
like that
@Override
public boolean supportRequestWindowFeature(int featureId) {
return mActivity.requestWindowFeature(featureId);
}
So it has no effect on the variable mHasActionBar
, that's why getSupportActionBar()
returns null.
We almost there. I came to two different solutions.
First way
import source project of appcompat from git
change overridden method in
ActionBarActivityDelegateICS.java
to something like this@Override
public boolean supportRequestWindowFeature(int featureId) {
boolean result = mActivity.requestWindowFeature(featureId);
if (result) {
switch (featureId) {
case WindowCompat.FEATURE_ACTION_BAR:
mHasActionBar = true;
case WindowCompat.FEATURE_ACTION_BAR_OVERLAY:
mOverlayActionBar = true;
}
}
return result;
}place this line in activity's
onCreate
method beforegetSupportActionBar()
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
Second way
import project of appcompat from android SDK (which is with empty src directory)
add this method to your activity
private void requestFeature() {
try {
Field fieldImpl = ActionBarActivity.class.getDeclaredField("mImpl");
fieldImpl.setAccessible(true);
Object impl = fieldImpl.get(this);
Class<?> cls = Class.forName("android.support.v7.app.ActionBarActivityDelegate");
Field fieldHasActionBar = cls.getDeclaredField("mHasActionBar");
fieldHasActionBar.setAccessible(true);
fieldHasActionBar.setBoolean(impl, true);
} catch (NoSuchFieldException e) {
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
} catch (IllegalAccessException e) {
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
} catch (ClassNotFoundException e) {
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
}
}call
requestFeature()
inonCreate
method of your activity like thisif (Build.VERSION.SDK_INT >= 11) {
requestFeature();
}
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
I used the second way. That's all.
How To Show and hide ActionBar with AppCompat v.7
In the activities where you want to have no action bar use a theme derived from Theme.AppCompat.NoActionBar
or Theme.AppCompat.Light.NoActionBar
. This activity will never be able to show the action bar unless you supply your own via setSupportActionBar(Toolbar)
.
In the activities where you want to have the action bar use a theme derived from Theme.AppCompat
, Theme.AppCompat.Light
or Theme.AppCompat.Light.DarkActionBar
. This will allow you to dynamically hide or show the action bar in such activity. You will not be able to supply your own action bar using these themes.
When working with appcompat-v7 action bar you need to obtain it by calling getSupportActionBar()
instead of getActionBar()
.
How to Show/Hide Action Bar item programmatically via Click Event
In your onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_items, menu);
if (hideIcon){
menu.findItem(R.id.action_share).setVisible(false);
}else{
menu.findItem(R.id.action_share).setVisible(true);
}
return true;
}
In method where you want to show/hide the icon, just set the boolean hideIcon to true or false and call :
invalidateOptionsMenu();
to refresh the menu.
How to hide action bar buttons in certain activity?
When you switch between Activities every time you enter one it's onCreateOptionsMenu
is called. It is where you inflate your menu. If you want to not have action bar buttons in MainActivity then inflate empty menu in it. In SubActivity inflate menu with action bar buttons.
MainActivity.java
package com.example.user.twoactivities;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SubActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
SubActivity.javva
package com.example.user.twoactivities;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class SubActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SubActivity.this, MainActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.subactivity_menu, menu);
return true;
}
}
activity_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button android:id="@+id/button"
android:text="Go to SubActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_sub.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button android:id="@+id/button"
android:text="Go to MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
</menu>
subactivity_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings" android:icon="@drawable/ic_launcher"
android:orderInCategory="100" app:showAsAction="always" />
</menu>
Don't forget to add MainActivity and SubActivity to Manifest file.
Android hiding custom ActionBar
Create a theme whose parent is Theme.Holo.Light.NoActionBar
. Assign it to your login activity with android:theme
parameter.
Related Topics
How to Set Onclicklistener on a Radiobutton in Android
Show Alertdialog in Any Position of the Screen
Pass Data from Activity to Service Using an Intent
How to Change the Android Actionbar Title and Icon
Lollipop's Backgroundtint Has No Effect on a Button
Alarmmanager Fires Alarms at Wrong Time
Android. Is Workmanager Running When App Is Closed
Placing/Overlapping(Z-Index) a View Above Another View in Android
What Characters Allowed in File Names on Android
How to Change the Text Color of Menu Item in Android
Media Player Called in State 0, Error (-38,0)
Android Singletask or Singleinstance Launch Mode
Programmatically Select Item Listview in Android
Android Locationclient Class Is Deprecated But Used in Documentation
Android Popupwindow with Tooltip Arrow
Cannot Retrieve Field Values from Realm Object, Values Are Null in Debugger