Using navigation component Load fragment inside a fragment - kotlin

In Home Fragment I am having xml as
<androidx.constraintlayout.widget.ConstraintLayout
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/bottomNavi"
app:navGraph="#navigation/bottm_navi" />
<meow.bottomnavigation.MeowBottomNavigation
android:id="#+id/bottomNavi"
app:mbn_circleColor="#color/white"
app:mbn_backgroundBottomColor="#color/app_dark_blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and my bottom navigation is
<?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/bottm_navi"
app:startDestination="#id/homeBottomOneFragment">
<fragment
android:id="#+id/homeBottomOneFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomOneFragment"
android:label="fragment_home_bottom_one"
tools:layout="#layout/fragment_home_bottom_one" />
<fragment
android:id="#+id/homeBottomTwoFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomTwoFragment"
android:label="fragment_home_bottom_two"
tools:layout="#layout/fragment_home_bottom_two" />
<fragment
android:id="#+id/homeBottomFourFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomFourFragment"
android:label="fragment_home_bottom_four"
tools:layout="#layout/fragment_home_bottom_four" />
<fragment
android:id="#+id/homeBottomThreeFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomThreeFragment"
android:label="fragment_home_bottom_three"
tools:layout="#layout/fragment_home_bottom_three" />
<fragment
android:id="#+id/homeFragment2"
android:name="com.krassier.customer.ui.home.HomeFragment"
android:label="HomeFragment" >
<action
android:id="#+id/action_homeFragment2_to_homeBottomOneFragment"
app:destination="#id/homeBottomOneFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomTwoFragment"
app:destination="#id/homeBottomTwoFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomThreeFragment"
app:destination="#id/homeBottomThreeFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomFourFragment"
app:destination="#id/homeBottomFourFragment" />
</fragment>
</navigation>
in homefragment I wrote
findNavController().navigate(R.id.action_homeFragment2_to_homeBottomOneFragment)
when I clicked on bottomnavigation I am trying to load another fragment in homefragment as bottom navigation is in home fragment, but error is:
java.lang.IllegalArgumentException: Navigation action/destination com.krassier.customer:id/action_homeFragment2_to_homeBottomOneFragment cannot be found from the current destination Destination(com.krassier.customer:id/homeFragment) label=fragment_home class=com.krassier.customer.ui.home.HomeFragment

At last I followed conventional way
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = activity?.supportFragmentManager
val fragmentTransaction = fragmentManager?.beginTransaction()
fragmentTransaction?.replace(R.id.homeFragmentContainer, fragment)
fragmentTransaction?.commit()
}

Since you are in a fragment, you should try finding the navController this way.
val navHostFragment = supportFragmentManager.findFragmentById(R.id. fragment) as NavHostFragment. // the id of your FragmentContainerView
navController = navHostFragment.navController
You should take advantage of the NavigationUI library and let it handle the navigation fragment change logic for you
binding.bottomNavi.setupWithNavController(navController)

You can try this:
currentDestination?.getAction(direction.actionId)?.run { navigate(direction) }
}
fun NavController.safeNavigate(
#IdRes currentDestinationId: Int,
#IdRes id: Int,
args: Bundle? = null
) {
if (currentDestinationId == currentDestination?.id) {
navigate(id, args)
}
}
This crash occurs when you make multiple calls the navigate method.
https://nezspencer.medium.com/navigation-components-a-fix-for-navigation-action-cannot-be-found-in-the-current-destination-95b63e16152e

If you want to only just change the fragment like Fragment Transaction using Navigation component, you could do it like this:
private fun getNavController(): NavController {
val navController = findNavController(R.id.fragment)
return navController
}
private fun changeFragment(#IdRes viewId: Int) {
getNavController().popBackStack()
getNavController().navigate(viewId)
}
// how to use it
changeFragment(R.id.homeBottomFourFragment)

Related

Buttons don't work in new activity crated from service foreground window

I have a foreground Service with a button without any activities (initial was closed). When I click button the new activity window is created. The problem is that all buttons in new activity don't work
In foreground service I do:
override fun onClick(p0: View?) {
if (!moving) {
startActivity(Intent(applicationContext, Mmap_activity::class.java)
.setAction(Intent.ACTION_VIEW)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT))
}
}
In Manifest new activity declared:
<activity
android:name=".Mmap_activity"
android:exported="true"
android:theme="#style/ActionBarTheme">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
activity_mmap.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=".Mmap_activity">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/leftBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent=".80"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/bottomBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".10" />
<ImageButton
android:id="#+id/mapexitButton"
style="#style/Widget.AppCompat.ImageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFFFF"
android:contentDescription="#string/Enter"
android:src="#drawable/ic_baseline_close_24"
app:layout_constraintBottom_toBottomOf="#id/bottomBorder"
app:layout_constraintLeft_toLeftOf="#id/leftBorder"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mmap_activity.kt:
class Mmap_activity : AppCompatActivity() {
private lateinit var binding: ActivityMmapBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mmap)
binding = ActivityMmapBinding.inflate(layoutInflater)
binding.mapexitButton.setOnClickListener{
println("button pressed")
}
}
}
How to repair button in new activity? Why it doesn't work?
The error was here.
binding = ActivityMmapBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.bottomNavigationView.setupWithNavController doesn't work

I'm developing an Android app, in which I utilize both Android Navigation Component and BottomNavigationView.
The problem is that assigning setupWithNavController to my BottomNavigationView, and when i run the app is crashed and generate java.lang.RuntimeException error
Code
NewsActivity.ky
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.myapplication.databinding.ActivityNewsBinding
import com.example.myapplication.db.ArticleDatabase
import com.example.myapplication.repository.NewsRepository
import kotlinx.android.synthetic.main.activity_news.*
class NewsActivity : AppCompatActivity() {
lateinit var viewModel: NewsViewModel
lateinit var binding : ActivityNewsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityNewsBinding.inflate(layoutInflater)
setContentView(binding.root)
val newsRepository = NewsRepository(ArticleDatabase(this))
val viewModelProviderFactory = NewsViewModelProviderFactory(newsRepository)
viewModel = ViewModelProvider(this, viewModelProviderFactory)[NewsViewModel::class.java]
binding.bottomNavigationView.setupWithNavController(binding.newsNavHostFragment.findNavController())
}
}
activity_news.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=".ui.NewsActivity">
<FrameLayout
android:id="#+id/flFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/newsNavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="#navigation/news_nav_graph"/>
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
app:menu="#menu/bottom_navigation_menu"
android:layout_height="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>
**news_nav_graph.xml**
<?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"
android:id="#+id/news_nav_graph"
app:startDestination="#id/breakingNewsFragment">
<fragment
android:id="#+id/articleFragment"
android:name="com.example.myapplication.ui.fragments.ArticleFragment"
android:label="ArticleFragment" />
<fragment
android:id="#+id/breakingNewsFragment"
android:name="com.example.myapplication.ui.fragments.BreakingNewsFragment"
android:label="BreakingNewsFragment">
<action
android:id="#+id/action_breakingNewsFragment_to_articleFragment2"
app:destination="#id/articleFragment"
app:enterAnim="#anim/slide_in_right"
app:exitAnim="#anim/slide_out_left"
app:popEnterAnim="#anim/slide_in_left"
app:popExitAnim="#anim/slide_out_right" />
</fragment>
<fragment
android:id="#+id/savedNewsFragment"
android:name="com.example.myapplication.ui.fragments.SavedNewsFragment"
android:label="SavedNewsFragment">
<action
android:id="#+id/action_savedNewsFragment_to_articleFragment2"
app:destination="#id/articleFragment"
app:enterAnim="#anim/slide_in_right"
app:exitAnim="#anim/slide_out_left"
app:popEnterAnim="#anim/slide_in_left"
app:popExitAnim="#anim/slide_out_right" />
</fragment>
<fragment
android:id="#+id/searchNewsFragment"
android:name="com.example.myapplication.ui.fragments.SearchNewsFragment"
android:label="SearchNewsFragment">
<action
android:id="#+id/action_searchNewsFragment_to_articleFragment2"
app:destination="#id/articleFragment"
app:enterAnim="#anim/slide_in_right"
app:exitAnim="#anim/slide_out_left"
app:popEnterAnim="#anim/slide_in_left"
app:popExitAnim="#anim/slide_out_right" />
</fragment>
</navigation>
bottom_navigation_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/breakingNewsFragment"
android:title="Breaking news"
android:icon="#drawable/ic_breaking_news"/>
<item android:id="#+id/savedNewsFragment"
android:title="Breaking news"
android:icon="#drawable/ic_favorite"/>
<item android:id="#+id/searchNewsFragment"
android:title="Search news"
android:icon="#drawable/ic_all_news"/>
</menu>
Error
2022-06-29 13:55:57.305 19324-19324/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 19324
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.ui.NewsActivity}: java.lang.IllegalStateException: View androidx.fragment.app.FragmentContainerView{b62db28 V.E...... ......I. 0,0-0,0 #7f080148 app:id/newsNavHostFragment} does not have a NavController set
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalStateException: View androidx.fragment.app.FragmentContainerView{b62db28 V.E...... ......I. 0,0-0,0 #7f080148 app:id/newsNavHostFragment} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
at androidx.navigation.ViewKt.findNavController(View.kt:28)
at com.example.myapplication.ui.NewsActivity.onCreate(NewsActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

How to hide the FloatingActionButton that is part of the Activity in the Fragment in Kotlin?

In my 'HomeActivity', I have a FloatingActionButton and four Fragments. I want the FloatingActionButton to be shown only in the 'fragment1' and 'fragment2' make hide it in the 'fragment3' and 'fragment4'. I tried but it didn't work.
Following is what I have in the xml file of the 'HomeActivity'
<?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/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_hide_category"
app:layout_anchor="#id/bottom_appbar"/>
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation"
app:layout_anchor="#id/bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I added the below code in the 'HomeActivity'
fun hideFabOne(){
binding.fabOne.hide()
}
and below code in the onCreateView of the fragments where I want to hide the FAB. But, it didn't work.
HomeActivity().hideFabOne()
Looks like you are creating new instance of activity , try
(requireActivity() as HomeActivity).hideFabOne()
*Add for comment
You can modify the hideFabOne() something like
fun hideFabOne(){
binding.fabOne.visiblity = if( binding.fabOne.isVisible ) View.VISIBLE else View.GONE
}

Rounded corners for custom AlertDialog

I have been updating my apps forgot password functionality from Activity to simple custom Alert Dialog. How should I add rounded corners to Dialog window? I have already read multiple tutorials, but none seem to work in my case. I read from somewhere that I should use setBackgroundResources method somewhere, but I'm not sure where.
Kotlin code
// Forgot password textview onClick Listener
binding.tvForgotPassword.setOnClickListener {
// Inflate add_item_dialog.xml custom view
val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_forgot_password, null)
// Add AlertDialog Builder
val dialogBuilder = AlertDialog.Builder(this)
.setView(dialogView)
// Binding add_item_dialog layout
val dialogBinding = DialogForgotPasswordBinding.bind(dialogView)
// Show Forgot password Dialog
val customAlertDialog = dialogBuilder.show()
dialogBinding.ivCloseDialog.setOnClickListener {
customAlertDialog.dismiss()
}
}
dialog_forgot_password.xml
<androidx.cardview.widget.CardView
android:layout_width="350dp"
android:layout_height="500dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="350dp"
android:layout_height="500dp">
<ImageView
android:id="#+id/iv_closeDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.94"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.060000002"
app:srcCompat="#drawable/ic_close" />
<TextView
android:id="#+id/tv_forgotPasswordInfo"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:textSize="18sp"
android:text="#string/enter_email"
android:textColor="#color/ColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_forgotPassword" />
<Button
android:id="#+id/submitBtn"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:background="#drawable/button"
android:elevation="4dp"
android:text="#string/submit"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emailForgot"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/tv_forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:fontFamily="sans-serif-black"
android:text="#string/forgot_password"
android:textColor="#color/ColorPrimary"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_closeDialog" />
<EditText
android:id="#+id/emailForgot"
android:layout_width="300dp"
android:layout_height="40dp"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:hint="#string/email_address"
android:importantForAutofill="no"
android:inputType="textEmailAddress"
android:paddingStart="15dp"
android:textColorHint="#color/ColorPrimary"
app:layout_constraintBottom_toTopOf="#+id/submitBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_forgotPasswordInfo"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Rounded_framelayout.xml
<solid android:color="#FFFFFF"/>
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/>
<corners android:radius="20dp" />
Please take a look at this guide for how to customize dialogs.
In short, you need to create a dialog like normal, but set the background resource before you show it:
val builder = AlertDialog.Builder(context)
builder.setView(R.layout.item_dialog)
val dialog = builder.create()
dialog.window?.decorView?.setBackgroundResource(R.drawable.dialog_background) // setting the background
dialog.show()
where dialog_background is defined like so:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="16dp" />
<solid android:color="?android:colorBackground" />
</shape>
TRY THIS:
class your_kotlin_filename :DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog!!.window?.setBackgroundDrawableResource(R.drawable.Rounded_framelayout);
return inflater.inflate(R.layout.dialog_forgot_password, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
............your code for forget password...........
}
override fun onStart() {
super.onStart()
val width = (resources.displayMetrics.widthPixels * 0.95).toInt()
val height = (resources.displayMetrics.heightPixels * 0.43).toInt()
dialog!!.window?.setLayout(width, height)
dialog!!.setCanceledOnTouchOutside(true)
}

Attempt to invoke virtual method on a null object reference -> resolve?

I try to do a button for switch from a main activity to an other, but when I try to run the app, it close itself immediatly after click on the app. I find in the command that message of error :
Unable to start activity ComponentInfo{fr.amseu.mystretching/fr.amseu.mystretching.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
My code in the MainActivity is this one :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<ImageButton>(R.id.imageButton)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent);}
That is my Main Activity :
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/item_main"
android:background="#color/light_red">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="100dp"
android:layout_height="120dp"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/lower_legs"
android:contentDescription="#string/home_page_first_button" />
</androidx.cardview.widget.CardView>
<View
android:id="#+id/view_separation"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/name_item"
style="#style/SubTitleTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/default_margin"
android:text="#string/home_page_first_button"
app:layout_constraintBottom_toTopOf="#+id/view_separation"
app:layout_constraintStart_toEndOf="#+id/cardView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/description_item"
style="#style/DefaultTextStyle"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/home_page_description_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/name_item"
app:layout_constraintTop_toBottomOf="#+id/view_separation" />
</androidx.constraintlayout.widget.ConstraintLayout>
And for my recycler view, I take this model in my page and the following item are siblings but I change the image and texts, with an adapter, HomeFragment...
Can someone help me please ?
(It is my first app)
Thanks a lot !