I have a matter with my android studio project. In this project, I was using data binding.
it all works fine. But, when I create a new kotlin class the problem occurs. My Activity.kt cannot recognize my id in XML layout.
activity_chapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:background="#android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChapterActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="#color/textColor"
android:background="#color/columnColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:padding="16dp"
android:weightSum="4"
android:background="#color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txt_comic"
android:text="Chapter (10)"
android:textColor="#color/textColor"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_chapter"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
ChapterActivity.kt
package com.tutorial.kotlinfirebasecomicreader
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.tutorial.kotlinfirebasecomicreader.databinding.ActivityMainBinding
class ChapterActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_chapter)
binding.toolbar
}
}
Issue is
private lateinit var binding: ActivityMainBinding
Use
private lateinit var binding: ActivityChapterBinding
Related
I am writing codes for Android app. The layout is scrollview fragment is up, and bottom navigation bar down. It displays menu items well and click event works well, however, depending on contents amount displayed in fragment, bottom navigation bar's height is slightly changing.
What solution for this phenomenon? The following is the codes for this:
MainActivity.kt
package info.shutterpress.idols
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.databinding.DataBindingUtil.setContentView
import com.google.android.material.bottomnavigation.BottomNavigationView
import info.shutterpress.idols.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var view: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView<ActivityMainBinding>(this, R.layout.activity_main)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val mBottomNavView = findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
mBottomNavView.setupWithNavController(navController)
mBottomNavView.visibility = View.VISIBLE
view = mBottomNavView
val mBottomNavigationView = view as BottomNavigationView
mBottomNavigationView.setOnNavigationItemSelectedListener { mi ->
when (mi.itemId) {
R.id.home_dest -> {
navController.navigate(R.id.main_fragment)
}
R.id.list_dest -> {
navController.navigate(R.id.recyclerview_fragment)
}
R.id.quiz_dest -> {
}
}
true
}
}
}
MainFragment.kt
package info.shutterpress.idols
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import info.shutterpress.idols.databinding.FragmentMainBinding
class MainFragment : Fragment() {
lateinit var bind: FragmentMainBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
bind = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false)
return bind.root
}
}
ListFragment.kt
package info.shutterpress.idols
import android.content.res.TypedArray
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import info.shutterpress.idols.databinding.FragmentRecyclerviewBinding
class ListFragment : Fragment() {
lateinit var bind: FragmentRecyclerviewBinding
var title: Array<String>? = null
var image: TypedArray? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
bind = DataBindingUtil.inflate(inflater, R.layout.fragment_recyclerview, container, false)
title = resources.getStringArray(R.array.idols_title_array)
image = resources.obtainTypedArray(R.array.idols_list_image_array)
var adapter = IdolsListAdapter(image, title) {
position -> onListItemClick(position)
}
bind.idolsList.adapter = adapter
return bind.root
}
private fun onListItemClick(position: Int) {
val action = ListFragmentDirections.actionRecyclerviewFragmentToDetailFragment(position)
findNavController().navigate(action)
}
inner class ListHolder(
view: View,
private val onItemClicked: (position: Int) -> Unit
) : RecyclerView.ViewHolder(view), View.OnClickListener {
var titleTextView: TextView = view.findViewById(R.id.detail_item_title)
var imageView: ImageView = view.findViewById(R.id.detail_item_image)
init {
view.setOnClickListener(this)
}
override fun onClick(v: View) {
val position = adapterPosition
onItemClicked(position)
}
}
inner class IdolsListAdapter(
var image: TypedArray?,
var title: Array<String>?,
val onItemClicked: (position: Int) -> Unit) :
RecyclerView.Adapter<ListHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : ListHolder {
val view = layoutInflater.inflate(R.layout.item_view, parent, false)
return ListHolder(view, onItemClicked)
}
override fun getItemCount() = title!!.size
override fun onBindViewHolder(holder: ListHolder, position: Int) {
var _image = image!!.getResourceId(position, -1)
var _title = title!![position]
holder.apply {
titleTextView.text = _title
imageView.setImageResource(_image)
}
}
}
}
DetailFragment.kt
package info.shutterpress.idols
import android.content.res.TypedArray
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.navArgs
import info.shutterpress.idols.databinding.FragmentDetailBinding
class DetailFragment : Fragment() {
lateinit var bind: FragmentDetailBinding
var title: Array<String>? = null
var image: TypedArray? = null
var description: Array<String>? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
bind = DataBindingUtil.inflate(inflater, R.layout.fragment_detail, container, false)
val args by navArgs<DetailFragmentArgs>()
var _position = args.positionDatum
title = resources.getStringArray(R.array.idols_title_array)
image = resources.obtainTypedArray(R.array.idols_list_image_array)
description = resources.getStringArray(R.array.idols_description_array)
bind.writingDetailTitle.text = title!![_position]
bind.writingDetailImage.setImageResource(image!!.getResourceId(_position, -1))
bind.writingDetailContent.text = description!![_position]
return bind.root
}
}
activity_main.xml
<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/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkBeige"
android:layout_margin="0dp"
android:paddingBottom="0dp"
android:orientation="vertical"
tools:context="info.shutterpress.idols.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_main" />
<com.google.android.material.bottomnavigation.BottomNavigationView
app:menu="#menu/menu_bottom_navigation"
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:padding="10dp"
android:theme="#style/Theme.bottomNavApp"
app:backgroundTint="#color/beige"
app:background="#color/beige"
app:itemIconTint="#color/black"
app:itemTextColor="#color/black" />
</LinearLayout>
</layout>
fragment_detail.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<LinearLayout
android:id="#+id/detail_outermost_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_margin="0dp"
android:background="#color/darkBeige"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="#+id/writing_detail_title"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingTop="10dp"
android:gravity="center"
android:textSize="35sp"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkBeige"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:scrollbarSize="12dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_marginTop="20dp"
android:background="#color/darkBeige"
android:gravity="start"
android:orientation="vertical">
<ImageView
android:id="#+id/writing_detail_image"
android:layout_width="320dp"
android:layout_height="320dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
app:srcCompat="#drawable/idols1" />
<TextView
android:id="#+id/writing_detail_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkBeige"
android:layout_marginTop="20dp"
android:layout_marginBottom="25dp"
android:paddingTop="0dp"
android:padding="20dp"
android:gravity="start"
android:textSize="#dimen/text_size_main"
android:textColor="#de000000"
android:lineSpacingExtra="#dimen/line_spacing_main"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</layout>
fragment_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_margin="0dp"
android:background="#color/darkBeige"
android:gravity="start"
android:orientation="vertical">
<TextView
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:paddingTop="10dp"
android:gravity="center"
android:textSize="35sp"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkBeige"
android:layout_margin="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:scrollbarSize="12dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_margin="0dp"
android:background="#color/darkBeige"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="#+id/welcome_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkBeige"
android:layout_margin="0dp"
android:paddingTop="0dp"
android:padding="20dp"
android:gravity="start"
android:textSize="#dimen/text_size_main"
android:lineSpacingExtra="#dimen/line_spacing_main"
android:text="#string/welcome_message"
android:textColor="#de000000" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</layout>
fragment_recyclerview.xml
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="35dp"
android:id="#+id/recyclerview_fragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/idols_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:context="info.shutterpress.idols.MainActivity"
tools:listitem="#layout/item_view"/>
</LinearLayout>
</layout>
I have asked expert and he gave me appropriate solution! This works!
First, from activity_main.xml,
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" // this should be 0dp
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_main" />
<com.google.android.material.bottomnavigation.BottomNavigationView
app:menu="#menu/menu_bottom_navigation"
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".1" // this should be deleted
android:padding="10dp"
android:theme="#style/Theme.bottomNavApp"
app:backgroundTint="#color/beige"
app:background="#color/beige"
app:itemIconTint="#color/black"
app:itemTextColor="#color/black" />
This works! It was layout XML problem, not Kotlin code.
My project has a class called Welcome.kt
class Welcome : AppCompatActivity() {
private lateinit var vb: ActivityWelcomesBinding
fun runDemo(v: View) {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vb = ActivityRegisterBinding.inflate(layoutInflater)
setContentView(vb.root)
}
}
In the layout of this class (Welcome.kt) I have the usual views and also an included layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
tools:context=".Welcome">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
...
...
<include layout="#layout/layout_buttons" />
...
...
</FrameLayout>
</ScrollView>
</FrameLayout>
And this is the included layout (layout_buttons), a series of buttons that have the onClick attribute, which should execute the "startDemo" function that is in the activity Welcome.kt
<?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="horizontal">
...
...
<Button
android:id="#+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:onClick="runDemo"
android:text="#string/text1" />
<Button
android:id="#+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:onClick="runDemo"
android:text="#string/text2" />
<Button
android:id="#+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:onClick="runDemo"
android:text="#string/text3" />
</LinearLayout>
Since the buttons are inside an included layout, I can't find a way to get them to call the "runDemo" function of the Welcome.kt activity.
I have searched for information but nothing I have found has solved my problem.
Thanks in advance
you should give your include tag an id for example
<include id="#+id/buttonsLayout"
layout="#layout/layout_buttons" />
After this you can use it like this :
class Welcome : AppCompatActivity() {
private lateinit var vb: ActivityWelcomesBinding
fun runDemo(v: View) {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vb = ActivityRegisterBinding.inflate(layoutInflater)
setContentView(vb.root)
vb.buttonsLayout.yourBtnId
}
}
if you want still to use onClick in the xml then you should use data binding not view binding
I'm working on a sample of Accessibility with espresso UI tests. To implement accessibility checks with UI test, I'm using ATF (Accessibility Test Framework).
While running the test case with accessibility check, it doesn't show all the accessibility errors. In the sample below it should throw TouchTargetSizeCheck error. But in my case, it is getting compiled successfully.
Lib reference
androidTestImplementation
'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.4.0'
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
var count = 0
binding.tvCount.text = count.toString()
binding.btnIncrement.setOnClickListener {
count++
binding.tvCount.text = count.toString()
}
binding.btnDecrement.setOnClickListener {
count--
binding.tvCount.text = count.toString()
}
}
}
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AccessibilityAutomation.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/Theme.AccessibilityAutomation.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#+id/btnDecrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="#+id/tvCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0" />
<TextView
android:id="#+id/btnIncrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivityTest.kt
#RunWith(AndroidJUnit4::class)
#LargeTest
class MainActivityTest {
#get:Rule
var activityScenarioRule: ActivityScenarioRule<MainActivity> =
ActivityScenarioRule(MainActivity::class.java)
init {
AccessibilityChecks.enable().setRunChecksFromRootView(true)
}
#Test
fun testIncrement() {
Espresso.onView(withId(R.id.btnIncrement))
.perform(ViewActions.click())
Espresso.onView(withId(R.id.tvCount))
.check(ViewAssertions.matches(withText("1")))
}
}
TouchTargetSizeCheck error is expected while running the UI test.
Any help would be appreciated.
I have this issue where whenever I try to interact with an element in Kotlin I just can't select it the way I was doing before. For example if I had a button in my XML with an ID of button I would just do this from Kotlin:
button.setOnClickListener {
...
}
But now this simply does not work and I don't know the reason why...
Here is my MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Retrofit
import retrofit2.awaitResponse
const val BASE_URL = "https://cat-fact.herokuapp.com/"
class MainActivity : AppCompatActivity() {
private var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getCurrentData()
}
private fun getCurrentData() {
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.build()
.create(ApiRequests::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.getFacts().awaitResponse()
if (response.isSuccessful) {
val data = response.body()!!
Log.d(TAG, data.text)
withContext(Dispatchers.Main) {
}
}
}
}
}
And activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center|center_horizontal"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="0.4"
android:foregroundGravity="bottom"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:id="#+id/gen_new_fact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="150dp"
android:text="Generate Another Fact"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
I am running the latest stable version of Android Studio 4 on Windows 10
You need a reference to your button in your code. An example would be using findViewById to get a reference to the Button in your Kotlin code. It could look like this:
val button = findViewById<Button>(R.id.gen_new_fact);
button.setOnClickListener {
// Your code here.
}
I have a navigation header as bellow
I want to update TextView in nav_header.xml from MainActivity.kt by Kotlin
I want to update text of TextView(id=drawer_user_name) from MainActivity.kt
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="45dp"
android:background="#android:drawable/screen_background_light_transparent"
tools:listitem="#layout/item_list" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="200dp"
android:background="#4cAF50"
android:orientation="vertical"
android:layout_gravity="top"
android:paddingTop="35dp"
android:paddingLeft="15dp"
android:paddingBottom="15dp"
android:id="#+id/ddd">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:id="#+id/imageView"
android:layout_marginBottom="15dp"/>
<TextView
android:id="#+id/drawer_user_name" //--> i want to update this TextView from MainActivity.kt
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this our navigation drawer"
android:textColor="#FFFFFF" />
</LinearLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/colorPrimary"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:id="#+id/home_page_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat"
android:textColor="#FFFFFF" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is my content"/>
</LinearLayout>
</LinearLayout>
Please Help me,I really need a way to solve this problem
thank you
Babbab forces us to write stories here to allow us to post a question(It looks like your post is mostly code; please add some more details)
Edit
my MainActivity.kt
package com.example.myapplication
...
import android.view.LayoutInflater
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main_page.*
import kotlinx.android.synthetic.main.nav_header.view.*
class MainPageActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener,CustomAdapter.OnItemClickListener {
lateinit var toolbar:androidx.appcompat.widget.Toolbar
lateinit var drawerLayout: DrawerLayout
lateinit var navView:NavigationView
private var data = arrayListOf<UserChanel>()
private lateinit var myadapter:CustomAdapter
private val gson = Gson()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar =findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,0,0)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
// val userNameTextView = nav_view.findViewById<TextView>(R.id.drawer_user_name)
// userNameTextView.text = "SOME TEXT" // error
nav_view.drawer_user_name.text = "SOME TEXT" // Error
val recyclerview = findViewById<RecyclerView>(R.id.recycler_view)
// for insert line divider to recyclerview items
recyclerview.addItemDecoration(SimpleDividerItemDecoration(this)) // for insert line divider to recyclerview items
send_request()
val adapter = CustomAdapter(this,data,this)
recyclerview.layoutManager = LinearLayoutManager(this)
recyclerview.adapter = adapter
recyclerview.setHasFixedSize(true)
myadapter = adapter
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.nav_profile->{
Toast.makeText(this,"profile clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_message->{
Toast.makeText(this,"message clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_frinds->{
Toast.makeText(this,"frinds clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_update->{
Toast.makeText(this,"update clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_logout->{
Toast.makeText(this,"logout clicked",Toast.LENGTH_SHORT).show()
}
}
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
private fun send_request(){
...
}
override fun onItemClick(position: Int,adapter: CustomAdapter,v:View) {
...
}
}
You can find the target view from its parent in your activity like either of these ways:
nav_view.getHeaderView(0).findViewById<TextView>(R.id.drawer_user_name).text = "SOME TEXT"
or
import kotlinx.android.synthetic.main.nav_header.view.*
...
nav_view.getHeaderView(0).drawer_user_name.text = "SOME TEXT"
It's possible to access the header view using getheaderview(int index) according to the documentation. As you have one header view in the NavigationView, it is accessible by index 0.