-3

My goal is to have a TextView at the right of the Title, Stars and Thoughts. I normally know how to do it with orientation="horizontal" but actually this is quite tricky because I can't see what I am doing wrong.

Here's my code:

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:background="@color/white">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_height="match_parent">
                    <TextView
                        android:id="@+id/offertitle"
                        android:paddingTop="15dp"
                        android:paddingLeft="15dp"
                        android:paddingRight="15dp"
                        android:textSize="24dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/blue" />


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/troisieme"
                        android:orientation="horizontal">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="15dp"
                            android:id="@+id/offerstars"
                            android:src="@drawable/stars" />
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:gravity="center_vertical"
                            android:id="@+id/offerthoughts"
                            android:text="53 avis"
                            android:textColor="@color/blue" />
                    </LinearLayout>
                </LinearLayout>


                <TextView
                    android:layout_height="match_parent"
                    android:layout_width="wrap_content"
                    android:id="@+id/offer_price"
                    android:textColor="#000000"
                    android:gravity="right"
                    android:text="blablabla"
                    android:background="#D3D3D3"/>

Here is an image:

Doodle

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Can you show us an example of what is your desired output? – Orkun Kocyigit Aug 21 '17 at 12:48
  • 1
    can you attach an image from what you want? – Behnam Eskandari Aug 21 '17 at 12:49
  • Yes, thanks for your quick answers ! – Hippocampe Aug 21 '17 at 12:51
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 21 '17 at 12:56
  • I'm really sorry, i'll delete my question but keep in mind that this was my first question, i never asked or answered before ! I'm just a beginner that got stuck for days and days on the same error, sorry for bad langage and all things else. – Hippocampe Aug 21 '17 at 15:10
  • Well I can't delete this question but I "swear" a lot and I'm sorry if I offended anyone. But besides this, what else is wrong with my question ? – Hippocampe Aug 21 '17 at 15:23
  • You don't have to delete the question, just bear it in mind for the future. Please also don't roll back edits here - even after the swearing is removed, it was far too chatty. If you wish to insist on your version, please ping me with `@halfer` and I'll ask a moderator for their opinion. – halfer Aug 21 '17 at 16:19
  • Okay got it ! But the thing is that i'm 17 y.o and french so it's hard for me to " not be "chatty" ", this isn't on purpose i just try to explain myself ^^ But thanks for your answers, I'll try to do my best for the future, have good night (or day) ! See you soon – Hippocampe Aug 21 '17 at 20:21

3 Answers3

1

The Layout which contains title and etc. and is side by side with your TextView has width set as match_parent, it's wrong. there is no space for TextView then, below you have your design corrected. It has set width as 0dp and weight as 1 - so it fills available space.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorWhite"
    android:orientation="horizontal">


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/offertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="15dp"
            android:textColor="@color/colorRed"
            android:textSize="24dp"/>


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

            <ImageView
                android:id="@+id/offerstars"
                android:layout_width="wrap_content"
                android:layout_height="15dp"
                android:src="@drawable/ic_menu_start"/>

            <TextView
                android:id="@+id/offerthoughts"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:text="53 avis"
                android:textColor="@color/colorOrange"/>
        </LinearLayout>
    </LinearLayout>


    <TextView
        android:id="@+id/offer_price"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#D3D3D3"
        android:gravity="right"
        android:text="blablabla"
        android:textColor="#000000"/>
</LinearLayout>
b2mob
  • 3,419
  • 2
  • 14
  • 22
0

If you want to achieve some part to left and some part to right you can go with RelativeLayout

If you still want to do in Linearlayout than you should use Weight inside it.

You can do like this

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">


    <LinearLayout
        android:id="@+id/lin_left"
        android:layout_toLeftOf="@+id/offer_price"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/offertitle"
            android:paddingTop="15dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:textSize="24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/troisieme"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="15dp"
                android:id="@+id/offerstars"
                android:src="@mipmap/ic_launcher" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:id="@+id/offerthoughts"
                android:text="53 avis"
                android:textColor="@android:color/black" />
        </LinearLayout>
    </LinearLayout>


    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:id="@+id/offer_price"
        android:textColor="#000000"
        android:gravity="right"
        android:text="blablabla"
        android:background="#D3D3D3"/>

    </RelativeLayout>
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
0

Try this. You can use layout_weight and weightSum to manage portions in of layout. and set width to 0dp so weight can affect.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3"
    android:background="#FFFFF0"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical">

        <TextView
            android:id="@+id/offertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="15dp"
            android:text="Hello"
            android:textColor="@color/colorPrimary"
            android:textSize="24dp" />


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

            <ImageView
                android:id="@+id/offerstars"
                android:layout_width="wrap_content"
                android:layout_height="15dp"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/offerthoughts"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:text="53 avis"
                android:textColor="@color/colorAccent" />
        </LinearLayout>
    </LinearLayout>


    <TextView
        android:id="@+id/offer_price"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#D3D3D3"
        android:gravity="center"
        android:text="blablabla"
        android:textColor="#000000" />
</LinearLayout>

Upendra Shah
  • 2,218
  • 17
  • 27