1

I have a background in iPhone development, which may be a cause of some of my confusion with Android, which I am very new at developing.

My question is this: How to I create two TextViews, and specify their exact location on screen? For example, on the iPhone, I would create a UILabel, generate a rectangular frame that specified the label's size and position, and then set this rectangle to the frame property of the UILabel.

If you can help me understand the similarities with Objective C and iOS' UILabel, that would be most helpful.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Kiran Kulkarni
  • 1,434
  • 2
  • 18
  • 40
  • Is the gist of your question how to position the 2 dynamically created text views with absolute positions on the screen? Can you share your use case on why you decide not to use LinearLayout etc? – yjw Aug 12 '11 at 16:41
  • :) :) Well, I am an Illiterate in Android Dev., My objective is just to set two text labels on the screen – Kiran Kulkarni Aug 12 '11 at 16:43
  • 2
    I may be thinking too much but it sounds like there's more to this question that I can see. Have you gone through the Linear Layout tutorial http://developer.android.com/resources/tutorials/views/hello-linearlayout.html? Is there something beyond this tutorial you need? – yjw Aug 12 '11 at 16:48
  • @yjw, thanks That would do, One quick question..! Can we specify the position of the text in the code.., for e.g: exampleText.setPosition(0,100) or something like that.? – Kiran Kulkarni Aug 12 '11 at 16:52
  • 1
    Or may be I need to search for a book titled "Android development for iPhone developers " :-) – Kiran Kulkarni Aug 12 '11 at 16:54
  • 3
    Just to make sure we are on the right track, Android phones does not have standard screen size like iphones. Absolute positioning does not seem to be the proper way to go (AbsoluteLayout is deprecated). You could try RelativeLayout to achieve some sort of relativity between the 2 views. Beyond that, you probably have to play around with the xml attributes layout_gravity, gravity, margic, etc – yjw Aug 12 '11 at 16:54
  • Thanks a lot for providing me ..basic things that I need to know to start working on Android development.. Thanks a lot!! – Kiran Kulkarni Aug 12 '11 at 16:57

2 Answers2

2

On Android, we don't use absolute screen positions. This is highly discouraged. It's pretty understandable that you think this way if you are coming from iOS. But you need to revise your habits.

Instead of absolute positions, we use layouts, such as LinearLayout, RelativeLayout or FrameLayout. All of these allow you to arrange your views dynamically. And in many cases, it will automagically adapt to the screen size, which vary a lot from device to device.

Actually, there's nothing exotic about dynamic layouts. Many major UI toolkits, such as GTK, or Qt, work similarly. Pixel position are a bad idea in my opinion, except maybe in the Apple world, where the OS and the hardware are tightly coupled, but this is an exception actually.

So, in your case, all that you need is to put your text views into the appropriate layout. Please read the documentation and tutorials about the different types of layouts mentioned above to decide which one is best. The question is how you want your views to be placed relatively to each other.

olivierg
  • 10,200
  • 4
  • 30
  • 33
  • iPhone is having a single form factor "till now". In Android the device screen size is not fixed and the platform is suppose to scale the graphics. Applications though will look pretty good, if done properly. – Pradeep Sharma Aug 14 '11 at 04:04
0

Create a basic Android project in eclipse. You will be having a main.xml layout file in your project. You can open it in Eclipse using Ctrl+Shift+r and keying in main.xml

copy paste this in your xml after clearing its content.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:text="TextView One"
        android:layout_height="fill_parent"
        android:layout_weight="1"></TextView>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:text="TextView Two"
        android:layout_height="fill_parent"
        android:layout_weight="1"></TextView>
</LinearLayout>
  • Thanks.for the codesnippet., @yjw helped me to gain some quick insight on how to start up with. – Kiran Kulkarni Aug 12 '11 at 17:14
  • But.., Do we need to do all these in the xml file itself.. ? That would be tedious I guess.., Which could be of just two lines of code in an implementation file – Kiran Kulkarni Aug 12 '11 at 17:16
  • 2
    The xml file has got two tabs at the bottom. Instead I would suggest that over the weekend you watch the series of screencasts on [Marakana.com - Android bootcamp](http://marakana.com/techtv/android_bootcamp_screencast_series.html). It will give you a head start in 2 days. – Pradeep Sharma Aug 12 '11 at 17:22
  • Thanks buddy for yo response ! – Kiran Kulkarni Aug 12 '11 at 17:28