Nurkanat Khametov
Nurkanat Khametov

Reputation: 40

Layout overlaying another layout after dynamically change

I'm suffering some problems with my layouts in my app. After changing text of TextView (dynamically) to some long text, it overlays my TableLayout. Hope you can help me to determine how to move TableLayout to visible range.

UPD. My bad, that I'm not defined which TextView I've changed. It's TextView above TableLayout with id = textView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.home.myapp1.MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:text="Hello world!"
        android:singleLine="false"/>

    <TableLayout android:layout_marginTop="10dp"
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*">
        <TableRow android:padding="5dp">
            <TextView android:text="Валюта" android:gravity="center_horizontal"/>
            <TextView android:text="Покупка" android:gravity="center_horizontal"/>
            <TextView android:text="Продажа" android:gravity="center_horizontal"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

Upvotes: 0

Views: 79

Answers (2)

Gautam Malik
Gautam Malik

Reputation: 514

I tried using the given layout xml and changed the text of textview dynamically (for testing purpose I changed text on button click which I placed below the TableLayout) but it was working fine until and unless TextView covers the whole screen height. If there is no limit for your textview, then should use ScrollView as the parent of your layout. So try replacing your xml content with below content and it may solve your issue:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.home.myapp1.MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:text="Hello world!"
        android:singleLine="false"/>

    <TableLayout android:layout_marginTop="10dp"
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*">
        <TableRow android:padding="5dp">
            <TextView android:text="Валюта"   android:gravity="center_horizontal"/>
            <TextView android:text="Покупка" android:gravity="center_horizontal"/>
            <TextView android:text="Продажа" android:gravity="center_horizontal"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

</ScrollView>

Upvotes: 0

Arjun saini
Arjun saini

Reputation: 4182

Try to weight and single line to textview Like this........

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"

        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:text="Hello world!"
            android:singleLine="false"/>

        <TableLayout android:layout_marginTop="10dp"
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*">
            <TableRow android:padding="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3">
                <TextView android:text="Валюта"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:layout_weight="1" android:gravity="center_horizontal"/>
                <TextView

                    android:text="Покупка"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:maxLines="1"
                    android:gravity="center_horizontal"/>
                <TextView android:text="Продажа"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:maxLines="1"
                    android:gravity="center_horizontal"/>
            </TableRow>
        </TableLayout>
    </LinearLayout>

**Here **

 android:maxLines="1"

Used for View is make good if you not give this than the layout view make bad when textview length many words...or you also give the length to textview

Upvotes: 1

Related Questions