I am trying to make my Bottom Navigation Bar clickable so that it actually redirects me to the correct page, however the app doesn't even start. It crashes before I can even click anything. The thing is, I don't even know what the error is. The debugger is telling me that the following line has something wrong with it:
val navHostFragment = supportFragmentManager.findFragmentById(R.id.frameLayout) as NavHostFragment
So this is the exact error I'm getting:
java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment
This is my Kotlin code (MainActivity.kt):
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.frameLayout) as NavHostFragment
val navController = navHostFragment.navController
bottomNavigationView.setupWithNavController(navController)
recyclerView_main.setBackgroundColor(Color.WHITE)
recyclerView_main.layoutManager = LinearLayoutManager(this)
recyclerView_main.adapter = MainAdapter()
}
}
Here's the [updated after seeing replies] code for the Fragment:
<?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/frameLayout"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".apply">
<Button
android:id="#+id/button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="100dp"
android:text="#string/apply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="175dp"
android:layout_marginEnd="182dp"
android:layout_marginBottom="100dp"
android:text="#string/total_200"
android:textColor="#000000"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="177dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="177dp"
android:text="Application Menu"
android:textColor="#000000"
android:textSize="36sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
did you add the name attribute?
<androidx.fragment.app.FragmentContainerView
android:id="#+id/frameLayout"
android:name="androidx.navigation.fragment.NavHostFragment"
The NavHostFragment must be in an activity. Remove the id and name attributes from your fragment and put them in the main activity layout.
// activity_main.xml
<androidx.fragment.app.FragmentContainerView
android:id="#+id/frameLayout"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
// fragment.xml
<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">
...
</androidx.constraintlayout.widget.ConstraintLayout>
Related
I'm having a problem with my toolbar.
I want the device app and toolbar to be the same color, but don't use findviewbyid. (I'm migrating the entire project just to view binding
)
OK behavior (using findviewbyid).
NOK behavior (using view binding), is giving a little misalignment of the textView, it seems to receive a margin that was not placed.
But if I declare different in onWindowsInsetsChanged method or don't use it, I get another problem, the color is not correct.
Using findviewbyid:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
val toolbar = fragmentEditObjectiveNameRoot.findViewById<View>(R.id.toolbar)
toolbar.updatePadding(top = topInset)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
Using view binding:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
//calling the include toolbar and the root of the toolbar fragment
toolbar.viewImageToolbar.updatePadding(
top = topInset
)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
Different declaration in onWindowsInsetsChanged:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
//calling root from fragment and instead of calling the include toolbar and its root
fragmentEditObjectiveNameRoot.updatePadding(
top = topInset
)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
I would like to leave the same color behavior when using findviewbyid, but with view binding, but without losing textView alignment.
Here is the xml that receives the toolbar include:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragmentEditObjectiveNameRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/editObjectiveNameRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/bgColorFrameLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/color_frame_layout_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/screen_margin_horizontal"
android:layout_marginTop="18dp"
android:layout_marginEnd="#dimen/screen_margin_horizontal"
android:paddingBottom="16dp">
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/bari_white"
app:cardCornerRadius="#dimen/card_corner_radius"
app:cardElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/card_padding">
<TextView
android:id="#+id/titleSubAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/sub_account_control_settings_edit_name"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<br.com.bancobari.core_ui.views.text_input.BariTextInputLayout
android:id="#+id/nameInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:helperIconEnabled="true"
app:helperStart_enabled="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/titleSubAccount">
<br.com.bancobari.core_ui.views.text_input.BariTextInputEditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName|textCapWords"
android:maxLength="16"
android:textAlignment="center"
android:textSize="16sp" />
</br.com.bancobari.core_ui.views.text_input.BariTextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<br.com.bancobari.core_ui.views.SubmitButton
android:id="#+id/saveButton"
style="#style/DefaultButton.Icon.Arrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/objective_settings_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cardView"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<include
android:id="#+id/toolbar"
layout="#layout/view_image_toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here the toolbar 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="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/viewImageToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="#dimen/custom_toolbar_height"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:layout_width="0dp"
android:layout_height="#dimen/custom_toolbar_height" />
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="#+id/iconEmojiView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:gravity="bottom"
android:textSize="22.5sp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/toolbarTitleTextView"
app:layout_constraintEnd_toStartOf="#+id/toolbarTitleTextView"
tools:visibility="visible" />
<TextView
android:id="#+id/toolbarTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:fontFamily="#font/nunito_bold"
android:gravity="center"
android:lines="1"
android:textColor="#color/text_neutral_gray_800"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#id/viewImageToolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Teste" />
<ImageView
android:id="#+id/toolbarRightIconImageView"
style="#style/Widget.AppCompat.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_settings"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/toolbarTitleTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#id/toolbarTitleTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
In your first block of code with findViewById, this
val toolbar = fragmentEditObjectiveNameRoot.findViewById<View>(R.id.toolbar)
returns the ConstraintLayout at the root of your included layout with the ID toolbar.
The property toolbar in your binding is another binding for the included layout rather than the root view of that layout. The root property of that included binding in this case is the same ConstraintLayout from above, so you should use instead:
toolbar.root.updatePadding(
top = topInset
)
What you were doing was changing the padding of an inner view of your complete Toolbar layout, the viewImageToolbar element.
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.
In login_signup activity, change the onCreate() from
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)
to
override fun onCreate(savedInstanceState: Bundle?)
Not really sure what the problem might be, but I'll give a couple of possible solutions.
1st:
Try using a FragmentContainerView to contain your fragment. Something like this:
<androidx.fragment.app.FragmentContainerView
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="PATH TO YOUR FRAGMENT">
In the name tag you can out the path to the fragment that you want to inflate first.
The path should something like com.ak_applications.kottry.login_fragment.
2nd:
You can try a different way to use the supportFragmentManager. I use it like this:
supportFragmentManager.beginTransaction().replace(R.id.frag_container, login_fragment()).commit()
Just give it a go on any of the two.
Below is my main layout where I have included another layout
<layout 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">
<data>
<variable
name="userName"
type="String" />
<variable
name="hasEnded"
type="Boolean" />
<variable
name="showLoader"
type="Boolean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/base_chat_background">
<com.commonui.AppBar
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{userName}"
app:addWindowInsetPaddingTop="#{true}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:clipToPadding="false"
android:paddingTop="16dp"
android:paddingBottom="70dp"
app:addWindowInsetPaddingBottom="#{true}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar" />
<include
android:id="#+id/feedbackLayout"
visibleIf="#{hasEnded}"
layout="#layout/chat_feedback"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ProgressBar
android:id="#+id/loader"
visibleIf="#{showLoader}"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
I have an extension function with which I am handling the visibility of views in XML
#BindingAdapter("visibleIf")
fun setVisibleIf(view: View, visible: Boolean) {
view.visibility = if(visible) View.VISIBLE else View.GONE
}
When I add visibleIf="#{hasEnded}" to my included layout and build the project I get the error
error: cannot find symbol
import com.chat.databinding.FragmentPeerchatBindingImpl;
symbol: class FragmentChatBindingImpl
location: package com.chat.databinding
What could be the cause of it?
Apparently included layout doesn't support binding adapter for now
Alternative is to send your variable from main layout to included layout
We can use app:showFeedback="#{hasEnded}" here showFeedback is the binding variable defined in included layout. hasEnded is the variable defined in the main layout
I have a fragment with an xml file. The xml file tries to include an other xml file. When I try to use binding, to set different variables of the included xml, nothing happens. Here is my code:
fragment xml (short version):
<include
android:id="#+id/include2"
layout="#layout/sende_option_box"
android:layout_width="0dp"
android:layout_height="147dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
app:title='#{"foo"}'
app:numbervalue='#{"foo"}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView25" />
Included xml:
<layout>
<data>
<variable name="title" type="String" />
<variable name="numbervalue" type="String" />
</data>
<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="147dp"
android:background="#drawable/parcel_list_item_borderbox"
android:elevation="5dp">
<TextView
android:id="#+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:layout_marginTop="33dp"
android:text="#{title}"
android:textAppearance="#style/TextAppearanceBoldBody22"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Opptil 10kg"/>
<TextView
android:id="#+id/textView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:text="Norgespakken"
android:textAppearance="#style/TextAppearanceBody15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView26" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:layout_marginBottom="15dp"
android:background="#color/postenVeryLightPink"
android:padding="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{numbervalue}"
android:textAppearance="#style/TextAppearanceBoldBody15"
android:textColor="#color/postenPrimaryDark"
tools:layout_editor_absoluteX="50dp"
tools:layout_editor_absoluteY="17dp"
tools:text="Fra 149,-"/>
</LinearLayout>
<ImageView
android:id="#+id/imageView6"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginEnd="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/alert_message_borderbox_yellow" />
</androidx.constraintlayout.widget.ConstraintLayout>
The build runs, but there is no foo showing in the view as I wanted it to. What am I doing wrong? Is it because of its a fragment?
You have to call DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main) in Your layout file where layout is included. Example:
included_layout:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<variable
name="myTitle"
type="String"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{myTitle}"/>
</layout>
main_activity:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/included_layout"
app:myTitle="#{`Your Title`}" />
</LinearLayout>
</layout>
MainActivty:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.myniprojects.bindingtest.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main) // HERE You have to call this function
}
}
If You include layout in fragment You call the same function in onCreateView but not with this but requireActivity and of course as 2nd argument You pass right XML layout. In fragment You can call this function and return value:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return DataBindingUtil.setContentView<FragmentBlankBinding>(
requireActivity(),
R.layout.fragment_blank
).root
}
I'm building my first android app through android studio using kotlin, and I'm having issues with the elements of an activity showing up when I run the app.
I created a button in the main activity that navigates to the new activity, and inside the new activity is a prompt for a text input.
However, the new activity is just blank when ran.
<?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=".CalcActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="39dp"
android:layout_marginBottom="651dp"
android:text="Enter Polynomial Fraction"
android:textAppearance="#style/TextAppearance.AppCompat.Display3"
android:textColor="#435768"
android:textSize="30sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<EditText
android:id="#+id/poly_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="107dp"
android:layout_marginEnd="167dp"
android:layout_marginBottom="579dp"
android:ems="10"
android:inputType="textLongMessage"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_click_me = findViewById<Button>(R.id.CalcButton)
// set on-click listener
btn_click_me.setOnClickListener {
// your code to perform when the user clicks on the button
val intent = Intent(this, CalcActivity::class.java)
startActivity(intent)
}
}
}
package com.example.prac
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
class CalcActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_calc)
}
}
Why hard-coding margin with big values this will not work on all screen sizes also you need to know how to use contraintlayout this is a link to codelab also this is a working sample for your 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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Enter Polynomial Fraction"
android:textAppearance="#style/TextAppearance.AppCompat.Display3"
android:textColor="#435768"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="#+id/poly_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textLongMessage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView2"
/>
</androidx.constraintlayout.widget.ConstraintLayout>