5

I need to make a layout which is specific to webpage and it needs to look like a web article with an image and a text left to and under it. Look at the image.

As this is an easy thing to do in a web world, I am not sure how to do it in XML layout.

Obviously, the TextView must be on the left while the image is present, and after it reaches the bottom of the image, it has to stretch to the screen width. I tried this with two TextViews (one for left and another for bottom), but it just does not look right due to the text font size.


EDIT

I also tried to dinamically catch the height of image and then assign it to my TextView, and then make another TextView with the size of the screen under these two elements. This does not work as well as I cannot control the text that is not visible due to limited height of the first TextView.

sandalone
  • 41,141
  • 63
  • 222
  • 338
  • add your code which u have tried than can able to answer in better way – Khan Apr 04 '12 at 09:37
  • 1
    I tried to do this before, but did not be able to accomplish. I believe this can be happen only if you take the TextView class and manipulate it (override) in order to do what you need. Hard to do. or maybe it can be done in coding. If you have an answer please share it. – Hesham Saeed Apr 04 '12 at 10:24
  • 1
    Maybe at this link you will find something useful: http://dev.androidteam.ru/snippets/textview/leadingmarginspan2 – Ant4res Apr 04 '12 at 13:06
  • @Khan My code LOL. This is so simple task that putting 2 textviews and 1 imageview would help you none. – sandalone Apr 04 '12 at 16:47
  • @HeshamSaeed Yes and I would have to implement counter of how many characters can fit in TextView of such and such height. – sandalone Apr 04 '12 at 16:51
  • i have add some code in my ans may it helps u – Khan Apr 05 '12 at 07:46
  • @V4l3ri4 This may look correct, but I was unable to make it work. Have you? – sandalone Apr 12 '12 at 16:28
  • @sandalone Yes, the example works, but actually, I haven't tried to set the image on the right. Have you tried to change it? – Ant4res Apr 13 '12 at 12:02

3 Answers3

1

If you haven't found the answer yet. Here is the same kind of question that is being answered.

How to align TextView around an ImageView?

The only thing what you might have to change in your case is to, make the alignment to be opposite way.

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
    android:textSize="18.0sp"
    android:id="@+id/message_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text" />
<ImageView 
    android:src="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/message_view"
    android:id="@+id/icon" />

Taken from here,

http://dev.androidteam.ru/snippets/textview/leadingmarginspan2

Community
  • 1
  • 1
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
0

check out this. it does same as you want

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_marginTop="5dp"
            android:background="#FFFFFF"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="fill_parent"
            android:background="#FFFFFF"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:layout_marginTop="5dp"
            android:text="TextView" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"

        android:gravity="center"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#FFFFFF" />

    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView6"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#FFFFFF"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView7"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#FFFFFF"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView8"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#FFFFFF"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView9"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#FFFFFF"
        android:text="TextView" />

</LinearLayout>
</LinearLayout>
Ravi1187342
  • 1,247
  • 7
  • 14
0

use this way layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="21dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/imageView1"
        android:layout_marginLeft="30dp"
        android:textColor="#fff"
        android:text="TextView" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:text="TextViewaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        android:layout_alignBottom="@+id/textView1"></TextView>

</RelativeLayout>

And add in code

   TextView tv1=(TextView)findViewById(R.id.textView1);
   tv1.setMaxHeight(maxHeight);

where maxHeight is dynamically get height of imageview

and than put condition to by getting height of textview1 to assign text to second textview

may it helps u

enter image description here like this am I right?

Khan
  • 7,585
  • 3
  • 27
  • 44
  • Can you explain more part where I dynamically detect when TextView1 is full and that remaining text goes to TextView2? This part was confusing for me when I started this topic. – sandalone Apr 05 '12 at 19:09
  • 1
    i think use example as given at http://dev.androidteam.ru/snippets/textview/leadingmarginspan2 it's good enough – Khan Apr 06 '12 at 05:34
  • more thing you can do it is from your textview1 textsize get it line height and divide your imageview height by that line height and set ans to textview max lines and get substring from maintext according to char per line and assign to first textview and remaing text to second textview – Khan Apr 06 '12 at 11:48
  • Hm, can you make the Russian example work? It does not work in my case. – sandalone Apr 06 '12 at 14:56