5

I'm writing a program for a mobile app development class that I am taking. The app involves me placing 2 buttons on the screen. I know that when I put /2 it places it in the middle, and when I put /3 it places the button a third of the way from the left of the screen. Since I need two buttons I would like to put one a third of the the way from the left(which I know how to do), and a third from the right(which I don't know how to do). What should I put to accomplish that?

My code snippet:

myRedButton.x = display.contentWidth /3 
myRedButton.y = display.contentHeight -50 
myGreenButton.x = display.contentWidth /2 
myGreenButton.y = display.contentHeight -100

I'm new to the mobile app programming scene, so keep it simple. Thanks!

greatwolf
  • 20,287
  • 13
  • 71
  • 105

3 Answers3

2

Try this:

local myGreenButton = display.newRect(0,0,50,50)
myGreenButton.x = display.contentWidth- (display.contentWidth/3)
myGreenButton.y = 100

OR simply,

local myGreenButton = display.newRect(0,0,50,50)
myGreenButton.x = (2/3)*display.contentWidth
myGreenButton.y = 200

Keep Coding.............. :)

Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
  • 1
    @user1641187: If you find this answer as an useful one, then please mark the answer as accepted, otherwise the question will remain as an unanswered one. Please do remember this in future also. Thanks. Keep Coding..... :) – Krishna Raj Salim Jan 23 '14 at 19:05
0

I have used weight for dividing screen into 3 parts and than placed button in it, check if it works.

<LinearLayout
    android:id="@+id/lnrLeftContent"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lnrCenterContent"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lnrRightContent"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Hardik4560
  • 3,202
  • 1
  • 20
  • 31
  • Don't use three `LinearLayout` for accomplish this. It can be done by one `LinearLayout`. This will make view more recursive and you should avoid it. –  Jan 23 '14 at 06:17
  • That was an suggestion, its upto you what the requirement is. – Hardik4560 Jan 23 '14 at 06:42
  • i was just suggested you not to make your `Layout` nested please have a look on this thread. [Stackoverflow: Caused by nested views?](http://stackoverflow.com/questions/9946368/stackoverflow-caused-by-nested-views/9950339#comment22630565_9950339) –  Jan 23 '14 at 07:09
  • Ya sure, I understand that, android says it. I used it because I require my layout to be like that, But Did the answer help ? – Hardik4560 Jan 23 '14 at 07:44
  • i didn't ask the question ;-) so don't know what op is tried i have just answered this question and seen your answer so i have just drop a comment by seeing your code. –  Jan 23 '14 at 08:35
0

here i have put three TextView you can change it to button as you like . This will put three views in a row which takes same space on screen.

<LinearLayout android:id="@+id/total_row_holder"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:orientation="horizontal" >

    <TextView android:id="@+id/total_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="some other"
        android:padding="2dp"
        android:layout_gravity="center"
        android:background="#e8f4f9"
         />

    <TextView android:id="@+id/total_value_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="1dp"
        android:text="you know"
        android:padding="2dp"

        android:background="@android:color/white" />

    <TextView android:id="@+id/net_value_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:layout_weight="1"
        android:text="some value"
        android:padding="2dp"

        android:background="@android:color/white" />


</LinearLayout>