0

I am trying to position a button in a different place using absolute layout. I am using the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:background="@drawable/bcgrd">
  <Button
    android:id="@+id/start_challenge"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text = "Start Challenge"
    android:textColor= "@color/light_gray"
    android:background="@color/background">
    </Button>  
</AbsoluteLayout>

the java file contains the following code:

    Button start_it = (Button)findViewById(R.id.start_challenge);
    start_it.layout(200, 200, 200, 200);

but nothing happen (the '200's are just for the example. can anyone please tell me what I am doing wrong.

Thanks in advance.

hultqvist
  • 17,451
  • 15
  • 64
  • 101
amigal
  • 517
  • 3
  • 8
  • 21

1 Answers1

0

What you're doing wrong is using AbsoluteLayout. :) Seriously, you really shouldn't use it; try using a FrameLayout instead.

However, to answer your question, you shouldn't call layout on it, do this instead:

AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)start_it.getLayoutParams();
params.x = 200;
params.y = 200;
start_it.setLayoutParams(params);

Or with less code, you could do this instead:

start_it.setLayoutParams(new AbsoluteLayout.LayoutParams(
    start_it.getWidth(), start_it.getHeight(), 200, 200));
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • can you please elaborate why should not we use .layout function @kcoppock – Muhammad Babar Apr 11 '13 at 04:29
  • @MuhammadBabar AbsoluteLayout is deprecated and doesn't support multiple screen sizes well. – Kevin Coppock Apr 11 '13 at 06:08
  • Well i have asked some thing different,but now i have solved the problem,the problem was that i was using `layout(int left, int top, int right, int bottom)` function which we should **NEVER** use because the framework can call this function whenever the view is invalidated(refresh) and will reposition it,We should use **LayoutParams** for any change in postion/size of the view – Muhammad Babar Apr 11 '13 at 06:35
  • Yeah, you should only use `layout()` from within `onLayout()` (if you're writing a custom `ViewGroup`). – Kevin Coppock Apr 11 '13 at 16:47