How to add different activities with tab activity, for example i have many tab like home, add music, add video etc, home tab attach with home activity. In home activity there is a button , after clicking this button page will go to another activity, So how can i see tab to this activity.
Asked
Active
Viewed 84 times
2 Answers
1
As Anrijs said TabActivity has been deprecated on android so use the support design library's TabLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextAppearance="@style/TabTextStyle"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Then create a ViewPager
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then implement a Fragment
for each of your activities
And follow this tutorial to make everything work nicely

Teslim Olunlade
- 31
- 7
-
if i do without fragments, so how can i do..... – pankaj singh rawat Aug 24 '15 at 05:12
0
TabActivity is was deprecated in API level 13. You should consider using ViewPager and TabLayout.
Add Design Support library to your project build.gradle:
dependencies {
compile 'com.android.support:design:22.2.0'
}
Add TabLayout and ViewPager to you
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
Create fragment:
// In this case, the fragment displays simple text based on the page
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
Create FragmentPageAdapter:
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
Set up Activity:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
Source: https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout

Anrijs
- 61
- 5
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers will not be of any use if the external linked page changes in future – Panther Aug 24 '15 at 05:24
-
i have done layout , so please suggest me how can i do without fragment – pankaj singh rawat Aug 24 '15 at 05:39