Home > Blockchain >  Constraint layout not visible on the device/emulator
Constraint layout not visible on the device/emulator

Time:09-17

Hi I have a constraint layout in my fragment and I have added view binding to it.

when I run it on the device, the UI is blank but displays the correct fragment heading

fragment_enter_details.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/purple_200"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".EnterDetailsFragment">

    <EditText
        android:id="@ id/et_first_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <EditText
        android:id="@ id/et_mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:layout_constraintTop_toBottomOf="@ id/et_first_name"/>

    <Button
        android:id="@ id/btn_verify_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Verify details"
        android:layout_margin="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

EnterDetailsFragment

class EnterDetailsFragment : Fragment() {

    private var _binding: FragmentEnterDetailsBinding? = null
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

         _binding = FragmentEnterDetailsBinding.inflate(inflater, container, false)
        val view = binding.root
        initView()

        //val rootView = inflater.inflate(R.layout.fragment_enter_details, container, false)

        return view
    }

    private fun initView() {
        binding.btnVerifyDetails.setOnClickListener {
            findNavController().navigate(R.id.action_enterDetailsFragment_to_verifyDetailsFragment)
        }
    }
}

Added viewbinding in the gradle

buildFeatures {
    viewBinding = true
}

Nav graph

this is my nav graph for reference

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/enterDetailsFragment">

    <fragment
        android:id="@ id/enterDetailsFragment"
        android:name="com.example.navjetpack.EnterDetailsFragment"
        android:label="fragment_enter_details"
        tools:layout="@layout/fragment_enter_details" >
        <action
            android:id="@ id/action_enterDetailsFragment_to_verifyDetailsFragment"
            app:destination="@id/verifyDetailsFragment" />
    </fragment>
    <fragment
        android:id="@ id/verifyDetailsFragment"
        android:name="com.example.navjetpack.VerifyDetailsFragment"
        android:label="fragment_verify_details"
        tools:layout="@layout/fragment_verify_details" />
</navigation>

Ant ideas on what I might be doing wrong here

Thanks Rao

EDIT

MainActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = Navigation.findNavController(this, R.id.fragment)
        NavigationUI.setupActionBarWithNavController(this, navController)
    }
}

Activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<fragment
    android:id="@ id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:navGraph="@navigation/nav_graph"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Try constraint your again. I'm seeing you're constraint bottom of to top of the parent.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<fragment
    android:id="@ id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="parent" // Fix to app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:navGraph="@navigation/nav_graph"
    />

</androidx.constraintlayout.widget.ConstraintLayout>
  • Related