1

I want to have Buttons next to each other on the same line, and I want to have one Button next to an EditText.

I have the Eclipse addon, so I have both the graphical layout, as well as the text. I can't post an image, but I'll describe it: it has two TextViews (irrelevant), followed by an EditText, and then a small unlabeled Button on the next line. The next four lines are buttons labeled Turn N, Turn E, Turn S, and Turn W.

So, next to that EditText is where I want the untitled Button to be, and I want the N, E, S, W Buttons to be setup in a compass-like fashion. However, when I try to drag them on the GUI, it binds them to the row, and whenever I try to change the layout, it affects everything, rather than only the one object I'm right-clicking.

How do I fix this?

2 Answers 2

1

Something like this:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/textView1"
        android:text="TextView" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/textView2"
        android:layout_toLeftOf="@+id/button1" >
    </EditText>

    <Button
        android:id="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/textView2"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText1"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button2"
        android:layout_toLeftOf="@id/button2"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button2"
        android:layout_toRightOf="@id/button2"
        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button4"
        android:layout_toLeftOf="@id/button4"
        android:text="Button" />

</RelativeLayout>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, this was very helpful, and now my graphical layout view of the XML is displaying it just as I want it to. (I added a few more buttons as well), however, now in my Java file, there are errors on everything that references main. Under "setContentView(R.layout.main);" it says error: "main cannot be resolved or is not a field." My xml file does not display any errors, and I've tried restarting eclipse, I can't find anything wrong with it. Any suggestions? Thanks!
@Zeldarulah Try to clean your project(In menu Project-> Clean) and also check if you imported the correct R class(with CTRL + SHIFT + O) from your package(and NOT import android.R;).
I do have "import android.R;", but I tried both Project -> Clean and CTRL + Shift + O, and it did not change it. Should "import android.R;" be something different? Thanks again.
@Zeldarulah That line import android.R; hides your project generated R class and android doesn't find the layout, ids. Delete that line and you should be Ok. (if you use some part of the android.R namespace then don't import the import android.R; and simply use android.R.something where you need)
Well, that fixed it! Thanks a ton. I'm not quite sure why that import was causing a problem, but thanks a ton. If I could upvote your answer, I would, but my rep is still too low.
|
0

I can't really say about it without xml, but if you want to position buttons in compas-like fashion, you should try to create quadrate relative layout, put buttons to it, and then position buttons at top, left, right and bottom of the parent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.