Getting null at setting actionBar icon - android

I've got problem with my actionBar. Tryin' to set an icon but still getting null :/ Here's code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setIcon(R.drawable.logo);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#51717e")));
}
Ofc I've got logo in my drawable folder. Both lines are incorrect - Icon and Background color. Help :/

You are getting Actionbar null because the Theme don't have ActionBar.
Try to set the background and icon in your styles.xml

You need to call getSupportActionBar() on an ActionBarActivity. Do not call getActionBar() -- that is not available on older devices, and for the new r21 edition of appcompat-v7, I would expect it to return null all the time, as the new ActionBarActivity disables and replaces the system action bar.and surround these two line in try catch like:
try{
getSupportActionBar().setIcon(R.drawable.logo);
getSupportActionBar() .setBackgroundDrawable(new ColorDrawable(Color.parseColor("#51717e")));
}catch(NullPointerException e)
{
e.printstacktrace();
}

Try this:
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();

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.

How to fix getActionBar method may produce java.lang.NullPointerException

I am using a toolbar as my actionbar in an activity. I am trying to add the method getActionBar().setDisplayHomeAsUpEnabled(true); to the Activity.java file for Up navigation for older devices.
The method produces the following error message in Android Studio:
Method invocation may produce java.lang.NullPointerException
The Up navigation on the toolbar works fine on newer devices...now I'm trying to figure out how to make sure it will work for older devices.
Please advise.
From build.gradle:
dependencies {
compile "com.android.support:appcompat-v7:22.1.0"
}
From AndroidManifest.xml:
android:theme="#style/Theme.AppCompat.NoActionBar.FullScreen"
From styles.xml
<style name="Theme.AppCompat.NoActionBar.FullScreen" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
from Activity.java
public class CardViewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
// Up navigation to the parent activity for 4.0 and earlier
getActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
Actually Android Studio isn't showing you an "error message", it's just a warning.
Some answers propose the use of an assertion, Dalvik runtime has assertion turned off by default, so you have to actually turn it on for it to actually do something. In this case (assertion is turned off), what you're essentially doing is just tricking Android Studio to not show you the warning. Also, I prefer not to use "assert" in production code.
In my opinion, what you should do is very simple.
if(getActionBar() != null){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Update:
In case you're using the support library version of the Action Bar, you should replace getActionBar() with getSupportActionBar().
if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
First off, you need to set the toolbar as the support ActionBar.
Then if you're certain it's going to be there all the time, just assert it as != null. This will tell the compiler it won't be null, so the null check passes.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // it's getSupportActionBar() if you're using AppCompatActivity, not getActionBar()
}
Thank You Andrew for your answer.
If you have a Nav Drawer or something else that uses getSupportActionBar() you need to add assert getSupportActionBar() != null;
Peace,
Example:
#Override
public void setTitle(CharSequence title) {
mTitle = title;
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(mTitle);
}
Try this :
private ActionBar getActionBar() {
return ((AppCompatActivity) getActivity()).getSupportActionBar();
}
What I have done is override the getSupportActionBar() method in my base Activity and add a #NonNull annotation. This way, I only get one lint warning in the base activity about how I use #NonNull annotation for something that has a #Nullable annotation.
#NonNull
#Override
public ActionBar getSupportActionBar() {
// Small hack here so that Lint does not warn me in every single activity about null
// action bar
return super.getSupportActionBar();
}
I created a generic class such as:
public final class Cast
{
private Cast() {}
/**
* Helps to eliminate annoying NullPointerException lint warning.
*/
#android.support.annotation.NonNull
public static <T> T neverNull(T value)
{
return value;
}
}
then I can use it for any call with NullPointerException warning for which I am sure that it will never happen, e.g.
final ActionBar actionBar = Cast.neverNull(getSupportActionBar());
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
P.S. don't forget to add "com.android.support:support-annotations" to your gradle file.
add assert getSupportActionBar() != null; before getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setBackgroundDrawable(ContextCompat.getDrawable(mContext,
R.drawable.action_bar_gradient));
}
use this theme: android:theme="#style/Theme.AppCompat.Light.NoActionBar"
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Title");
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_action_previous_item);
actionBar.setDisplayHomeAsUpEnabled(true);
Alternatively you could assert actionbar to not null.Add the assertion before calling your actionbar as follows
assert getSupportActionBar() != null;
Final snippet would therefore look as follows:
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Try this :
setSupportActionBar (toolbar);
if(getSupportActionBar () != null) {
assert getSupportActionBar () != null;
getSupportActionBar ().setDisplayHomeUpEnabled(true);
}
Note that setSupportActionBar(toolbar) should be before getSupportActionBar().
if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
OR
Replace the MainActivity extends AppCompatActivity to public class MainActivity extends AppCompatActivity
just check getSupportActionBar not equal to null
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Daily Shopping List");
}
If you are importing
android.app.ActionBar
you have to use getActionBar()
and if you are importing
android.support.v7.app.ActionBar
use getSupportActionBar()

set title bar name of a activity

I'm trying to change my applications activity menu bar title. I managed to change font as follows. Anyone help me to change the name of the title. It may be just a one line, but I can't figure it out.
int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);
if(actionBarTitleView != null){
actionBarTitleView.setTypeface(typeFace);
}
Try this
getActionBar().setTitle("Your title");
or this if you use appcompat-v7
getSupportActionBar().setTitle("Your title");
You can set the title in action-bar using AndroidManifest.xml. Just add label to the activity. Like
<activity
android:name=".DownloadActivity"
android:label="Your Title"
android:theme="#style/AppTheme" />
Not sure you can do it your way or not but you can use the bellow solution:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
final ActionBar actionBar = getActionBar();
// actionBar
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// titleTextView
TextView titleTextView = new TextView(actionBar.getThemedContext());
titleTextView.setText("Title");
titleTextView.setTypeface( your_typeface);
titleTextView.setOtherProperties();
// Add titleTextView into ActionBar
actionBar.setCustomView(titleTextView);
}
By doing this solution, you have FULL control of your textview title.
For the activity you want to change the title bar or action bar name, go to it's java file
for example if you want to change the name of MainActivity then go to MainActivity.java and add the following code
getSupportActionBar().setTitle("name of the action bar");
below this code (it will be there by default)
setContentView(R.layout.activity_main);
in the protected void onCreate(Bundle savedInstanceState)
It worked for me. I hope it will help you too.
Try calling actionbar.setTitle method.
getActionBar().setTitle(title);
OR this if you are using appcompat
getSupportActionBar().setTitle(title);
There is setTitle method in activity class as well.

Start activity without ActionBar and add it in the onCreate event

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

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

Categories

Resources