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.
Related
I was create android application with tab and TabHost but users cant switch between tabs via swipe.
I use TabActivity for this app. Users most click on tabs for switch between tabs or activities.
MainActivity.Java :
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
//Home Intent
Intent intentHome = new Intent().setClass(this, HomeActivity.class);
TabSpec tabSpedHome = tabHost
.newTabSpec("Home")
.setIndicator("",ressources.getDrawable(R.drawable.icon_home_config))
.setContent(intentHome);
//Search Intent
Intent intentSearch = new Intent().setClass(this, SearchActivity.class);
TabSpec tabSpedSearch = tabHost
.newTabSpec("Search")
.setIndicator("",ressources.getDrawable(R.drawable.icon_search_config))
.setContent(intentSearch);
//Archive Intent
Intent intentArchive = new Intent().setClass(this, ArchiveActivity.class);
TabSpec tabSpedArchive = tabHost
.newTabSpec("Archive")
.setIndicator("",ressources.getDrawable(R.drawable.icon_archive_config))
.setContent(intentArchive);
//Download Intent
Intent intentDownload = new Intent().setClass(this, DownloadActivity.class);
TabSpec tabSpedDownload = tabHost
.newTabSpec("Download")
.setIndicator("",ressources.getDrawable(R.drawable.icon_download_config))
.setContent(intentDownload);
//Conctactus Intent
Intent intentConctactus = new Intent().setClass(this, ContactusActivity.class);
TabSpec tabSpedConctactus = tabHost
.newTabSpec("Conctact us")
.setIndicator("",ressources.getDrawable(R.drawable.icon_contactus_config))
.setContent(intentConctactus);
//AddAllTab
tabHost.addTab(tabSpedHome);
tabHost.addTab(tabSpedSearch);
tabHost.addTab(tabSpedArchive);
tabHost.addTab(tabSpedDownload);
tabHost.addTab(tabSpedConctactus);
//Set Default Tab
tabHost.setCurrentTab(0);
}
}
And activity_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"
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>
TabActivity has been deprecated for over three years. Please use another tab solution. Particularly if you want tab contents to be able to be swiped, use ViewPager for the tab contents and a tabbed indicator (e.g., PagerTabStrip) for the tabs.
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.
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...
}
}
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.
I have created a tab based app, in one of the tab, there is also inner tab where some data is displayed in tabular format using adapter. It is perfectly ok in all devices with Android 2.1. But in all the devices with Android 2.2 devices, it is just showing only first tab only with black screen above. I have given two different screenshot for the reference.
Screenshot of 2.1 device
Screenshot of 2.2 device
XML for 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:id="#+id/myinfotrackerlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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:layout_weight="0"
android:gravity="bottom"
android:isScrollContainer="false"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="none"
android:background="#FFFFFF"/>
</LinearLayout>
</TabHost>
Code for Tabactivity
public class MyInfoTracker extends TabActivity {
private int tabid = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myinfotracker);
try{
final TabHost innerTabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
Intent myint = this.getIntent();
tabid = myint.getIntExtra("tab_id", 0);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, MyinfoActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.putExtra("addType","CD4");
// Initialize a TabSpec for each tab and add it to the TabHost
spec = innerTabHost.newTabSpec("CD4Count").setIndicator("CD4 Count").setContent(intent);
innerTabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, MyinfoActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.putExtra("addType","VL");
spec = innerTabHost.newTabSpec("ViralLoad").setIndicator("Viral Load").setContent(intent);
innerTabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, MyinfoActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.putExtra("addType","Wt");
spec = innerTabHost.newTabSpec("Weight").setIndicator("Weight").setContent(intent);
innerTabHost.addTab(spec);
innerTabHost.setScrollbarFadingEnabled(false);
innerTabHost.setScrollContainer(false);
innerTabHost.setCurrentTab(tabid);
}catch(Exception e){}
}
}
What may cause this type of problem?
It is not at all tab related prob. In onCreate() there is a code-
datePickerDialog = new DatePickerDialog(getParent(), ButtonTestDateListener,
mYear, mMonth, mDay);
I have just declare the mYear, mMonth and mDay as Integer, but no initialization was there. It is working fine in 2.1, but giving prob in 2.2. Thats why it is giving such problem.