-1

I am trying to control weightSum for linear layout from fragment. I wondered if it's possible to do that using setWeightSum(float) method. I tried to control weight sum in Linear layout which id is header_Linear.

<LinearLayout
android:id="@+id/header_Linear"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:orientation="horizontal"
android:weightSum="8">

        <TextView
            android:id="@+id/table_0.0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textAlignment="center"
            android:gravity="center"
            android:text="M"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="T"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="W"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="T"
            android:visibility="visible"
            android:textColor="@color/colorTableItem"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="F"
            android:visibility="visible"
            android:textColor="@color/colorTableItem"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="S"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="S"
            android:textColor="@color/sunday_text_color"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>
</LinearLayout>

This is my fragment that using setWeightSum

public class TimeTableFragment extends Fragment {

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

    View root = inflater.inflate(R.layout.fragment_timetable, container, false);

    LinearLayout linearLayout = root.findViewById(R.id.header_Linear);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getContext()));
    String header_value = sharedPreferences.getString("Header","0");
    Toast.makeText(getContext(),""+header_value,Toast.LENGTH_LONG).show();

    if (header_value.equals("0")){
        linearLayout.setWeightSum(6);
    }

    return root;
}

When I start the app it crashes. I'd like to know if it's possible to control weightSum without errors. Thank you very much

Upate

The reason why I got nullPointException even though I declared Linear Layout using findViewById was due to xmlns:android="http://schemas.android.com/apk/res/android"in Layout.

The View that contains xmlns:android="http://schemas.android.com/apk/res/android" would return null after using findViewById in this situation

  • What is the crash Can you share your logcat text here ? – Paranoid Mar 24 '20 at 18:38
  • Sure I got .java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setWeightSum(float)' on a null object reference –  Mar 24 '20 at 18:41
  • Why you are getting nullPointerException listen..i m gonna edit my answer just see that and then tell me itx work or not. – Paranoid Mar 24 '20 at 18:54

1 Answers1

0
        public class TimeTableFragment extends Fragment {

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

            View root = inflater.inflate(R.layout.fragment_timetable, container, false);

            return root;
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            LinearLayout linearLayout = view.findViewById(R.id.header_Linear);

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getContext()));
            String header_value = sharedPreferences.getString("Header","0");
            Toast.makeText(getContext(),""+header_value,Toast.LENGTH_LONG).show();

            if (header_value.equals("0")){
                linearLayout.setWeightSum(6);
            }

    }
}
Paranoid
  • 214
  • 2
  • 14
  • The third one parameter is basically a weight. You can set it like this. – Paranoid Mar 24 '20 at 18:47
  • Thank you very much. I've tried linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));, but still got NullPointerException –  Mar 24 '20 at 18:58
  • Check this solution that i edit now the problem is not with your setWeightSum() method >> problem is you are getting NullPointerException just overide OnViewCreated() Method and do the same thing that you were doing in the OnCreateView() method – Paranoid Mar 24 '20 at 19:06
  • I appreciate your effort. I've tried to create onViewCreated and using findViewById with view which inside of argument, however it kept saying NullPointerException. I guess I should find different method that similar to WeightSum in LinearLayout –  Mar 24 '20 at 19:23
  • Its a shocking thing that you are getting nullPointerException. This only means that your Linear Layout Object is null . You must need to check your ID that you declared in your XML file. Other wise paste the whole stack trace here Thanks. – Paranoid Mar 24 '20 at 19:26
  • After trying, finally I have solved this problem. I had used Linear Layout that contains `xmlns:android="http://schemas.android.com/apk/res/android"`,then I changed it as Relative Layout then create new Linear Layout which named `header_Linear` as Id then it worked! I think I got the nullPointerException due to the Linear Layout was the base of the layout –  Mar 24 '20 at 21:50
  • yes i told you last day that the problem was with your linear layout not the weightSum() method. Anyways enjoy – Paranoid Mar 25 '20 at 07:49