Android: Start Activity on tab change - android

I have the following class in java. My purpose is to design a tabhost and have an activity start when the first tab is displayed. Also i want to have a different activity start when another tab is clicked.
I have implemented the onTabChanged method but it doesn't seem to work. Can you help me?
Here is my class:
public class Tabs extends Activity implements OnTabChangeListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
TabHost th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec profile = th.newTabSpec("tag1");
profile.setContent(R.id.tab1);
profile.setIndicator("", getResources().getDrawable(R.drawable.profile_tab));
TabSpec matches = th.newTabSpec("tag2");
matches.setContent(R.id.tab2);
matches.setIndicator("", getResources().getDrawable(R.drawable.matches_tab));
TabSpec friends = th.newTabSpec("tag3");
friends.setContent(R.id.tab3);
friends.setIndicator("", getResources().getDrawable(R.drawable.friends_tab));
th.addTab(profile);
th.addTab(matches);
th.addTab(friends);
}
#Override
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
}
}
This is my tabs.xml layout file:
<?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" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/activity_main"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/matches_page"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>

you miss th.setOnTabChangeListener(this);
You have to inform TabHost which implementation of the OnTabChangeListener interface has to call

Related

Tabwidget not visible under toolbar/navbar in android studio

I have constructed a TabHost inside my navigation drawer. That means I've created 3 Fragment with xml files each: ExhibitorFragment, TabInternationalFragment, and TabLocalFragment. Now the problem is, I can't see my tabwidget under the Toolbar even though it displays design xml in either two of the fragments.
below is my code for ExhibitorFragment.java:
public class ExhibitorFragment extends Fragment{
public ExhibitorFragment() {
// Required empty public constructor
}
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_exhibitor);
Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
TabInternationalFragment.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
TabLocalFragment.class, arg2);
return mTabHost;
}
#Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
fragment_exhibitor.xml :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.guitarista.citem.Example">
<TabHost android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/International"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="#+id/Local"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
The TabInternationalFragment and TabLocalFragment are set to default when created same as their respected xml (fragment_tab_international, fragment_tab_local).
Your Java Code Is correct Try Changing the XML file to this format
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="45dp"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Usr this code
<TabHost android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_below="#android:id/tabs"
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/International"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="#+id/Local"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab2"),
tab1.class,null);
mTabHost.addTab(mTabHost.newTabSpec("tab2")
.setIndicator("tab2"), tab2.class, null);
I've followed this tutorial Android Tabs in Fragments. Though this one is likely similar to Pager Sliding tab strip. Take Note that you still need to implement fragment codes for this to work.

Android tabHost?

I am trying to create a tab host to but I cant seem to make it work, what am i doing wrong? I have setup up createTab() on my onCreate method but it crashes.
I am simply creating a tabhost that holds around 5 tabs. Also I am trying to implement swipe view if anyone knows how to implement it
Main activity
public class MainActivity extends Activity {
TabHost tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void createTab() {
TabHost.TabSpec tab1Spec = tab.newTabSpec("tab1");
tab1Spec.setIndicator("TAB 1");
Intent p = new Intent(this, tab1.class);
tab1Spec.setContent(p);
TabHost.TabSpec tab2Spec = tab.newTabSpec("tab2");
tab2Spec.setIndicator("TAB 2");
Intent p = new Intent(this, tab2.class);
tab2Spec.setContent(p);
tab.addTab(tab1Spec);
tab.addTab(tab2Spec);
}
}
Activity_main xml
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#+id/tab"
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">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
You didn't initialize your tab host , then you've used it in this line which throw a NullPointerException :
TabHost.TabSpec tab1Spec = tab.newTabSpec("tab1");
before using tab initialize it like :
tab = (TabHost)this.findViewById(android.R.id.tabhost)

How child of tabview can access elements of view which holds tabview?

I have a button and below i have tabview with several tabs.
Wherewer I choose a tab I want to listen clics for elements on main activity. For example how can I listen clicks for imRefresh?
If I set onitem click listener I recieve nullpointer exception
main activity, which ititialise tabview
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, Tab1Activity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Test1")
.setIndicator("Test1")
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, Tab2Activity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Test2")
.setIndicator("Test2")
.setContent(intentApple);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(2);
}
}
layout of main activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/for_t"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/imRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_toLeftOf="#+id/imageView3"
android:src="#drawable/ic_action_refresh" />
<ImageView
android:id="#+id/imSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_search" />
</RelativeLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="yarh.youtubenblog.MainActivity"
tools:ignore="MergeRootFrame" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
</LinearLayout>
one of tabs activity
public class Tab2Activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView refresh = (ImageView) findViewById(R.id.imRefresh);
refresh.setOnClickListener(refreshV);
TextView textview = new TextView(this);
textview.setText("This is Apple tab");
setContentView(textview);
}
private OnClickListener refreshV = new OnClickListener() {
#Override
public void onClick(View v) {
GetAdds feed = new GetAdds();
feed.execute();
}
};
}
You can't - period. Not even via hacking around with ViewParents and window hierarchies. Sorry. This is due to how TabActivity works: Each of its tabs is actually its own self-contained Activity, that gets kind of "transplanted" into the TabActivity via some magic on the Android Framework's side. Separate activities have absolutely no way (via Android APIs) of getting view information about each other.
What'd make your task a lot simpler, is to switch to Fragments. Your use case is one of the most common examples: check out http://android.codeandmagic.org/android-tabs-with-fragments/

Android: redrawable ListView with Adapter inside TabActivity. View doesn't change FIXED

So I have a ListView inside my TabHost and I want to redrow its content every time I change the tab. Nevertheless I never get the list drawed.
My xml file looks like:
<?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" />
<!-- Dishes Panel -->
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dishSelector"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
tools:context=".MenuSelectorActivity" />
</LinearLayout>
</TabHost>
And my TabActivity:
public class MenuSelectorActivity extends TabActivity implements TabHost.TabContentFactory{
private ListView dishSelector;
private DishSelectorAdapter dishSelectorAdapter;
private TabHost tabHost;
private DishDao dishDao;
private DishTypeDao dishTypeDao;
private Map<Integer, List<DishPojo>> dishesByType;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_selector);
tabHost = getTabHost(); // The activity TabHost
tabHost.setup();
TabHost.TabSpec spec;
dishDao = new DishDao(this);
dishTypeDao = new DishTypeDao(this);
dishesByType = new HashMap<Integer, List<DishPojo>>();//new ArrayList<List<DishPojo>>();
List<DishType> types = dishTypeDao.list();
TextView dTypeNameView;
for(DishType dType : types){
dishesByType.put(dType.getId().hashCode(), createDishPojoList(dishDao.list(
new String[]{DishDbContract.SQL_WHERE_CLAUSE_LIST_BY_TYPE}, new String[]{dType.getId()})));
}
tabHost.setCurrentTab(0);
for(DishType dType : types){
dTypeNameView = new TextView(this);
dTypeNameView.setText(dType.getDescription());
spec = tabHost.newTabSpec(dType.getId()).setIndicator(dTypeNameView).setContent(this);
tabHost.addTab(spec);
}
}
#Override
public View createTabContent(String tag) {
dishSelector = (ListView) findViewById(R.id.dishSelector);
if(dishSelectorAdapter == null){
dishSelectorAdapter = new DishSelectorAdapter(this, dishesByType.get(tag.hashCode()));
}else{
dishSelectorAdapter.setDishToSelect(dishesByType.get(tag.hashCode()));
}
dishSelector.setAdapter(dishSelectorAdapter);
dishSelector.refreshDrawableState();
return dishSelector;
}
private List<DishPojo> createDishPojoList(List<Dish> dishes){
//creates a list of Pojos
}
}
The adpter DishSelectorAdapter has been already used in other situation and it works ok, so I don't think it is relevant here.
Thanks by hand.
Well well, it was not as hard as I've thought. The answer was just in front of my eyes.
I didn't put the ListView inside de FrameLayout. The correct code will be:
<?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" />
<!-- Dishes Panel -->
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dishSelector"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
tools:context=".MenuSelectorActivity" />
</FrameLayout>
</LinearLayout>
</TabHost>
Still rest of the code has some issues to work about, but nothing I coudln't fix with a quick look.

java.lang.IllegalStateException..... at tabhost.add(tabspec);

Hi guyzz I have TabActivity as following simple code
public class TabhostActivity extends Activity{
TabHost host;
public class TabhostActivity extends Activity{
TabHost host;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
host = (TabHost) findViewById(R.id.mytabhost);
host.setup();
Intent intent = new Intent(getApplicationContext(), RatingBarActivity.class);
TabHost.TabSpec spec = host.newTabSpec("positions").setIndicator("Positions",getResources().getDrawable(R.drawable.add)).setContent(intent);
host.addTab(spec);
host.setCurrentTab(0);
}
and has a simple tab layout as
<?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" >
<TabHost
android:id="#+id/mytabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FF0000">
</FrameLayout>
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TabWidget
android:layout_marginLeft="5dp"
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tabStripEnabled="true" >
</TabWidget>
</HorizontalScrollView>
</LinearLayout>
</TabHost>
</LinearLayout>
So please help me where the code is getting wrong?? I got exception as
02-23 14:09:47.920: E/AndroidRuntime(1000): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
you are extending Activity class, try extending TabActivity class
or
if you want to use the Activity class then use the following :
host = (TabHost) findViewById(R.id.tabhost); //here tabHost will be your Tabhost
LocalActivityManager mLocalActivityManager = new LocalActivityManager(mActivity, false);
mLocalActivityManager.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
tabHost.setup(mLocalActivityManager);

Categories

Resources