Suhrahj Rothgar
Suhrahj Rothgar

Reputation: 283

Changing layout parameters inside a fragment with a button's onClick method

I have an activity that bears exactly 5 fragments. In one of these fragments there is a Button and a RelativeLayout. I want my button to be able to change this RelativeLayout's height or width.

my xml:

     <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical" >

<Button
    android:id="@+id/sSumBut"
    android:layout_width="250dp"
    android:layout_height="40dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:onClick="onClick" >
</Button>

<RelativeLayout
    android:id="@+id/sSum"
    android:layout_width="250dp"
    android:layout_height="0dp"
    android:layout_gravity="center_horizontal"
    android:background="@android:color/black" >

</RelativeLayout>


      </LinearLayout>

         </ScrollView>

As you see the height of the RelativeLayout is 0. I want the button to be able to change it to 100dp.

This is my Fragment class:

  @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View myFragmentView = inflater.inflate(R.layout.up, container, false);

  Button sButton = (Button) myFragmentView.findViewById(R.id.sSumBut);
  final RelativeLayout sSummary = (RelativeLayout) myFragmentView.findViewById(R.id.sSum);

  sButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // When button is clicked.
            sSummary.setLayoutParams(new RelativeLayout.LayoutParams(250, 250));
        }
    });

But this gives out the error Cannot refer to a non-final variable and if I make the variable final, I normally can't change it inside the onClick method dynamically. Please considering that this class is a Fragment, how can I work around this?

error log:

       11-01 15:07:03.035: E/AndroidRuntime(3048): FATAL EXCEPTION: main
       11-01 15:07:03.035: E/AndroidRuntime(3048): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:360)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1284)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.ScrollView.onMeasure(ScrollView.java:354)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.View.measure(View.java:8366)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.os.Handler.dispatchMessage(Handler.java:99)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.os.Looper.loop(Looper.java:130)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at android.app.ActivityThread.main(ActivityThread.java:3693)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at java.lang.reflect.Method.invokeNative(Native Method)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at java.lang.reflect.Method.invoke(Method.java:507)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
       11-01 15:07:03.035: E/AndroidRuntime(3048):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 3075

Answers (3)

laalto
laalto

Reputation: 152887

sSummary.setLayoutParams(new RelativeLayout.LayoutParams(250, 250));

Your sSummary, even if it is a RelativeLayout, is inside a LinearLayout. A view's layout params refer to the parent layout so the correct type of layout params here would be LinearLayout.LayoutParams. This is what causes the class cast exception.

Often a good approach is to just modify the existing layout params as retrieved with getLayoutParams(). After modifying the returned params in-place, request a re-layout with requestLayout() (setLayoutParams() also does this implicitly).

Upvotes: 1

Suhrahj Rothgar
Suhrahj Rothgar

Reputation: 283

Okay, turns out the problem was LinearLayout for some reason. I changed it to RelativeLayout and my problem was fixed. What the hell LinearLayout? I trusted you.

Upvotes: 0

David
David

Reputation: 7115

The reason why you are getting an error is because you can't reference a variable within the OnClickListener that is instantiated outside of class without it being final.

The fact that it is a Fragment shouldn't change anything, it's just a matter of correctness. you could simply make the following update:

final RelativeLayout Sum = (RelativeLayout) myFragmentView.findViewById(R.id.Sum);

And that should fix the error that you are receiving.

Upvotes: 0

Related Questions