Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place textview using x,y position

I am new to android,Can anyone help me?.I need to show the textview values and button and imageview using x,y position.how its possible.My coding.

  <?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"
   android:orientation="vertical"
   android:background="@drawable/levelbg" >

<Button
    android:id="@+id/level1"
    android:layout_width="560dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp"
    android:background="@drawable/levelbox"
   />

<TextView
    android:id="@+id/levelno1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="43dp"
    android:background="@drawable/levelno" />

<TextView
    android:id="@+id/lastshoot1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/level1"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="18dp"
    android:layout_marginLeft="85dp"
    android:text="134"
    android:textSize="20sp" />

<TextView
    android:id="@+id/bestshoot1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/lastshoot1"
    android:layout_alignBottom="@+id/lastshoot1"
    android:layout_marginLeft="50dp"
    android:layout_toRightOf="@+id/lastshoot1"
    android:text="123"
    android:textSize="20sp" />

<ImageView
    android:id="@+id/levelonestar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/bestshoot1"
    android:layout_marginLeft="17dp"
    android:layout_marginBottom="10dp"
    android:layout_toRightOf="@+id/bestshoot1"
    android:src="@drawable/star1" />

<ImageView
    android:id="@+id/levelonestar2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/levelonestar1"
    android:layout_toRightOf="@+id/levelonestar1"
    android:src="@drawable/star1" />

<ImageView
    android:id="@+id/levelonestar3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/levelonestar2"
    android:layout_toRightOf="@+id/levelonestar2"
    android:layout_marginRight="2dp"
    android:src="@drawable/star1" />

I need this via programmatically up to 50 times it should be shown.(50 buttons).Can any one suggest me the alternative way,Thanks for advance.

like image 682
user123 Avatar asked Jan 07 '13 11:01

user123


People also ask

How do I find X and Y coordinates on android?

getX() + ":" + (int)button1. getY(), Toast.

How do I move TextView Center?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

Which method is used to set the text in a TextView?

SetText(String, TextView+BufferType) Sets the text to be displayed using a string resource identifier.


1 Answers

Android >= Honeycomb (self-explaining)

view.setX(x);
view.setY(y);

Android < Honeycomb Instead of directly setting it to the position you will tell "set it x pixels away from the left/top/right/bottom border".

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.setMargins(x, y, 0, 0);

view.setLayoutParams(lp);
like image 156
Simon Schubert Avatar answered Sep 19 '22 09:09

Simon Schubert