android activity call requestWindowFeature first and show the title - android

My Activitys use a theme which set android:windowNoTitle = true.But I have to show the title in some activities. I want show the title in my activity by call requestWindowFeature(), but it's not work! Any help would be great.
Thanks.
ps:
My activitys hava to be switched between two themes.The homeActivty use the actionbar and others no title.These activtys are use the same themes.So when before use requestWindowFeature() it's been hiding, and i just want show it now.
Here is my codes:
Defined theme:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowAnimationStyle">#style/activityAnimation</item>
<item name="android:windowNoTitle">true</item>
<!-- <item name="android:background">#color/background_holo_light</item> -->
</style>
And use the theme in application:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Get and change the theme if necessary:
int mThemeId = mSettingPreferences.getInt(MySettings.THEME_ID, -1);
if (-1 != mThemeId) {
this.setTheme(mThemeId);
}
// do something to show the title

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.yourlayout);
You don't need to apply any Theme if you use this.

you have to use this method before your setContentView method...
requestWindowFeature(Window.FEATURE_NO_TITLE);
OR
And yes you can add theme in Manifest file in which Activity you don't want to Title..Then use this..
<activity
android:name=".activity name"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:screenOrientation="potrait" >
</activity>
first change this:
if (mThemeId != -1) {
this.setTheme(mThemeId);
}
and after that you can use
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

<activity
android:name=".HistoryImage"
android:screenOrientation="sensorPortait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name=".HistoryVideo"
android:screenOrientation="sensorPortait" >
</activity>
<activity
android:name=".HistoryVideo_2"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:screenOrientation="sensorPortait" >
</activity>

you must call requestWindowFeature(); before setContentView()

The activities in which you don't want to show the title bar. just do this to hide the title bar. befor setcontentView requestWindowFeature(Window.FEATURE_NO_TITLE);

I hope this will help you
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_page);

Related

Activity does not show title bar in Android Studio

I have create demo application in Android Studio with two Activity. In First Activity Show Title Bar in preview as well as in emulator but on second Activity does not show Title Bar in Emulator but it display in preview. I have used API 22 in Android Studio.
Please help me....thanx in advn
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/BaseTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DrawingActivity"
android:label="#string/app_name"
android:theme="#style/BaseTheme"
android:screenOrientation="portrait" />
</application>
Theme style file like this....
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="BaseTheme" parent="AppTheme">
<item name="colorPrimary">#color/action_bar_backgound</item>
</style>
I have also try this...
First Activity Work's fine so I have set that in Second Activity as a setContentView(R.layout.activity_main);
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonAction(View v) {
Intent intent = new Intent(MainActivity.this, DrawingActivity.class);
startActivity(intent);
}
}
DrawActivity.java
public class DrawingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
What you are facing is a bug related to the API 22(5.1) SDK.
Kindly refer the following links for the details on the bug in which the Action Bar is not displayed:
https://code.google.com/p/android/issues/detail?id=159793
https://code.google.com/p/android/issues/detail?id=159780
What you can do for now
Either revert back to API 21 if possible or,
Select API 21 in the compile with (render with) option in Android Studio.
Hope this helps.

getActionBar() is always returning null

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)

why this screen, while app opens every time

Here I am getting this screen every time just like a splash for few seconds and then opening the home activity.I don't want to this screen in my app. How to fix it.
Here is my manifest file
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.asdf.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
For your Information, I am using Support Library for developing.
I changed the app Style to "#android:style/Theme.Translucent.NoTitleBar" then this screen is not showing but It has the impact on other UI components of the same App like.. Spinner, text-view etc.
Add this line in your style.xml
<style name="AppTheme" parent="android:style/Theme.Holo">
<item name="android:windowDisablePreview">true</item>
try this..well previous one should work.It worked for me
If you want to remove the title bar, then use the following in your Activity initialization:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
...
move Intent-filter code to whichever activity of your application. and remove that activity code.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Hope it will work

Fullscreen Android "Unfortunately application has stopped" Why?

I wanted to implement in my application the fullscreen. I found this code to do it:
public class FullScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
or through the AndroidManifest.xml
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
well none goes. Both makes my app crash when I launch it. The error is "Unfortunately application has stopped". Anyone knows the problem?
Try this code to make your activity FULL_SCREEN
Looking for find: styles.xml in res\values\ add
<style name="NoTitle" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
</style>
Open file Manifest
Looking for application
Add
android:theme="#style/NoTitle">
It looks like:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/NoTitle" >
put your code BEFORE super.onCreate(savedInstanceState)
This worked for me.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

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