adrian
adrian

Reputation: 4594

Loading animations with buttons in android

I have an activity in android which I set up a button under a SurfaceView. And the result looks like this:

And I'm satisfied with it except one thing, the writing on the button is astray and must be setup correctly. For this I used animations like this:

takePhoto = (Button) findViewById(R.id.btnPhoto);
takePhoto.setText(t);

RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim);
ranim.setFillAfter(true);
takePhoto.setAnimation(ranim);

WHERE the res/anim/myanim looks like:

<?xml version="1.0" encoding="utf-8"?>
<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromDegrees="0" 
       android:toDegrees="-90"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="0" />

But the result is a little bit disappointing cause the whole looks like:

enter image description here

The text is ok, but the dimensions are reduced.

What I want is to keep the text correctly and the initial size.How could I do that??? It has nothing to do with the button's properties in the xml file cause I haven't changed them when loading the animation....Ideas?Thanks...

EDIT: xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>

<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="@+id/surface_camera" />

<RelativeLayout android:layout_width="fill_parent"
android:layout_toRightOf="@id/surface_camera"
android:layout_height="fill_parent"
 >

<Button android:layout_width="680dip"
android:layout_height="680dip" 
android:id="@+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo" 
android:layout_gravity="bottom|right" />

</RelativeLayout>

</RelativeLayout>





IMPORTANT:

In the cases described below...my activity is in `LANDSCAPE` mode and the position of the phone is portrait.Another issue is that when I turn my phone in landscape mode(and here I mean the position of the phone, cause the activity is still landscape) the button doesn't go at the bottom of the phone is stays there.No matter how I turn the phone the button is in the same position!!!!

Upvotes: 1

Views: 1012

Answers (2)

Vaibhav Jani
Vaibhav Jani

Reputation: 12548

You should read this

Make two layout folders like follows ...

enter image description here

In both of folders your layout file name should be same.

In layout-port folder your layout file should look like this ...

android:layout_toRightOf="@id/surface_camera"

replaced by ...

android:layout_Below="@id/surface_camera"

as follows ...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>

<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="@+id/surface_camera" />

<RelativeLayout android:layout_width="fill_parent"
android:layout_toBelow="@id/surface_camera" 
android:layout_height="fill_parent"
 >

<Button android:layout_width="680dip"
android:layout_height="680dip" 
android:id="@+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo" 
android:layout_gravity="bottom|right" />

</RelativeLayout>

</RelativeLayout>

Even this will considered as bad practice but you can also handle this manually, try this ..

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //setContentView(R.layout.main);

      int orientation = getResources().getConfiguration().orientation;

      if(orientation == Configuration.ORIENTATION_PORTRAIT) {
         setContentView(R.layout.main1);

      }
      else {

         setContentView(R.layout.main2);
      }
   }

Also If you are doing this way No need to make two layout folders, but needs two layout files main1 and main2 in same folder

Yet another option ...

In manifest ...

<activity android:name=".ActivityName" android:configChanges="keyboardHidden|orientation">

+

In your Activity ...

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);

      int orientation = newConfig.orientation;
      if(orientation == Configuration.ORIENTATION_PORTRAIT) {

        setContentView(R.layout.main2);
      }
      else {

        setContentView(R.layout.main);
      }
    }

Upvotes: 2

Graeme
Graeme

Reputation: 25864

Try this XML instead:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:orientation="horizontal"
                    android:layout_height="fill_parent">

    <SurfaceView    android:id="@+id/surface_camera" 
                    android:layout_width="450dip"
                    android:layout_height="fill_parent" />

    <FrameLayout    android:layout_width="fill_parent"
                    android:layout_toRightOf="@id/surface_camera"
                    android:layout_height="fill_parent">
        <Button     android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        <TextView   android:id="@+id/btnPhoto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" 
                    android:text="Take Photo" />
    </FrameLayout>

    </RelativeLayout>

You will rotate the Text not the button.

Upvotes: 0

Related Questions