setDisplayHomeAsUpEnabled(true) doesn't show arrow if called from non Activity class - android

In my Activity I want to show arrow to left of ActionBar icon, so in Activity I write:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
It works fine, but I decide move ActionBar initialization to another class and use it for all activities in my application, new class for this:
public class Utils {
public static void initActionBar(Activity activity, boolean homeIconNeeded) {
ActionBar actionBar = activity.getActionBar();
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(homeIconNeeded);
actionBar.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.action_bar_background));
}
public static void initActionBar(ActionBar actionBar, boolean homeIconNeeded) {
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(true);
actionBar.setBackgroundDrawable(SmartVmsApplication.getContext().getResources().getDrawable(R.drawable.action_bar_background));
}
}
Then in my activity I insert in onCreate() callback initActionBar(this, true), however arrow doesn’t appear, no matters I passed Activity or ActionBar as parameter and it is an issue.

You forgot to call the setDisplayHomeAsUpEnabled() method.

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.

ActionBar Back Button and Title

I have an activity that I would like the "up affordance" on.
public class ItemListActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// ...
}
}
This adds the "back button" like I want. The problem is that the Title is not considered part of the back button in this instance. Is there any way to make it so that it behaves more like the Messaging app? In the Messaging app you can tap either the back button or the title and it will take you back to the previous activity.
Add an onClickListener to the title of your ActionBar. See this post for example: Set OnClick Listener on Action Bar Title in Android

how to reuse custom actionbar to all the activities in an application?

I have created a custom action bar which works fine on my starting activity but gives an error when I call the method from other activities in the same application.
This is the code I am using to set the ActionBar in my first activity
firstAct.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBarSetup(this);
}
void actionBarSetup(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
LayoutInflater inflator = LayoutInflater.from(context);
View v = inflator.inflate(R.layout.actionbar_layout, null);
//assign the view to the actionbar
ab.setCustomView(v);
}
}
**secondAct.java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pg);
(new firstAct()).actionBarSetup(secondAct.this);
}
I get NullPointerException when i call actionBarSetup() from secondAct.java
on line ActionBar ab = getActionBar().
Is it that getActionBar() cannot be called directly from other activities besides the main activity ie. firstAct.
How to call it from other Activities then?
You don't create new activities by calling their constructor. You have the system create and open them for you. I'm talking about the line
(new firstAct()).actionBarSetup(secondAct.this)
What are you trying to do here?
You probably want to make the actionBarSetup method accessible for all classes and not just instances of firstAct. Then declare it like this (maybe move it to a utility class?):
public static void actionBarSetup(Activity activity) {
ActionBar ab = activity.getActionBar(); // you need activity, not just context
// ...
}
Then call it from other classes like this:
firstAct.actionBarSetup(this);
Making a method static detaches it from an instances resources. You were taking in second activity (context parameter) but asking for an action bar from instance of the first activity (essentially this.getActionBar()) which was not setup by system (because you misused constructor).
Note: Please use PascalCase notation for class names (capital first letter).
EDIT
Warning: Your action bar may have different styling from your activity (e.g. black toolbar and white activity). In that case using the activity's inflater to inflate contents of the action bar will produce undesired results (inverted text color mainly). The following line is safer. But it's available no sooner than API 14.
LayoutInflater inflator = LayoutInflater.from(ab.getThemedContext());
You need create BaseActivity like
public class BaseActivity extends Activity {
public void actionBarSetup(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
LayoutInflater inflator = LayoutInflater.from(context);
View v = inflator.inflate(R.layout.actionbar_layout, null);
//assign the view to the actionbar
ab.setCustomView(v);
}
}
then you need firstAct and secondAct extend BaseActivity then in onCreate method call actionBarSetup()
This may help
private void showCustoNavBar(){
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.new_gradient));
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
View customNav = LayoutInflater.from(this).inflate(R.layout.actioncustomview, null);
getSupportActionBar().setCustomView(customNav);
}

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

ActionBarSherlock: Change dropdown navigation title

currently i am using ActionBarSherlock for my project. I am creating my actionbar with this code.
setTheme(R.style.Theme_Sherlock);
Context context = getSupportActionBar().getThemedContext();
list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
My question is. After I choose an option from the dropdown navigation, How do I keep that state throughout my activities?
For Example, in the homescreen, I choose "Sports" under my dropdown navigation. The title of the dropdown navigation then becomes "Sports". When I change activites however, the dropdown navigation title defaults back to the first item on the list.
One method I used was to create a base activity that each navigation item / activity extended from. Within the base activity, I overloaded onResume with an int to track which activity was active, and set the selected navigation item in that method.
Example:
public class BaseActivity extends FragmentActivity {
//...
protected void onResume(final int actId) {
super.onResume();
//...setup your action bar via getSupportActionBar() calls...
getSupportActionBar().setSelectedNavigationItem(actId);
}
Then in your individual activities:
public class ExampleActivity extends BaseActivity {
private final int ACT_ID = 1;
//...
protected void onResume() {
super.onResume(ACT_ID);
//...
}
}
Hope that helps!

Categories

Resources