Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Android and Linear Layout question

I am hoping someone can point me in the right direction for my question. I'm making an app that's sort of based on the Crystal Ball app for Android and I'm having problems with making sure my text/answers are anchored in a certain location. I was going by the videos using a Linear Layout and using views to center the text but now I want my text to be lower in my layout. I have tried using views to push the text down more into the desired area but to no avail. Is there a mathematical equation or something easier that I'm missing?

Thanks in advance for any help!

4 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Layouts can be tricky. I plan on creating some content that covers different layouts in detail, but it won't be out for a while. You might want to switch to a RelativeLayout, or maybe use some layout padding or margins to move things around.

If you can post some sort of picture of what you are trying to achieve then maybe we can give you a little more concrete advice, too.

Thanks for the advice Ben! I've enjoyed all your courses and I feel like I should know this but feel lacking at the moment.

I have an image but I have no idea how to attach here.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

The best way is to upload it to a site like imgur.com and then link to it using Markdown. There's a Markdown Cheatsheet link below the text box when you enter a comment that you can refer to the for the format for an image. It looks like this: ![alt text](/path/to/img.jpg "Title")

http://i.imgur.com/cZR3phn.png

Hope I did this right! The only items on the layout are the button on the bottom and then the answer/text where indicated. Still playing with graphics so that's why it's a little bare. I think using padding and margins will probably be the way I want to go since I'm thinking relative might put things a little wonky.

Thanks again!

http://imgur.com/cZR3phn

Sorry other link didn't work correctly....operator error on my part.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Nanette,

Sorry for my delayed response! I'm a bit behind from the weekend but am catching up on the Forum and support now.

Adapting this kind of layout for multiple devices can be tough. If you are trying to line it up with a background image, like we do in the Crystal Ball app, then you will need to check it on different screens to see how it looks.

Since you are closer to the bottom, maybe you can use that as your anchor point for the image and text and let it stretch up top, if needed. Perhaps you could try something like the following, where the text is pulled to the bottom with android:gravity="bottom" and then bumped up with android:layout_marginBottom="100dp":

<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"
    android:gravity="top" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/ball01" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:layout_marginBottom="100dp"
            android:shadowColor="#ffffff"
            android:shadowRadius="10"
            android:text="Android layouts are fun!"
            android:textColor="#ffffff"
            android:textSize="32sp" />

</RelativeLayout>

Awesome!! Thanks for the info. I'm very interested on seeing how this works and how it would be viewed on different devices. Thanks again for all your help!

By the way, I haven't had a chance to work on it either during the weekend so no worries!