getActionBar() returning null - android

I need my actionbar to be ready before setContentView because it is used by the navDrawerFragment but at this point:
public class BaseActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
Log.d("", getActionBar().toString());
setContentView(R.layout.activity_base);
}
It is returning null
My theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/light_blue</item>
<item name="colorPrimaryDark">#color/dark_blue</item>
<item name="colorAccent">#color/dark_blue</item>
<item name="android:windowBackground">#color/white</item>
<item name="android:positiveButtonText">#color/white</item>
<item name="android:windowActionBar">true</item>
</style>
and the declaration at manifest:
<activity
android:name=".controller.activity.BaseActivity"
android:label="#string/app_name" >
</activity>

Your BaseActivity must extends from ActionBarActivity and not Activity
public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
Use getSupportActionBar(); to get the ActionBar

Your problem is either one of these two things, or both. At least they should be, unless I'm insane..
Either the problem is that you are calling
getActionBar()
before you set the contentView
So change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_base);
Log.d("", getActionBar().toString());
}
OR
It is that you need to call
getSupportActionBar()
Try both and tell me which works!

You're using the support library (AppCompat), so you have to call getSupportActionBar()

Related

Android Window title showing after requested not to show

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, BaseActivity.class);
startActivity(i);
}
and my next activity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_base);
}
I still see blue bar with application name at top.
I tried changing my application manifest to use a no-title theme and the program crashed because the activities inherit from AppCompatActivity.
How do I inherit from AppCompatActivity and get rid of the application title name up top?
It's the actionbar, you can use the theme Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar or a theme which descents from them.
To make disappear is the action bar also this should work:
getSupportActionBar().hide();
Try this...
If you don't want the action bar in your activity then,
Create a style
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and apply this style for the activity in Manifest for which you don't need action bar,
<activity android:name=".BaseActivity"
android:theme="#style/AppTheme.NoActionBar"></activity>
Do this in your onCreate() method.
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here);
this refers to the Activity
OR
You can modify your AndroidManifest.xml:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
You can use this for each activity in your project.
OR
also change from style.xml
<style name="generalnotitle" parent="general">
<item name="android:windowNoTitle">true</item>
</style>
Hope this helps you.

How to create a single activity without headers

I'm developing an application where I set up an activity for the settings.
I tried to use the android studio template to create an activity setting but the structure of the code that is being created is too complex.
Can anyone tell me how to create a settingsActivity similar to the android studio template but without headers?
Thanks in advance for the answer.
Greetings.
To create a single settingActivity you can try the below pseudo code:
public class SettingsUI extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
PreferenceManager.setDefaultValues(this, R.xml.settings_preference, false);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preference);
}
}
}
Then settings_preference.xml code:
PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="sample_key"
android:title="#string/sample_title"
android:inputType="number"
android:summary="#string/sample_summary"/>
<EditTextPreference
android:key="sample2_key"
android:title="#string/sample2_title"
android:inputType="number"
android:summary="#string/sample2_summary"/>
</PreferenceScreen>
In the styles.xml file, please make one style tag if you want to remove actionbar(you said header) in only specific activity.
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar" />
And goto AndroidManifest file, please apply above style to your activity something like this.
<activity android:name=".SettingActivity" android:theme="#style/AppTheme1"></activity>
if you want to remove actionbar in all of your activities, then you can change AppTheme style in the styles.xml file like below.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/mainColor</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
... ...

Action Bar Back Button is not showing in Android

Hi I have created an activity which extends ActionBarActivity & using material theme in my application. In the Action Bar, Back button is not showing.
I didn't find why it is not showing. Any help ?
public class RegistrationActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_background_light));
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!--Support Library compatibility-->
<item name="actionBarStyle">#style/MyTheme.ActionBarStyle</item>
</style>
<!-- ActionBar styles -->
<style name="MyTheme.ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar">
<!--Support Library compatibility-->
<item name="titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
AndroidManifest.xml
<activity
android:name=".RegistrationActivity"
android:label="#string/title_activity_registration" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeScreenActivity" />
</activity>
Thanks in advance.
add the property
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
to show the "back button"
If the Jorgesys's solution not worked for you. Try overriding the onOptionsItemSelected method.
public class MyActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home)
{
onBackPressed();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
}
there might be a problem with your toolbar theme:
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Light"

Hide Action Bar in a Fragment Activity

I am creating the framework for an app and wish to hide the action bar for the login activity. The app is going to be entirely composed of fragments because they are awesome.
I can hide the action bar fine for my activity, but every time I run the app I see my inflated blank fragment and default action bar for about 2 seconds before it manages to inflate the login fragment.
Any idea on how / where I should hide the action bar, (whilst keeping my framework) to stop this from happening?
LoginActivity.java
public class LoginActivity extends SingleFragmentActivity {
#Override
protected Fragment createFragment() {
return new LoginFragment();
}
}
SingleFragmentActivity.java
public abstract class SingleFragmentActivity extends ActionBarActivity {
protected abstract Fragment createFragment();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(this instanceof LoginActivity){
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
LoginFragment.java
public class LoginFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, parent, false);
return v;
}
}
I originally had SingleFragmentActivity extend FragmentActivity and did this in the if statement:
requestWindowFeature(Window.FEATURE_NO_TITLE);
But I've got a feeling that's just a different way of doing the same thing
Edit:
I have also tried setting the fragment_activity to use a different style but it doesn't seem to work either:
<style name="NoActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
</style>
Okay after a few hours I think I have found the solution to keeping fragments in place for everything and removing the action bar (for all devices) for the launch activity only:
Android Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.company.app.LoginActivity"
android:theme="#style/NoActionBar"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Where the style is defined as like so:
<style name="NoActionBar" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
</style>
That took a while to figure out because there is no Theme.AppCompat.Light.NoActionBar , the action bar is basically known as the title bar in older APIs, and when I applied that style to the activity itself (which I thought was the only way of using dynamic themes) it doesn't seem to work at all.
So hopefully this simple solution will keep the action bar for any other activities and it may help someone in the future :)
try this:
if(this instanceof LoginActivity) {
if (getActionBar().isShowing()) getActionBar().hide();
} else {
if (getActionBar().isShowing()) getActionBar().hide();
}
or
if(this instanceof LoginActivity){
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
} else {
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
Daniel Wilson's comment above does work but just a minor difference in how I did it.
I did not define style at all, all I did in AndroidManifext.xml is that I added this to my activity:
android:theme="#style/Theme.AppCompat.NoActionBar"
So, I did not have to redefine style for NoActionBar, I just added it from Theme.AppCompat
I'm using this code in Activity.onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hiding the title bar has to happen before the view is created
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// ...
}

Android FEATURE_CUSTOM_TITLE giving error

I have the following code which results in error message that I *can not combine FEATURE_CUSTOM_TITLE with other title features* (which I am not to my knowledge...)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String customTitleStr = getIntent().getExtras().getString("Title");
Boolean customTitleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if ( (customTitleSupported) && (customTitleStr != null) ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.override_titlebar);
final TextView myTitleText = (TextView) findViewById(R.id.myTitleText);
if ( myTitleText != null ) {
myTitleText.setText(customTitleStr);
}
}
I have tried place setContentView(R.layout.); both before and after this.requestWindowFeature but no change.
My manifest targets this:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
/>
For reference (don't know if related), I also have trouble theming my titlebar. (Simply ignored.)
Just in case it is relevant, some code snippets for my theming:
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/replace__logo__app_android"
android:label="#string/MicAppName"
android:theme="#style/MicTheme"
android:name="com.examples.example.MicApp"
>
styles.xml:
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
// ...
<style name="MicTheme" parent="AppTheme">
<item name="android:windowTitleSize">44dip</item>
<item name="android:windowTitleBackgroundStyle">#style/MicWindowTitleBackground</item>
<item name="android:windowTitleStyle">#style/MicWindowTitle</item>
</style>
Please try with this code.
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class CustomTitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
}
for more information about how to set custom title bar please follow below tutorials.
http://www.edumobile.org/android/android-programming-tutorials/creating-a-custom-title-bar/
http://www.powenko.com/en/?p=214
http://7thursdays.wordpress.com/2012/10/14/how-to-make-a-custom-title-with-android/
hope this helps you.
EDIT
here is the working screenshot.

Categories

Resources