Milan Gajera Milan Gajera - 3 months ago 23
Android Question

How can I set TextView into LinearLayout programmatically in Android?

I am creating a

TextView
dynamically. Here, I am creating a mainLayout in which I have two child layout and I wan gave them weight.

Here, is my Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">

<LinearLayout
android:id="@+id/Layout_second_overs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical">

<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="@+id/Layout_second_balls"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">

<!--Second textview balls--> //here i inflate 12 textview dynamically but not 12 seen on the device while i run the app.
</LinearLayout>
</LinearLayout>
</LinearLayout>


Code to create a 12
TextViews
and put into layout.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

for(int a = 0; a <= 12; a++)
{
TextView first = new TextView(getApplicationContext());
first.setLayoutParams(params);
first.setTextColor(Color.parseColor("#000000"));
first.setTextSize(12);
first.setGravity(Gravity.LEFT);
first.setPadding(5, 0, 5, 0);
first.setText("12");
layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}


Problem is my all data are print into log but not visible on the device.

Please, help me to solve out this problem.

Answer

There are two issues

  • One is for the Layout Design related
  • Second based on the layout design and property you also need to handle is while you are adding dynamically view into linear layout

Here what i have changed in Your xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:baselineAligned="false"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/Layout_second_overs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--Second textview overs-->
        </LinearLayout>
        <LinearLayout
            android:id="@+id/Layout_second_balls"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" android:background="#FF13C7FF"
            android:orientation="horizontal">

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

In Layout_second_overs linearlayout height , Width & weight is the issue.
& in Layout_second_balls linear layout you have used weight as 1 so you also required layout_height as 0dp if orientation is horizontal

Now below things i have changed in code

LinearLayout.LayoutParams params = new
                LinearLayout.LayoutParams
                (0, LinearLayout.LayoutParams.WRAP_CONTENT);
        for(int a = 0;a<=12;a++)
        {
            TextView first = new TextView(getActivity());
            first.setLayoutParams(params);
            first.setTextColor(Color.parseColor("#000000"));
            first.setTextSize(12);
            params.weight = 1; /// Need to give weight if you want equal size of textview inside Linear
            first.setGravity(Gravity.LEFT);
            first.setPadding(5, 0, 5, 0);
            first.setText("12");
            Layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
        }

above things are based on the question which you ask

Suggestion

Avoid adding dynamic view like you asked . prefer to use ListView, GridView, RecyclerView for this type of functionality.