Home > front end >  Android make layout and use it again
Android make layout and use it again

Time:02-15

I have my main activity. Inside this main activity I want to create an unknown number of layouts that each one of them include one button. I want smart way to do it - make the layout one time and than use it a lot of time.

enter image description here

What is a good way to do it? Thanks

CodePudding user response:

Make One Layout Resource File Using Below Code

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/_100sdp"
            android:layout_marginTop="@dimen/_15sdp"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="@dimen/_13sdp" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="@dimen/_35sdp"
            android:layout_marginStart="@dimen/_5sdp"
            android:layout_marginTop="@dimen/_20sdp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Submit"
            android:textColor="@color/white"
            android:textSize="@dimen/_10sdp" />
    
    </LinearLayout>

And In Your Main Activity Layout You Can Use Only

  <include layout="@layout/layoutresourcefilename"/>

CodePudding user response:

Trying to be smart commonly results in the opposite. Better keep it simple;
Which means, just add three buttons and then show either one of them.
This has the advance, that the events are already bound, ready to click.
And also, meanwhile it's a whole lot more common to inflate Fragment
or to data-bind views, which would permit for hiding/showing buttons.

  • Related