Flo
Flo

Reputation: 1207

Android LinearLayout in ScrollView

I tryning to get a LinearLayout in a ScrollView like this:

The greenspace should be wrap_content and the red one should take the remaining space.

But this is my Result:

This is my Code:

foreach(Ele el : elements) {}
    LinearLayout layout = new LinearLayout(getActivity());
    LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setLayoutParams(layout_parm);

    TextView tv = new TextView(getActivity());
    tv.setLayoutParams( new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1) );
    tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    tv.setText("Name...");
    tv.setPadding(0, 0, 0, getDP(5));
    layout.addView(tv);

    TextView iView = new TextView(getActivity());
    iView.setText("OTO");
    iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
    iView.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0) );
    layout.addView(iView);

    frg_layout.addView(layout); 
}

I am so confused! Maybe you can help me to find out my fail... Thank you!

The Parent-ViewGroup:

<LinearLayout 
    android:id="@+id/frg_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:gravity="top"
    android:orientation="vertical" />

Upvotes: 3

Views: 375

Answers (3)

Flo
Flo

Reputation: 1207

Using LayoutInflater with xml will bring the solutuion!

Thank you!

Upvotes: 0

Jai
Jai

Reputation: 486

you change the layout orientation

 LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(layout_parm);

uset this type in xml

<LinearLayout 
 android:id="@+id/frg_layout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

<TextView
android:id="@+id/TextView1"
android:layout_width="250dp"
android:layout_height="60dp"/>

<TextView
android:id="@+id/textview2"
android:layout_width="250dp"
android:layout_height="60dp"
/>
</linearlayout>

Upvotes: 1

Matter Cat
Matter Cat

Reputation: 1578

I think you've mostly got it right, just need a few minor tweaks:

 for each(Ele el:elements){

        LinearLayout layout = new LinearLayout(getActivity());
        LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(layout_parm);

        TextView tv = new TextView(getActivity());

        //adjust the last number here to make the first block wider or larger as you want
        tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
        tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        tv.setText("Name...");
        tv.setPadding(0, 0, 0, getDP(5));
        layout.addView(tv);

        TextView iView = new TextView(getActivity());
        iView.setText("OTO");
        iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));

        //adjust your layout param to be a weight instead of wrap_content
        iView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        layout.addView(iView);

        frg_layout.addView(layout);
    }

See if that works! An alternative would be to build an xml layout which I think is generally cleaner, but programatically it should work roughly the same way.

Upvotes: 0

Related Questions