I have a TabActivity with 4 tabs. When clicking on a button within one of the tabs and starting a new Activity (a new Activity not within the TabHost), the new Activity does not register OnClick(). The new Activity can't even show a Toast wich makes me think the TabHost is somehow blocking the ui?
When putting the Activity as one of the Tabs the OnClick works just fine.
Any ideas what the reason for this is?
I've included 3 classes:
1) The TabActivity
2) The Activity in a tab that starts:
3) The new Activity that cannot register OnClick()
1) TabActivity:
public class OverView extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
/** TabHost will have Tabs */
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
/** TabSpec used to create a new tab.
* By using TabSpec only we can able to setContent to the tab.
* By using TabSpec setIndicator() we can set name to tab. */
/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec Search = tabHost.newTabSpec("tid1");
TabSpec AllArtists = tabHost.newTabSpec("tid1");
TabSpec Favorites = tabHost.newTabSpec("tid1");
TabSpec About = tabHost.newTabSpec("tid1");
/** TabSpec setIndicator() is used to set name for the tab. */
Search. setIndicator("Search");
AllArtists. setIndicator("AllArtists");
Favorites. setIndicator("Favorites");
About. setIndicator("About");
/** TabSpec setContent() is used to set content for a particular tab. */
Search.setContent (new Intent(this, Search.class));
AllArtists.setContent (new Intent(this, AllArtists.class));
Favorites.setContent (new Intent(this, Favorites.class));
About.setContent (new Intent(this, About.class));
/** Add tabSpec to the TabHost to display. */
tabHost.addTab(Search);
tabHost.addTab(AllArtists);
tabHost.addTab(Favorites);
tabHost.addTab(About);
}
}
2) AllArtists (The Activity within the tab. Clicking a list item starts a new Activity):
public class AllArtists extends Activity {
// Debug
private final String TAG = this.getClass().getSimpleName();
// XML
EditText searchBox;
ListView listView;
// Adapter
ListAdapter listAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_allartists);
listAdapter = new ListAdapter(this, null);
// XML
listView = (ListView)findViewById(R.id.allartists_listview);
searchBox = (EditText)findViewById(R.id.allartists_searchbox);
listView.setAdapter(listAdapter);
listView.setFastScrollEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String memberID = (String)listAdapter.getID(position).toString();
if (!memberID.equals("HEADER")){
Log.d(TAG, "Jumping to Artists.class");
Intent intentArtist = new Intent (AllArtists.this, Artist.class);
intentArtist.putExtra("ID", memberID);
startActivity(intentArtist);
}
}
});
}
3) Artist (The new Activity started. This class does not register OnClick):
public class Artist extends Activity implements OnClickListener{
// Debug
private final String TAG = this.getClass().getSimpleName();
// XML
Button favorite_btn;
LinearLayout tel;
LinearLayout mob;
LinearLayout email;
LinearLayout www1;
LinearLayout www2;
LinearLayout add;
TextView name_tv;
TextView tel_tv;
TextView mob_tv;
TextView email_tv;
TextView www_tv1;
TextView www_tv2;
// Strings
String memberID;
String sirName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artist);
Toast.makeText(this, "OK, your in Activity_Artist..", Toast.LENGTH_SHORT);
// XML
favorite_btn = (Button)findViewById(R.id.artist_ib_favorite);
tel = (LinearLayout)findViewById(R.id.artist_tel_container);
mob = (LinearLayout)findViewById(R.id.artist_mob_container);
email = (LinearLayout)findViewById(R.id.artist_email_container);
www1 = (LinearLayout)findViewById(R.id.artist_www_container1);
www2 = (LinearLayout)findViewById(R.id.artist_www_container2);
add = (LinearLayout)findViewById(R.id.artist_add_container);
name_tv = (TextView)findViewById(R.id.artist_tv_name);
tel_tv = (TextView)findViewById(R.id.artist_tel_dynamic);
mob_tv = (TextView)findViewById(R.id.artist_mob_dynamic);
email_tv = (TextView)findViewById(R.id.artist_email_dynamic);
www_tv1 = (TextView)findViewById(R.id.artist_www_dynamic1);
www_tv2 = (TextView)findViewById(R.id.artist_www_dynamic2);
// OnClickListeners
favorite_btn.setOnClickListener(this);
tel.setOnClickListener(this);
mob.setOnClickListener(this);
email.setOnClickListener(this);
www1.setOnClickListener(this);
www2.setOnClickListener(this);
add.setOnClickListener(this);
// Code here to get memberID for fillContent()
}
private void fillContent(String memberID) throws JSONException {
// Code here to fill the TextViews etc with content from the DataBase.
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.artist_ib_favorite:
Toast.makeText(this, "onClick", Toast.LENGTH_SHORT);
Log.d(TAG, "Neo is attempting to insert member into favorites");
MyDB db = new MyDB(this);
db.insertFavorite(memberID, sirName);
break;
case R.id.artist_tel_container:
break;
case R.id.artist_mob_container:
Log.d(TAG, "OMG CLICKED THE MOBILE!");
break;
case R.id.artist_email_container:
break;
case R.id.artist_www_container1:
break;
case R.id.artist_add_container:
break;
}
}
Thanks ;)
For Toast you need to call show.
Toast.makeText(this, "OK, your in Activity_Artist..", Toast.LENGTH_SHORT).show();
For the click Operation on anything apart from Button you need to define
android:clickable="true"
in the layout.
Solved this. In the layout there was an (empty) GridView at the bottom of the layout set to android:layout_height="fill_parent" wich stole the touchevent.
The weird part about this is that when putting the exact same activity with the exact same XML inside a tab, the onClick() worked fine.
Related
I am developing android application which having multiple tabs.
Now in launcher activity tabs display perfectly and can navigate through the tabs.
but if i call activity(which i wanted to show as Tab) on Button Clicked then tabs seems disappear.
please refer the given code and let me know if i am doing something wrong.
This is Main TabActivity
public class MyTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabHost tabHost=getTabHost();
TabSpec deshTab=tabHost.newTabSpec("Deshboard");
deshTab.setIndicator("Deshboard");
Intent DeshboardIntent=new Intent(this,DeshboardActivity.class);
deshTab.setContent(DeshboardIntent);
tabHost.addTab(deshTab);
TabSpec clientTab=tabHost.newTabSpec("client");
clientTab.setIndicator("client");
Intent intent=new Intent(this,ClientActivity.class);
clientTab.setContent(intent);
tabHost.addTab(clientTab);
}
}
Now i wanted to start client activity like
void onButtonClick(View view)
{
int id = view.getId();
switch(id)
{
case R.id.client_btn:
Intent clientIntent = new Intent(DeshboardActivity.this,ClientActivity.class);
startActivity(clientIntent);
break;
}
}
but when i click this button it starts new activity but NOT in tab.
what should i do to display this activity in tab on button click also.?
Below code is ClientActivity which i wanted to display as TAB.
public class ClientActivity extends Activity
{
private DatabaseHandler dbHandler;
private ListView clientListView ;
private BaseAdapter listAdapter;
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.clientactivity);
dbHandler = new DatabaseHandler(this);
final List<Client> clientList = dbHandler.getAllclient();
clientListView = (ListView)findViewById(R.id.client_list);
listAdapter = new ClientListAdapter(this, clientList);
clientListView.setAdapter(listAdapter);
clientListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Client client = clientList.get(position);
Toast.makeText(getApplicationContext(), client.getFirstName(), Toast.LENGTH_LONG).show();
Intent clientInfoIntent = new Intent(getApplicationContext(), ClientInfoActivity.class);
clientInfoIntent.putExtra("client",client);
startActivity(clientInfoIntent);
//finish();
}
});
}
}
That's because your ClientActivity is launched on top of your TabActivity. That way your tabbar is not shown anymore.
I suggest you use fragments for your ClientActivity.
A tutorial how to use fragments:
http://developer.android.com/guide/components/fragments.html
I have an android program which sends to another activity after clicking on a button. Mainly, I want to set the text from textview in the new windows so that it corresponds to the selected button. For example, if I click on button Writers, the next new activity should have a textview on which the word Writers appears. Everything works fine, except when I try to setText on the TextView icon for category. I also tried to call this change in the first activity, before launching the second one, it didn't work.
I also mention that if I comment the line with the setText, the program works just fine.
private String category;
public final static String CATEGORY_MESSAGE = "e.c.project.CATEGORY";
public void onClick(View v) {
switch(v.getId())
{
case R.id.actors:
category = "actors";
playTheGame(v);
break;
case R.id.cartoons:
category = "cartoons";
playTheGame(v);
break;
case R.id.singers:
category = "singers";
playTheGame(v);
break;
case R.id.writers:
category = "writers";
playTheGame(v);
break;
}
}
public void playTheGame( View view ){
Intent intent = new Intent(this, PlayGame.class);
String category = playGameButton.getText().toString();
intent.putExtra(CATEGORY_MESSAGE, category);
// TextView tv = (TextView) findViewById(R.id.categoryTV);
// tv.setText(category);
startActivity(intent);
}
this is the OnCreate method from the second activity:
private TextView categoryTV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String category = intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);
categoryTV = (TextView) findViewById(R.id.categoryTV);
categoryTV.setText(category);
setContentView(R.layout.activity_play_game);
// Show the Up button in the action bar.
setupActionBar();
}
You need to call setContentView(R.layout.activity_play_game); before categoryTV = (TextView) findViewById(R.id.categoryTV); Otherwise your TextView is null
private TextView, categoryTV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game); // make this call BEFORE initializing ANY views
Intent intent = getIntent();
String category = intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);
categoryTV = (TextView) findViewById(R.id.categoryTV);
categoryTV.setText(category);
// Show the Up button in the action bar.
setupActionBar();
}
Your Views exist in your Layout so if you try to access any of them before inflating your Layout, with setContentView() or an inflater, they will be null resulting in a NPE when you try to call a method on them such as setText()
Calling Specific Method from ListActivity which invokes method in one of the Parent(TabActivity) w/o Losing TabHost Layout
I have Two Activity Inside TabHost
1.PlayingActivity
2.AlbumActivity
By Clicking Button inside AlbumActivty will jump to LIstActivity
->By clicking Item in ListActivity, I want to jump back to one of the method inside-PlayingActivity w/o Losing Tab Layout.
I can accomplish task by calling Activity n specific Method using these
ListActivity clicking on Item invokes specific Method in PlayingActivity
Class SongList extends ListActivty
{
public void onCreate(Bundle savedInstanceState)
{
//implemented list adapter-ls
listadapter. . . .
ls.setOnItemClickListener(New View.onItemClickListener)
{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
int songIndex=position;
Intent i=new Intent(getApplicationContext(),AlbumActivity.class);
i.putExtra("methodName", "myMethod");
i.putExtra("index", songIndex);
startActivity(i);
}
}
}
}
MainActivity which host PlayingActivity & AblumActivity Under TabHost
public class MainActivity extends TabActivity {
private static final String NOW_PLAYING = "Playing";
private static final String ALBUM = "Album";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Playing Tab
TabSpec PlayingSpec = tabHost.newTabSpec(NOW_PLAYING);
// Tab Icon
PlayingSpec.setIndicator(NOW_PLAYING, getResources().getDrawable(R.drawable.icon_now_playing));
Intent PlayingIntent = new Intent(this, PlayingActivity.class);
// Tab Content
PlayingSpec.setContent(PlayingIntent);
// Album Tab
TabSpec AlbumSpec = tabHost.newTabSpec(ALBUM);
AlbumSpec.setIndicator(ALBUM, getResources().getDrawable(R.drawable.icon_music));
Intent AlbumIntent = new Intent(this, AlbumActivity.class);
AlbumSpec.setContent(AlbumIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(PlayingSpec); // Adding Playing tab
tabHost.addTab(AlbumSpec); // Adding Album tab
}
}
In PlayingActivity called specific method(playSong())
class PlayingActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String index=intent.getStringExtra("index");
if(intent.getStringExtra("methodName").equals("myMethod")){
playSong(Integer.parseInt(index));
}
}
private void playSong(int i)
{
}
}
Now the Catch iz I can somehow invokes specific Method in PlayingActivity but Playing Activity which is under TabHost loses it's TabLayout
Is there AnyWay we can save Playing Activity to lose from it's TabLayout??
Just Found out that. Little bit of logic have saved my time.
Adding
finish();
inside the activity would done all my work.
I am making an application where i have set two bottom tabs,
namely "Exit" and "Back".
Now i want to exit from the application on the click of the "Exit" tab(not button).
How can i do that, i know that in android we can never actually close the application, but i want to go to the Home-screen on clicking "exit".
i have studied following link also along with other links
Is quitting an application frowned upon?
EDIT
public class Man_age_ur_PhoneActivity extends TabActivity {
/** Called when the activity is first created. */
ListView listview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
setTabs();
}
private void setTabs()
{
addTab("Exit", R.drawable.tab_exit);
addTab("Back", R.drawable.tab_back);
//To add more tabs just use addTab() method here like previous line.
}
private void addTab(String labelId, int drawableId)
{
TabHost tabHost = getTabHost();
// Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
// spec.setContent(intent);
tabHost.addTab(spec);
}
}
on Click of your second tab , you just have to write "finish();". This will close previously opened activities and you can exit from your application.
you can use this code
public class Man_age_ur_PhoneActivity extends TabActivity {
/** Called when the activity is first created. */
ListView listview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
setTabs();
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = getTabHost().getCurrentTab();
Log.i("######## ANN CLICK TAB NUMBER", "------" + i);
if (i == 0) {
Log.i("########## Inside onClick tab 0", "onClick tab");
}
else if (i ==1) {
Log.i("########## Inside onClick tab 1", "onClick tab");
}
}
});
}
private void setTabs()
{
addTab("Exit", R.drawable.tab_exit);
addTab("Back", R.drawable.tab_back);
//To add more tabs just use addTab() method here like previous line.
}
private void addTab(String labelId, int drawableId)
{
TabHost tabHost = getTabHost();
// Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
// spec.setContent(intent);
tabHost.addTab(spec);
}
}
Also import this
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
Taken from this link
Android TabWidget detect click on current tab
Generally Android apps shouldn't have an exit option, see for instance this post: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
If you really want to, you can also just call System.exit(), this is guaranteed to close down the application completely.
I am working on an application that pulls information from the internet. The information is sorted into categories, sub-categories and, sub-sub-categories.
My main view is a TabHost view (the parent categories) with 3 tabs, and the initial list view (the sub-categories). When the user clicks an item in the list view it calls a new list view that displays the child-categories of the chosen sub-category.
I got everything to work except that when a sub category is chosen the tabHost view disappears and the sub-sub-categories are displayed in full screen.
How can I change the intent of the tab to display the child-categories of the sub-category?
EDIT: here is my code, sorry I didn't post it earlier!
My Main view which contains the tabhost:
public class tabwidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, category1Activity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("category1").setIndicator("Category1",
res.getDrawable(R.drawable.ic_tab_category1))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, category2Activity.class);
spec = tabHost.newTabSpec("category2").setIndicator("Category2",
res.getDrawable(R.drawable.ic_tab_category2))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, category3Activity.class);
spec = tabHost.newTabSpec("category3").setIndicator("Category3",
res.getDrawable(R.drawable.ic_tab_category3))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
When the application is started the alcohol tab is selected by default. This is the category1Acitivity listview with the onlclick action that calls the child-categories:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(), "You clicked item at position"+position,
//Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Loading "+((TextView) view.findViewById(R.id.categoryname)).getText(),
Toast.LENGTH_SHORT).show();
Intent i = new Intent(category1Activity.this, subCategoryActivity.class);
i.putExtra("id", ((TextView) view.findViewById(R.id.message)).getText());
i.putExtra("catname", ((TextView) view.findViewById(R.id.categoryname)).getText());
i.putExtra("parentcatid", "0");
startActivityForResult(i, ACTIVITY_CREATE);
}
});
The listviews are generated by the category Id which is sent to the server pulls results from the database.
You will have to use ActivityGroups to do that.
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity
However, keep in mind that ActivityGroups are deprecated in ICS.
EDIT: This is my implementation of ActivityGroup:
Activity in a tab:
Intent i = new Intent(v.getContext(), SearchList.class);
i.putExtra("search", search);
View view = SearchActivityGroup.group.getLocalActivityManager()
.startActivity("SearchList", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
SearchActivityGroup.group.replaceView(view);
ActivityGroup:
package nl.dante.SuperDeals;
import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SearchActivityGroup extends ActivityGroup {
View rootView;
// Keep this in a static variable to make it accessible for all the nested
// activities, lets them manipulate the view
public static SearchActivityGroup group;
// Need to keep track of the history if you want the back-button to work
// properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* this.history = new ArrayList<View>(); group = this;
*
* // Start the root activity within the group and get its view View
* view = getLocalActivityManager().startActivity("Search", new
* Intent(this,Search.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
* .getDecorView();
*
* // Replace the view of this ActivityGroup replaceView(view);
*/
}
#Override
protected void onResume() {
super.onResume();
this.history = new ArrayList<View>();
group = this;
// Start the root activity within the group and get its view
View view = getLocalActivityManager().startActivity("Search", new Intent(this, Search.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
if (history.size() == 0) {
if (rootView != null) {
history.add(rootView);
rootView = null;
}
}
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
public void back() {
try {
if (history.size() > 0) {
if (history.size() == 1) {
rootView = history.get(0);
Toasts.ToastImageView(this, "Druk nogmaals BACK om af te sluiten", R.drawable.power_64_off, "red");
}
history.remove(history.size() - 1);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
if (history.size() < 3) {
// Tabhost.bannerImage2.setImageResource(0);
Tabhost.banner.setBackgroundResource(R.drawable.gradient_blue);
}
if (history.size() == 2) {
Tabhost.bannerImage1.setImageResource(R.drawable.sorteer_btn);
}
} catch (Exception ex) {
}
}
public int getHistorySize() {
return history.size();
}
#Override
public void onBackPressed() {
try {
SearchActivityGroup.group.back();
} catch (Exception ex) {
}
return;
}
}