Start activity without ActionBar and add it in the onCreate event - android

I'm using the AppCompat/ActionBarCompat library and I need to create a custom ActionBar. I need to open the activity without an ActionBar and enable it only when I add the custom view. How can I do this?
PS: I need to define the activity to not use an ActionBar through the AndroidManifest.xml and my application minimum API level is 10.

.hide() the action bar and then .show() the action bar when your ready for it
import android.support.v7.app.ActionBar;
...
private ActionBar mActionbar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionbar = getSupportActionBar();
mActionbar.hide()
}
...
somewhere when something cool happens
mActionbar.show();

Related

getSupportActionBar setSubtitle only shows after activity recreation

I have a generic
MyActivity extends AppCompatActivity
I don't override the toolbar with a custom xml defined toolbar, just use the generated one Android provides.
I can set the title via your normal
getSupportActionBar().setTitle("foo");
but setting the subtitle via
getSupportActionBar().setSubtitle("bar");
doesn't set it. It remains blank. I'm doing this onCreate()
(I feel I've done this many times before with no fail)
Although I've noticed if I visit another activity, then return, the subtitle would then show... not on orientation change, not on recreate() but only when I'm returning from an activity.
I'm experiencing this on 5.0 and 7.0
For the time being I'll likely define my own Toolbar and move forward since that seems where most people have solutions for this same problem.
Relevant code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replenishment_list);
ButterKnife.bind(this);
MyApplication.getInstance().getComponent().inject(this);
setupUI();
}
private void setupUI() {
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//TODO: not working unless activity is recreated...
// explore custom xml defined toolbar
//actionBar.setTitle("different title than what is defined in manifest"); <-- this does work, but not this
actionBar.setSubtitle(UserUtil.getFormattedFirstNameLastName(userService.getUserFromJWT(), this));
}
}
I have put the below code in my onCreate() method.
ActionBar actionBar = getActionBar();
if (actionBar==null) {
System.out.println("TEST NULL");
} else {
System.out.println("TEST NOT NULL");
}
The result is null. When I add the toolbar first it works fine.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("TESTING");
Your getSupportActionBar or getActionBar will return null if you didn't set toolbar to it. You need to set the toolbar to your action bar before using getSupportActionBar or getActionBar.

Remove just the TitleBar from an ActionBar

The red rectangle (The bar with the title of the activity) it is what I want to remove (see the picture):
http://subefotos.com/ver/?8e6468bdbc19b0864d5090fa79e54120o.jpg
What I have in my code:
public class HomeActivity extends ActionBarActivity implements ActionBar.TabListener, OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_home);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
} }
I can remove all the bar, including the HOME-STATISTIC-CLASSIFICATION, but I don't want to remove this.
I think that what I want to do it is impossible if we take into account this link: http://developer.android.com/guide/topics/ui/actionbar.html
Although if somebody knows how can be done, please post it.
Thank you for your time.

Saving action bar state

In my Android application I'm using the ActionBar. I want to show users some status using logo placed in the left side of the ActionBar. I can do this:
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.new_icon);
and it works.
The problem is that this state in to persistent. If I change activity ActionBar reset itself, so default logo is shown. Is there any way to save state of the ActionBar during application runtime?
Use the standar lifecycle callbacks from Activities
In the OnCreate method of your activity try to read the state
public void onCreate(Bundle icicle) {
if (data!=null)
{
ActionBar actionBar = getSupportActionBar();
int state = data.getInt("status");
if (state==1)
actionBar.setIcon(R.drawable.icon_a);
else
actionBar.setIcon(R.drawable.icon_b);
}
}
And save the state to recovery it later
protected void onSaveInstanceState(Bundle data) {
super.onSaveInstanceState(data);
data.putInt(icon_state);
}

ActionBar Compat not showing app icon

I'm setting an ActionBar in my app with the Compatibility version. For now I've done:
Import android-support-v7-appcompat and add as library to my project
Set Aplication theme as: Theme.AppCompat
Extend Activities to ActionBarActivity
After this, I use a method to set dynamically the subtitle:
private final void setStatus(int resId) {
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle(resId);
}
private final void setStatus(CharSequence subTitle) {
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle(subTitle);
}
While testing the app, the subtitle doesn't appear. If I add this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
Then the subtitle appears, but the app icon dissapears. What can I do to mantain the app icon while showing the subtitle?
The display options are bitfields, so you should be able to enable several at the same time (using the OR operator), like this:
getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Or, to just add one value without affecting other fields, call the version with bitmask:
getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE,
ActionBar.DISPLAY_SHOW_TITLE);
This is what I get to solve the problem:
/**Resolves the issue, shows the app icon*/
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled (true);
Use getSupportActionbar instead of actionbar
Actionbar actionbar = getSupportActionBar()
actionbar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionbar.setIcon(YOUR ICON);
Ok, all of the above answer looks similar with minor differences, none of it work for me but this combo
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_HOME);
actionBar.setDisplayShowHomeEnabled (true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setIcon(R.drawable.rn_logo_icon);
Please note, this fix is if you are using AppCompat theme

Exception on hiding title of the application using ListActivity

I have a class extending ListActivity and I am trying to hide the default TitleBar (I think it's called ActionBar) using the code below. It works on a regular activity, but not on the ListActivity. How do I accomplish the same in this scenario?
public class MyClass extends ListActivity{
#Override
public void onCreate(Bundle b) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(b);
// Tried Window.FEATURE_NO_TITLE here as well
setContentView(R.layout.activity_myclass);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
The result is a NullPointerException. The full error stack trace is here if you need it: http://pastebin.com/VLR5dE8m
to disable Titltbar,, use this code in your manifest file ,it will help you.
<
activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
>
To remove the actionbar: getActionBar().hide(). Also see this answer: How to disable action bar permanently
I think You should not use requestWindowFeature() if you are using ActionBar. You can hide title of ActionBar with this:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
http://developer.android.com/reference/android/app/ActionBar.html#setDisplayShowTitleEnabled(boolean)
It seems like the NullPointerException is generated by getActionBar(). Removing the following line from the code fixed the issue.
getActionBar().setDisplayHomeAsUpEnabled(true);
The final (working) code:
public class MyClass extends ListActivity{
#Override
public void onCreate(Bundle b) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(b);
setContentView(R.layout.activity_myclass);
}
}
Note that requestWindowFeature must be called before calling the super class and setContentView.

Categories

Resources