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.
Related
I am trying to use Fragment
This is my activity class:
public class InterfaceAct extends FragmentActivity implements View.OnClickListener {
private ViewPager mViewPager;
private List<Fragment> datas;
private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
private LinearLayout mLLHome,mLLTimer,mLLEdit,mLLAbout;
private ImageView mImageViewHome,mImageViewTimer,mImageViewEdit,mImageViewAbout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activiy_interface);
initDatas();// Init Data For Fragments
initView();// Init Component
initEvent();// Sign for Click Listener
viewPagerFragmentAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),datas);// Init Adpater Class
mViewPager.setAdapter(viewPagerFragmentAdapter);
}
private void initDatas() {
datas=new ArrayList<Fragment>();
datas.add(new MyFragment1());
datas.add(new MyFragment2());
datas.add(new MyFragment3());
datas.add(new MyFragment4());
}
private void initEvent() {
mLLHome.setOnClickListener(this);
mLLTimer.setOnClickListener(this);
mLLEdit.setOnClickListener(this);
mLLAbout.setOnClickListener(this);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//ViewPager Scrolling Switch Listner
#Override
public void onPageSelected(int arg0) {
int currentItem=mViewPager.getCurrentItem();
resetImag();
switch (currentItem) {
case 0:
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case 1:
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case 2:
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case 3:
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mLLHome = (LinearLayout) findViewById(R.id.ll_home);
mLLTimer = (LinearLayout) findViewById(R.id.ll_timer);
mLLEdit = (LinearLayout) findViewById(R.id.ll_edit);
mLLAbout = (LinearLayout) findViewById(R.id.ll_about);
mImageViewHome = (ImageView) findViewById(R.id.img_home);
mImageViewTimer = (ImageView) findViewById(R.id.img_timer);
mImageViewEdit = (ImageView) findViewById(R.id.img_edit);
mImageViewAbout = (ImageView) findViewById(R.id.img_about);
}
#Override
public void onClick(View v) {
resetImag();
switch (v.getId()) {
case R.id.ll_home:
mViewPager.setCurrentItem(0);
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case R.id.ll_timer:
mViewPager.setCurrentItem(1);
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case R.id.ll_edit:
mViewPager.setCurrentItem(2);
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case R.id.ll_about:
mViewPager.setCurrentItem(3);
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
private void resetImag() {// Reset Picture
mImageViewHome.setImageResource(R.drawable.home_no);
mImageViewTimer.setImageResource(R.drawable.timer_no);
mImageViewEdit.setImageResource(R.drawable.edit_no);
mImageViewAbout.setImageResource(R.drawable.about_no);
}
}
The ViewPagerFragmentAdapter Looks like this:
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> datas;
public ViewPagerFragmentAdapter(FragmentManager fm,List<Fragment> datas) {
super(fm);
this.datas=datas;
}
#Override
public Fragment getItem(int position) {// Return View Object
return datas.get(position);
}
#Override
public int getCount() {// Return View Num
return datas.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {// Init View
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {// Destroy View
super.destroyItem(container, position, object);
}
}
The layout for the page, it includes a viewpager and also a menu bar in linear layout:
?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.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ffffffff"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_home"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/home_yes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_timer"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_timer"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/timer_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_edit"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_edit"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/edit_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_about"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_about"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/about_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textColor="#b6b3b3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And This is one of the fragment:
public class MyFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1,null);
}
}
I have four fragment prepared for viewpager(they are basiclly the same but with differnt layout), here is what the tab1 layout might look like:
<?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:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Chat"
android:textSize="30sp" >
</TextView>
</LinearLayout>
And I only got the menu bar when I run it, the viewpager is not set and I don't know why
Remove android:layout_weight="1" <Viewpager> and add android:orientation="vertical" to Linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
></android.support.v4.view.ViewPager>
//-----------
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 am trying to update some views that place in a ViewPager in some fragment,
and every time I am using findViewById I am getting null back.
I tried various of solutions that I find in here (stack overflow) and nothing help.
here is all the code :
activity_main.xml
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
MainActivity.java:
PageManager _pageManager;
ViewPager _viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_viewPager = (ViewPager) findViewById(R.id.viewpager);
_pageManager = new PageManager(getSupportFragmentManager());
_viewPager.setAdapter(_pageManager);
_imageView=(ImageView)findViewById(R.id.PictureBox);
PageManager.java
public class PageManager extends FragmentStatePagerAdapter {
final int NUMBER_OF_PAGES = 3;
public PageManager(android.support.v4.app.FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new CountdownPage();
case 1:
return new VideosPage();
case 2:
return new SeasonSummary();
default:
return null;
}
}
#Override
public int getCount() {
return NUMBER_OF_PAGES;
}
}
CountdownPage.java
public class CountdownPage extends Fragment {
int resId=0;
ImageView _imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container==null)
ShowMessage.GetInstance(null).ShowMessage("error","container");
return inflater.inflate(R.layout.countdown_page,container,false);
}
}
countdown_page.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#color/Black"
android:layout_width="match_parent"
android:id="#+id/countdownLayout"
android:layout_height="match_parent">
<TextView
style="#style/HeaderStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Countdown"
android:gravity="center"
android:id="#+id/CountdownHeader"
/>
<ImageView
android:layout_width="320dp"
android:layout_height="320dp"
android:id="#+id/PictureBox"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:onClick="testF"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/counterTextView"
android:gravity="center"
android:textColor="#FFFF"
/>
</LinearLayout>
I am trying to solve this for hours
Thanks a lot,
Or Yaacov
Your ImageView(id is PictureBox) is in the 'countdown_page' layout xml not 'activity_main' layout xml ,So you should not get ImageView through findViewbyId(int) in 'onCreate()' method in Activity, The ImageView is belong to CountdownPage fragment.
had the same problem with getActivity().findViewById. Try getView().findViewById instead
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>
I am trying to implement an Activity, which contains 2 ViewPagers. Each ViewPager should have swipe independently. So i am taking ScrollView as my patent layout which have a LinearLayout as Chaild. I have added ViewPagers to that Linear layout. But only 1st ViewPager is getting Displayed and second one is not getting displayed. i have goggled alot for this and tryed with different solutions Here. but nothing worked for me.
here is my Xml.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:orientation="vertical"
android:weightSum="4">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dip"
android:text="trying to add First Fragment list"
android:layout_weight="1" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_weight="1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dip"
android:text="trying to add second Fragment list"
android:layout_weight="1" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager1"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_marginTop="30dip"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
and here is my activity.java
public class MainActivity extends FragmentActivity {
private ViewPager _mViewPager, _mViewPager1;
private ViewPagerAdapter _adapter, _adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
// setTab();
}
private void setUpView() {
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),
getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
_mViewPager1 = (ViewPager) findViewById(R.id.viewPager1);
_adapter1 = new ViewPagerAdapter(getApplicationContext(),
getSupportFragmentManager());
_mViewPager1.setAdapter(_adapter1);
_mViewPager1.setCurrentItem(0);
}
private void setTab() {
_mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int position) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 1:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 2:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 3:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.INVISIBLE);
break;
case 4:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab1).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab2).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab3).setVisibility(View.VISIBLE);
break;
}
}
});
}
}
with this out put is coming like the below screenshot.
second View Pager is not displayed.
please help me if you have any solutions.