Open a TabActivity with "simple" Activity - android

I have a problem when I try to open a TabActivity with an intent from an Activity.
The code of my activity (ConnexionActivity), which is NOT an activity from a tab :
buttonConnexion.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(ConnexionActivity.this, NeurokiffMobileActivity.class);
startActivity(intent);
}
});
and the TabActivity (NeurokiffMobileActivity) :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getIntent();
/* ******** Gestion Onglets ******** */
res = getResources(); // Resource object to get Drawables
tabHost = (TabHost) findViewById(android.R.id.tabhost); // The activity TabHost
/* *** Onglet "Evènements" *** */
/* Intent responsable de lancer l'activité depuis l'onglet */
intent = new Intent().setClass(this, EvenementActivity.class);
bundleEnvoye.putString("id_user", idUser);
intent.putExtras(bundleEnvoye);
/* Crée un TabSpec et l'ajoute au TabHost */
spec = tabHost.newTabSpec("evenement").setIndicator("Evenements",
res.getDrawable(R.drawable.ic_tab_evenement))
.setContent(intent);
tabHost.addTab(spec);
/* *** Onglet "Favoris" *** */
intent = new Intent().setClass(this, FavorisActivity.class);
bundleEnvoye.putString("id_user", idUser);
intent.putExtras(bundleEnvoye);
spec = tabHost.newTabSpec("favoris").setIndicator("Favoris",
res.getDrawable(R.drawable.ic_tab_favoris))
.setContent(intent);
tabHost.addTab(spec);
/* *** Onglet "Kiffs" *** */
intent = new Intent().setClass(this, KiffsActivity.class);
bundleEnvoye.putString("id_user", idUser);
intent.putExtras(bundleEnvoye);
spec = tabHost.newTabSpec("kiffs").setIndicator("Kiffs",
res.getDrawable(R.drawable.ic_tab_kiffs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
and the .xml of this TabActivity :
<?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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</TabHost>
The error is a NullPointerException on the onCreate method of NeurokiffMobileActivity, and the application close.
When I put another "simple" Activity instead of NeurokiffMobileActivity in the intent, it works.
Can someone help me, please ? It seems to be a problem due to TabActivity, but I don't know which...
Thanks in advance ! ;)

Here is the sample for you
showtab.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" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabcontent" >
</FrameLayout>
</LinearLayout>
</TabHost>
Below is the code of AfterLoginTabActivity(a tabactivity to be called)
public class AfterLoginTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.afterloginview);
#SuppressWarnings("deprecation")
TabHost tabHost =getTabHost();
Intent i = new Intent(AfterLoginTabActivity.this,ProfileClass.class);
TabSpec profileSpec = tabHost.newTabSpec("profile").setIndicator("Profile").setContent(i);
Intent j = new Intent(AfterLoginTabActivity.this,GroupClass.class);
TabSpec groupSpec = tabHost.newTabSpec("group").setIndicator("group").setContent(j);
tabHost.addTab(profileSpec);
tabHost.addTab(groupSpec);
tabHost.setCurrentTab(1);
}
}
Code i used inside my button.setOnClickListener
if(msg == "success")
{
System.out.println("Inside button");
try {
Intent intent = new Intent(MainActivity.this,AfterLoginTabActivity.class);
startActivity(intent);
finish();
} catch (Exception e) {
// TODO: handle exception
System.out.print("tHIS IS EXCCEPTION" + e);
}
}

Related

How to remove pixel errors when using TabHost

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>

Listening click TabWidget

My tabhost tools.java:
#Override
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);
}}
Each intent calls an activity.
and within each "file. java" commands have to perform the calculations.
Within "dados.java" is where I get the information of the User.
When they click on the tab "calcular" must verify that all data has been completed.
I guess I need a "onclicklistener" for each tab, like a button, right??
How? how to create an event to run code when a tab"Dados" tab"Legenda" tab"calcular" is clicked?
Attention: I need to check which tab was clicked from the file "ToolDadosCircular.java."
Edit:
tabtools.xml code:
<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>
</TabHost>
Try
tabHost.getChildAt(yourtab).setOnClickListener(new View.onClickListener()
{
#Override public void onClick(View view){
//your code
}
}
Edit: I'm guessing your class is like this
public class ToolDadosTubuCirc extends Activity{
#Override public void onCreate(Bundle bundle){
//code...
TabHost tabHost = (TabHost) getParent().findViewById(yourTabHostId);
//do what you want with tabHost
//code...
}
}

trying to creat welcom screen with AsyncTask

I am trying to creat welcome screen and after some time to display my tabs.
But get some error like:
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'
my tabs on one xml file, that works well before I did asynctask.
my code is :
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) {
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(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);
}
}
where my xml for welcome is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
</LinearLayout>
and the tabs 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">
<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>
</TabHost>
thanks for help!
Your asynctask does not have th e gettabhost() method you should rewrite this part. Gettabhost is only available to a class which extends tabactivity and not your asynctask.
Cheers
Your tabhost must have android.R.id.tabhost id see this example...
http://dewful.com/?p=15

load two .swf flies in a Tab Layout

I use a Tab Layout display two .swf files and a imageView in three tabs. However, when I wanted to change to imageView from a swf tab. The swf still exits.
//TabActivity
public class TabSwfActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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, OperatorActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("artists")
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, LearningActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("albums")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, QuestionActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("songs")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
//main.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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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" />
</LinearLayout>
</TabHost>
//swf tab learningActivity.java
public class LearningActivity extends Activity {
private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learninglayout);
String url ="file:///android_asset/sample/simple.swf";
mWebView=(WebView) findViewById(R.id.webView1);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl(url);
}
}
// learning.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</WebView>
</LinearLayout>
//picture tab
public class QuestionActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionlayout);
}
}

TabLayout Android, customize background color

in android tutorial there isn't a way for customize tab background. my app is official-tutorial based..can anyone help me with a tutorial or code for backgroundcolor white?
this is my code:
LauncherActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launcher);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, MyActivity.class);
spec = tabHost.newTabSpec("myactivity").setIndicator("Activity")
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, search.class);
spec = tabHost.newTabSpec("search").setIndicator("Search"
)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyActivity3.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs"
)
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
Launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="[url]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="0dp" />
</LinearLayout>
</TabHost>
If you mean to change background color of tabs,then here is the code:
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); //unselected tab
}
for selected tab,
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.GRAY); // selected tab
You can put above lines into a method (say setTabColors()) and call it:
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
setTabColors(tabHost);
}
});

Categories

Resources