The problem is that when the soft keyboard appears, it overlaps the recyclerView. But I want my soft keyboard just push recycler to the top, to be above the keyboard, like with windowSoftInputMode="adjustPan".
But adjustPan is not the option because I need my tool bar.
I have tried adjustResize already, but no luck.
Main activity 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<include
layout="#layout/toolbar_layout"
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/recyc"
app:layout_constraintBottom_toTopOf="#+id/message_edit_text"
app:layout_constraintTop_toBottomOf="#+id/toolbar_layout"/>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/message_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textShortMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter item 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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my MainActivity code, there is the only onCreate method
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyc.layoutManager = LinearLayoutManager(this)
val adapter = MyAdapter(
arrayOf("sd","sd2","sd2","sd2","sd2","s2d","s2d","sd",
"sd2","s2d","s2d","sd5","s8d","67sd","s76d")
)
recyc.adapter = adapter
}
Here is my adapter class
class MyAdapter(private val myDataset: Array<String>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val view: View) :
RecyclerView.ViewHolder(view)
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): MyAdapter.MyViewHolder {
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.container, parent, false)
return MyViewHolder(textView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int)
{ holder.itemView.textView.text = myDataset[position] }
override fun getItemCount() = myDataset.size
}
Related
I'm trying to display a list of files and folders using a RecyclerView.
Here is the Adapter and the ViewHolder :
class FilesRecyclerViewAdapter(private val files: Array<File>) : RecyclerView.Adapter<FilesRecyclerViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.fragment_file_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = files[position]
holder.icon.setImageResource(if (item.isDirectory) R.drawable.folder else R.drawable.file)
holder.fileName.text = item.name
}
override fun getItemCount(): Int = files.size
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val icon: ImageView = view.findViewById(R.id.icon)
val fileName: TextView = view.findViewById(R.id.fileName)
}
}
The layout for a single item in the list (R.layout.fragment_file_item) :
<?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:id="#+id/fileItem"
android:layout_width="match_parent"
android:layout_height="#dimen/file_item_height">
<androidx.cardview.widget.CardView
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"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_foreground" />
<TextView
android:id="#+id/fileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Element"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/icon"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
And this is how it's included into the 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/fileSystem"
android:layout_width="match_parent"
android:layout_height="match_parent"
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>
Finally the code in the onCreate function of the main activity :
private lateinit var fileSystem: RecyclerView
// Later in the code
fileSystem = findViewById(R.id.fileSystem)
fileSystem.layoutManager = LinearLayoutManager(this)
fileSystem.adapter = FilesRecyclerViewAdapter(Environment.getExternalStorageDirectory().listFiles())
I expected to have a list of items with the icon on the left and the name of the folder slighly to the right like on the item layout preview :
Instead, I have this mess :
What did I do wrong ?
app:layout_constraintHorizontal_bias="0.5" makes your RecyclerView start on 50% of view.
Remove it, if you dont need them
I want to use databinding for recyclerview. I created a model with BindignAdapter annotation. But after that I don't know what should I do and I didn't find any solution.
I want to set some data for recyclerview adapter and set the adapter as recyclerview adapter and show data in the recyclerview.
This is my MainActivity XML file(This line has an error:android:recyclerUser="#{dog.recyclerViewBinder()}"):
<?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>
<variable
name="dog"
type="com.hooman.databinding.DogModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:recyclerUser="#{dog.recyclerViewBinder()}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
This is my model class:
class DogModel(var dogName:String, var dogImage:String?, var dogBreed:String,var context: Context){
#BindingAdapter("android:loadImage")
fun loadImage(imageView: ImageView,dogImage:String?){
Picasso.get().load(dogImage).into(imageView)
}
#BindingAdapter("android:recyclerUser")
fun recyclerViewBinder(recyclerView: RecyclerView,dogList: MutableList<DogModel>){
var dogAdapter = DogAdapter(dogList)
recyclerView.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
recyclerView.adapter = dogAdapter
}
}
This is recyclerview's adapter:
class DogAdapter(var dogList:List<DogModel>): RecyclerView.Adapter<DogAdapter.DogViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DogViewHolder {
val view = DataBindingUtil.inflate<RvDogItemBinding>(LayoutInflater.from(parent.context),R.layout.rv_dog_item,parent,false)
return DogViewHolder(view)
}
override fun onBindViewHolder(holder: DogViewHolder, position: Int) {
holder.view.dog = dogList[position]
holder.view.executePendingBindings()
}
override fun getItemCount(): Int {
return dogList.size
}
inner class DogViewHolder(var view: RvDogItemBinding) : RecyclerView.ViewHolder(view.root)
}
This is rv_dog_item.xml file:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="dog"
type="com.hooman.databinding.DogModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
app:cardCornerRadius="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/img_dog"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:loadImage="#{dog.dogImage}"/>
<TextView
android:id="#+id/txt_dogName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#id/img_dog"
app:layout_constraintEnd_toStartOf="#id/img_dog"
android:textSize="22sp"
android:padding="10dp"
android:textColor="#color/black"
android:text="#{dog.dogName}"/>
<TextView
android:id="#+id/txt_dogBreed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/txt_dogName"
app:layout_constraintStart_toStartOf="#id/txt_dogName"
app:layout_constraintEnd_toEndOf="#id/txt_dogName"
android:layout_marginTop="32dp"
android:text="#{dog.dogBreed}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
And this is mainActivity class:
class MainActivity : AppCompatActivity() {
lateinit var binding:ActivityMainBinding
lateinit var adapter:DogAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
var model = DogModel("Foo Dog",null,"Husky",this)
binding.dog = model
}
}
So what I should I do next and what did I make a mistake?
The RecyclerView shows simple items. Each item has one TextView inside FrameLayout. TextView has no any paddings, but in some cases they appears unexpectedly. The picture below shows that the padding appears immediately after opening the keyboard. The padding appears only if the text contains newline characters. How to fix it ?
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var adapter : MyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView : RecyclerView = findViewById(R.id.recyclerView)
val button : Button = findViewById(R.id.button)
adapter = MyAdapter();
recyclerView.adapter = adapter;
button.setOnClickListener {
adapter.notifyDataSetChanged()
}
}
}
class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView;
init {
textView = view.findViewById(R.id.txt)
}
}
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
private val data = listOf("Text without \\n ", "multiline\ntext")
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
MyViewHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false))
override fun getItemCount() = data.size;
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = data[position]
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#android:color/holo_blue_light"
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="notifyDataSetChanged"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:id="#+id/button"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="#+id/button"
android:id="#+id/recyclerView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
app:layout_constraintStart_toEndOf="#+id/button" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I`m new here and need help.
This my code.
Activity
class ProfileActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
var adapter: PagerAdapter = IntroSliderAdapter(applicationContext)
viewpager.adapter = adapter
}
Adapter
class IntroSliderAdapter(var context: Context) : PagerAdapter() {
lateinit var inflater: LayoutInflater
var texts: Array<Int> = arrayOf(R.string.intro_text1,R.string.intro_text2,R.string.intro_text3)
override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object` as ConstraintLayout
override fun getCount(): Int = texts.size
override fun instantiateItem(container: ViewGroup, position: Int): Any {
inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var view: View = inflater.inflate(R.layout.intro_slider, container, false)
Log.i("INFO", "View: " + view.toString())
Log.i("INFO", "Container:" + container.toString())
container!!.addView(view)
return view
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container!!.removeView(`object` as ConstraintLayout)
}
activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/profileContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context=".ProfileActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
slider layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:id="#+id/introLayout">
<ImageView
android:id="#+id/mainLogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:contentDescription="#string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/introText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
...
</android.support.constraint.ConstraintLayout>
How I understood inflater.inflate(R.layout.intro_slider, container, false) must push intro_slider.xml content to view, but when I run my application I see just empty slides. Plz, help me understand why it's happening.
In my case problem was in complex of mistakes. But main problem was: not correct filled xml layout, for example, my logo image not been visible because app:srcCompat must be android:src and so on..
Thanks Spritzig for help!
I have a simple question , I have a RecyclerView with 10 items, an Horizontal Scroll with paging Activated and my items appears in the middle like this :
What do I want now it's to have this. As you can see the next item is visible on the screen :
Here is my xml file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<ImageView
android:id="#+id/square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:src="#drawable/square_elem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
My MainActivity :
class MainActivity : AppCompatActivity() {
{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerInit()
}
fun recyclerInit()
{
val list : ArrayList<String> = ArrayList()
for (i in 1..10) {
list.add("$i")
}
recyclerview.layoutManager = LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false)
recyclerview.setHasFixedSize(true)
recyclerview.adapter = RecyclerAdapter(list)
val snapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(recyclerview)
}
}
And This is my Adapter :
class RecyclerAdapter(val list: ArrayList<String>) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>()
{
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate((R.layout.box_elem), parent, false)
return ViewHolder(view)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItemViewType(position: Int): Int {
return position
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.image.setImageResource(R.drawable.original_box)
holder.itemView.setOnClickListener(View.OnClickListener {
removeElemAt(position);
})
}
fun removeElemAt(position: Int) {
list.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, list.size)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
var image: ImageView = itemView.findViewById(R.id.box_elem)
}
}
Try this code
RecyclerView xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
YourItem xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl"
android:layout_width="YourOption"
android:layout_height="YourOption">
<ImageView
android:id="#+id/img"
android:layout_width="YourOption"
android:layout_height="YourOption"
android:gravity="YourOption"
android:scaleType="YourOption"
/>
</RelativeLayout>
change your layout with this code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:src="#drawable/square_elem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>