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.
Related
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
I have a tab group activity containing 4 tabs. tab1, tab2, tab3, tab4 and two child activities in tab1. How to display action bar in a child activity2 of tab1. But the action bar is not displaying. I want the action bar to be displayed in one child activity.
activity of tab1
public class EntriesTabActivity extends TabGroupActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("EntriesContentActivity", new Intent(this,EntriesContentActivity .class));
}
}
ChildActivity
public class EntriesContentActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tab host
setContentView(R.layout.entries_content);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_menu_entry, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iga.allergy"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- We must declare these two permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<application
android:name="com.iga.allergy.AJ_Constant"
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.iga.allergy.MainActivity"
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>
<activity
android:name="com.iga.allergy.TabController"
android:screenOrientation="portrait" />
<activity
android:name="com.iga.allergy.EntriesTabActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
<activity
android:name="com.iga.allergy.EntriesContentActivity"
android:screenOrientation="portrait" />
<activity>
</application>
</manifest>
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
I feel like you are developping with support library since you want actionbar for api8.have you included appcompat_v7 and android-support-v4 to your project properly?
ActivityGroup inherits android.app.activity and I dont think you can add multiple actionbar to childs or fragments but only to the parent activity.so to my point of view its impossible to add an actionbar to child activities.besides activityGroup is deprecated we should use Fragment and FragmentManager APIs instead.
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
I've tried to make a "return" button for my app, or as Google call it, Up Button for Low-level Activities.
I wrote everything properly:
in My XML I put the meta-data and the parent activity (Example from Google, I modified the text to my app in my files)
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
in My class I put the next line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
but when I click this up button, the app closes, then opened again from the main screen.
I want it to return to the main screen without closing (finishing) the app.
Can you help me? Thanks.
Well, according to this guide,
When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity from the Support Library, performing Up navigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.
So, I have two Activities: FirstActivity, which is my launcher and parent Activity and SecondActivity, which is my child Activity.
My FirstActivity code:
public class FirstActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
Button secondButton = (Button) findViewById(R.id.secondButton);
secondButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
My SecondActivity code:
public class SecondActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_place_activity);
//enable the ActionBar behaviour
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
My Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.testes.activity.FirstActivity"
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="com.testes.activity.SecondActivity"
android:parentActivityName="com.testes.activity.FirstActivity" >
</activity>
</application>
</manifest>
And this is all I need to have it working your way. I start my FirstActivity, click my Button to go to the SecondActivity, click the ActionBar home button and it goes back to FirstActivity.
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);