Reputation: 914
I have 6 Fragments each with a RadioGroup containing 4 radio buttons they are all displayed on the same activity using a viewpager so the user can swipe through them.
i have been trying to implement an onCheckedChangedListener on each RadioGroup so I can know which radio button was checked in each fragment.
I've tried many things over the past days and my working solution is extremely long winded and there is surely a better way.
This is my activity that calls the fragments with the current solution in it
public class QuestionsActivity extends AppCompatActivity {
Toolbar toolbar;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
// tool bar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mViewPager = (ViewPager) findViewById(R.id.pager);
/** set the adapter for ViewPager */
mViewPager.setAdapter(new MyPagerAdapter(
getSupportFragmentManager()));
}
/**
* Defining a FragmentPagerAdapter class for controlling the fragments to be shown when user swipes on the screen.
*/
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return new Q1Fragment();
} else if (position == 1) {
return new Q2Fragment();
} else if (position == 2) {
return new Q3Fragment();
} else if (position == 3) {
return new Q4Fragment();
} else if (position == 4) {
return new Q5Fragment();
} else
return new Q6Fragment();
}
@Override
public int getCount() {
// Show 2 total pages.
return 6;
}
}
public void onRadio1Clicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio1";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton2:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio2";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton3:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio3";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton4:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio4";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
}
}
public void onRadio2Clicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio1";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton2:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio2";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton3:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio3";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
case R.id.radioButton4:
if (checked) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
CharSequence text = "radio4";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
}
}
}
For each radio group i use android:onClick function in xml and create a new method for each one. above is just 2 of them.
Here is what a fragment looks like
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio"
android:onClick="onRadio1Clicked"
/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio1"
android:onClick="onRadio1Clicked"/>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio2"
android:onClick="onRadio1Clicked"/>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/border"
android:layout_marginTop="18dp"
android:padding="10dp"
android:text="@string/radio3"
android:onClick="onRadio1Clicked"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/submit"
android:layout_marginTop="13dp"
/>
</RadioGroup>
I have tried using a listener like in both the main activity and the fragment activity and i get null pointer exceptions for the second radiogroup.
Here is what a variation of the listener looks like, this is one i tried to use in a fragment activity
RadioGroup rg1 = (RadioGroup) getView().findViewById(R.id.radioGroup1);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
Context context = getContext();
RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
toast.show();
}
}
);
Whatever I do I cannot get the listener to work on fragment other than the first.
Any Ideas?
Upvotes: 1
Views: 713
Reputation: 101
Just remove all android:onClick="onRadio1Clicked" from your layouts and setOnCheckedChangeListener code from your activity and implement it in fragments like in Q1Fragment override onViewCreated method and write code there, so on in other fragments.
in Q1Fragment :-
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup rg1 = (RadioGroup) view.findViewById(R.id.radioGroup1);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
Context context = getContext();
RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
toast.show();
}
}
);
}
in Q2Fragment :-
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup rg2 = (RadioGroup) view.findViewById(R.id.radioButton2);
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
Context context = getContext();
RadioButton thisButton = (RadioButton) getView().findViewById(checkedId);
Toast toast = Toast.makeText(context, thisButton.getText(), Toast.LENGTH_SHORT);
toast.show();
}
}
);
}
Upvotes: 2