Add FragmentActivity in TabHost - android

actually I have a FragmentActivity with a Tabhost, some Fragment inside and you can slide between the different Fragment thanks to a ViewPager.
With this FragmentActivity, I would like to incorporate it into a other Tabhost, and so have the one below the other.
For the moment, I have this solution :
public class TabsViewPagerFragmentActivity extends ActivityGroup {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout
setContentView(R.layout.main);
TabHost mTabHost = (TabHost)findViewById(R.id.testtabhost1);
mTabHost.setup(getLocalActivityManager());
TabSpec ts = mTabHost.newTabSpec("tab_test1").setIndicator("TAB1");
ts.setContent(new Intent(this,PageGaucheFragment.class)); <--- PageGaucheFragment is the FragmentAtivity with the ViewPager that I would like to add into the TabHost
mTabHost.addTab(ts);
mTabHost.setCurrentTab(0);
}
It works, but ActivityGroup was deprecated :( And I don't find any other solution to solve this problem.How can I have 2 TabHost and that the second can slide between the different Fragment ?
Thanks for your answer, and sorry for my English ;)

Look here: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html
I've used it successfully.

Related

How to show tabs and action bar in the same activity of android application?

i am developing android application. i extend my class to Tab Activity.
while i am using getActionBar() or getSupportActionBar(), the activity stops working. when i extend my activity to Activity or ActionBarActivity getTabHost() will not work. Any one pls help me. thanks in advance.
Here is my coding.
public class CommonTabActivity extends TabActivity
implements OnTabChangeListener {
TabHost tabHost;
Bundle response = new Bundle();
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_tab);
....
tabHost = getTabHost();
....
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
you can extend your activity to AppCompatActivity (ActionbarActivity is deprecated now), use tablayout from android design library to show tabs and associate viewpager to tablayout to switch between fragments corresponding to each tab.
your activity layout would be something like this.
android.support.v7.widget.Toolbar
android.support.design.widget.TabLayout
android.support.v4.view.ViewPager
There are plenty of examples available to implement this.
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/

Android ExpandableListActivity and TabActivity in same Class

I am trying to use the ExpandableListActivity in my program but I have already inherited the TabActivity in my class. I cannot do multiple inheritance and I need to use both the ExpandableListView and the Tabs in my program.
I checked Google's code for the ExpandableListView here http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html.
But here they are inheriting the ExpandableListActivity which I cannot do as I have already inherited TabActivity in my class.
I am aware that I can use ListView without inheriting ListActivity in my class. Can I do the same for the ExpandableListView class also ? If so, then can anyone please provide me with any sample code to use ExtendableListView without inheriting the activity ?
You can have a custom view that extends ExpandableListView and use it inside the TabActivity.
Expandable List View
public class CustomExpListView extends ExpandableListView{
/** Your code **/
}
Main Activity that hosts the tabs
public class MyTabActivity extends TabActivity{
private TabHost tabhost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
tabHost.addTab(tabHost
.newTabSpec("Tab 1")
.setIndicator("Tab 1",
this.getResources().getDrawable(
R.drawable.tab_icon))
.setContent(new Intent(this, Tab1.class)));
/** Further code**/
}
}
XML Layout file for the tab with the exp list (Tab 1)
/**Other layout stuff goes here**/
<com.jmango.nexus.view.ProductListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:groupIndicator="#drawable/group_indicator"
android:childIndicator="#drawable/child_indicator"
android:cacheColorHint="#00000000"
android:divider="#00BBFF"
android:childDivider="#android:drawable/divider_horizontal_dark"
android:smoothScrollbar="true"
android:id="#+id/exp_list_view"/>
In the class for the tab with the exp list
CustomExpListView custExpListView = (CustomExpListView) this
.findViewById(R.id.exp_list_view);
You can also extend the listeners and customize it.
Hope it helps!

How to display tabbar in whole app in android?

Hi I am creating an application. In this I need to display a TabBar in all Activities. I set the TabBar bottom using 4 tabs; home, contact, about and call us.
Inside home tab I have some buttons. When I click inside home any button that time I need to move some other Activity. Using intent I moved to another Activity but here the TabBar was not displayed. However, I need to display the same TabBar in all Activities. If any one knows how to do this, please suggest a solution to me.
DefenceLaywer.java:
public class DefenceLaywer extends TabActivity {
TabHost tabhost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabhost.newTabSpec("Home");
TabSpec secondTabSpec = tabhost.newTabSpec("Claimonline");
TabSpec thirdTabSpec = tabhost.newTabSpec("CallUs");
TabSpec fourthTabSpec = tabhost.newTabSpec("AboutUs");
// TabSpec thirdTabSpec = tabhost.newTabSpec("Interactive");
firstTabSpec.setIndicator("Home", getResources().getDrawable(R.drawable.home));
secondTabSpec.setIndicator("ContactUs", getResources().getDrawable(R.drawable.contactus));
thirdTabSpec.setIndicator("CallUs", getResources().getDrawable(R.drawable.callus));
fourthTabSpec.setIndicator("AboutUs", getResources().getDrawable(R.drawable.aboutus));
firstTabSpec.setContent(new Intent(this,HomeTab.class));
secondTabSpec.setContent(new Intent(this,ContactUs.class));
thirdTabSpec.setContent(new Intent(this,CallUs.class));
fourthTabSpec.setContent(new Intent(this,AboutUs.class));
tabhost.addTab(firstTabSpec);
tabhost.addTab(secondTabSpec);
tabhost.addTab(thirdTabSpec);
tabhost.addTab(fourthTabSpec);
}
}
use fragments in activity and set that activity on tabspecs.
To learn FrAgment use following lin:
http://developer.android.com/guide/topics/fundamentals/fragments.html
and for how to use fragments to implement tab, follow followig tutorial:
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/

How to change tabs in Android?

I have three tabs that each has its own Activity. The tabs are as follows:
Home [HomeActivity]
Search [SearchActivity]
Account [AccountActivity]
I have a Main Activity which handles the main TabHost object and this is its content:
public TabHost tabHost;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Home").setContent(new Intent(this, HomeActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Search").setContent(new Intent(this, SearchActivity.class).putExtra("callX", true)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Account").setContent(new Intent(this, AccountActivity.class)));
tabHost.setCurrentTab(0);
}
Now I have a button in Search tab which I need when it is clicked, no matter what, the Home tab should activate. I guess I should somehow call the setCurrentTab() method on tabHost object but I don't how to access it inside the SearchActivity class?
I probably should use Intent for that which I have no idea how to use.
set a method to my main class, which extends TabActivity let's call it "MainActivity"
public TabHost getMyTabHost() {
return tabHost;
}
Then add my tab activity class;
MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);
or follow a better aproach at this

Creating a tabbed UI without using Layout

As per the android developer docs for creating tab UI you need to have a TabHost and TabWidget and the TabHost must be the root node for the layout.
All perfect, I tried the example and everything's fine.
Just while looking at the API Samples of tabs, I came across tabs1.java (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Tabs1.html) which was not using any tab elements in the layout.
Here is the sample working code that creates a tab, without using any layout at all.
public class HelloAndroid extends TabActivity implements TabHost.TabContentFactory {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(this));
}
public View createTabContent(String tag) {
TextView text = new TextView(this);
text.setText("tab1");
return text;
}
}
Can anyone explain that how this is working ? And how this is different that using the Layout based approach as explained in the tutorial.
Thanks.
This is because TabActivity programatically creates a TabHost layout.
You can check http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/app/TabActivity.java&q=TabActivity&sa=N&cd=1&ct=rc

Categories

Resources