• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Reusing part of a layout

markdem

Lurker
Oct 24, 2022
1
0
Sorry in advance if this is something that has been answer before. I am a C# dev that never deals with UI, trying to learn android for fun and has no idea how to word his question...

I have made a layout I am happy with, like the following;

Code:
 <LinearLayout
        android:layout_column="1"
        android:layout_row="0"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_marginLeft="40dp"
        android:orientation="horizontal">

            <TextView
                android:text="Run For:"
                android:textSize="30dp"
                android:layout_width="0sp"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:textAlignment="center"/>

            <Button
                android:id="@+id/btn_down"
                android:layout_width="0sp"
                android:layout_height="match_parent"
                android:layout_weight=".15"
                android:textAlignment="center"
                android:text="-" />

            <TextView
                android:id="@+id/run_time"
                android:textSize="30dp"
                android:layout_width="0sp"
                android:layout_height="match_parent"
                android:layout_weight=".2"
                android:textAlignment="center"
                android:inputType="number"
                android:text="0" />

            <Button
                android:id="@+id/btn_up"
                android:layout_width="0sp"
                android:layout_height="match_parent"
                android:layout_weight=".15"
                android:textAlignment="center"
                android:text="+" />
    </LinearLayout>

and some handeling code;
Code:
 AppCompatTextView runTime = FindViewById<AppCompatTextView>(Resource.Id.run_time);
            AppCompatButton up = FindViewById<AppCompatButton>(Resource.Id.btn_up);
            AppCompatButton down = FindViewById<AppCompatButton>(Resource.Id.btn_down);

            up.Click += delegate
            {
                Int16 time;

                if(Int16.TryParse(runTime.Text, out time))
                {
                    if (time < 10)
                    {
                        time++;
                        runTime.Text = time.ToString();
                    }
                }
            };

            down.Click += delegate
            {
                Int16 time;

                if (Int16.TryParse(runTime.Text, out time))
                {
                    if (time > 0)
                    {
                        time--;
                        runTime.Text = time.ToString();
                    }
                }
            };

Great, all works.

How do I now repeat that 64 more times? I have a GridLayout already, I just have no idea how to repeat the buttons layout and click events. I guess I could copy and paste it 63 more times, but I feel like that might not be the best way here…

Has anyone got a example or something for me to read about how to do this?
I am writing this in C# but an example in java would be fine.

Thanks
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones