B
B
Boldy2015-08-10 09:46:47
Android
Boldy, 2015-08-10 09:46:47

How to stick two children of a TableRow to different screen edges?

We want the text to stay on the left and the button to float on the right, like a float: right. How to implement it?
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="kg.zuber.pocketlawyer.activities.MyLawsActivity">


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button"
                android:layout_marginRight="0dp"
                android:layout_marginLeft="0dp"
                android:layout_weight="0" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

java: (adds such tableRows in onCreate)
for (int i = 0; i < savedLaws.size(); i++) {
                final Law law = savedLaws.get(i);
                TableRow tableRow = new TableRow(this);
                tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));
                TextView lawTitleLabel = new TextView(this);
                lawTitleLabel.setText(law.getTitle());
                tableRow.addView(lawTitleLabel);
                Button deleteButton = new Button(getApplicationContext());
                deleteButton.setBackgroundResource(R.drawable.button);
                deleteButton.setText("X");
                TableRow.LayoutParams rlp = new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.MATCH_PARENT);
                tableRow.setGravity(Gravity.END);
                deleteButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dbm.deleteLaw(law.getId());
                        startActivity(new Intent(MyLawsActivity.this, MyLawsActivity.class));
                    }
                });
                tableRow.addView(deleteButton);
                tableLayout.addView(tableRow, i);
            }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arseniy Lisunov, 2015-08-10
@Boldy

Try using android:layout_weight="1" along with android:layout_width="0dp" in TextView:

<TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView"/>

Programmatically, this is how it should work:
TextView lawTitleLabel = new TextView(this);
lawTitleLabel.setText(law.getTitle());
lawTitleLabel.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 0, 1f));

I
Ivan, 2015-08-10
@sputnic

android:gravity = right for button

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question