How to show action bar in a tab group activity? - android

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.

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.

Option button not showing in action bar Android

I am trying to add action button , but they do not showing in action bar, they are display at button when click on hardware menu button . And the up button is also not working .
i am sharing my code , tell me where i am wrong
this 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=".MainActivity"
android:label="#string/app_name"
>
<!-- Parent activity meta-data to support API level 7+ -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".secindActivity"
android:label="Items"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
I am adding the up button in second activity ,. it is showing but not working .
And also trying to add action bar buttons in secondActivity . Menu file is as below :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:Digicare="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item_back"
android:icon="#drawable/home2"
Digicare:showAsAction="ifRoom"
android:title="Home">
</item>
</menu>
this is my java file of secondActivity :
it never comes in the if statement .
i put log in it , so i get it that it never comes in if statement
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if ( item.getItemId() == R.id.home){
Log.w("asdfasdfasdf","asdfasdf");
Intent upIntent = NavUtils.getParentActivityIntent(this);
NavUtils.navigateUpTo(this, upIntent);
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater Mymenu = getMenuInflater();
Mymenu.inflate(R.menu.item_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
These are problems .
how to done this ????
First issue
This is a duplicate of this question. I think that it doesn't show up because you have an options button on your device.
See this post about modifying this behavior in reflection (however, I'm not sure I would recommend it, as people have expectations about their device's behavior).
Second issue
If I followed your code correctly, then the item id is item_back and not R.id.home...

Up Button - getSupportActionBar().setDisplayShowHomeEnabled(true) - closes app

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.

Action Items not showing in ActionBar with showAsAction="ifRoom"

I'm trying to add some Action Items to my Support Action Bar. In my activity, I have also tabs added to the Action Bar.
This is an excerpt of the activity:
public class ShowEmails extends ActionBarActivity implements ShowEmailsFragmentInteractionListener {
private IMAPClientService service;
private boolean bound;
private ActionBar ab;
private MailDBHelper mdbhelper;
private SQLiteDatabase db;
private Intent client_service;
<.........................>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.client_service = new Intent(this, IMAPClientService.class);
this.mdbhelper = new MailDBHelper(this.getApplicationContext(), MailDBHelper.MAIL_DB_NAME, null, MailDBHelper.MAIL_DB_VERSION);
this.db = this.mdbhelper.openWriteable();
this.ab = this.getSupportActionBar();
this.ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
this.ab.show();
Tab t = ab.newTab().setText(R.string.all_emails)
.setTabListener(new TabListener<ShowEmailsFragment>(this, "all", ShowEmailsFragment.class));
ab.addTab(t);
new LoadTabsInBackground().execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show_emails_bulk_action, menu);
return super.onCreateOptionsMenu(menu);
}
}
The class LoadTabsInBackground adds some tabs to the ActionBar after doing some database operation.
This is the menu resource I'm inflating:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/email_refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:icon="#drawable/ic_menu_refresh"/>
<item
android:id="#+id/email_bulk_seen"
android:orderInCategory="20"
android:showAsAction="ifRoom"
android:title="#string/action_seen"
android:icon="#android:drawable/sym_action_email"/>
<item
android:id="#+id/email_bulk_delete"
android:orderInCategory="40"
android:showAsAction="ifRoom"
android:title="#string/action_delete"
android:icon="#android:drawable/ic_menu_delete"/>
</menu>
And here is an excerpt from AndroidManifest.xml, where you can sse the theme I'm using is Theme.AppCompat.Light:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.dndonline.battleclient4android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<.......................>
<activity
android:name="it.dndonline.battleclient4android.Activities.ShowEmails"
android:label="#string/title_activity_show_folders"
android:theme="#style/Theme.AppCompat.Light" >
</activity>
<......................................>
</application>
</manifest>
Everything seems correct to me, unfortunately, although tabs are loaded correctly, this means that ActionBar is properly working, none of the menu items are loaded in the action bar. Setting the showAsAction value to always doesn't change anything.
I'm testing it on Android 2.3.3.
I found the problem. There was an error in the menu .xml file. In fact, I've added a new namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
But then, I still refer the property as if it belongs to the android namespace:
android:showAsAction="ifRoom"
The right way to refer this property is to use:
app:showAsAction="ifRoom"
Cause it belongs to the namespace app.
Here is the relevant part in documentation:
If your app is using the Support Library for compatibility on versions
as low as Android 2.1, the showAsAction attribute is not available
from the android: namespace. Instead this attribute is provided by the
Support Library and you must define your own XML namespace and use
that namespace as the attribute prefix. (A custom XML namespace should
be based on your app name, but it can be any name you want and is only
accessible within the scope of the file in which you declare it.)
I found the same error, and i resolve it like this
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:showAsAction="ifRoom"
android:icon="#android:drawable/ic_delete"
android:title="Delete"/>
add,xmlns:app="http://schemas.android.com/apk/res-auto" in the middle of the code, works for me :)

Navigating back to parent activity in ActionBar without handling Up button event

I have two activities that I want this navigation to happen, they are VendorsActivity and QuestionsActivity. The following how my AndroidManifest.xml looks like:
(I am not using the full name of my activities like com.hello.world.MyActivity as I am defined package attribute in manifest node.)
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".VendorsActivity"
android:label="#string/vendors_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestsActivity"
android:label="#string/tests_activity_title"
android:parentActivityName=".VendorsActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".VendorsActivity" />
</activity>
<activity
android:name=".QuestionsActivity"
android:label="#string/questions_activity_title" >
</activity>
</application>
And in TestsActivity, I am calling getActionBar().setDisplayHomeAsUpEnabled(true); method from within onCreate method.
The problem is, it won't work unless I implement the following method in .TestsActivity class:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(TestsActivity.this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But Android Developer Guide says that I don't have to handle the Up button's event as mentioned at the very bottom of hit page: https://developer.android.com/training/basics/actionbar/adding-buttons.html
Because the system now knows MainActivity is the parent activity for
DisplayMessageActivity, when the user presses the Up button, the
system navigates to the parent activity as appropriate—you do not need
to handle the Up button's event.
Edit and Answer:
As Android Developer Guide says:
Beginning in Android 4.1 (API level 16), you can declare the logical
parent of each activity by specifying the android:parentActivityName
attribute in the element.
If your app supports Android 4.0 and lower, include the Support
Library with your app and add a element inside the
. Then specify the parent activity as the value for
android.support.PARENT_ACTIVITY, matching the
android:parentActivityName attribute.
So I think my problem was because of two reasons:
Running the app on a proper emulator. I was targeting a higher version but the emulator was running on API 14 (Android 4.0) so it didn't know how to handle android:parentActivityName attribute.
Targeting the right API level in Project Build Target properties as shown below:
If you receive an error similar to
E/NavUtils﹕ getParentActivityIntent: bad parentActivityName
or
does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)
You will probably need to change android:value to the full path.
So instead of
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
do
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.package.SecondActivity" />
android:parentActivityName attribute supported only after API level.
One alternative is using support-library:v7 combined with NavUtils.
There is a great training material about this topic (include compatibility issue).
please check
- http://developer.android.com/training/implementing-navigation/ancestral.html
That being said, i am posting my code below because it is working :-
MainActivity.Java
package com.example.activitydatasend;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button) findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Second.class);
intent.putExtra("first", "first");
intent.putExtra("second", "second");
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
SecondActivity.java
package com.example.activitydatasend;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle bundle = getIntent().getExtras();
String a = bundle.getString("first");
String b = bundle.getString("second");
System.out.println(a+b);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitydatasend"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.activitydatasend.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="com.example.activitydatasend.Second"
android:label="#string/app_name"
android:parentActivityName="com.example.activitydatasend.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/click"
android:text="Click" />
</RelativeLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
you can add metadata in manifest to support pre JellyBean OS.
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".VendorsActivity" />
What API version are you using? In my experience, for older versions it is necessary to handle the up button event yourself, whereas with later versions it's done for you.
I haven't looked into which exact version changes this, but I know that API 13 doesn't handle it for you, while API 16 does.

Categories

Resources