Hayk Abelyan
Hayk Abelyan

Reputation: 326

Android-Make TextView to fill all the remaining space in layout

I have a news activity with ImageView and TextView with news content. I want to make news content fill all the space.

The news content filled all the space to the right of image view, so the text was placed to the right of picture, but under the picture it was free space. I decided to put the ImageView and TextView in separated linear layout. But the text disappeared after the area right from image was filled.

The problem is displayed here.

My questions are:

  1. How I can place the remaining text under the image view?

  2. How I can make the picture to keep his height/width ratio?

Here is the XML code:

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.tutorialsbuzz.phrasebook.NewsActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textViewHeaderNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewDateNews"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="header"
            android:textColor="@color/mycolor"
            android:textSize="16dp"
            android:textStyle="bold|italic" />

        <LinearLayout
            android:id="@+id/newslayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewHeaderNews"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageViewNews"
                android:layout_width="165dp"
                android:layout_height="165dp"
                android:layout_margin="2dp"
                android:layout_weight="0"
                android:scaleType="fitXY"
                android:src="@drawable/world"/>

            <TextView
                android:id="@+id/textViewContentNews"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:text="jklasdf as ;AJFWADLHF LAS HFLAF VANLASN: l galgsalgngan  ;wafasldwlaf alla lnaf alf lg;aeguewaf hwla hfnalgh wa
                j;fklasgj;s;nva;   alsf las ps z; laszfj alsz vzlxg a lrg z;dnlxfdj glzf ;f glaerzlls az
                ;asdjk  aF{ af[ [D{j  as ;as ; wjv a jkn da weq  g ; aeskj; g ;eakr;ja r"
                android:textSize="14dp" />
        </LinearLayout>

        <TextView
            android:id="@+id/textViewDateNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="2dp"
            android:layout_marginTop="2dp"
            android:text="date"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/textViewViewsNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/newslayout"
            android:layout_margin="3dp"
            android:text="views"
            android:textSize="13dp" />

        <TextView
            android:id="@+id/textViewRating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/newslayout"
            android:layout_margin="3dp"
            android:text="rating"
            android:textSize="13dp" />

        <TextView
            android:id="@+id/textViewRatingNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/spinnerRatingNews"
            android:layout_margin="4dp"
            android:layout_toLeftOf="@+id/spinnerRatingNews"
            android:text="Գնահատեք."
            android:textSize="14dp" />

        <Spinner
            android:id="@+id/spinnerRatingNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/textViewRating"
            android:layout_margin="3dp" />

    </RelativeLayout>
</ScrollView>

Upvotes: 0

Views: 1316

Answers (1)

yotam hadas
yotam hadas

Reputation: 853

Well its pretty complex to explain (for me anyway). There is no way to do it with xml but you can do it through code,I will refer your to post I used to achive the same resault as you want. (wrap textview around other view).

Read this post

the above like is what I used and its have pretty nice resault.

You requested in the comment to resize your image btu to keep its ratio.

This is a post about the subject of resizing image.

first check if the image is larger then X if it is then resize it with factor that match your needs.

Upvotes: 2

Related Questions