CraZyDroiD
CraZyDroiD

Reputation: 7115

Place a image view and a Text view in same line

I have several logos and i want to set a Textview right next to it so that they are in the same line. I can't place them in the same line.please help me. I want my textview right next to my image.

my 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" >

    <!--
      <fragment 
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    -->

    <TextView
        android:id="@+id/filmhall_contactdetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_contactdet"
        android:textColor="#000000"
        android:layout_margin="4dp"
        android:toRightof="@+id/pointer"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/filmhall_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="2dp"
        android:text="@string/cinema_filmhall_address"
        android:textColor="#000000"
        android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/pointer"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"

        android:src="@drawable/pointer" />

    <TextView
        android:id="@+id/filmhall_telephone1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_tp1"
        android:textColor="#000000"
         android:layout_marginTop="2dp"
         android:layout_marginLeft="32dp"
          android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/telephone"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"
        android:src="@drawable/call" />

    <TextView
        android:id="@+id/filmhall_telephone2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_tp2"
        android:textColor="#000000"
         android:layout_marginTop="2dp"
         android:layout_marginLeft="32dp"
          android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/fax"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"
        android:src="@drawable/fax" />

    <TextView
        android:id="@+id/filmhall_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_email"
        android:textColor="#000000"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="32dp"
         android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/message"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"
        android:src="@drawable/message" />

    <TextView
        android:id="@+id/filmhall_facility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="10dp"
        android:text="@string/cinema_filmhall_facility"
        android:textColor="#000000"
        android:textStyle="bold" />

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


        <ImageView
            android:id="@+id/fac1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="4dp"
            android:src="@drawable/fac1" />

        <ImageView
            android:id="@+id/fac3"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="4dp"
            android:src="@drawable/fac3" />

        <ImageView
            android:id="@+id/fac2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="4dp"
            android:src="@drawable/fac2" />
    </LinearLayout>

</LinearLayout>

Upvotes: 2

Views: 17419

Answers (3)

sai anudeep
sai anudeep

Reputation: 1265

You can simply use android:layout_centerVertical="true" for all the views in Relative Layout to make them in a single vertical line.

 <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="0.5"
     android:layout_centerVertical="true"
     />

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="10dp"
     android:layout_weight="0.5"
     android:text="Test2"
     android:layout_centerVertical="true" />

Upvotes: 0

Ranjith Subramaniam
Ranjith Subramaniam

Reputation: 119

You can use TableRow as well. Here you don't need to specify the android:orientation="horizontal".

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">
<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/TextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/> 

    <ImageView
        android:layout_width="33dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginRight="15dp"
        android:src="@drawable/nextarrow" />
</TableRow>
</LinearLayout>

Upvotes: 0

Shobhit Puri
Shobhit Puri

Reputation: 26027

Method 1:

Like others have suggested, you can have a nested LinearLayoutwith horizontal orientation. It will contain both TextView and ImageView with weights set according to your need.

See How to set ImageView and the textView in a single line inside a LinearLayout

Eg:

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" >

     <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="0.5"
         ...
         />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
         android:layout_weight="0.5"
         android:text="Test2" />
</LinearLayout>

Method 2 [ Easier ]:

Since you mentioned that you've several logos and want to set the drawable/ImageView right next to it, there is an easier alternate way. Supposing you want something like this: enter image description here

You can use android:drawableLeft or similar in the TextView. You can also set the padding between the drawable and the text of the TextView using android:drawablePadding. Using this way you don't need the extra ImageView. But it depends at the end what you are looking for.

See Programmatically set left drawable in a TextView

Hope this helps.

Upvotes: 7

Related Questions