0

I'd like to make an android app where the user is presented with a series of questions, each on it's own screen. I can do this with navigation components but then every question will require it's own fragment and layout. So if I want to ask 15 questions it will require 15 fragment and 15 layout files.

Is there a way to do this without the redundancy?

dwvldg
  • 293
  • 4
  • 12
  • You can use Recyclerview with custom layout. Horizontal Recyclerview will be more suitable. Which will also not redundant. Viewpager might be also an another option. – Jaimin Modi Oct 03 '19 at 10:13
  • make 1 fragment and show it then when the user clicks next `replace` the fragment another instance of the same fragment with the proper params/bundle whatever u user with transition animation that slides to the side. Log the answer that the user gives as u replace the fragment, This way the layout looks smooth and you can use this for any number of questions – Pemba Tamang Oct 03 '19 at 10:14
  • @PembaTamang Great. – Jaimin Modi Oct 03 '19 at 10:15
  • see this for the transition. https://stackoverflow.com/a/33992609/8528047 – Pemba Tamang Oct 03 '19 at 10:16
  • @PembaTamang Awesome! I will do this! – dwvldg Oct 03 '19 at 10:18
  • ok I hope it works – Pemba Tamang Oct 03 '19 at 10:19
  • Just one fragment and one layout. On every onNext click, update the question value to next question, which automatically updates UI if you use dataBinding properly. Till here there would be no problem. But then, we have to store values of all these questions and show them back again if user comes backwards during his journey filling up the answers. For that you will have to maintain a map/list with Pair. On every previous and next click, update the values of question and answer variables from Map using data binding. – Narasimha Oct 03 '19 at 10:19
  • The only other issue is that the user input may be different by question. For example the response for the first question may be an edittext box, but the next will be a set of radio buttons. Maybe I can keep a list of Pair where the string is the question text and the view is the user input form? – dwvldg Oct 03 '19 at 10:27
  • @dwvldg Android provides us Visibility features for the views according to condition. – Jaimin Modi Oct 03 '19 at 11:04

1 Answers1

0

How can I make a series of questions on Android app and reuse the same fragment and layout?

This question cannot be answered, it's too broad and generic.

Yes, you can reuse android widgets, fragments, layouts, fragments, activities etc to achieve what you want.

Options

  1. Re-use the same fragment: when user answers a question, you pass the next question as an argument to the next fragment (which is the same Fragment code-wise) and you replace the current one.
  2. Use a vertical scrolling recyclerview
  3. (not recommended) Use an activity for each question
  4. ViewPager2

I'd recommend to google for "Quiz application android tutorial" and you will find plenty showing you how to start.

Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25