getActionBar() is always returning null - android

I have gone through lots of similar posts in Stackoverflow, but nothing seems to resolve my issue.
I am trying to get the ActionBar and invoke setDisplayHomeAsUpEnabled(true).
I can see the black ActionBar (as the theme is black background) with the Activity name displayed as title. But in my activity code, if I try to do getActionBar() it always returns null.
I tried the code in onResume() and onCreate() (after inflating the layout). Everywhere it returns null. Is there anything I am missing?
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityMain"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is my styles definition. I am using Theme.AppCompat.Light.DarkActionBar theme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Also, I am using following dependency:
compile 'com.android.support:support-v4:22.1.0'
compile 'com.android.support:appcompat-v7:22.2.0'
and my activity class definition is:
MainActivity extends AppCompatActivity

Try using getSupportActionBar(). The latest appcompat-v7 seems to disable the systembar.

If your class defenition is MainActivity extends AppCompatActivity, you should try using getSupportActionBar()
If your class defenition is MainActivity extends Activity, you can try using getActionBar() (this call requires API 11)

Related

Android ActionBar/Toolbar Disappears in my App

So I have an app. It has 3 activities at the moment... I am using intents to launch the activities from one and another
MainActivity > ChallongeLogin > ChallongeEvents
In my MainActivity and my ChallongeLogin, there is a large bar at the top of the app that lists the name of my app. However, when I eventually read the ChallongeEvents, this bar disappears... I don't remember doing anything special to make this bar disappear. Why did it go away?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.testingapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:screenOrientation="portrait"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:screenOrientation="portrait"
android:name=".ChallongeLogin" />
<activity
android:screenOrientation="portrait"
android:name=".ChallongeEvents" />
</application>
</manifest>
According to your requirement you must extends AppCompatActivity instead of Activity .
public class ActivityName extends AppCompatActivity {
// ...
}
AppCompatActivity is from the appcompat-v7 library. Principally, this
offers a backport of the action bar. Since the native action bar was
added in API Level 11, you do not need AppCompatActivity for that.
However, current versions of appcompat-v7 also add a limited backport
of the Material Design aesthetic, in terms of the action bar and
various widgets. There are pros and cons of using appcompat-v7, well
beyond the scope of this specific Stack Overflow answer.
Reference
Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?
It is called ActionBar/ToolBar.
Extend the AppCompatActivity class instead of plain Activity class in that activity's java class.

Android: Remove the top bar from an app?

I'm trying to remove the top bar from the app. I try to write in the xml file
<activity
android:theme="#android:style/Theme.Black.NoTitleBar">
</activity>
But it give me the error:
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
I also try do use the code after OnCreate method:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.your_layout_name_here);
But it also crash....how can i do? Thanks!
You could keep the default theme in AndroidManifest file. There is only one reason you are seeing the title bar.
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And you could add
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
It is because your activity is extending ActionBarActivity. You could replace by extending Activity for your activity.
public class MainActivity extends Activity
I hope this will solve your problem.

Remove Action Bar Android

Hi to android stack overflow communities,
I want to remove the action bar which contain the title and the three dots. I have tried some of the solutions such as android:theme="#android:style/Theme.NoTitleBar" but, the app went crash.
Is there any possible other solution? Thanks
This is the nuclear option.
Replace
public class MainActivity extends ActionBarActivity
with
public class MainActivity extends Activity
in all the Activitys where you don't want ActionBar.
And to remove the three-dots button, add
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
to your Activity.
Try this. This will work.
You have to use Activity not ActionBarActivity. So extends your javaclass from Activity. For removing the three dots remove onCreateOptionsMenu method from your Activity.
Create Your code like this
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Enter this line android:theme="#android:style/Theme.Black.NoTitleBar" in Activity
You just need to call function
getActionBar().hide();
Tthis will hide action bar for you simply.

Android 5.0 Back icon parent activity

I'm migrating my projects to new Material design / Android 5.0 Lollipop now.
In previous Android versions it was easy to create Activity with back ActionBar button (arrow) using android:parentActivityName in AndroidManifest.xml. But it seems that in doesn't work any more on new API with support libraries com.android.support:appcompat-v7:21.+.
Below is my code and screenshots from previous and updated to Lollipop support example project:
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="org.serge.androidprobe.app.MainActivity"
android:label="Activity1:Parent" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="org.serge.androidprobe.app.SecondActivity"
android:parentActivityName="org.serge.androidprobe.app.MainActivity"
android:label="Activity2" >
</activity>
</application>
Before migration to Lollipop:
public class MainActivity extends Activity {/**/ }
public class SecondActivity extends Activity {/**/ }
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"/>
</resources>
After migration to Lollipop:
public class MainActivity extends android.support.v7.app.ActionBarActivity {/**/ }
public class SecondActivity extends android.support.v7.app.ActionBarActivity {/**/ }
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat"/>
</resources>
Note: before migration to Lollipop I haven't used support/appcompat libs at all.
How to make back arrow visible & clickable on the top-left corner using new v21 of support/appcompat libraries?
In yours onCreate try calling
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
You then handle the click by checking android.R.id.home in onOptionsItemSelected

How set Theme in main activity api 7?

I offer my users a light and dark option for theming.
The error in the Log:
Caused by: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
The error occurs in setContentView(R.layout.activity_main); in the MainActivity.
MainActivity
public class MainActivity extends SherlockFragmentActivity {.....public static int globalTheme;
Context context;
protected void onCreate(Bundle savedInstanceState) {
context = getApplicationContext();
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = mySharedPreferences.edit();
editor.putBoolean("proxy", false);
editor.commit();
if (mySharedPreferences.getString(Preferences.PREF_THEME, "1").trim().equals("1"))
globalTheme = R.style.Sherlock___Theme;
else
globalTheme = R.style.Sherlock___Theme_Light;
setTheme(globalTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
I did not add something in the manifest, because the themes should be changed dynamically
AndroidManifest.xml
<uses-sdk android:minSdkVersion="7"
/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name="com.belasheuski.activities.MyApplication"
>
<activity
android:name="com.belasheuski.activities.MainActivity"
android:label="#string/name_main"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This error occurs on API 7. On API 15 all works very well.
I'm setting this in this way ...
setTheme(isLightTheme ? R.style.MyApp_Light : R.style.MyApp);
where the style definition are looking like ...
<style name="MyApp.Light" parent="#style/Theme.Sherlock.Light.ForceOverflow">
<item name="overlayedActionBarBackground">#color/overlayedActionBarLight</item>
...
</style>
<style name="MyApp" parent="#style/Theme.Sherlock.ForceOverflow">
<item name="overlayedActionBarBackground">#color/overlayedActionBarDark</item>
...
</style>
That works pretty fine. So I think you took the wrong style definition from ABS.
Cheers!
The error message tells you what is wrong, your theme must be "Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative."
This is occuring on API 7 but not on API 15 because API 15 has native ActionBar support, so that is used, while this is not the case on API 7, and hence the need for the theming.
Going from the error:
You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative."
It seems that your custom style does not inherit from a Sherlock theme. Check the styles.xml file in your values folder and ensure they inherit from one of those.

Categories

Resources