How to compose a Linear Layout in Activity using JAVA only

0

In my application it has a vertical linear layout with id:

@+id/items

In this layout for each item contained in a list, I would like to create another Horizontal Linear Layout called, Row containing 2 ImageButton and an EditText, an ImageButton to be clicked would remove the current item, another would mark the item as correct. p>

Activity XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.example.leonardo.quiz.QuestionFormActivity">

<ImageButton
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/btn_media"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/ipt_media"
    android:src="@drawable/question" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ipt_value"
    android:layout_below="@+id/btn_media"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />


<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/items"
    android:layout_below="@+id/ipt_value"
    android:layout_centerHorizontal="true"
    ></LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_add"
    android:id="@+id/btn_add"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/items"
    />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_back"
    android:id="@+id/btn_back"
    android:layout_below="@id/btn_add"
    />
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_exit"
    android:id="@+id/btn_exit"
    android:layout_below="@id/btn_back"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_save"
    android:id="@+id/btn_save"
    android:layout_below="@+id/btn_exit"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

However, what is the correct way to fill this LinearLayout entirely in JAVA, reminding you that you will have another LinearLayout containing ImageButton and EditText.

    
asked by anonymous 16.10.2014 / 06:08

1 answer

1

Real example:

private ViewGroup createAlphabetTrack() {
        final LinearLayout layout = new LinearLayout(getActivity());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (30 * getResources().getDisplayMetrics().density), LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params.addRule(RelativeLayout.BELOW, R.id.tv_title);
        layout.setLayoutParams(params);
        layout.setOrientation(LinearLayout.VERTICAL);

        final LinearLayout.LayoutParams textparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        textparams.weight = 1;
        final int height = getResources().getDisplayMetrics().heightPixels;
        int iterate = 0;
        if (height >= 1024){
            iterate = 1; layout.setWeightSum(26);
        } else {
            iterate = 2; layout.setWeightSum(13);
        }
        for (char character = 'a'; character <= 'z'; character+=iterate) {
            final TextView textview = new TextView(getActivity());
            textview.setLayoutParams(textparams);
            textview.setGravity(Gravity.CENTER_HORIZONTAL);
            textview.setText(Character.toString(character));
            layout.addView(textview);
        }

        return layout;
    }

All attributes are set via code.

Finally, you get the created linear and add it to another view.

If it's in a fragment, you can do:

ViewGroup.class.cast(getView()).addView(sua view criada);

Or

  [sua View Group].addView([sua view criada]);
    
21.10.2014 / 16:05