Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Laying out controls on Android

Options
  • 06-04-2014 8:03am
    #1
    Closed Accounts Posts: 295 ✭✭


    I'm trying to write my first Android app (I've written a few Windows Phone app's in Visual Studio) and I'm running into what seems like a very basic, but very frustrating problem.

    I am using Xamarin Studio as I am trying to design the UI but I can't seem to place controls wherever I want on the UI. For example, I want to add 4 image buttons to my UI but they don't seem to drop/appear on the UI. I tried adding a relative layout which fills the entire UI but that will only let me render one image button and it snaps it to the top left.

    I tried adding linear layouts but I'm not really sure if that's the right thing to do. I was hoping to layout 4 controls in a kind of square shape, two at the top, two at the bottom.

    Can I just drop controls and freely move them around like I would in Visual Studio?

    BTW I am using Xamarin Studio just so I can perhaps re-use some of my C# code for my Windows Phone apps when I try to create their Android equivalent. But I am not completely opposed to just working in Java with Android Studio or IntelliJ, especially if it will be easier to learn.

    Thanks.


Comments

  • Registered Users Posts: 403 ✭✭counterpointaud


    I can't speak to your particular situation, but the different layouts in Android took me a while to get my head around. I, like you, came to Android from .Net and after messing with Xamarin for while decided that it would be much better to develop at least one app in Java before moving to Xamarin.

    From memory I think there is either no absolute positioning or it is discouraged. I think you should be able to achieve what you want fairly easily with relative layout.

    http://developer.android.com/guide/topics/ui/declaring-layout.html

    *EDIT* Maybe something like this (if I am understanding you correctly, if not, maybe post a sketch)
    <?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="match_parent" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Button" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="Button" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="Button" />
    
    </RelativeLayout>
    


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    The reason that you can't drag and drop form elements in Android, the same way as you might in Visual studio, is the same reason you can't really do so for HTML. You have differing resolutions, UI's, screen sizes and then there are OS versions too do deal with.

    Sticking to layouts (let's not get down the discussion about Fragments), I've found that the best approach is to use relative layouts and only use other layout types sparingly (typically embedded in a parent relative layout). Absolute layouts are a bad idea, because ultimately of all those screen sizes and resolutions (and even OS versions) and I've only used them when doing layouts where the app was only ever going to run on one spec.

    As for GUI's, I've come across a few, but the one that comes with Eclipse and the SDK is probably still the best. As such, you may want to install this, even if you use Xamarin normally, just for doing layouts.


  • Registered Users Posts: 44 SurplusToys


    Android studio is a buggy but it has a nice device preview window that should help you with your layouts


Advertisement