0

I want to add two linear layouts after one layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programatically. I could do this within xml, but I want to do this in program. here is what i want:

all are button

first button contains full width of screen but after this two button will share equal width and so on...

obviously, need to do it programmatically.

my layout:

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

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </LinearLayout>


    </ScrollView>
</LinearLayout>

code behinds:

Button button = new Button(HomeActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 564);
                        layoutParams.setMargins(60, 80, 60, 40);

 Drawable drawable = Drawable.createFromStream(getAssets().open("image/GridButton/NovoTel_IGW_lg.png"), null);
button.setBackground(drawable);
button.setLayoutParams(layoutParams);

linearLayout.addView(button);

I got output like this:

enter image description here

Please suggest, which is the best way.

Syed Md. Kamruzzaman
  • 979
  • 1
  • 12
  • 33

2 Answers2

2

The simplest way to reach that would be using LinearLayout(s) with weight. Here's a basic example:

<?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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:weightSum="3">


    <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="2">
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="2">
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

Output: enter image description here

Then you can start implementing your graphics. You don't need a ScrollView in the suggested layout, but of course you'll be able to add it later if it becomes necessary.

Neria Nachum
  • 1,519
  • 1
  • 20
  • 37
1

Here's the answer. I tasted it. If it fulfills then marked as answer:

try {

   DisplayMetrics dm = new DisplayMetrics();
   this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;   
    int j = 0 ;
    String image;
    for(int i=0;i<(**JsonArray**).length();i++) {

        JSONObject jsonObject1 = **JsonArray**.getJSONObject(i);


        if(i==0) {
            image = //DrawableImage;
            drawable = Drawable.createFromStream(getAssets().open(image), null);
            Button button = new Button(HomeActivity.this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 564);
            layoutParams.setMargins(60, 80, 60, 40);
            button.setBackground(drawable);
            button.setLayoutParams(layoutParams);
            linearLayout.addView(button);
        }
        else
        {
            if(i%2 == 1)
            {
               image = //DrawableImage;
                drawable = Drawable.createFromStream(getAssets().open(image), null);
                LinearLayout linearLayout1=new LinearLayout(this);
                linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
                Button button = new Button(HomeActivity.this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width/2-120, 564);
                layoutParams.setMargins(60, 80, 60, 40);
                button.setBackground(drawable);
                button.setLayoutParams(layoutParams);
                linearLayout1.addView(button);


                if(i+1 < data.length())
                {
                    JSONObject jsonObject2 = data.getJSONObject(i+1);
                    image = //DrawableImage;
                    drawable = Drawable.createFromStream(getAssets().open(image), null);
                    Button button2 = new Button(HomeActivity.this);
                    button2.setBackground(drawable);
                    button2.setLayoutParams(layoutParams);
                    linearLayout1.addView(button2);

                }

                linearLayout.addView(linearLayout1);

            }
            else
            {
                continue;
            }

        }


    }




}catch (IOException ex)
{
    return;
}