How to compose a Linear Layout in Activity using JAVA only

Asked

Viewed 538 times

0

In my application 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 when clicked would remove the current item, another would mark the item as correct.

XML da Activity

<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, remembering that it will have another Linearlayout containing Imagebutton and Edittext.

  • Will you have to create these elements based on a list? If yes, a ListView doesn’t meet your need? Otherwise, have a look at these responses: http://answall.com/questions/35636/como-addir-um-imageview-em-um-relativelayout-em-runtimer/35638#35638:http://answall.com/questions/32225/botão-para-addir-um-novo-campo/32244#32244 and http://en.stackoverflow.com/questions/35636/how-to-add-an-imageview-in-a-relativelayout-in-runtimer/35638#35638 and see if it doesn’t meet.

  • Actually I prefer not to use a Listview, and the View items should increase with the number of items and should not be rolling

  • Then you will have to make the addition programmatically. Take a look at these two questions/answers to see if it no longer answers your question.

  • I’m already able to create programmatically, but Imagebutton needs to have a specific size, how do I do it , using Layoutparams.

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.

To finish, you take the created linear and add in another view.

If it’s an Fragment, you can do it:

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

Or

  [sua View Group].addView([sua view criada]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.