Home > database >  Android : Layout width gets automatically changed to wrap_content from match_parent
Android : Layout width gets automatically changed to wrap_content from match_parent

Time:08-14

I have set the layout width as "match_parent" and can see it as matching parent width in Design & Blueprint, but during execution the layout width gets automatically changed to "wrap_content".

Could someone help explain what's wrong with my code? Layout code:

    <?xml version="1.0" encoding="utf-8" ?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@ id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:background="@color/purple_200"
    android:orientation="vertical"
    tools:context=".view.diary.DiaryViewFragment">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.textview.MaterialTextView
                android:id="@ id/detailsTextView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="?attr/actionBarSize"
                android:scrollbars="vertical" />
        </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment:

package com.ramvastechnologies.myday.view.diary;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textview.MaterialTextView;
import com.ramvastechnologies.myday.R;
import com.ramvastechnologies.myday.databinding.DiaryViewFragmentBinding;
import com.ramvastechnologies.myday.model.diary.DiaryEntry;
import com.ramvastechnologies.myday.viewmodel.diary.DiarySharedViewModel;

import java.util.concurrent.atomic.AtomicInteger;

public class DiaryViewFragment extends Fragment {

    private DiarySharedViewModel mDiarySharedViewModel;

    public DiaryViewFragment() {
        // Required empty public constructor
    }

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

        View diaryViewScreen = inflater.inflate(R.layout.diary_view_fragment,container,false);
        return diaryViewScreen;
    }

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

        mDiarySharedViewModel = new ViewModelProvider(requireActivity()).get(DiarySharedViewModel.class);

        AtomicInteger diaryItemId = new AtomicInteger(DiaryViewFragmentArgs.fromBundle(getArguments()).getUid());

        DiaryEntry currentDiaryItem = mDiarySharedViewModel.getClikedDiaryItem(diaryItemId.get());

        StringBuffer display = new StringBuffer();
        display.append(currentDiaryItem.getDiaryDate()).append("\n").append(currentDiaryItem.getDiaryTitle()).append("\n")
                        .append(currentDiaryItem.getDiaryDetails());

        MaterialTextView diaryView = (MaterialTextView)view.findViewById(R.id.detailsTextView);
        diaryView.setText(display.toString());

    }

}

Layout displayed in Design & Blueprint

Display showing layout width = wrap_content

Thanks Biju

CodePudding user response:

I have found the issue myself. In my activity_main.xml, FragmentContainerView layout width & height were set to "wrap_content". I have changed those to "match_parent" and app started displaying correctly.

  • Related