Fragment Question, where to build buttons etc

I am using fragments to update a text view I have so when the person clicks a button the text view moves on to the next question. I’m not sure if I am doing the correct work in one fragment instead of the other. My current screen looks like this:

Current Screen

I will probably have to add some more buttons/widgets to this but should I be adding it into the XML for the fragment or the fragment container? Here is XML for fragment actions:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/fragment_question_layout1"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:padding="16dp"     tools:context=".FragmentActions"      >     <!-- this is where fragments will be shown-->     <FrameLayout         android:id="@+id/question_container1"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="4"          android:scaleType="centerInside" />       <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"          android:orientation="horizontal">          <Button             android:id="@+id/questions_yes1"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_weight="1"             android:gravity="center"             android:text="@string/yes" />          <Button             android:id="@+id/questions_no1"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_weight="1"             android:gravity="center"              android:text="@string/no" />      </LinearLayout>   </LinearLayout> 

And here is the fragment details:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/button_layout1"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_marginTop="10dp"     tools:context=".FragmentDetails">      <!--Blank Fragment Layout-->     <TextView         android:id="@+id/questions_text_view1"         android:layout_width="match_parent"         android:layout_height="91dp"         android:gravity="center"         android:textAlignment="center"         />  </FrameLayout> 

Updated FragmentDetails

public class FragmentDetails extends Fragment {       private final String TAG = getClass().getSimpleName();     private List<Integer> mQuestionIds;     private int mListIndex;        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         //Inflate the fragment layout         View rootView = inflater.inflate(R.layout.fragment_details, container, false);          //Get a reference to the textView in the fragment layout         final TextView textView = (TextView) rootView.findViewById(R.id.questions_text_view1);         if (mQuestionIds != null) {             textView.setText(mQuestionIds.get(mListIndex));             //Increment the position in the question lisy as long as index is less than list length             if (mListIndex < mQuestionIds.size() - 1) {                 mListIndex++;                 setmQuestionIds(QuestionList.getQuestions());                 setmListIndex(mListIndex);             } else {                 //end of questions reached                 textView.setText("End of questions");             }             //Set the text resource to display the list item at that stored index             textView.setText(mQuestionIds.get(mListIndex));          }     else {         //Log message that list is null         Log.d(TAG, "No questions left");     }      //return root view             return rootView;  }       public void setmQuestionIds (List < Integer > mQuestionIds) {         this.mQuestionIds = mQuestionIds;     }      public void setmListIndex ( int mListIndex){         this.mListIndex = mListIndex;     } } 

Fragment Actions activity

public class FragmentActions extends AppCompatActivity {     @Override     protected void onCreate(@Nullable Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.fragment_actions);          Button yes = findViewById(questions_yes1);          // Only create new fragments when there is no previously saved state         if (savedInstanceState == null) {             //Create Question Fragment             final FragmentDetails fragmentDetails = new FragmentDetails();             fragmentDetails.setmQuestionIds(QuestionList.getQuestions());               yes.setOnClickListener(new View.OnClickListener() {                 @Override                 public void onClick(View v) {              //set the list of question Ids for the head fragent and set the position to the second question              //Fragment manager and transaction to add this fragment             FragmentManager fragmentManager = getSupportFragmentManager();                    fragmentManager.beginTransaction()                          .replace(R.id.question_container1, fragmentDetails)                             .commit();                  }             });         }     } } 
Add Comment
2 Answer(s)

If your Buttons remain the same while the TextView changes, you may add your Buttons to the fragment container.

Remember that, your fragments will be presented inside the FrameLayout of the fragment container. You gotta keep your Buttons, outside the FrameLayout.

Or if you want to have different Buttons for different fragments (Questions, in your case), you can also add the Buttons to the fragments. But in that case, you gotta add them separately to each of the fragments.

Answered on July 16, 2020.
Add Comment

I guess there’s no right answer to your question. You could try different approaches.

Maybe you could implement the buttons in the fragment container, as @smmehrab pointed out. I see this as a more difficult solution, because when you click on an item from the container you can manage the views of the container, not the fragment’s views. You would get NullPointer if I recall correctly. This happens because the context when the button is clicked in the fragment container is different than the context when clicking from within the fragment. So you should implement an interface on the fragment container that listens to clicks, and the fragment catches the click. You could do this, and I actually am doing it in my current app, but I have no choice.

You could instead use Motion Layout (which extends from Constraint Layout) as the root view of your fragment, instead of CardView. This way you could set all the fragment’s views with a flat hierarchy (flat hierarchies improves rendering time, so that’s an improvement, and you can use CardView as one child) and set the buttons right there, in the Motion Layout (remember, the motion layout would be the fragment’s root view). You could set the click listener right there and implement animations between different textViews.

I’m sure there are plenty of other solutions, take this only as a contribution.

If you’re unfamiliar with Motion Layout you can just google it, android official documentation about it is great.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.