Search code examples
androidandroid-fragmentslayoutandroid-linearlayoutandroid-layout-weight

Change Weight of a LinearLayout in a Fragment - Android Studio


(First of all I must say that I have a regular English). Hi guys, I'm trying to change the weight of a LinearLayout from a Fragment. This is my code for that, but at the moment of clicking the fragment it crashes. The idea is that at the moment of clicking on the fragment it fills the mobile screen, and the same with the other fragments.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v=inflater.inflate(R.layout.fragment_info1, container, false);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
            XX.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT,0));
        }
    });
    return v;
}

Here is the Error:

10-10 20:06:54.222 30286-30286/com.example.jhon.myapplication E/AndroidRuntime:
  FATAL EXCEPTION: main: com.example.jhon.myapplication, PID: 30286
      java.lang.NullPointerException: Attempt to invoke virtual method
      'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference 
          at com.example.jhon.myapplication.Info1$1.onClick(Info1.java:79) 
          at android.view.View.performClick(View.java:5612)
          at android.view.View$PerformClick.run(View.java:22285)
          at android.os.Handler.handleCallback(Handler.java:751)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6123)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

In the xml code I tried to put each fragment in a Linear Layout to be able to modify their weight more easily.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.jhon.myapplication.Informacion">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/LinearInfo1">
            <fragment
                android:id="@+id/fragment"
                android:name="com.example.jhon.myapplication.Info1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout="@layout/fragment_info1" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/LinearInfo2"
            android:layout_weight="1">
            <fragment
                android:id="@+id/fragment2"
                android:name="com.example.jhon.myapplication.Info2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout="@layout/fragment_info2" />
        </LinearLayout>
    </LinearLayout>
</FrameLayout>  

New Logcat info:

10-10 21:06:03.232 10887-10887/com.example.jhon.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.jhon.myapplication, PID: 10887
        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
            at com.example.jhon.myapplication.Info1$1.onClick(Info1.java:80)
            at android.view.View.performClick(View.java:5612)
            at android.view.View$PerformClick.run(View.java:22285)
            at android.os.Handler.handleCallback(Handler.java:751)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6123)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

Solution

  • Try this in your code .

    private LinearLayout XX;
    
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_info1, container, false);
        // add here 
        XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                param.weight = 1;
    
                // edited here
                if (XX != null) {
                    XX.setLayoutParams(param);
                }
            }
        });
        return v;
    }
    

    - Make the LinearLayout as global variable .

    - Make LinearLayout final

    final LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
    

    Find the corresponding ID of LinearLayout