Android How to Replace the Deprecated Tabhost

Android how can i replace the deprecated tabhost?

"Deprecated" in Android means "we think there is a better solution that you should investigate". Rarely does "deprecated" mean "it is unusable". TabHost, AFAIK, works fine on Android 4.0.

That being said, I would recommend considering switching to tabs in the action bar, using ActionBarSherlock to give you backwards compatibility to Android 2.1.

UPDATE

Besides, TabHost is not deprecated. TabActivity is deprecated. You can still use TabHost, with views for your tabs. Or, use:

  • ViewPager with a tabbed indicator, like PagerTabStrip
  • FragmentTabHost, for a TabHost that uses fragments for tabs

The action bar also has tab support, but that was deprecated starting with the "L" Developer Preview.

Replacement deprecated TabActivity with FragmentActivity

I suggest that you base your solution on the Action Bar (or Action Bar Sherlock ) and move your tabs to the top. I suggest a FragmentActivity (or SherlockFragmentActivity) with a Fragment for each tab. I have done this in my app and the programming is straightforward and well-documented. I suggest you
start by looking at this:

http://developer.android.com/guide/topics/ui/actionbar.html

Good Luck !

Alternative of TabHost

It was terrible question I had asked. There's better way to do it. I was thinking how to do that. Then, I used TabLayout and FrameLayout. I thought FrameLayout will hide others linearlayout without first layout. Then, I had added to my activity_main.xml

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_layout">

<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="TextView" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>

<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</FrameLayout>

<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.tabs.TabItem
android:id="@+id/tabitem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday" />

<com.google.android.material.tabs.TabItem
android:id="@+id/tabitem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tuesday" />

<com.google.android.material.tabs.TabItem
android:id="@+id/tabitem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wednesday" />
</com.google.android.material.tabs.TabLayout>

But, FrameLayout didn't work as I thought. So, I discouraged again. But, I keep thinking of it. I didn't remove anything. Then, something else came to my head. What if I hide others linearLayout without first ones. How I thought Framelayout should work. Then, I set on LinearLayout Visibility = "GONE".

LinearLayout l1 = findViewById(R.id.tab1);
LinearLayout l2 = findViewById(R.id.tab2);
LinearLayout l3 = findViewById(R.id.tab3);

TabLayout tabLayout = findViewById(R.id.tab_layout);

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.d("TAB_POSITION", String.valueOf(tab.getPosition()));
if (tab.getPosition()==0){
l1.setVisibility(View.VISIBLE);
if (l2.getVisibility()==View.GONE){

}else{
l2.setVisibility(View.GONE);
}
if (l3.getVisibility()==View.GONE){

}else{
l3.setVisibility(View.GONE);
}
}else if (tab.getPosition()==1){
l2.setVisibility(View.VISIBLE);
if (l1.getVisibility()==View.GONE){

}else{
l1.setVisibility(View.GONE);
}
if (l3.getVisibility()==View.GONE){

}else{
l3.setVisibility(View.GONE);
}
}else if (tab.getPosition()==2){
l3.setVisibility(View.VISIBLE);
if (l2.getVisibility()==View.GONE){

}else{
l2.setVisibility(View.GONE);
}
if (l1.getVisibility()==View.GONE){

}else{
l1.setVisibility(View.GONE);
}
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

So, while I am clicking on another tabs it is hiding others layout without main ones. So, it worked for me. There's no alternative I have get. I will say that it is my answer.

Replacement of TabActivity with FragmentActivity and Fragments

Since TabActivity is deprecated I need to find a way to do it with Fragments.

If you don't want to use TabActivity - forget about putting FragmentActivities into tab's content.

I remind that you can use TabWidget without TabActivity. So you can try this solution:

  1. Create just one FragmentActivity.
  2. Put TabWidget into FragmentActivity's layout. Make TabWidget's content's height = 0.
  3. Under TabWidget in XML declare container for you Fragments (FrameLayout for example).
  4. In FragmentActivity just handle which tab is selected (TabHost.OnTabChangeListener) and put needed Fragment into container.
  5. Put programm logics (which was earlier in different activities) into different Fragments.

Or you can create FragmentActivity with TabWidget, and instead of switching Fragments you can directly put Fragments into each tab's content.

For example if you have 3 tabs and 3 fragments try what i do. Call showFragmentX when you need to change one fragment to another.

public class Test extends FragmentActivity {

private Fragment1 fragment1=new Fragment1();
private Fragment2 fragment2=new Fragment2();
private Fragment3 fragment3=new Fragment3();

private void showFragment1(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_container, fragment1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}

private void showFragment2(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_container, fragment2);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}

private void showFragment3(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_container, fragment3);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}

@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.fragmentactivity_layout);
}
}

If you do so your fragmentX variables will not be deleted each time you put them in fragment_container. They will live while your FragmentActivity live. Take look at fragments lifecycle.
Only OnCreateView and onDestroyView methods of fragments will call again and again while you replace one fragment to another.

Also Fragments has their onSaveInstanceState method where you can save the state of your fragment. For example: user typed his name in fragment1's editText. If you want to keep this data(name string) while user discover other fragments you should

1.save name string in fragment1's onSaveInstanceState method

2. into fragment1's onCreateView method check savedInstanceState bundle, if it's not null - fill edittext with string that you get from bundle.

public class Fragment1 extends Fragment {

EditText nameEditText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

View fragment1View=inflater.inflate(R.layout.fragment1_layout, container, false);
nameEditText=(EditText) fragment1View.findViewById(R.id.edittext_name);

//check for saved data. if it is not null - fill your edittext with saved string
if(savedInstanceState!=null){
String nameString=(String) savedInstanceState.get("nameString");
nameEditText.setText(nameString);
}
return fragment1View;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//save data that user have entered
outState.putString("nameString", nameEditText.getText().toString());
}

}

You can check data before saving. I hope my point is clear now.

Also if you call setRetainInstance(true) in onCreateView() method of your fragment - system will try to save the state of fragment (with all input data). link to description

Change Tab color depending on which tab is selected

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#54C4C6")); // unselected
}

tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#114C5A")); // selected
}
});


Related Topics



Leave a reply



Submit