i use this tutorial http://developer.android.com/resources/tutorials/views/hello-tabwidget.html to create tabs which opens new intents. however, the tabs in the tutorials are located on the top, so I followed this solution here https://stackoverflow.com/a/2710404/301584 to move the tabs to bottom (basically i just move the TabWidget to be after FrameLayout in the xml file, modify the neccessary layout_height and add layout_weight, as suggested by the solution on the link). the problem is, my apps will always forced close when I did this. here's the error reported by logcat
01-09 04:30:09.838: ERROR/AndroidRuntime(336): FATAL EXCEPTION: main
01-09 04:30:09.838: ERROR/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geoflex.trymasak/com.geoflex.trymasak.TryMasaktTabActivity}: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): at com.geoflex.trymasak.TryMasaktTabActivity.onCreate(TryMasaktTabActivity.java:34)
this is my full code
package com.geoflex.trymasak;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
public class TryMasaktTabActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable 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, Tab1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tabOne");
spec.setContent(intent);
spec.setIndicator("Tab One");
tabHost.addTab(spec);
// Squish the tab a little bit horizontally
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
// Bump the text size up
LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
android.widget.TabWidget tw = (android.widget.TabWidget) ll.getChildAt(0);
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
TextView lf = (TextView) rllf.getChildAt(1);
lf.setTextSize(20);
// Do the same for the other tabs
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("tabTwo");
spec.setContent(intent);
spec.setIndicator("Tab Two");
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
TextView rf = (TextView) rlrf.getChildAt(1);
rf.setTextSize(20);
tabHost.setCurrentTab(0);
}
}
and my main.xml file
<?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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TabWidget
android:id="#android:id/tabs"
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
and my tab1.java (tab2.java is the same)
package com.geoflex.trymasak;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Tab1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("This is tab 1");
setContentView(tv);
}
}
Try this to set Tabs in the bottom :
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_main"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"
/>
</RelativeLayout>
</TabHost>
And use the following code for your main activity (thats extends the TabActivity):
public class TabTestActivity extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this,Tab1.class);
spec = tabHost.newTabSpec("projects").setIndicator("Projects",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab2.class);
spec = tabHost.newTabSpec("news").setIndicator("News",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
Try this
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:paddingBottom="#dimen/tab_space_top"
android:layout_width="fill_parent" android:paddingLeft="#dimen/tab_space_gap"
android:layout_height="fill_parent" android:paddingRight="#dimen/tab_space_gap" >
<LinearLayout android:id="#+id/tab_relative_layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#drawable/background">
<FrameLayout android:id="#android:id/tabcontent" android:layout_weight="1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_below="#android:id/tabs"></FrameLayout>
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0"></TabWidget>
</LinearLayout>
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
I think this is supposed to be casted to Framelayout according to your logs.
set in fragment
if(getActivity() != null && isAdded) {
//do your operation
}
Related
PS. i am new in android and java programming.
i have this problem in my application it crashes when i clicked on my intent button which handles tab activity (the deprecated one), there are no errors with some warnings but i can't seem to figure out what i am doing wrong.
here is the code.
InformationActivity.class
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class InformationActivity extends TabActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,OfflineTab.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,OnlineTab.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
}
}
OfflineTab.class
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
public class OfflineTab extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("Contains R.E.D. e - Kit Offline module");
setContentView(tv);
}
}
OnlineTab.class
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
public class OnlineTab extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
activity_information.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+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="wrap_content"
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>
The log said:
java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
So just don't define a new id for tabhost:
activity_information.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:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
onCraete() of InformationActivity
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
// create the TabHost that will contain the Tabs
TabHost tabHost = getTabHost(); // <- get tabhost by this
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,OfflineTab.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,OnlineTab.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
}
And it worked!
I am using TabHost in my application and I need to show 2 maps in 2 different tabs. I have been able to integrate maps but the problem is when I move from one tab to another the map hangs/stucks and does not respond i.e. maps only works on one of the screen
Below is xml and class for my main layout which has TabHost
<?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="#ff0000" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f00"
android:foregroundGravity="top"
android:layout_above="#android:id/tabs"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
android:tabStripEnabled="false"
>
</TabWidget>
</RelativeLayout>
</TabHost>
package com.cotechnica.alps;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
import com.ankitkedia.alps.R;
public class Main extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Map1.class);
spec = tabHost.newTabSpec("home").setIndicator("Map1",
getResources().getDrawable(android.R.drawable.star_on)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Map2.class);
spec = tabHost.newTabSpec("rescue").setIndicator("Map2",
getResources().getDrawable(android.R.drawable.star_big_off)
).setContent(intent);
tabHost.addTab(spec);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have seen many people are facing same issue but there is no proper solution available for it.
Any help would be appreciated, I can sent the sample project source if required
Regards,
Ankit
This guy has a great solution
http://www.ankitkedia.com/2013/07/07/android-google-map-v2-use-multiple-maps-in-application/
I am learning to make Tab based apps in android.Got a “you must specify a way to create tab indicator” error in LogCat.
Heres the code......
.java file......
package com.example.tabdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabhost=getTabHost();
TabHost.TabSpec spec1=tabhost.newTabSpec("Tab 1");
Intent intent1=new Intent(this, Tabone.class);
spec1.setContent(intent1);
// spec1.setIndicator("Tab 1", getResources().getDrawable(R.drawable.face));
spec1.setIndicator("Tab 1");
tabhost.addTab(spec1);
TabHost.TabSpec spec2=tabhost.newTabSpec("Tab 2");
Intent intent2=new Intent(this,Tabtwo.class);
spec2.setContent(intent2);
// spec1.setIndicator("Tab 2", getResources().getDrawable(R.drawable.face));
spec1.setIndicator("Tab 2");
tabhost.addTab(spec2);
tabhost.setCurrentTab(1);
}
}
activity xml file...
<?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>
You didn't set indicator for the tab2
check your code
spec2.setContent(intent2);
// spec1.setIndicator("Tab 2", getResources().getDrawable(R.drawable.face));
spec1.setIndicator("Tab 2");
you overwrite your tab1 indicator.
I have a tab bar that I want to add to multiple activities. I have a TabController.java which looks like this
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabController extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabcontroller);
TabHost tabHost = getTabHost();
Intent intent;
intent = new Intent().setClass(this, Help.class);
TabSpec specHelp = tabHost.newTabSpec("Help").setIndicator("Help")
.setContent(intent);
intent = new Intent().setClass(this, Services.class);
TabSpec specServices = tabHost.newTabSpec("Services").setIndicator("Services")
.setContent(intent);
intent = new Intent().setClass(this, Inbox.class);
TabSpec specInbox = tabHost.newTabSpec("Inbox").setIndicator("Inbox")
.setContent(intent);
intent = new Intent().setClass(this, About.class);
TabSpec specAbout = tabHost.newTabSpec("About").setIndicator("About")
.setContent(intent);
intent = new Intent().setClass(this, More.class);
TabSpec specMore = tabHost.newTabSpec("More").setIndicator("More")
.setContent(intent);
tabHost.addTab(specHelp);
tabHost.addTab(specServices);
tabHost.addTab(specInbox);
tabHost.addTab(specAbout);
tabHost.addTab(specMore);
}
and a tabcontroller.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_alignParentBottom = "true"
android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</RelativeLayout>
</TabHost>
</merge>
What I want to do with that is to add that layout to multiple activities. When I try and add the layout I use <include layout="#layout/tabcontroller"/>. When I run the project my tab bar does not appear on the screen.
How can I add this tab bar to my activities? PS. TabController.java is not my main activity
Try taking a look at this tutorial over at : http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android
One Thing i know ,maybe i am wrong, is that the Root Activity class must inherit TAbActivity and for you your TabController.java is not the main activity.
EDIT: THis is how i am getting Tabs which is not my Root Activity :
public class ListFragment extends Fragment implements OnTabChangeListener{
View v;
TabHost tabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.main_menu_libraries, container, false);
tabHost=(TabHost) v.findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("About",getResources().getDrawable(R.drawable.android));
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Blog",getResources().getDrawable(R.drawable.android));
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("Donate",getResources().getDrawable(R.drawable.android));
spec3.setContent(R.id.tab3);
TabSpec spec4=tabHost.newTabSpec("Tab 4");
spec4.setIndicator("Explore",getResources().getDrawable(R.drawable.android));
spec4.setContent(R.id.tab4);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4);
return v;
}
As in the code above i am using fragments . So i inflate my VIew with the menu_libraries.xml which contains the tabs and the tabwidgets.
Inside the OncreateView function i am defining and inflating the tabs as well.
Not the best of answers but i guess you could try that.
I'm trying to make a TabHost on HoneyComb as if I was on 1.6+ but the fact is when I copy the code (which is the same for both HoneyComb and previous version), the TabHost do not match the screen width. I don't get it?
Using Halo theme on API 3.0 with the emulator.
Here's the 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">
<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" />
</TabHost>
And here is my Activity :
package org.agetac;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabsActivity extends TabActivity {
private static final String TAB_SITAC = "tab_sitac";
private static final String TAB_SOEI = "tab_soei";
private static final String TAB_MOYEN = "tab_moyen";
private static final String TAB_TAB4 = "tab_tab4";
private static final String TAB_TAB5 = "tab_tab5";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
TabHost tHost = getTabHost();
TabHost.TabSpec spec;
Intent tabIntent;
// Initialize a TabSpec for each tab and add it to the TabHost
tabIntent = new Intent().setClass(this, SITACActivity.class);
spec = tHost.newTabSpec(TAB_SITAC).setIndicator(getString(R.string.sitac),
getResources().getDrawable(R.drawable.ic_tab_sitac))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, SOEIActivity.class);
spec = tHost.newTabSpec(TAB_SOEI).setIndicator(getString(R.string.soei),
getResources().getDrawable(R.drawable.ic_tab_soei))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, MoyenActivity.class);
spec = tHost.newTabSpec(TAB_MOYEN).setIndicator(getString(R.string.moyen),
getResources().getDrawable(R.drawable.ic_tab_moyen))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, Tab4Activity.class);
spec = tHost.newTabSpec(TAB_TAB4).setIndicator(getString(R.string.tab4),
getResources().getDrawable(R.drawable.ic_tab_tab4))
.setContent(tabIntent);
tHost.addTab(spec);
tabIntent = new Intent().setClass(this, Tab5Activity.class);
spec = tHost.newTabSpec(TAB_TAB5).setIndicator(getString(R.string.tab5),
getResources().getDrawable(R.drawable.ic_tab_tab5))
.setContent(tabIntent);
tHost.addTab(spec);
tHost.setCurrentTab(0);
}
}
Does anyone know why does TabHost behave like that with HoneyComb?
Thats normal behaviour and how it's planned by Google. The Tabs won't fill the whole Activity unless you write your own TabHost.