0

In my MainActivity i have 4 cardViews made as buttons every of them opens an Activity but now i would be able from settings to add "n" new buttons like one of yet created and set a path to open in BrowserView.

My main question is i have an xml file where i created the "CardView" that i use in main which has the ImageView, TextView so how can i programmatically set it in my MainActivity LinearLayout and even set different text in TextView for each "n" buttons.

Here is my MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="0dp"
        app:cardElevation="5dp"
        app:cardBackgroundColor="#00468a">

        <android.support.v7.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00468a"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_gravity="start"
                android:layout_marginStart="5dp"
                android:adjustViewBounds="true"
                android:cropToPadding="false"
                android:scaleType="fitCenter"
                android:src="@drawable/realco"
                tools:ignore="ContentDescription" />

            <ImageView
                android:id="@+id/gab"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:cropToPadding="false"
                android:padding="8dp"
                android:scaleType="fitCenter"
                android:src="@drawable/logo"
                tools:ignore="ContentDescription" />


        </android.support.v7.widget.LinearLayoutCompat>


    </android.support.v7.widget.CardView>



    <ScrollView
        android:id="@+id/scrollView"
        android:background="#ad00468a"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

                <android.support.v7.widget.CardView

                    android:id="@+id/orders"
                    app:cardCornerRadius="0dp"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    app:cardBackgroundColor="#00468a"
                    app:cardElevation="15dp">


                    <android.support.v7.widget.LinearLayoutCompat
                        android:padding="20dp"
                        android:layout_width="match_parent"
                        android:layout_height="140dp"
                        android:background="#004181"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            android:layout_gravity="start"
                            android:adjustViewBounds="true"
                            android:padding="5dp"
                            android:scaleType="fitCenter"
                            android:src="@drawable/order"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:text="ORDINI"
                            android:textAlignment="center"
                            android:textColor="#ffffff"
                            android:textSize="18sp"
                            tools:ignore="HardcodedText" />


                    </android.support.v7.widget.LinearLayoutCompat>

                </android.support.v7.widget.CardView
                  >

                <android.support.v7.widget.CardView
                    android:id="@+id/inventario"
                    app:cardCornerRadius="0dp"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    app:cardBackgroundColor="#001F54"
                    app:cardElevation="10dp">


                    <android.support.v7.widget.LinearLayoutCompat
                        android:layout_width="match_parent"
                        android:layout_height="140dp"
                        android:background="#001F54"
                        android:padding="20dp"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            android:layout_gravity="start"
                            android:adjustViewBounds="true"
                            android:padding="5dp"
                            android:scaleType="fitCenter"
                            android:src="@drawable/storage"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:text="INVENTARIO"
                            android:textAlignment="center"
                            android:textColor="#ffffff"
                            android:textSize="18sp"
                            tools:ignore="HardcodedText" />


                    </android.support.v7.widget.LinearLayoutCompat>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/prenotazioni"
                    android:layout_width="match_parent"
                    app:cardCornerRadius="0dp"
                    android:layout_height="0dp"
                    app:cardBackgroundColor="#0A1128"
                    android:layout_weight="1"
                    app:cardElevation="5dp">


                    <android.support.v7.widget.LinearLayoutCompat
                        android:layout_width="match_parent"
                        android:layout_height="140dp"
                        android:padding="20dp"
                        android:background="#0A1128"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            android:layout_gravity="start"
                            android:adjustViewBounds="true"
                            android:padding="5dp"
                            android:scaleType="fitCenter"
                            android:src="@drawable/prenotazioni"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:text="PRENOTAZIONI"
                            android:textAlignment="center"
                            android:textColor="#ffffff"
                            android:textSize="18sp"
                            tools:ignore="HardcodedText" />


                    </android.support.v7.widget.LinearLayoutCompat>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:visibility="gone"
                    android:id="@+id/btn1"
                    android:layout_width="match_parent"
                    app:cardCornerRadius="0dp"
                    android:layout_height="0dp"
                    app:cardBackgroundColor="#132150"
                    android:layout_weight="1"
                    app:cardElevation="5dp">


                    <android.support.v7.widget.LinearLayoutCompat
                        android:layout_width="match_parent"
                        android:layout_height="140dp"
                        android:padding="20dp"
                        android:background="#132150"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            android:layout_gravity="start"
                            android:adjustViewBounds="true"
                            android:padding="5dp"
                            android:scaleType="fitCenter"
                            android:src="?android:attr/fastScrollThumbDrawable"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:text="FTP"
                            android:textAlignment="center"
                            android:textColor="#ffffff"
                            android:textSize="18sp"
                            tools:ignore="HardcodedText" />


                    </android.support.v7.widget.LinearLayoutCompat>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    app:cardCornerRadius="0dp"
                    android:id="@+id/settings"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    app:cardBackgroundColor="#232323"
                    app:cardElevation="2dp">

                    <android.support.v7.widget.LinearLayoutCompat
                        android:layout_width="match_parent"
                        android:layout_height="140dp"
                        android:padding="20dp"
                        android:background="#232323"
                        android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="70dp"
                        android:layout_gravity="start"
                        android:adjustViewBounds="true"
                        android:padding="7dp"
                        android:scaleType="fitCenter"
                        android:src="@drawable/settings"
                        tools:ignore="ContentDescription" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="SETTINGS"
                        android:textAlignment="center"
                        android:textColor="#ffffff"
                        android:textSize="18sp"
                        tools:ignore="HardcodedText" />

                    </android.support.v7.widget.LinearLayoutCompat>


                </android.support.v7.widget.CardView>

            </LinearLayout>

    </ScrollView>






</LinearLayout>

While here is button.xml

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/orders"
    app:cardCornerRadius="0dp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:cardBackgroundColor="#00468a"
    app:cardElevation="15dp">


    <android.support.v7.widget.LinearLayoutCompat
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:background="#004181"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_gravity="start"
            android:adjustViewBounds="true"
            android:padding="5dp"
            android:scaleType="fitCenter"
            android:src="@android:drawable/star_big_on"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/textHERE"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text=""
            android:textAlignment="center"
            android:textColor="#ffffff"
            android:textSize="18sp"
            tools:ignore="HardcodedText" />


    </android.support.v7.widget.LinearLayoutCompat>

</android.support.v7.widget.CardView
    >

2 Answers 2

1

You inflate the new xml with View newView = LayoutInflater.from(context).inflate(resource_id, parentView); Cast the return result to whatever class you need it to be. Use the layout you want to put it in as the parent view. You can then use findViewById on the returned view to find subviews and set their values.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll try it immediatly
0

It is very easy

val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.view_from_xml, null)

viewToAddView.add(view)

1 Comment

It can be slightly easier than this. Use viewToAddView as the second parameter in inflate and it will add it for you. That's actually more correct, as your way may not set up the layout parameters correctly in some cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.