-1

I am trying to change my LinearLayout height to 50% of the parent layout when i click a button. I trired it with LinearLayout.LayoutParams but it doesn't change the height of my layout if i call it when buttonClicked from a method outside onCreate . But if i keep LinearLayout.LayoutParams inside onCreate then it works. How can i do it when i click a button?

This Code Works :

public class ViewReport extends AppCompatActivity {

Button button;
LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_report);

    button=(Button) findViewById(R.id.button);
    layout=(LinearLayout) findViewById(R.id.fold);

    final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
    lp.height = 0;
    lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
    lp.weight=0.5f;

}
}

This Code Doesn't Work :

public class ViewReport extends AppCompatActivity {

Button button;
LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_report);

    button=(Button) findViewById(R.id.button);
    layout=(LinearLayout) findViewById(R.id.fold);
}


public  void Click(View view)
{
    Toast.makeText(getApplicationContext(),"tapped",Toast.LENGTH_LONG).show();
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
    lp.height = 0;
    lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
    lp.weight=0.5f;
}
}

Layout XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.imbishal.imageblur.ViewReport"
android:weightSum="1">

<LinearLayout
    android:id="@+id/fold"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/profilepageback"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText4"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginBottom="3dp"
        android:background="@drawable/my_button"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/my_button"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/just_border"
        android:onClick="Click"
        android:text="Button" />
</LinearLayout>

1 Answers1

0

You have changed your LayoutParams but haven't set them. Add this line

layout.setLayoutParams(lp);