0

I am trying to make a new layout page where I want to put two buttons, and on the above of each button I need to give a frame animation. so on loading the buttons are looking like inside the bubbles. Following is the code I am using to achieve this:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_full">
    <Button android:id="@+id/btnMusic"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:gravity="center"
            android:layout_marginLeft="215dp"
            android:layout_marginTop="140dp"
            android:background="@drawable/icon"/>
    <ImageView android:id="@+id/imgMusic"
        android:layout_width="150dp"
            android:layout_height="150dp"
            android:gravity="center"
            android:layout_marginLeft="170dp"
            android:layout_marginTop="100dp"
            android:background="@drawable/button_background"/>      
    <Button android:id="@+id/btnMovies"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:gravity="center"
            android:layout_marginLeft="405dp"
            android:layout_marginTop="140dp"
            android:background="@drawable/icon1"/>
    <ImageView android:id="@+id/imgMovies"
        android:layout_width="150dp"
            android:layout_height="150dp"
            android:gravity="center"
            android:layout_marginLeft="360dp"
            android:layout_marginTop="100dp"
            android:background="@drawable/button_background"/>
    </RelativeLayout>

My jav code is like this:

    public class BubbleActivity extends Activity {
    /** Called when the activity is first created. */
    /** Called when the activity is first created. */
    Button btnMusic, btnMovies ;
    ImageView imgMusic,imgMovies;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
}
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
      super.onWindowFocusChanged(hasFocus);
      btnMusic = (Button)findViewById(R.id.btnMusic);
      btnMovies = (Button)findViewById(R.id.btnMovies);
      btnMusic.setOnClickListener(new View.OnClickListener() 
            {
               public void onClick(View v) {
                  Intent intent = new Intent(PixieActivity.this,Splash.class);
                  startActivity(intent);
               }
            });
      ImageView imgMusic = (ImageView)findViewById(R.id.imgMusic);                                        
      imgMusic.setBackgroundResource(R.drawable.frame_animation);
      AnimationDrawable frameAnimation =(AnimationDrawable) imgMusic.getBackground();  
      if (frameAnimation.isRunning()) {
             frameAnimation.stop();
             }
          else {
         frameAnimation.start();
          }
     ImageView imgMovies = (ImageView)findViewById(R.id.imgMovies);                                        
      imgMovies.setBackgroundResource(R.drawable.frame_animation);
      AnimationDrawable frameAnimation1 =(AnimationDrawable) imgMovies.getBackground();  
      if (frameAnimation1.isRunning()) {
             frameAnimation1.stop();
             }
          else {
             frameAnimation1.start();
          }
    }}

But due to the margins the button layout became distracted in different phone resolutions. Is there any other way to achieve the same layout with device resolution independant. Also I want to add the bubble animation to each of the icons i will make in next pages. Please help.

jyotiprakash
  • 2,086
  • 1
  • 20
  • 26

1 Answers1

0

I would suggest not hard-coding the margins, and instead wrap the Buttons and ImageViews in a LinearLayout each, then set the spacing using layout_weight so it is perfectly scalable.

The actual layout choice depends on if you need the button to be exactly 80x80 and the ImageView to be exactly 150x150.

For instance (pseudo-code: obviously many parameters are left out) :

<RelativeLayout>
   <LinearLayout id="buttons" >
      <LinearLayout layout_width = "0dp" layout_weight = "1"> <!-- 0 width is important! -->
          <Button layout_gravity="center" />
      </LinearLayout>
      <LinearLayout layout_width = "0dp" layout_weight = "1">
          <Button layout_gravity="center" />
      </LinearLayout>
   </LinearLayout>

   <LinearLayout id="images" align with @buttons>
      <LinearLayout layout_width = "0dp" layout_weight = "1">
          <ImageView layout_gravity="center" />
      </LinearLayout>
      <LinearLayout layout_width = "0dp" layout_weight = "1">
          <ImageView layout_gravity="center" />
      </LinearLayout>
   </LinearLayout>
</RelativeLayout>

And just make sure set the LinearLayouts "buttons" and "images" to the same height and full width so that the buttons and images overlap. More about layout_weight: What does android:layout_weight mean?

If you do not need the Buttons and ImageViews to be an exact size, think about sizing them by weight as well. Then no matter what screen you are on, if you tell a button to take up 1/4 of it through layout_weight, it will never be distorted

Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118