-1

I literally just started android development and cant seem to get my text views aligned wherever I want on the screen

this is my code

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="top text"
        android:textColor="white"
        android:layout_gravity="top|start"
        android:textSize="40sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bottom text"
        android:textSize="30sp"
        android:textColor="white"
        android:layout_gravity="bottom|end"/>

</LinearLayout>

The "output" that I am getting on the design tab is

top
bottom

I tried dragging and dropping the text views using Design and it added tools:layout_editor_absoluteX="138dp" tools:layout_editor_absoluteY="344dp" but this cant be the only way, can someone tell me what I am doing wrong.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sahil
  • 1,387
  • 14
  • 41
  • 1
    That's just how `LinearLayout` works. If you want those `View`s in diagonally-opposite corners, use a different `ViewGroup`, like `RelativeLayout`, or `ConstraintLayout`. – Mike M. Jun 10 '17 at 01:14
  • I was following a tutorial that used linear layout but sure let me try Relative layout @MikeM. – Sahil Jun 10 '17 at 01:15
  • nothing happened its still the same way @MikeM. – Sahil Jun 10 '17 at 01:17
  • @MikeM you can use LinearLayout for opposite corners. Just the gravity of the text needs changed – OneCricketeer Jun 10 '17 at 01:18
  • `RelativeLayout` will likely require different attributes on the `TextView`s, like `android:layout_alignParentTop="true"`, etc. – Mike M. Jun 10 '17 at 01:18
  • @Sahil See https://stackoverflow.com/questions/3482742/gravity-and-layout-gravity-on-android – OneCricketeer Jun 10 '17 at 01:20
  • Unless you add a picture of what you want, I think that post answers the question – OneCricketeer Jun 10 '17 at 01:24
  • @cricket_007 Uh, not really. You could get a similar effect, but the `View`s wouldn't really be in opposite corners, at least as far as I imagine that to mean. At least one of them would be stretched to fill the remainder of the parent. – Mike M. Jun 10 '17 at 01:25
  • @cricket_007 I just noticed, though, that their `LinearLayout` is wrapping the height, so what they have now should work, if that's actually what they want; two consecutive lines of text with opposite alignments. I made the mistake of assuming that was their whole `Activity` layout, and they want text in the far corners. Of course, your suggestion is appropriate in that case, too, I'd say. They'd just need to change the second `TextView` to `match_parent` for the width. Dunno what they have in mind. – Mike M. Jun 10 '17 at 01:51
  • @MikeM one is top start, the other is bottom end... I agree it's unclear, which is why I asked for a picture / drawing – OneCricketeer Jun 10 '17 at 02:17
  • 1
    @cricket_007 Yeah, given my assumption, I thought they were trying to say that the bottom gravity wasn't working, which it wouldn't in a vertical `LinearLayout`. Anyhoo, I'd say we've spent far too much time on this basic layout question as it is. :-) Cheers! – Mike M. Jun 10 '17 at 02:27
  • Use `RelativeLayout` and where you want them to align? – Manish Dhruw Jun 10 '17 at 06:08
  • I used the relative layout later and found the alignment tags later in the documentation – Sahil Jun 10 '17 at 20:04

1 Answers1

0

okay so I was trying to align texts on the top left and the bottom right corner of the screen and I switched to Relative layout and added an image as well,

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/party"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/hbd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Happy Brithday!!!"
        android:textSize="30sp"
        android:textColor="#ffffff"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        />

    <TextView
        android:id="@+id/sahil"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="From Sahil"
        android:textColor="#ffffff"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:textSize="30sp" />

</RelativeLayout>

the android:layout_alignParentposition="true" helped achieve that.

Sahil
  • 1,387
  • 14
  • 41