Can someone explain the correct way to use android:layout_toRightOf?

by Jonathan Nalewajek » Thu, 31 Dec 2009 04:56:07 GMT


Sponsored Links
  little background info: I am a C++ developer who has worked very
little with Java. Picking up Java wasn't very difficult since I know C+
+, but I am still playing around with the SDK. (In C++, my favorite
SDK/API is Qt)

Anyway, I am trying to stick to the Model-View-Controller design
philosophy, and was setting up the UI in XML. However, it is not
working out the way I wanted it too. I am trying to write a simple app
(to help get started with learning the SDK), where there are 4
buttons, all of which cause a different operation on a value in a
textview. For example, the textview starts out at 100, and I have 4
buttons: +1, -1, +5, -5. When you press +1, it adds 1 to the value in
the textview. When you press -5, it subtracts 5 from the value in the
textview, etc etc...

That part is working fine. However, I am trying to setup the UI as
follows:

[TextView]
[+1] [-1]
[+5] [-5]

Basically, I want all the + in one column, and all the - in another
column. I figured I would use a relativeLayout, however, it is not
working the way I want it to. I am getting the following
[TextView] [-5]
[+1]
[+5]
(the -1 button is totally missing)

I have tried to narrow down possible errors in my code, and I have
come to the conclusion that either I am using android:layout_toRightOf
wrong, or I misunderstand how this command is implemented.

Here is my main.xml file:
(note: bu1p = button for +1, bu1m = button for -1 [p=positive
m=minus]. I also have a custom button in there, so there are really 6
buttons in total)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/nums"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize = "20sp"
android:text = "100"
/>
<Button
android:id = "@+id/bu1p"
android:layout_width="70sp"
android:layout_height="wrap_content"
android:text = "+1"
android:layout_below="@id/nums"
/>
<Button
android:id = "@+id/bu1m"
android:layout_width="70sp"
android:layout_height="wrap_content"
android:text = "+1"
android:layout_toRightOf="@id/bu1p"
/>

<Button
android:id = "@+id/bu5p"
android:layout_width="70sp"
android:layout_height="wrap_content"
android:text = "+5"
android:layout_below="@id/bu1p"
/>
<Button
android:id = "@+id/bu5m"
android:layout_width="70sp"
android:layout_height="wrap_content"
android:text = "-5"
android:layout_toRightOf="@id/bucm"
/>
<Button
android:id = "@+id/bucp"
android:layout_width="70sp"
android:layout_height="wrap_conte



Can someone explain the correct way to use android:layout_toRightOf?

by Chander Pechetty » Thu, 31 Dec 2009 17:07:17 GMT


 ere's a partial layout for your case. Use the layout_below along with
RightOf and the text for the minus button was "+".
If you don't use that, then all of them are to the right of the
specified layout id , but not below the same parent as the id's
parent.

<Button
android:id = "@+id/bu1m"
android:layout_width="70sp"
android:layout_height="wrap_content"
android:text = "-1"
android:layout_below="@id/nums"
android:layout_toRightOf="@id/bu1p"
/>

You can also use a LinearLayout to represent you horizontal elements,
and specify the layout_below at this level.(ie. Plus and Minus button
in horizontal linear layout which is below the TextView)
Tip: Move the common attributes of a View to a style.xml file so that
you can change all of them in one go.
style="@style/button_style" where button_style is defined in
style.xml






On Dec 30, 8:02am, Jonathan Nalewajek <jonnalewa...@gmail.com> wrote:

--


Sponsored Links


Can someone explain the correct way to use android:layout_toRightOf?

by jotobjects » Fri, 01 Jan 2010 01:41:49 GMT


 



You cannot have a forward reference in the layout.    Here bucm is
later in the layout so that forward reference is not allowed.

--



Can someone explain the correct way to use android:layout_toRightOf?

by Jonathan Nalewajek » Fri, 01 Jan 2010 04:46:19 GMT


 hank you, this worked!

I was thinking toRightOf literally meant, "put the object to the right
of the object specified". I was thinking it carried over the y-
coordinate, and just added some value to the x-coordinate. However,
you need to specify x and y coordinate relations.

On Dec 31, 4:07am, Chander Pechetty <cspeche...@gmail.com> wrote:

--



Other Threads

1. Anything like isWifiEnabled and setWifiEnabled for 3g an 2g/gprs?

Hi all,

I see 
http://developer.android.com/reference/android/net/wifi/WifiManager.html#isWifiEnabled()
that you can see if the wifi is enabled or disabled and also us
http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
to turn on and off the wifi.

I'm wondering if it's possible to do the same for 3G and for 2G/GPRS?
I know it's possible because you can turn off 3G and left 2G on.

The reason I want to do this is to write a simple application that
will let you easily turn on and off the wifi/3g/2g. I'm in Ireland and
don't have a unlimited data plan. I'd also like to turn off wifi/3g/2g
easily at certain time to prolong battery life.

--~--~---------~--~----~------------~-------~--~----~

2. Can not install the Developer Tools

I have tried to install the android Developer Tools for a good 1 hour
now, I have deleted and reinstalled eclipse 3 times. Same goes with
the JRE/JDK.

And nothing works, here the error I am getting. I have no clue what
the problem is. Can anyone help me out here?

An error occurred during provisioning.
  Failed to prepare partial IU: [R]com.android.ide.eclipse.ddms
0.8.0.v200809220836-110569.


^ I get that error when I do the Help > Software Updates, Available
Updates...

--~--~---------~--~----~------------~-------~--~----~

3. Can't install Eclipses plugin on a Mac.

4. How do you pass a reference to non-serializable class to a new Activity?

5. GetJar networking event in San Mateo, CA. April 22nd

6. PackageManager.installPackage(Uri packageURI) error on Androdi 1.5 SDK

7. Distinguishing Between Edge and 3G network