Switching screens with the same XML file

Asked

Viewed 64 times

1

I am developing an educational application (with various exercises about java and explanations) for Android, but I have a question: the application becomes very heavy if I create a Activity (with an XML file) per exercise or screen with explanations, right? How would I solve that problem? In case, man app would have several types of exercises, such as exercises where the user marks the correct answer, selects the correct code block or even writes a line of code required for the question.

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

P.S: I already have experience in Java, but I started working with Android now.

  • In fact, you only need one activity to manage as many exercises as you want. 100, 200...

  • I believe that the best solution for your case would be to register the questions you want in a db sqlite and mount your screens from that base using Fragments, Voce can also write the questions in a text file and place that file in a /res/raw folder or even an Assets folder, so it would be easier and more dynamic, since Voce can even add more questions without having to change or implement new layouts for these new questions.

  • But do you want to use the same XML then for all questions? Or for each exercise create an XML ( is that this is able to get very heavy) ?

1 answer

0

Yes, it is possible to reuse a single XML file in several activities.

To change properties of views declared 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 am changing the text displayed on Textview to "Name the issue".

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


As to the number of Activities that may exist, it will be your choice. Like Iamluc mentioned, it is possible to have only one Activity that manages all your questions. You could also have a Activity for each type of question. Or even use Fragments, such as Armando Marques Sobrinho suggested.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.