How to Put a Linear Layout
How to Put a Linear Layout
I have created in my file .xml a linear layout
I want to create in this linear layout a linear layout which contains 3 linear layout with this latter contains a button and a text view below
I create a method but it does not display anything:
here's the method :
public void putLinearLayoutProduct(double count){
LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1);
int mButtonHeight = 60;//hauteur
int mButtonWidth = 120;//longueur
while(count >0)
{
LinearLayout L = new LinearLayout(this);
L.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //<--this line , is what you were missing
L.setLayoutParams(params);
for(int i=1;i<=3;i++)
{
LinearLayout L1 = new LinearLayout(this);
L1.setBackgroundColor(Color.WHITE);
L1.setLayoutParams(params);
Button b= new Button(this);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.BLUE);
tv.setText("name");
L1.setOrientation(LinearLayout.VERTICAL);
b.setWidth(mButtonWidth);
b.setHeight(mButtonHeight);
L1.addView(b);
L1.addView(tv);
L.addView(L1);
count --;
}
Linear.addView(L);
}
}
suspect you are trying to do what a ListView already does. See the developers network article on ListViews. What I would suggest doing is create one Layout for just the inner most LinearLayout (I call this text_and_btton.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:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:text="" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/blue" />
</LinearLayout>
<?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:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:text="" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/blue" />
</LinearLayout>
Then you define the outer layout with a ListView:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
Then you make an adapter that basically is a list of the text and images you want to put into the ListView. The adapter creates the 'inner most' of you layouts - the one with the button and text. Here is an example:
public class ImageStuffAdapter extends ArrayAdapter<ImageStuffAdapter.Image>
{
private static final Image[] images = new Image[]
{
new Image("pizza", R.drawable.pizza),
new Image("fish", R.drawable.fish),
new Image("boissoins", R.drawable.boissoins)
};
public ImageStuffAdapter(Context context)
{
super(context, R.id.label);
}
@Override
public int getCount()
{
return images.