I have written a simple tab program using TabHost and TabWidget but I am able to add only one item to each tab.. I want to add a textview, an imageview and a button but I am unable to.. This is my xml code:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+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="98dp" >
<TextView
android:id="#+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Tab1"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Tab2"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Tab3"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
</TabHost>
How do I add an imageview along with the existing textview to Tab1?
you can use tab in action bar, like this:
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 3;
in onCreate()
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(Tab tab,
android.app.FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab,
android.app.FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab,
android.app.FragmentTransaction ft) {
}
};
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar
.newTab()
.setText(mPagerAdapter.getPageTitle(i))
.setIcon(
((ScreenSlidePagerAdapter) mPagerAdapter)
.getPageIcon(i))
.setTabListener(tabListener));
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment.instantiate(MainActivity.this,
Fragment_sports.class.getName());
case 1:
return Fragment.instantiate(MainActivity.this,
Fragment_casino.class.getName());
case 2:
return Fragment.instantiate(MainActivity.this,
Fragment_live_betting.class.getName());
default:
break;
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
#Override
public CharSequence getPageTitle(int position) {
String tabLabel = null;
switch (position) {
case 0:
tabLabel = " Sports";
break;
case 1:
tabLabel = "Casino";
break;
case 2:
tabLabel = "Live Betting";
break;
}
return tabLabel;
}
public int getPageIcon(int position) {
int id = 0;
switch (position) {
case 0:
id = R.drawable.icon_all_sports_d;
break;
case 1:
id = R.drawable.icon_favourites_d;
break;
case 2:
id = R.drawable.icon_live_d;
break;
default:
break;
}
return id;
}
}
and your main_activity.xml:
<RelativeLayout 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" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Related
I am using viewpager and segmented group to create swipeable fragments. These segmented group is actually based on RadioGroup.
What I want is that on clicking these segments(actually radiobuttons) the respective pages should be loaded. The problem is these radiobuttons are not clickable. But as soon as I comment out this line
viewPager.setAdapter(adapter)
These radiobuttons becomes clickable. I tried searching for the solution but could not resolve it. Also I do not want to use TabLayout because I want this UI somewhere in the middle of screen.
Here's the code -
public class MainActivity extends AppCompatActivity implements
ViewPager.OnPageChangeListener {
SegmentedGroup segmentedGroup;
ViewPager viewPager;
MyViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new MyViewAdapter(getSupportFragmentManager());
segmentedGroup = (SegmentedGroup) findViewById(R.id.segmentedgroup);
viewPager.setAdapter(adapter);
segmentedGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rb1:
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(0);
break;
case R.id.rb2:
Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(1);
break;
case R.id.rb3:
Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(2);
break;
}
}
});
viewPager.addOnPageChangeListener(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
segmentedGroup.check(segmentedGroup.getChildAt(0).getId());
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
segmentedGroup.check(R.id.rb1);
break;
case 1:
segmentedGroup.check(R.id.rb2);
break;
case 2:
segmentedGroup.check(R.id.rb3);
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
class MyViewAdapter extends FragmentPagerAdapter {
public MyViewAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0: return new FragmentOne();
case 1: return new FragmentTwo();
case 2: return new FragmentThree();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
}
Here is the XML-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:id="#+id/someView"
android:layout_width="wrap_content"
android:layout_height="105px"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/someView"
android:gravity="center_horizontal">
<info.hoang8f.android.segmented.SegmentedGroup xmlns:segmented="http://schemas.android.com/apk/res-auto"
android:id="#+id/segmentedgroup"
android:layout_width="match_parent"
android:layout_height="105px"
android:orientation="horizontal"
segmented:sc_border_width="1dp"
segmented:sc_corner_radius="10dp"
segmented:sc_tint_color="#36578B">
<RadioButton
android:id="#+id/rb1"
style="#style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ONE" />
<RadioButton
android:id="#+id/rb2"
style="#style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TWO" />
<RadioButton
android:id="#+id/rb3"
style="#style/RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="THREE" />
</info.hoang8f.android.segmented.SegmentedGroup>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
I am beginner and any help is appreciated.
I figured out the answer after playing around with android layout. In android studio layout editor, the viewpager was occupying the whole screen. After adding android:layout_below parameter in viewpager it worked.
I am trying to show a TextView between the main actionbar and viewpager action bar but it is showing under the viewpager action bar. here is my code
promotions.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/promotionheader"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtViewHeading"
android:text="#string/heading_promotion"
android:textColor="#color/white"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_promotion"
android:padding="10dp"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<android.support.v4.view.ViewPager xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/promotionheader"
tools:context=".Promotions"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dip"
android:layout_weight="1"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btnStores"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#color/colorPrimary"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:onClick="startSelectStore"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_home" />
<ImageButton
android:id="#+id/btnCoupons"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:background="#color/colorPrimary"
android:onClick="startCouponActivity"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_coupon" />
<ImageButton
android:id="#+id/btnNotifications"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:background="#color/colorPrimary"
android:onClick="startNotificationActivity"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_notification" />
<ImageButton
android:id="#+id/btnAbout"
style="?android:attr/imageButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:background="#color/colorPrimary"
android:onClick="startAboutActivity"
android:layout_gravity="center_horizontal|center"
android:src="#drawable/btn_about" />
</LinearLayout>
</RelativeLayout>
Promotions.java
public class Promotions extends AppCompatActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
ArrayList<String> shopList;
// TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.promotions);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View customActionBar = layoutInflater.inflate(R.layout.actionbar_promotion, null);
//-- Set Custom icon in ActionBar
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(customActionBar);
ImageButton ibItem1 = (ImageButton) customActionBar.findViewById(R.id.imgBtnSettings);
ibItem1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Promotions.this, SelectStore.class));
}
});
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//View customActionBar1 = layoutInflater.inflate(R.layout.actionbar_promotion_tab, null);
//actionBar.setCustomView(customActionBar1);
actionBar.invalidateOptionsMenu();
//-- Get value from intent
Intent intent = getIntent();
shopList = (ArrayList<String>) intent.getSerializableExtra("selectedStoreList");
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab().setText(shopList.get(i)).setTabListener(this));
}
}
public void startSelectStore(View view) {
Intent intent = new Intent(Promotions.this, SelectStore.class);
startActivity(intent);
}
public void startCouponActivity(View view) {
Intent intent = new Intent(Promotions.this, Coupons.class);
startActivity(intent);
}
public void startNotificationActivity(View view) {
Intent intent = new Intent(Promotions.this, Notifications.class);
startActivity(intent);
}
public void startAboutActivity(View view) {
Intent intent = new Intent(Promotions.this, AboutStore.class);
startActivity(intent);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case 0:
fragment = new PromotionFragment();
args.putInt("fragNum", 1);
fragment.setArguments(args);
break;
case 1:
fragment = new PromotionFragment();
args.putInt("fragNum", 2);
fragment.setArguments(args);
break;
case 2:
fragment = new PromotionFragment();
args.putInt("fragNum", 3);
fragment.setArguments(args);
break;
case 3:
fragment = new PromotionFragment();
args.putInt("fragNum", 4);
fragment.setArguments(args);
break;
case 4:
fragment = new PromotionFragment();
args.putInt("fragNum", 5);
fragment.setArguments(args);
break;
case 5:
fragment = new PromotionFragment();
args.putInt("fragNum", 6);
fragment.setArguments(args);
break;
case 6:
fragment = new PromotionFragment();
args.putInt("fragNum", 7);
fragment.setArguments(args);
break;
case 7:
fragment = new PromotionFragment();
args.putInt("fragNum", 8);
fragment.setArguments(args);
break;
case 8:
fragment = new PromotionFragment();
args.putInt("fragNum", 9);
fragment.setArguments(args);
break;
case 9:
fragment = new PromotionFragment();
args.putInt("fragNum", 10);
fragment.setArguments(args);
break;
}
return fragment;
}
#Override
public int getCount() {
if (shopList.isEmpty()) {
return 0;
} else {
return shopList.size();
}
}
}
}
My UI looks like
I want Promotions & Events to be on top of ALL Stores
Someone pls help...
you can try this :---
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Dashboard">
<!-- our toolbar -->
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:alpha="3"
android:background="#5d4292"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/menu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:paddingLeft="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/menu_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:paddingRight="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/search_icon" />
</LinearLayout>
</LinearLayout>
<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:alpha="3"
android:background="#5d4292"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabLayout" />
</RelativeLayout>
In my android activity I have a radio button group and a tab layout using viewpager.
There are two tabs and 2 radio buttons.
The first tab is an attachment tab
second one is an event tab.
When i checked the first radio button then user is not allowed to see the first tab,so i need to disable the first tab and shows the second tab for the user.If i selected the second radio button i need to enable the first tab and user is allowed to access the first tab.I don't want to remove the tab,i just want to disable it so that user can't access it by swiping or clicking on the tab.
How to do that? Below is my code to show tab.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_asset);
//remaining code
viewPager2 = (ViewPager) findViewById(R.id.viewPager2);
setupViewPager2(viewPager2);
tabLayout2 = (TabLayout) findViewById(R.id.tab_layout2);
tabLayout2.setupWithViewPager(viewPager2);//setting tab over viewpager
rdbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
}
});
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//Implementing tab selected listener over tablayout
tabLayout2.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
switch (tab.getPosition()) {
case 0:
Log.e("TAG","TAB1");
break;
case 1:
Log.e("TAG","TAB2");
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
//Setting View Pager
private void setupViewPager2(ViewPager viewPager) {
expAttList = new ArrayList<COAAccount>();
adapter2 = new ViewPagerAdapter(getSupportFragmentManager());
adapter2.addFrag(new AttachmentFragment("Attachments",expAttList,fontFamily), "Attachments");
adapter2.addFrag(new EventFragment("Events",fontFamily), "Events");
viewPager2.setAdapter(adapter2);
}
//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
//adding fragments and title method
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
layout file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/Lavender"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Name" />
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:id="#+id/rdbGroup"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:background="#drawable/round_border"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/rdb1"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_weight="1"
android:background="#drawable/bg_blue"
android:button="#android:color/transparent"
android:textColor="#drawable/txt_color"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:singleLine="true"
android:text="radio1"
android:checked="true"
android:textSize="13sp" />
<View
android:id="#+id/vSep2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000"
android:visibility="visible" />
<RadioButton
android:id="#+id/rdb2"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_weight="1"
android:background="#drawable/bg_red"
android:button="#android:color/transparent"
android:textColor="#drawable/txt_color"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:singleLine="true"
android:text="radio2"
android:textSize="13sp" />
</RadioGroup>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/SkyBlue"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="#android:color/holo_red_dark"
app:tabIndicatorHeight="4dp"
local:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/tab_layout"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="#layout/drawer_header_expense"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
It's a tricky one.You need to use a custom ViewPager for this.Below is the code.
Change your ViewPager in xml to this :
<yourPackage.NonSwipeableViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="#id/tab_layout"/>
Below is the custom view pager class :
public class NonSwipeableViewPager extends ViewPager {
private boolean enabled;
public NonSwipeableViewPager(Context context) {
super(context);
this.enabled = true;
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Now in your activity use below code :
NonSwipeableViewPager viewPager = (NonSwipeableViewPager) findViewById(R.id.pager);
//write code to set viewpager to the tablayout that u have already done.Use the same.
rdbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
pos = rdbGroup.indexOfChild(findViewById(checkedId));
LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
switch (pos) {
case 0:
viewPager.setCurrentItem(1);
viewPager.setPagingEnabled(false);
tabstrip.getChildAt(0).setClickable(false);
tabstrip.getChildAt(0).setEnabled(false);
break;
case 1:
viewPager.setCurrentItem(0);
viewPager.setPagingEnabled(true);
tabstrip.getChildAt(0).setClickable(true);
tabstrip.getChildAt(0).setEnabled(true);
break;
default:
//here add whatever you like
break;
}
}
});
Use the swicth condition as per your requirement.
You can try this:
ViewPager viewPager2;
TabLayout tabLayout2;
ViewPagerAdapter adapter2;
RadioGroup rdbGroup;
RadioButton rdb1, rdb2;
LinearLayout tabstrip;
int pos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//remaining code
viewPager2 = (ViewPager) findViewById(R.id.viewPager2);
setupViewPager2(viewPager2);
tabLayout2 = (TabLayout) findViewById(R.id.tab_layout2);
rdbGroup = (RadioGroup) findViewById(R.id.rdbGroup);
rdb1 = (RadioButton) findViewById(R.id.rdb1);
rdb2 = (RadioButton) findViewById(R.id.rdb2);
tabLayout2.setupWithViewPager(viewPager2);//setting tab over viewpager
//get position of already checked radiobutton
int radioButtonID = rdbGroup.getCheckedRadioButtonId();
View radioButton = rdbGroup.findViewById(radioButtonID);
pos = rdbGroup.indexOfChild(radioButton);
tabstrip = (LinearLayout) tabLayout2.getChildAt(0);
//check which radiobutton is already checked and as per its pos disbale or enable the tabs as below
if (pos == 0) {
tabLayout2.getTabAt(1).select();
tabstrip.getChildAt(0).setClickable(false);
tabstrip.getChildAt(0).setEnabled(false);
} else if (pos == 1) {
tabstrip.getChildAt(0).setClickable(true);
tabstrip.getChildAt(0).setEnabled(true);
}
rdbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
pos = rdbGroup.indexOfChild(findViewById(checkedId));
switch (pos) {
case 0:
tabstrip.getChildAt(0).setClickable(false);
tabstrip.getChildAt(0).setEnabled(false);
break;
case 1:
tabstrip.getChildAt(0).setClickable(true);
tabstrip.getChildAt(0).setEnabled(true);
break;
default:
//here add whatever you like
tabstrip.getChildAt(0).setClickable(true);
tabstrip.getChildAt(0).setEnabled(true);
break;
}
}
});
//Implementing tab selected listener over tablayout
tabLayout2.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
switch (tab.getPosition()) {
case 0:
Log.e("TAG", "TAB1");
break;
case 1:
Log.e("TAG", "TAB2");
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
//Setting View Pager
private void setupViewPager2(ViewPager viewPager) {
expAttList = new ArrayList<COAAccount>();
expAttList = new ArrayList<COAAccount>();
adapter2 = new ViewPagerAdapter(getSupportFragmentManager());
adapter2.addFrag(new AttachmentFragment("Attachments", expAttList, fontFamily), "Attachments");
adapter2.addFrag(new EventFragment("Events", fontFamily), "Events");
viewPager2.setAdapter(adapter2);
}
//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
//adding fragments and title method
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I have created a PagerTabStrip for my ViewPager and it is all working fine. Using a switch and case, I have the titles set up for each page. However I can't find how to set the background colour or or text colour within the java. I can change the colour in the xml, but I want the colour to change with my switch and case.
Here is my code:
EDIT: Added more code
public class RemViewPagerActivity extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_activity);
mPager = (ViewPager) findViewById(R.id.viewpager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter); /
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 1) {
super.onBackPressed();
} else if (mPager.getCurrentItem() == 0){
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
else if (mPager.getCurrentItem() == 2){
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { // Sets the content of the adapter
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) { // Sets the fragment for each position
switch (position) {
case 0: return new Fragment1();
case 1: return new Fragment2();
case 2: return new Fragment3();
}
return null;
}
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "1";
case 1: return "2";
case 2: return "3";
}
return null;
}
#Override
public int getCount() { // Sets the number of pages
return NUM_PAGES;
}
}
}
Code of view_pager_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
Thank you
How do you set a default tab when the FragmentActivity is first created? I have 5 tabs and want it to open to tab 2, not 1. It seems like it should be much easier than it is!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0: {
f = new MasterFrag();
Bundle args = new Bundle();
f.setArguments(args);
break;
}
// case 1 - 3 edited out; i'd like default view "case 1".
default:
throw new IllegalArgumentException("not this many fragments: "
+ position);
}
return f;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.mastercattab1).toUpperCase();
// case 1- 3 edited out
}
return null;
}
}
And my XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
</LinearLayout>
Have you tried calling mViewPager.setCurrentItem(tab.getPosition()); at the end of onCreate()?