I have a tab widget at the bottom of an activity , which consist of an icon and text but with my theme its only showing texts .
I have seen that , this problem occurring which with my current project theme which is "Theme.AppCompat.Light.NoActionBar" . And of i use "NOTITLEBAR" theme it works fine but gives me other problem due to which i cannot use "No TITLE BAR" theme .
i really don't know why this is happening . Please help me in finding
bug . Also,suggestion are welcome.
TabWidget.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:showDividers="middle"
android:divider="#color/dividerColor"
android:background="#color/button_color"
android:tabStripEnabled="false"
/>
</LinearLayout>
</TabHost>
TabActivity
public class MainActivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// stop executing code by return
return;
}
context = getApplicationContext();
// TabHost tabHost = getTabHost();
tabHost = getTabHost();
SharedPreferences preferences2 = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String u_id1 = preferences2.getString("Userid","");
Bean b = new Bean();
b.setDeviceid(u_id1);
// tabHost.getTabWidget().getResources().getDrawable(R.drawable.tab);
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Favourite");
photospec.setIndicator("Favorites", getResources().getDrawable(R.drawable.tab_favourites));
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Contacts");
// setting Title and Icon for the Tab
songspec.setIndicator("Contacts", getResources().getDrawable(R.drawable.tab_contacts));
Intent songsIntent = new Intent(this, Contacts_new.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Meeting");
videospec.setIndicator("Meeting",getResources().getDrawable(R.drawable.tab_meeting));
Intent videosIntent = new Intent(this, Meeting.class);
videospec.setContent(videosIntent);
// Tab for Future SMS
TabSpec futuresms = tabHost.newTabSpec("Map");
futuresms.setIndicator("Map",getResources().getDrawable(R.drawable.tab_map));
Intent futureIntent = new Intent(this, Map.class);
futuresms.setContent(futureIntent);
// Tab for Future SMS
TabSpec setting = tabHost.newTabSpec("Setting");
setting.setIndicator("Setting",getResources().getDrawable(R.drawable.tab_setting));
Intent settingintent = new Intent(this, Setting.class);
setting.setContent(settingintent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
tabHost.addTab(futuresms);
tabHost.addTab(setting);
for (int j=0;j<tabHost.getTabWidget().getChildCount();j++){
TextView textView = (TextView)tabHost.getTabWidget().getChildAt(j).findViewById(android.R.id.title);
textView.setTextSize(5);
}
for (int i = 0; i <tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundColor(getResources().getColor(R.color.button_color));
}
String tabid = Bean.getDeviceid();
if(tabid == null)
{
int in = Integer.parseInt("2");
tabHost.setCurrentTab(in);
}
else
{
int in = Integer.parseInt(tabid);
tabHost.setCurrentTab(in);
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("msg","Hi");
super.onDestroy();
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
System.gc();
MainActivity.this.finish();
System.exit(0);
}
}
Theme i am using
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/green_top_bar</item>
<item name="colorPrimaryDark">#color/green_top_bar</item>
<item name="colorControlNormal">#ECEFF1</item>
<!--<item name="colorAccent"></item>-->
<item name = "android:textColorSecondary">#727272</item>
Image(showing issue) :
This is happening due to the height of the bottom bar lets test if by increasing the height of bottom bar, then sure your will see the image.
Related
What i want to do : I want to make exactly like below :
i managed to do something like below :
Code:
ISSUE : i want to following things :
i want to add divider the same in above picture .
i want to remove the space between the icon and text . Also want my icon in centre .
Following is code :
Tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:showDividers="middle"
android:background="#color/button_color"
android:tabStripEnabled="false"
/>
</LinearLayout>
</TabHost>
TabActivity.class
public class MainActivity extends TabActivity {
protected static Context context;
TabHost tabHost;
SharedPreferences mpref;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
// TabHost tabHost = getTabHost();
tabHost = getTabHost();
SharedPreferences preferences2 = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String u_id1 = preferences2.getString("Userid","");
Bean b = new Bean();
b.setDeviceid(u_id1);
// tabHost.getTabWidget().getResources().getDrawable(R.drawable.tab);
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Favourite");
photospec.setIndicator("Favorites", getResources().getDrawable(R.drawable.tab_favourites));
Intent photosIntent = new Intent(this, Favourite.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Contacts");
// setting Title and Icon for the Tab
songspec.setIndicator("Contacts", getResources().getDrawable(R.drawable.tab_contacts));
Intent songsIntent = new Intent(this, Contacts.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Meeting");
videospec.setIndicator("Meeting",getResources().getDrawable(R.drawable.tab_meeting));
Intent videosIntent = new Intent(this, Meeting.class);
videospec.setContent(videosIntent);
// Tab for Future SMS
TabSpec futuresms = tabHost.newTabSpec("Map");
futuresms.setIndicator("Map",getResources().getDrawable(R.drawable.tab_map));
Intent futureIntent = new Intent(this, Map.class);
futuresms.setContent(futureIntent);
// Tab for Future SMS
TabSpec setting = tabHost.newTabSpec("Setting");
setting.setIndicator("Setting",getResources().getDrawable(R.drawable.tab_setting));
Intent settingintent = new Intent(this, Setting.class);
setting.setContent(settingintent);
tabHost.getTabWidget().setDividerDrawable(R.color.dividerColor);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
tabHost.addTab(futuresms);
tabHost.addTab(setting);
// tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_favourites);
// tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_contacts);
// tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab_meeting);
// tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab_map);
// tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.tab_setting);
int k = tabHost.getTabWidget().getTabCount();
for (int j=0;j<tabHost.getTabWidget().getChildCount();j++){
TextView textView = (TextView)tabHost.getTabWidget().getChildAt(j).findViewById(android.R.id.title);
textView.setTextSize(10);
}
for (int i = 0; i <tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundColor(getResources().getColor(R.color.button_color));
}
// ((TextView)tabHost.getChildAt(2).findViewById(android.R.id.title)).setTextSize(10);
// ((TextView)tabHost.getChildAt(3).findViewById(android.R.id.title)).setTextSize(10);
// ((TextView)tabHost.getChildAt(4).findViewById(android.R.id.title)).setTextSize(10);
// tv.setTextSize(20);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_click);
int in = Integer.parseInt(tabid);
tabHost.setCurrentTab(in);
}
}
Please help me to achieve this either by some other approach or some
snippet .
This is my MainActivity.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
registerReceiver(myBroadcastReceiver1, new IntentFilter(
MYBROADCAST_INTENTFILTER));
Intent intentFriends = new Intent().setClass(this,
TabFriendsActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabFriends = tabHost
.newTabSpec("Friends")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_friends))
.setContent(intentFriends);
// Talks tab
Intent intentTalks = new Intent()
.setClass(this, TabTalksActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabTalks = tabHost
.newTabSpec("Talks")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_talk))
.setContent(intentTalks);
// Add friends tab
Intent intentAddFriends = new Intent().setClass(this,
TabAddFriendsActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabAddFriends = tabHost
.newTabSpec("Add friends")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_addfriend))
.setContent(intentAddFriends);
// Other tab
Intent intentOther = new Intent()
.setClass(this, TabOtherActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabSpecOther = tabHost
.newTabSpec("Other")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_other))
.setContent(intentOther);
// add all tabs
tabHost.addTab(tabFriends);
tabHost.addTab(tabTalks);
tabHost.addTab(tabAddFriends);
tabHost.addTab(tabSpecOther);
// set Windows tab as default (zero based)
tabHost.setCurrentTab(0);
TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);
badge = new BadgeView(this, tabs, 1);
badge.setText("new");
}
I want to show text "new" in tabTalks when I got message from GCM by BroadcastReceiver. I try to debug and got information of "intentChatting" that means BroadcastReceiver is working, but text "new" not show in tabTalks icon.
private BroadcastReceiver myBroadcastReceiver1 = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String intentChatting = intent.getExtras().get("Intent").toString();
if (intentChatting != null) {
pushBadge(intentChatting);
}
}
};
private void pushBadge(String intentChating) {
badge.show();
}
And this is my main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f2f2f2">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="462dp"
android:layout_weight="0.07">
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000" />
</LinearLayout>
</TabHost>
So, what wrong with my code. I want to show badge in tabhost when receiver message from GCMby BroadcastReceiver. But it's not work!
Any help much appreciated! Thanks.
We have a strange problem concerning a TabHost-widget in Android. How can we fix the pixel-Line error (shown on the image below):
Thanks for your help.
I also have Same problem before some days. But now got solution with changing it to images.
By Default First Tab is selected and when the Tab Getting change the all tab get changed automatically based on the Tab Selection.
See below Code:
public class MainTabActivity extends TabActivity {
TabHost mTabHost;
TabWidget mTabWidget;
private TextView title;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_layout);
//Resources res = getResources();
mTabHost = getTabHost();
TabHost.TabSpec spec;
mTabWidget = mTabHost.getTabWidget();
Intent intent;
intent = new Intent().setClass(MainTabActivity.this, TodaysDealsGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_todays_deal)).setIndicator(getResources().getString(R.string.tab_todays_deal), getResources().getDrawable(R.drawable.tab_loyalshop_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, BuddiesGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_buddies)).setIndicator(getResources().getString(R.string.tab_buddies), getResources().getDrawable(R.drawable.tab_buddies_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, SearchGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_search)).setIndicator(getResources().getString(R.string.tab_search), getResources().getDrawable(R.drawable.tab_search_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, ProfileGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_profile)).setIndicator(getResources().getString(R.string.tab_profile), getResources().getDrawable(R.drawable.tab_profile_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, NotificationsGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_notifications)).setIndicator(getResources().getString(R.string.tab_notifications), getResources().getDrawable(R.drawable.tab_notification_selector)).setContent(intent);
mTabHost.addTab(spec);
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg));
title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title);
title.setTextColor(Color.WHITE);
title.setTextSize(10);
}
// check if App starts from the Notification click or not
if(getIntent().hasExtra("notification")){
// for the current tab selection
mTabHost.getTabWidget().getChildAt(4).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
mTabHost.setCurrentTab(4);
title = (TextView) mTabWidget.getChildAt(4).findViewById(android.R.id.title);
}else{
// for the current tab selection
mTabHost.getTabWidget().getChildAt(0).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
mTabHost.setCurrentTab(0);
title = (TextView) mTabWidget.getChildAt(0).findViewById(android.R.id.title);
}
title.setTextColor(Color.BLACK);
title.setTextSize(10);
// listener for the tab changed
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg));
title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title);
title.setTextColor(Color.WHITE);
title.setTextSize(10);
}
int tab = mTabHost.getCurrentTab();
mTabHost.getTabWidget().getChildAt(tab).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
title = (TextView) mTabWidget.getChildAt(tab).findViewById(android.R.id.title);
title.setTextColor(Color.BLACK);
title.setTextSize(10);
}
});
}
}
And here is the XMl file main_tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:tabStripEnabled="false" >
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
Just Replace with your images and See the Result.
Hope this will help you a lot as it help me.
Enjoy Coding...
I have three tabs and a custom divider, however a custom xml (customtabview.xml) is used for all five tabs.
Tabs.java
tabHost = getTabHost();
TabHost.TabSpec spec;
LinearLayout v = (LinearLayout)(getLayoutInflater().inflate(R.layout.customtabview, null));
//FIRST TAB
((TextView)v.findViewById(R.id.title)).setText("FIRST");
((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable .tab_back,0,0 );
spec = tabHost.newTabSpec("first").setIndicator(v).setContent(R.id.tab_first_info);
tabHost.addTab(spec);
//SECOND TAB
((TextView)v.findViewById(R.id.title)).setText("SECOND");
((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0 );
spec = tabHost.newTabSpec("second").setIndicator(v).setContent(R.id.tab_second_info);
tabHost.addTab(spec);
//THIRD TAB
((TextView)v.findViewById(R.id.title)).setText("THIRD");
((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0 );
spec = tabHost.newTabSpec("third").setIndicator(v).setContent(R.id.tab_third_info);
tabHost.addTab(spec);
customtabview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="#+id/title"
android:drawablePadding="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
My Tools.java :
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabtools);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// TabDados
intent = new Intent().setClass(this, ToolDadosTubuCirc.class);
spec = tabHost.newTabSpec("dados")
.setIndicator("Dados", res.getDrawable(R.drawable.icondados))
.setContent(intent);
tabHost.addTab(spec);
// TabLegenda
intent = new Intent().setClass(this, ToolLegendaTubuCirc.class);
spec = tabHost
.newTabSpec("legenda")
.setIndicator("Legenda",
res.getDrawable(R.drawable.iconlegenda))
.setContent(intent);
tabHost.addTab(spec);
// TabCalcular
intent = new Intent().setClass(this, ToolCalcularTubuCirc.class);
spec = tabHost
.newTabSpec("calcular")
.setIndicator("Calcular",
res.getDrawable(R.drawable.iconcalcular))
.setContent(intent);
tabHost.addTab(spec);
// TabCorrente
tabHost.setCurrentTab(0);}}
My tabtools.xml
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+layout/rowLog"
android:layout_below="#+layout/rowLine" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
The first tab is called the class "ToolDadosTubuCirc.java" and this activity has the following code:
package br.com.mobile4you.engtools;
import android.app.Activity;
import android.os.Bundle;
public class ToolCalcularTubuCirc extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.toolcalculartubucirc);
}
}
**How to create an event within the file onClickListener "ToolDadosTubuCirc.java"?
I need to create a function that when people click on the tab "calcular" do some test data it should fill in the tab "dados".
I have 3 tabs: tab1 = data; tab2 = legend; tab3 = calculate.
The corrent tab is the "data". I need to check when the User click on the tab "calculate" to all fields of the tab "Data" was completed. I do not know create the onclickListener event for TabDados in other activity(class). I dont know the id of the tabhost and TabWidget.
How to create this event? Thank you!**
If the id of my tabhost is standard android and my id is also TabWidget. I am not able to do this event. Help me.
Question is unclear
"I need to create a function that when people click on the tab". Assume mTabWidget is your TabWidget control, and nTabOffset = 0, i.e. the offset of your dados tab:
mTabWidget.getChildAt(nTabOffset).setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO:
}
});
TabHost will instantiate your activity and call onCreate(). You would initialize your activities views there, though if you want a tab click event to re-initiate some data update, you could do this via the onClick() method above, perhaps sending a broadcast intent that is registered in your activity.
I am trying to work with AsyncTasks in Android 2.1 but get errors on my code.
I want to display to the user logo and after that to display my tabs but get errors on my AsyncTask.If you can see my code and tell me where my errors are.
the error is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.News/startPakage.tabs}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
where my code is:
public class tabs extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logoscreen);
new GetDataTask(this).execute();
private class GetDataTask extends AsyncTask<Void, Void, Integer> {
Context context;
GetDataTask(Context context){this.context=context;}
protected Integer doInBackground(Void... params) {
int waited = 0;
while (waited < 5000) {
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
waited += 100;
}
return 1;
}
protected void onPostExecute(Integer result) {
tabs.this.setContentView(R.layout.tabs);
//setContentView(R.layout.tabs);
TabHost tabHost= (TabHost)tabs.this.findViewById( android.R.id.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(context,start.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Heb news").setIndicator("Heb news").setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(context, rusNewsP.ListRusNews.class);
spec = tabHost.newTabSpec("Rus News").setIndicator("Rus News").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
And my xml is:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
thanks a lot!
The problem is that you're using a TabActivity but the first time you set the content view you do NOT have a tabhost (in the R.layout.logoscreen file). You can add the tabhost to your logo layout (it won't hurt) and it should work. Alternatively, you can split the code into two activities and just kick off a new activity after the AsyncTask is done.
You can try this:
Give id as : android:id="#+id/tabhost"
and in your code use:
TabHost tabHost= (TabHost)tabs.this.findViewById( R.id.tabhost );
Do this for all the declarations in your xml file.