SirArt
SirArt

Reputation: 425

Set the same height to a TextView and an EditText in a row of a TableLayout?

I need a TableLayout with TextViews and EditTexts and it should be like a crossword puzzle.

I've tested this with LinearLayout, width=0dp and weight=1, but it didn't worked like I wanted. Then I've tried it with a TableLayout and I think I'm near to the solution. I've tested with heights and widths in the XML or in the Java but it didn't work. My Problem now is that all TextViews are thinner than the EditText and I don't know why and how I can make them look the same.

XML-files:

<!-- activity_sraetsel_main.xml -->
<TableLayout    xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/table"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:orientation="vertical" >        
</TableLayout>

<!-- activity_sraetsel_row.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TableRow   xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableRow"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center_horizontal">
</TableRow>

<!-- activity_sraetsel_edittext.xml -->
<?xml version="1.0" encoding="utf-8"?>
<EditText   xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/cell_border"
        android:id="@+id/textView"
        android:inputType="textCapCharacters"
        android:gravity="center_horizontal"
        android:maxLength="1">
</EditText>

<!-- activity_sraetsel_textview.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView   xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/cell_border"
        android:id="@+id/textView"
        android:textSize="6.5sp"
        android:singleLine = "false" >
</TextView>

<!-- cell_border.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#515151"/>
<padding android:left="10dp" android:top="5dp"
android:right="10dp" android:bottom="5dp" />
</shape>

and the java part, where I put all together:

TableLayout table = (TableLayout) this.findViewById(R.id.table);
for (int y = 0; y < hoehe; y++) {
    // Inflate your row "template" and fill out the fields.
    TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.activity_sraetsel_row, null);
    for (int x = 0; x < breite; x++) {
        if (raetsel[y][x].startsWith(",", 1)) {
            EditText eText = (EditText) LayoutInflater.from(this).inflate(R.layout.activity_sraetsel_edittext, null);
            if (!(raetsel[y][x].startsWith("a"))) {
                eText.setText(raetsel[y][x].substring(0, 1));
            } //if
            eText.setWidth(60);
            eText.setHeight(60);
            row.addView(eText);
            Log.d(TAG, "SRaetselMain, AntwortFeld erstellt");
        } else {
            TextView vText = (TextView) LayoutInflater.from(this).inflate(R.layout.activity_sraetsel_textview, null);
            vText.setText(raetsel[y][x].substring(0, raetsel[y][x].indexOf("|")));
            vText.setWidth(60);
            vText.setHeight(60);
            row.addView(vText);
            Log.d(TAG, "SRaetselMain, FrageFeld erstellt");
        } //if
    } //for
    table.addView(row);
} //for

table.requestLayout();

And finally how it looks like:

EDIT1: With android:layout_height="match_parent" for TextView in XML and without the setHeight() for TextView in Activity:

https://i.sstatic.net/btzj4.png

EDIT2: With android:layout_weight="1" and android:layout_height="0dp"it looks the same like EDIT1

Now I've tested it without TextViews and only with EditText, and I found that the problem is the textSize. If I make the TextView without the option android:textSize="6.5sp" it works. But then the text is to big and unfortunately I need this text that small.

EDIT3: I've tried this with LinearLayout and with two TextViews. I really don't know how I can change that this two Views are at the same height.

https://i.sstatic.net/wIENf.png

Thank you for your help in this matter.

Upvotes: 2

Views: 6780

Answers (2)

SirArt
SirArt

Reputation: 425

Answer to my own Question:

The Problem is solved with this: android:baselineAligned="false" Put this line into this XML and it works!

 <!-- activity_sraetsel_row.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TableRow   xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableRow"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:gravity="center_horizontal"
    android:baselineAligned="false">
</TableRow>

Thank you for your help!

Upvotes: 2

Krrishnaaaa
Krrishnaaaa

Reputation: 689

Use android:layout_height="match_parent" for TextView in XML and remove the setHeight() for TextView in Activity. Since, you are setting size of EditText manually, so, there is no need for change in it. By doing this TextView will take all the space for height.

EDIT

<!-- activity_sraetsel_edittext.xml -->
<?xml version="1.0" encoding="utf-8"?>
<EditText   xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/cell_border"
        android:id="@+id/textView"
        android:inputType="textCapCharacters"
        android:gravity="center_horizontal"
        android:layout_width="60dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:maxLength="1">
</EditText>

<!-- activity_sraetsel_textview.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView   xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/cell_border"
        android:id="@+id/textView"
        android:textSize="6.5sp"
        android:layout_width="60dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:singleLine = "false" >
</TextView>

Try setting layout_height to 0dp and layout_weight to 1.

If you want same height of both Views then layout_weight could help.

Upvotes: 0

Related Questions