Fragment Not Showing in the Activity
Posted By: Anonymous
I have tried everything but i am not able to find the bug in the code what was it, i want to show the fragment in the activity but the fragment is not showing, What i have tried is
followed instruction as per https://developer.android.com/guide/fragments/create#kotlin, result = not worked.
Watched and followed 6 to 10 youtube tutorials, result = not worked.
tried to find the solution on the google, result = tried and checked everything as per every single article but not worked.
Tried different layouts in the XML layout file like as Fragment, FrameLayout, <androidx.fragment.app.FragmentContainerView>
i was learning the java for android dvelopment but when i know that the primary language for android development is kotlin then i just move to the kotlin.
So i am very new to kotlin please tell me what is the problem why the fragment is not working.
MainActivity
package com.ak_applications.kottry.screens
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.ak_applications.kottry.R
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnGo: Button = findViewById(R.id.btn_go)
btnGo.setOnClickListener {
startActivity(Intent(this, login_signup::class.java))
}
}
}
MainActiviy.Layout
<?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=".screens.MainActivity"
android:background="@drawable/blue_and_purple">
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:id="@+id/btn_go"
android:layout_width="220dp"
android:layout_height="60dp"
app:strokeColor="@color/white"
app:strokeWidth="1dp"
android:text="GO"
android:textColor="@color/white"
android:textSize="24sp"
app:elevation="34dp"
app:rippleColor="@color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Class in which i want to show the fragment.
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"/>
</LinearLayout>
Class
package com.ak_applications.kottry.screens
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import com.ak_applications.kottry.R
import com.ak_applications.kottry.screens.fragments.login_fragment
class login_signup: AppCompatActivity(R.layout.login_signup_layout) {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
if (savedInstanceState == null) {
supportFragmentManager.commit {
setReorderingAllowed(true)
add(R.id.frag_container, login_fragment())
}
}
}
}
The Fragment that i want to show
Fragment Layout
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".screens.fragments.login_fragment"
android:background="@color/black">
<TextView
android:id="@+id/txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:fontFamily="@font/raleway_semi_bold"
android:textColor="@color/white"
android:textSize="32sp"
android:letterSpacing="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/txt_login_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Credintials to get access"
android:textColor="#9CFFFFFF"
android:fontFamily="@font/raleway_medium"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@id/txt_login"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/user_id_feild"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="100dp"
app:boxStrokeColor="@color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:boxCornerRadiusTopStart="8dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:startIconDrawable="@drawable/user_active_state"
app:layout_constraintTop_toBottomOf="@id/txt_login_des"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="@id/password_feild"
app:layout_constraintVertical_bias="0">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email or User name or Phone Number"
android:textColorHint="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_feild"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
app:boxStrokeColor="@color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:boxCornerRadiusTopStart="8dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:startIconDrawable="@drawable/lock_unlock_anime"
app:layout_constraintTop_toBottomOf="@id/user_id_feild"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@id/btn_signIn"
app:layout_constraintVertical_bias="1">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="•••••••••••••"
android:textColorHint="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_signIn"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:text="Sign In"
android:backgroundTint="@color/white"
android:textAllCaps="false"
android:textColor="@color/black"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/password_feild"
app:layout_constraintBottom_toTopOf="@id/fbtn_google"
app:strokeColor="@color/white"
app:strokeWidth="1dp"
app:rippleColor="@color/black"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_register"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:text="Register"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/fbtn_google"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0"
android:layout_marginTop="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_signIn"
app:rippleColor="@color/black"
app:strokeColor="@color/white"
app:strokeWidth="1dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fbtn_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#6F000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="@drawable/google_cute_icon"
android:tintMode="add"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fbtn_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/black"
android:background="@drawable/icons8_facebook"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@id/fbtn_google"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1"
android:layout_marginEnd="15dp"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="@drawable/icons8_facebook"
android:tintMode="add"
app:maxImageSize="30dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fbtn_twitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#6F000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@id/fbtn_google"
app:layout_constraintHorizontal_bias="0"
android:layout_marginStart="15dp"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="@drawable/google_cute_icon"
android:tintMode="add"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Class
package com.ak_applications.kottry.screens.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import com.ak_applications.kottry.R
class login_fragment : Fragment(R.layout.fragment_login_fragment) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
Plase help me out i don’t understand what is the issue with my code.
Solution
In login_signup
activity, change the onCreate()
from
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)
to
override fun onCreate(savedInstanceState: Bundle?)
Answered By: Anonymous
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.