I am trying to get an Activity to show in a tab but just can't get it to work.
I have checked that the activity shows when on its own but as soon as I try to put it in a tab I get an empty tab.
This is the tab screen and its XML layout file:
public class TestTabs extends TabActivity{
private TabHost tabHost; // The tabhost for all the tabs
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.tabbed_screen);
tabHost = getTabHost();
initTabs();
}
protected void addTab(String tabName, Intent tabIntent){
TabSpec tabSpec = tabHost.newTabSpec(tabName);
tabSpec.setIndicator(tabName);
tabSpec.setContent(tabIntent);
tabHost.addTab(tabSpec);
}
protected void initTabs() {
addTab("1",new Intent(this,TestActivity.class));
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout android:id="#+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="#android:id/tabs" android:layout_height="fill_parent" android:layout_width="fill_parent"></TabWidget>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
This is the "child" activity:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// The super.onCreate() call will set the general frame also for this Screen:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Layout:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
android:text="Hello World"
android:textSize="12sp"
android:textStyle="bold" />
As I said, the child activity shows ok when alone but when I put it in the tab I get the tab without any contents.
I am probably missing something obvious but this is the first time I have done anything with tabs in Android.
You should change name of method "addTab" because its default method of TabActivity that might be causing this problem.secondly,your code should be like:
public class TestTabs extends TabActivity{
private TabHost tabHost; // The tabhost for all the tabs
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.tabbed_screen);
tabHost = getTabHost();
TabSpec tabSpec;
Intent intent = new Intent().setClass(this, tab1_activity.class);
tabSpec = tabHost.newTabSpec("Tab-1").setIndicator("Tab-1",drawable1_obj).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, tab2_activity.class);
tabSpec = tabHost.newTabSpec("Tab-2").setIndicator("Tab-2",drawable2_obj).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
Related
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 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
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 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);
}
}
I have an android activity in which I'm using tabs.
public class UnitActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unit_view);
TabHost tabHost = getTabHost();
TabSpec spec;
spec = tabHost.newTabSpec("controls");
spec.setIndicator("Control");
spec.setContent(R.layout.unit_control);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
spec.setContent(R.layout.unit_data);
tabHost.addTab(spec);
}
}
However when I run the program it crashes with the error:
"Could not create tab content because could not find view with id 2130903042". I don't understand what the problem is because R.layout.unit_data refers to a layout file in my resource directory (res/layout/unit_data.xml)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:stretchColumns="*"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner android:id="#+id/unit_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/choose_unit"/>
<TableRow android:padding="2dp">
<TextView
android:gravity="right"
android:padding="5dp"
android:text="#string/Power"/>
<TextView android:id="#+id/unit_power"
android:layout_span="3"
android:gravity="center"
android:padding="5dp"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:text="AUTO"/>
</TableRow>
...
</TableLayout>
</ScrollView>
as far as I can tell unit_data.xml is well formed and I've even referenced it successfully in another activity
class UnitData extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unit_data);
Toast.makeText(this, "Hi from UnitData.onCreate", 5);
}
}
which does not give an error and renders the layout just fine.
What's going on? Why can't I reference this layout when creating a tab?
While Activity.setContentView takes an id of a Layout, TabSpec.setContent takes an id of a View. This means you need to pass an id that looks like R.id.something and not R.layout.something.
To solve your particular problem, give the top level view in your layout a view id:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/unit_data"> <!-- NOTE THE CHANGE -->
...
</ScrollView>
and update your source:
public class UnitActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unit_view);
TabHost tabHost = getTabHost();
TabSpec spec;
spec = tabHost.newTabSpec("controls");
spec.setIndicator("Control");
spec.setContent(R.id.unit_control); // NOTE THE CHANGE
tabHost.addTab(spec);
spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
spec.setContent(R.id.unit_data); // NOTE THE CHANGE
tabHost.addTab(spec);
}
}
For more information, see the tab examples in the ApiDemos:
If you have your tab layout in a different file, you have to inflate the XML.
spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
// Add the layout to your tab view
getLayoutInflater().inflate(R.layout.unit_data, tabHost.getTabContentView(), true);
spec.setContent(R.id.unit_data);
tabHost.addTab(spec);
You also have to use LayoutInflater after TabHost
LayoutInflater.from(this).inflate(R.layout.unit_data, tabHost.getTabContentView(), true);
I also got stuck with this and finally figure it out.