0

I am making an app that contains a small questionnaire for users asking them about their food preferences. To achieve this, I am trying to create a ViewPager whose layout contains Radiobuttons inside a Radiogroup. I would like my View pager to move to the next page when the user selects a radio button. I am unable to get this to work. I couldn't find anything similar to this on Google.

Pager Adapter:

public class SliderAdapter extends PagerAdapter {

Context context;
LayoutInflater layoutInflater;
int position;
RadioGroup radioGroup;
ImageView imageView;
TextView textView;
RadioButton one, two, three;
// SharedPreferences sharedPreferences;

public SliderAdapter(Context context) {
    this.context = context;
    position = 0;
}

int[] images = {R.drawable.meat_preferences,
                R.drawable.dairy_preferences,
                R.drawable.spices_preferences};

int[] questions = {R.string.onboarding_2, R.string.onboarding_3, R.string.onboarding_4};
int[][] answers = {{R.string.onboarding_2_1, R.string.onboarding_2_2, R.string.onboarding_2_3},
                   {R.string.onboarding_3_1, R.string.onboarding_3_2, R.string.onboarding_3_3},
                   {R.string.onboarding_4_1, R.string.onboarding_4_2, R.string.onboarding_4_3}};

@Override
public int getCount() {
    return questions.length;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == (ConstraintLayout) object;
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.onboarding_slides_layout, container, false);
    imageView = view.findViewById(R.id.slide_image);
    textView = view.findViewById(R.id.questions);
    radioGroup = view.findViewById(R.id.radio_group);
    // view.setTag("Radio" + position);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radio_button = group.findViewById(checkedId);
            Toast.makeText(context, radio_button.getText(), Toast.LENGTH_SHORT)
                    .show();

        }
    });

    imageView.setImageResource(images[position]);
    textView.setText(questions[position]);
    for (int i = 0; i < radioGroup.getChildCount(); i++) {
        ((RadioButton) radioGroup.getChildAt(i)).setText(answers[position][i]);
    }

    container.addView(view);
    return view;
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((ConstraintLayout)object);
}
}

Onboarding class which is called from the main activity:

public class Onboarding extends AppCompatActivity {

ViewPager viewPager;
LinearLayout linearLayout;
ImageView zero, one, two;
ImageView[] imageViews;
Button button;
SliderAdapter pagerAdapter;
RadioGroup radioGroup;
RadioButton option_1, option_2, option_3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_onboarding);

    viewPager = findViewById(R.id.slider);
    linearLayout = findViewById(R.id.dots);
    button = findViewById(R.id.next_btn);
    zero = findViewById(R.id.intro_indicator_1);
    one = findViewById(R.id.intro_indicator_2);
    two = findViewById(R.id.intro_indicator_3);
    imageViews = new ImageView[]{zero, one, two};

    //call adapter
     pagerAdapter = new SliderAdapter(this);
     viewPager.setAdapter(pagerAdapter);


    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @SuppressLint("UseCompatTextViewDrawableApis")
        @Override
        public void onPageSelected(int position) {
            updateIndicators(position);
            if (position == 2){
                button.setVisibility(View.VISIBLE);
                button.setCompoundDrawableTintList(ColorStateList.valueOf(Color.parseColor("#4D000000")));
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                });
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

void updateIndicators(int position) {
    for (int i = 0; i < imageViews.length; i++) {
        imageViews[i].setBackgroundResource(
                i == position ? R.drawable.indicator_selected : R.drawable.indicator_unselected
        );
    }
}
}

How do I get the radiogroup selections in the "Onboarding" class?

VaM999
  • 453
  • 1
  • 9
  • 23
  • Have you tried creating some kind of ``onRadioButtonSelected`` function in ``Onboarding`` (or an interface it implements) and calling that from your ``onCheckedChanged`` callback in the ``RadioGroup``? That way you could record the choice and then tell the ``ViewPager`` to flip to the next item – cactustictacs Jan 15 '21 at 17:17
  • you need to use an interface from fragment to your activity, [this](https://stackoverflow.com/a/65554394/9701793) will help you – rahat Jan 17 '21 at 08:21

1 Answers1

0

just call this

mRadioGroup.check(mRadioGroup.getChildAt(0).getId());

after

 mViewPager.setCurrentItem(0);