Switching screens with the same XML file

1

I'm developing an educational application (with several java exercises and explanations) for Android, but I have a question: the application becomes very cumbersome if I create an activity (with an XML file) by exercise or screen with explanations, is not it? How would I solve this problem? In this case, my app would have several types of exercises, such as exercises in which the user marks the correct answer, selects the correct block of code or even writes a line of code needed for the question.

Is there any way to use the same screen (XML) / exercise model for a specific type of exercise? Type, use a screen template to answer all the ticking questions, and another template for another type of exercise?

Q: I already have Java experience, however I started working with Android now.

    
asked by anonymous 05.07.2017 / 03:57

1 answer

0

Yes, you can reuse a single XML file in several activities .

To change declared views properties in XML, assign an id to them:

<LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      >

    <TextView
        android:id="@+id/question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="This is a placeholder text"
        />

    <EditText
        android:id="@+id/answer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
  </LinearLayout>

And look for your reference at runtime:

TextView questionTitle = (TextView) findViewById(R.id.question);
questionTitle.setText("Nome da questão");

In this case I'm swapping the text displayed in TextView to "Question Name".

Thinking this way, you could create XML layouts that would be the "skeletons" of the questions, and fill them in at runtime.

As for the number of Activities that may exist, this will be your choice. As IAMLuc mentioned, you can only have one Activity that manages all your issues. You could also have an Activity for each type of question. Or even use Fragments, as the Armando Marques Sobrinho suggested.

You should evaluate which option will be easier to maintain without generating duplicate code.

    
05.07.2017 / 19:48