11

I'm having one empty Linear Layout in xml. And I'm adding some text view to it dynamically.

once these textviews exceeds 5 then i'm creating one more linear layout inside it and adding text views to newly created Layout. And finally adding this new layout to main layout.

I'm able to add, i.e in emulator it occupy that space but, will not display the info in the textviews.

my xml is as follows:

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

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/dyn_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/dyn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add TextViews" />

</LinearLayout>

and my java file is as follows:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddTextViewsDynamically extends Activity implements
        OnClickListener {

    Button button;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_textview_dynamically);

        button = (Button) findViewById(R.id.dyn_button1);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.dyn_button1:
            LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
            LinearLayout layout2 = null;
            LinearLayout layout3 = null;
            for (int i = 0; i < 10; i++) {
                if (i > 4) {
                    if (i == 5) {
                        layout2 = new LinearLayout(this);
                        layout2.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout2.setOrientation(LinearLayout.VERTICAL);
                        layout2.setPadding(10, 60, 10, 10);
                        layout3 = new LinearLayout(this);
                        layout3.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout3.setOrientation(LinearLayout.HORIZONTAL);
                        layout3.setPadding(10, 10, 10, 10);
                    }
                    System.out.println("**** Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout3.addView(text);

                    if (i == 9) {
                        System.out
                                .println("Added second linear layout to first");
                        layout2.setVisibility(View.VISIBLE);
                        layout2.addView(layout3);
                        layout.addView(layout2);
                    }
                } else {
                    System.out.println("###### Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout.addView(text);
                }
            }

        }

    }

}

Where am I going wrong?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Mouni
  • 113
  • 1
  • 1
  • 8
  • Can you tell why you are doing this, so that alternate and best solutions if available can be provided? – Andro Selva Jul 31 '12 at 07:11
  • I will be getting dynamically some info from service which i have to display in text views 5 in each line. if its more then in next line. – Mouni Jul 31 '12 at 07:24
  • Is there any other way from which i can do it..?? please let me know – Mouni Jul 31 '12 at 07:24
  • i implemented your code and the "the value of i is : 0..." is showing ok. please give more info or post manifest and other elements. – cosmincalistru Jul 31 '12 at 07:34
  • @cosmincalistru: Yeah u are right u will get one line with 5 text fields. But according to my code i should get two lines with 10 text fields data.. – Mouni Jul 31 '12 at 08:09

3 Answers3

5

Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don't see them. Try using

text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));

instead of

text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));

and you should see all your text views.

EDIT : In your case this should do the trick; xml :

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

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/dyn_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/dyn_layout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone"
            android:padding="10dip" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/dyn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add TextViews" />

</LinearLayout>

and code :

@Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.dyn_button1:
            LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
            LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
            LinearLayout layout3 = null;
            for (int i = 0; i < 10; i++) {
                if (i > 4) {
                    if (i == 5) {
                        layout2.setPadding(10, 60, 10, 10);
                        layout3 = new LinearLayout(this);
                        layout3.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout3.setOrientation(LinearLayout.HORIZONTAL);
                        layout3.setPadding(10, 10, 10, 10);
                    }
                    System.out.println("**** Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout3.addView(text);

                    if (i == 9) {
                        System.out
                                .println("Added second linear layout to first");
                        layout2.setVisibility(View.VISIBLE);
                        layout2.addView(layout3);
                    }
                } else {
                    System.out.println("###### Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout.addView(text);
                }
            }

        }

    }
cosmincalistru
  • 1,283
  • 9
  • 20
  • Thank u soo much. Yeah all 10 textviews are adding vertically. But i want only 5 in one line and another 5 in next what am i suppose to do to get the layout in such way..? plz help me out.. – Mouni Jul 31 '12 at 10:05
  • yeah it worked out but with little changes since i wont add layout in xml.. it will be dynamically added.. Now its working fine.. :) Thank u once again.. – Mouni Jul 31 '12 at 10:26
0

Try this one, then you will see that your code is OK.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="80" >

    <LinearLayout
        android:id="@+id/dyn_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >
    </LinearLayout>
</HorizontalScrollView>

<Button
    android:id="@+id/dyn_button1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="20"
    android:text="Add TextViews" />

</LinearLayout>
osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • Nope dint worked out.. :( the diff is just button is coming at the end of the screen.. Is there anything that i have to change..?? – Mouni Jul 31 '12 at 07:27
  • see my edited answer, if you want to add the text view horizontally you need to use HorizontalScrollView instead of regular one. And let me know if it works, (By the way I tried it, and it worked with me) – osayilgan Jul 31 '12 at 07:50
  • I just couldn't get the point, what do you want exactly ? Everything works fine, what do you mean about "info" ?? If you make it clear then I can show you the way. – osayilgan Jul 31 '12 at 08:27
  • info is nothing but data.. I want data as 5 text views in horizontal linear layout and if data what i get is more than 5 then i have to create one more such layout and add it. at that time i should have two rows of data each having five text fields arranged horizontally.. I think now u got what i want.. if not please let me know.. – Mouni Jul 31 '12 at 08:30
0

Actually you are doing things right, I just removed padding and made orientation of all Linear Layouts to vertical. Modify this on xml file too and it will work :))) Its just you are adding a padding of 10 or 60 which is going of the view taken by the parent.

Modified Java Code:

@Override
public void onClick(View v) {

 switch (v.getId()) {
    case R.id.dyn_button1:
        LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
        LinearLayout layout2 = null;
        LinearLayout layout3 = null;
        for (int i = 0; i < 10; i++) {
            if (i > 4) {
                if (i == 5) {
                    layout2 = new LinearLayout(this);
                    layout2.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    layout2.setOrientation(LinearLayout.VERTICAL);
                 //   layout2.setPadding(10, 10, 10, 10);
                    layout3 = new LinearLayout(this);
                    layout3.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    layout3.setOrientation(LinearLayout.VERTICAL);
               //     layout3.setPadding(10, 10, 10, 10);
                }
                System.out.println("**** Adding text view " + i);
                TextView text = new TextView(this);
                text.setText("The Value of i is :" + i);
                text.setTextSize(12);
                text.setGravity(Gravity.LEFT);
                text.setLayoutParams(new LayoutParams(155,
                        LayoutParams.WRAP_CONTENT));
                layout3.addView(text);

                if (i == 9) {
                    System.out
                            .println("Added second linear layout to first");
                    layout2.setVisibility(View.VISIBLE);
                    layout2.addView(layout3);
                    layout.addView(layout2);
                }
            } else {
                System.out.println("###### Adding text view " + i);
                TextView text = new TextView(this);
                text.setText("The Value of i is :" + i);
                text.setTextSize(12);
                text.setGravity(Gravity.LEFT);
                text.setLayoutParams(new LayoutParams(155,
                        LayoutParams.WRAP_CONTENT));
                layout.addView(text);


            }
        }

    }

}
abhy
  • 933
  • 9
  • 13
  • Hello abhy, Are u getting two lines of information or just one..? – Mouni Jul 31 '12 at 08:15
  • yeah it worked fine with vertical layout.. But i want it in horizontal layout ie 5 values in each line.. what i should do for that..?? – Mouni Jul 31 '12 at 08:16