0

I have created a layout using RelativeLayout, but I need to change the top margin when orientation changed to landscape .

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

   // Checks the orientation of the screen
   if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

       RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
      // labelLayoutParams.setMargins(0, -100, 0, 0);
       layout.setLayoutParams(labelLayoutParams);

       Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
   } 

but i get following exception

 06-25 22:22:31.166: E/AndroidRuntime(8211): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams

so please help how can set layout parameters specially top margin for relativelayout? programaticaly

kameny
  • 2,372
  • 4
  • 30
  • 40
Ali
  • 10,774
  • 10
  • 56
  • 83
  • My suggestion is to add use LayoutParams with the Relativelayout prefix, like this RelativeLayout.LayoutParams.FILL_PARENT. Maybe the IDE uses the wrong import such as LinearLayout for LayoutParams. – kameny Jun 25 '12 at 18:33
  • RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); – Dheeresh Singh Jun 25 '12 at 18:34

2 Answers2

0

Try this:

RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
labelLayoutParams.topMargin = 10;
layout.setLayoutParams(labelLayoutParams);
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
petey
  • 16,914
  • 6
  • 65
  • 97
0

try this way as per Set the absolute position of a view

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

    params.topMargin = 60;

    layout.setLayoutParams(params);
Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36