I modified my code by referring to the "https://www.tutorialspoint.com/add-and-remove-views-in-android-dynamically-in-kotlin". There was no error in my code, but the view added when running in the app was not visible. I have no idea what the problem is. Please help me!!
Activity to which the view is added
package com.akj.callback
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import com.akj.callback.databinding.ActivitySearchSubject2Binding
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_search_subject_2.*
import kotlinx.android.synthetic.main.activity_search_subject_2.to_go_check
import kotlinx.android.synthetic.main.choiced_subject_list.*
import java.util.*
class search_subject_2 : AppCompatActivity() {
lateinit var binding3:ActivitySearchSubject2Binding
private var parentLinearLayout: LinearLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search_subject_2)
parentLinearLayout = findViewById(R.id.parent_linear_layout)
binding3= ActivitySearchSubject2Binding.inflate(layoutInflater)
setContentView(binding3.root)
userList3.onItemClickListener= AdapterView.OnItemClickListener{
parent, view, position, id ->
//parent?.getItemAtPosition(position).toString()
onAddField()
}
val user = arrayOf("abc", "bcd", "cde", "def", "efg")
val userAdapter: ArrayAdapter<String> = ArrayAdapter(
this,android.R.layout.simple_list_item_1,user)
to_go_check.setOnClickListener {
val intent6=Intent(this#search_subject_2,checkthesetting::class.java)
startActivity(intent6)
}
binding3.searchView3.setOnQueryTextListener(object :androidx.appcompat.widget.SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
binding3.searchView3.clearFocus()
if(user.contains(query)){
userAdapter.filter.filter(query)
}
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
if(newText==""){
}
else{
binding3.userList3.adapter=userAdapter
userAdapter.filter.filter(newText)
}
return false
}
})
}
fun onDelete(view: View) {
parentLinearLayout!!.removeView(view.parent as View)
}
fun onAddField() {
val inflater =
getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rowView: View = inflater.inflate(R.layout.choiced_subject_list, null)
parentLinearLayout!!.addView(rowView, parentLinearLayout!!.childCount - 1)
Toast.makeText(applicationContext,"fdsvkgjh", Toast.LENGTH_SHORT).show()
}
}
choiced_subject_list.xml
<?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="50dp"
android:orientation="horizontal">
<TextView
android:id="#+id/number_edit_text"
android:text="title_fdskhdsffsd"
android:textColor="#color/black"
android:textSize="30dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"
/>
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"
android:onClick="onDelete" />
</LinearLayout>
activity_search_subject_2.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=".search_subject_2">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.04" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.94" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.03" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.97" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.06" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.11" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.87" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/coner_search_bar"
android:theme="#style/AppSearchView"
app:layout_constraintBottom_toTopOf="#id/guideline6"
app:layout_constraintEnd_toEndOf="#id/guideline4"
app:layout_constraintStart_toStartOf="#id/guideline3"
app:layout_constraintTop_toBottomOf="#id/guideline5"
app:queryHint="강좌 검색"
app:iconifiedByDefault="false"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/to_go_check"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="#id/guideline3"
app:layout_constraintEnd_toStartOf="#id/guideline4"
app:layout_constraintTop_toBottomOf="#id/guideline1"
app:layout_constraintBottom_toTopOf="#id/guideline2"
app:layout_constraintVertical_bias="0.98"
android:text="확인"
android:textColor="#FFF"
android:textSize="20dp"
android:background="#drawable/coner_search_bar"
android:stateListAnimator="#null"
/>
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="#+id/to_go_check"
app:layout_constraintEnd_toStartOf="#id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline3"
>
<LinearLayout
android:id="#+id/linear_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/njklsfd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="전공검색" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="교양검색" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="교수검색" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="정렬" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="학점:3" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="학점:2" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:text="학점:1"
/>
</LinearLayout>
</HorizontalScrollView>
<ListView
android:id="#+id/userList3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline8"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline6"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:orientation="vertical"
android:id="#+id/parent_linear_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
app:layout_constraintBottom_toTopOf="#id/scrollView"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toEndOf="#id/guideline3"
app:layout_constraintTop_toBottomOf="#id/guideline8" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try this:
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rowView: View = inflater.inflate(R.layout.row_child_view, null, false)
Mainview.removeView(rowView)
Related
I am hoping that someone could please help me. I am creating an application using Kotlin. Mostly so far, so good, but I am having issues with one area.
The issue is with that 'distance.amountPicker.value' does not seem to be communicating with the layout. All of the other areas are working just fine i.e hikeName.text.toString(), etc, but I cannot get amountPicker to work.
I am not sure if the issue is with the import 'import kotlinx.android.synthetic.main.activity_hike.view.', as these seems to be a different import than the rest, which use 'import kotlinx.android.synthetic.main.activity_hike.'
If anyone could please guide me in the right direction, I'd really appreciate it.
Many thanks
Here is my code:
View:
package org.wit.hikingtrails.views.hike
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_hike.*
import kotlinx.android.synthetic.main.activity_hike.view.*
import org.wit.hikingtrails.R
import org.wit.hikingtrails.models.HikeModel
import org.wit.hikingtrails.views.BaseView
import readImageFromPath
class HikeView : BaseView()
{
lateinit var presenter: HikePresenter
var hike = HikeModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hike)
progressBar.max = 1000
amountPicker.minValue = 1
amountPicker.maxValue = 1000
init(toolbarAdd)
presenter = initPresenter (HikePresenter(this)) as HikePresenter
btnAdd.setOnClickListener{
presenter.doAddOrSave(
hikeName.text.toString(),
description.text.toString(),
if(difficultyLevel.checkedRadioButtonId == R.id.Intermediate)
"Intermediate" else "Hard",
**distance.amountPicker.value**
) }
btnDelete.setOnClickListener { presenter.doAddOrSave(hikeName.text.toString(), description.text.toString(), difficulty.text.toString(), distance.text.length) }
chooseImage.setOnClickListener { presenter.doSelectImage() }
hikeLocation.setOnClickListener { presenter.doSetLocation() }
btnDelete.setOnClickListener { presenter.doDelete() }
}
override fun showHike(hike: HikeModel) {
hikeName.setText(hike.name)
description.setText(hike.description)
distance.setText("Distance - "+hike.distance+"km")
difficulty.setText("Difficulty Level - "+hike.difficultyLevel)
hikeImage.setImageBitmap(readImageFromPath(this, hike.image.toString()))
if (hike.image != null) {
chooseImage.setText(R.string.change_hike_image)
}
btnAdd.setText(R.string.save_hike)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_hike, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item?.itemId) {
if (hikeName.text.toString().isEmpty()) {
R.id.item_cancel -> {
presenter.doCancel()
}
}
return super.onOptionsItemSelected(item)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (data != null) {
presenter.doActivityResult(requestCode, resultCode, data)
}
}
override fun onBackPressed() {
presenter.doCancel()
}
}
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="org.wit.hikingtrails.views.hike.HikeView">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:fitsSystemWindows="true"
app:elevation="0dip"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#color/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/appBarLayout"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/hikeName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="8dp"
android:autofillHints=""
android:hint="#string/hint_hikeName"
android:inputType="text"
android:maxLength="25"
android:maxLines="1"
android:padding="8dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
<EditText
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="#string/hint_hikeDescription"
android:inputType="text"
android:maxLength="25"
android:maxLines="1"
android:padding="8dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
<Button
android:id="#+id/chooseImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/colorAccent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="#string/button_addImage"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
<ImageView
android:id="#+id/hikeImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher" />
<Button
android:id="#+id/hikeLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/colorAccent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:stateListAnimator="#null"
android:text="#string/button_location"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
<TextView
android:id="#+id/hikeSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/donateTitle"
android:layout_alignEnd="#+id/donateTitle"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/hikeSubtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/difficultyLevel"
android:layout_width="328dp"
android:layout_height="wrap_content"
android:layout_above="#+id/progressBar"
android:layout_alignParentStart="true"
android:orientation="horizontal">
<RadioButton
android:id="#+id/Hard"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/hard" />
<RadioButton
android:id="#+id/Intermediate"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:checked="false"
android:text="#string/intermediate" />
</RadioGroup>
<NumberPicker
android:id="#+id/amountPicker"
android:layout_width="wrap_content"
android:layout_height="121dp"
android:layout_alignEnd="#+id/donateSubtitle"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="349dp"
android:layout_height="14dp"
android:layout_above="#+id/donateButton"
android:layout_alignEnd="#+id/donateSubtitle"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:indeterminate="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7" />
<TextView
android:id="#+id/distance"
android:layout_width="267dp"
android:layout_height="38dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/distance" />
<TextView
android:id="#+id/difficulty"
android:layout_width="265dp"
android:layout_height="38dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/difficulty" />
<Button
android:id="#+id/btnAdd"
android:layout_width="375dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/colorAccent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:stateListAnimator="#null"
android:text="#string/button_addHike"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/red"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:stateListAnimator="#null"
android:text="#string/button_deleteHike"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I am trying to implement view binding to one of my activity. The issue is I am not able to access the views and it shows unresolved reference.
Now when I am using tools:viewBindingIgnore="true"
then there are no issues with the views and I am able to access them but then I am getting unresolved symbol for the activity class.
activity_single_movie.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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"/>
<TextView
android:id="#+id/text_error"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Connection gone!!"
android:textColor="#color/white"
android:visibility="gone" />
<ScrollView
app:layout_constraintTop_toTopOf= "parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/movie_poster"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/placeholder_image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp" >
<TextView
android:id="#+id/movie_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="movie"
android:textStyle="bold"
android:textSize="18sp" />
<TextView
android:id="#+id/movie_tagline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub title"
android:textStyle="bold"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="movie info"
android:textStyle="bold"
android:textSize="14sp" />
//Layout for Release Date
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Release Date: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="2019"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Rating
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Rating: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="5/10"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Runtime
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Runtime: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_runtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="2hrs 13mins"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Budget
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Budget: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_budget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="$550Million"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for revenue
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Budget: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_revenue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="$550Million"
android:textSize="12sp"
style="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
In SingleMovie.kt
package com.example.movieapp.ui.single_movie_details
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.example.movieapp.R
import com.example.movieapp.data.api.POSTER_BASE_URL
import com.example.movieapp.data.api.TheMovieDBClient
import com.example.movieapp.data.api.TheMovieDBInterface
import com.example.movieapp.data.repository.NetworkState
import com.example.movieapp.data.vo.MovieDetails
import com.example.movieapp.databinding.ActivitySingleMovieBinding
import java.text.NumberFormat
import java.util.*
class SingleMovie : AppCompatActivity() {
private lateinit var viewModel: SingleMovieViewModel
private lateinit var movieRepository: MovieDetailsRepository
private lateinit var binding: ActivitySingleMovieBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySingleMovieBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
val movieId: Int = intent.getIntExtra("id", 1)
val apiService: TheMovieDBInterface = TheMovieDBClient.getClient()
movieRepository = MovieDetailsRepository(apiService)
viewModel = getViewModel(movieId)
viewModel.movieDetails.observe(this, Observer {
bindUi(it)
})
viewModel.movieDetailsNetworkState.observe(this, Observer {
binding.progress_bar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)
binding.text_error.setVisibility( if(it == NetworkState.ERROR) View.VISIBLE else View.GONE)
})
}
fun bindUi(it: MovieDetails){
binding.movie_title.setText(it.title)
binding.movie_tagline.setText(it.tagline)
binding.movie_release_date.setText(it.releaseDate)
binding.movie_rating.setText(it.voteAverage.toString())
binding.movie_runtime.setText(it.runtime.toString() + "minutes")
val formatCurrency: NumberFormat = NumberFormat.getCurrencyInstance(Locale.US)
binding.movie_budget.setText(formatCurrency.format(it.budget))
binding.movie_revenue.setText(formatCurrency.format(it.revenue))
//for poster of movie
val moviePosterURL: String = POSTER_BASE_URL + it.posterPath
Glide.with(this)
.load(moviePosterURL)
.into(binding.movie_poster)
}
private fun getViewModel(movieId: Int): SingleMovieViewModel {
return ViewModelProviders.of(this, object: ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return SingleMovieViewModel(movieRepository, movieId) as T
}
}) [SingleMovieViewModel::class.java]
}
}
Errors while using tools:viewBindingIgnore="true"
Unresolved reference: ActivitySingleMovieBinding
Errors without using tools:viewBindingIgnore="true"
Unresolved reference: progress_bar
and similar for all the views
View Binding converts underscores to camelCase, the correct syntax is binding.progressBar
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"/>
binding.progressBar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)
android studio 3.6
compileSdkVersion 29
buildToolsVersion "29.0.3"
implementation 'com.google.android.gms:play-services-maps:17.0.0'
here my layout
<?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="200dp"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/titleContainerLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:background="#drawable/bottom_border_bg"
android:orientation="vertical">
<TextView
android:id="#+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:text="Gaz station"
android:textColor="#android:color/black"
android:textSize="15sp" />
<TextView
android:id="#+id/stateTtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:text="#string/open"
android:textColor="#android:color/holo_green_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/agentInfoContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_border_bg"
android:orientation="vertical">
<LinearLayout
android:id="#+id/containerAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin">
<TextView
android:id="#+id/addressLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/address_colon" />
<TextView
android:id="#+id/addressValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/half_default_margin"
android:gravity="end" />
</LinearLayout>
<LinearLayout
android:id="#+id/containerWorkingHours"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin">
<TextView
android:id="#+id/workingHoursLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/working_hours_colon" />
<TextView
android:id="#+id/workingHoursValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/half_default_margin"
android:gravity="end"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/containerPhon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:layout_marginBottom="#dimen/half_default_margin">
<TextView
android:id="#+id/phoneLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/phone_colon" />
<TextView
android:id="#+id/phoneValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/half_default_margin"
android:gravity="end" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/containerServices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:layout_marginBottom="#dimen/half_default_margin"
android:orientation="vertical">
<TextView
android:id="#+id/servicesLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/services_colon" />
<GridLayout
android:id="#+id/gridLayoutServiceContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:orientation="horizontal"
android:useDefaultMargins="true">
</GridLayout>
</LinearLayout>
</LinearLayout>
</layout>
styles.xml
<style name="mapMarkerInfoItemLabelTextStyle">
<item name="android:textSize">15sp</item>
<item name="android:textColor">#android:color/black</item>
</style>
here activity:
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import md.qsystems.android.tango.client.databinding.MapActivityBinding
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
class MapActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
mMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
override fun getInfoWindow(marker: Marker): View? {
return null
}
// Important! Not work with "androidx.constraintlayout.widget.ConstraintLayout"
override fun getInfoContents(marker: Marker): View? {
val markerLatLng = marker.position
val selectGazStation =
gazStationsList.first { it.latitude == markerLatLng.latitude && it.longitude == markerLatLng.longitude }
Debug.d(TAG, "getInfoContents: selectGazStation = $selectGazStation")
val mapMarkerInfoContentLayoutBinding =
MapMarkerInfoContentLayoutBinding.inflate(LayoutInflater.from(applicationContext));
//mapMarkerInfoContentLayoutBinding.setVariable(BR.model, selectGazStation)
mapMarkerInfoContentLayoutBinding.addressValueTextView.setText(selectGazStation.address)
var workingHours = "24/24"
if (!selectGazStation.workingHours?.contains("NON-STOP", true)!!) {
workingHours = selectGazStation.workingHours!!
}
mapMarkerInfoContentLayoutBinding.workingHoursValueTextView.setText(workingHours)
mapMarkerInfoContentLayoutBinding.phoneValueTextView.setText(selectGazStation.phone)
mapMarkerInfoContentLayoutBinding.gridLayoutServiceContainer.removeAllViews()
return mapMarkerInfoContentLayoutBinding.root
}
})
and here result whan I click on different markers:
as you can see the info window change it's width? Why? I fix it width by 200dp
android:layout_width="200dp"
why it's not work?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/titleContainerLayout"
android:layout_width="200dp"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:background="#drawable/bottom_border_bg"
android:orientation="vertical">
I just replace the main LinearLayout width to the second one.
I am using ConstraintLayout as a parent and put recyclerview in it to populate the list. Suppose I have to give item count 5 to populate the list and run the code then the list is showing perfectly but the last item of the recyclerview is showing half and not fully visible, and scroll stops at there. I found a solution for this is if am giving height match_parent to the recyclerview then it works fine but then all other view is hidden behind the recyclerview.
If I use another parent view like LinearLayout or RelativeLayout, then recyclerview with height wrap_content works fine and all the list item are fully visible.
I have tried by 2 ways that work for me but I found that wrong programming practice
I give height match_parent to recyclerview and give top_margin to the recylerview so all the other UI item show.
I give padding_bottom to the recyclerview until all the list item position visible
My XML file "activity_per_day_sale.xml" is below.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/btn_back"
android:padding="5dp"
android:onClick="OnClickPerDaySale"
android:background="?attr/selectableItemBackground"
android:src="#drawable/icon_back_white"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:layout_centerInParent="true"
android:id="#+id/home_title_text"
android:textColor="#color/black"
android:visibility="gone"
android:fontFamily="#font/seguisb"
android:textSize="18sp"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SKIP"
android:padding="10dp"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="#+id/skip"
android:textColor="#color/black"
android:fontFamily="#font/seguisb"
android:textSize="18sp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="10dp"
android:text="Per Day Sale Details"
android:textColor="#color/yellow_app_logo_color"
android:fontFamily="#font/segoeuib"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/TV_signupScreenText"
android:layout_marginLeft="45dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rv_perday_sale"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="10dp"
app:layout_constraintTop_toBottomOf="#+id/TV_signupScreenText"/>
</android.support.constraint.ConstraintLayout>
Activity class is "PerDaySaleActivity.kt"
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import com.nq.NQManager.R
import com.nq.NQManager.utils.BaseActivity
import kotlinx.android.synthetic.main.activity_per_day_sale.*
class PerDaySaleActivity:BaseActivity(){
var adapterPerdaySale : AdapterPerdaySale? = null
companion object {
fun start(context: Context) {
val starter = Intent(context, PerDaySaleActivity::class.java)
context.startActivity(starter)
}
}
override fun getID(): Int {
return R.layout.activity_per_day_sale
}
override fun iniView(savedInstanceState: Bundle?) {
initViews()
}
fun OnClickPerDaySale(v: View){
when(v){
btn_back->{
finish()
}
}
}
fun initViews() {
adapterPerdaySale = AdapterPerdaySale( this)
rv_perday_sale.layoutManager = StaggeredGridLayoutManager(1, 1)
rv_perday_sale.adapter = adapterPerdaySale
}
private fun setUpRecyclerView() {
runOnUiThread { adapterPerdaySale!!.notifyDataSetChanged() }
}
My Adapter class "AdapterPerdaySale.kt"
mport android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.nq.NQManager.R
import kotlinx.android.synthetic.main.adapter_perday_sale.view.*
class AdapterPerdaySale(context: Context):
RecyclerView.Adapter<AdapterPerdaySale.MyViewHOlder>() {
private var ctx:Context?=context
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHOlder {
return MyViewHOlder(LayoutInflater.from(ctx).inflate(R.layout.adapter_perday_sale, p0, false))
}
override fun getItemCount(): Int {
return 5
}
override fun onBindViewHolder(holder: MyViewHOlder, position: Int) {
if (position==1){
holder.tv_date.text="26 June, 2019"
}else if(position==2){
holder.tv_date.text="27 June, 2019"
}
}
inner class MyViewHOlder(view: View) : RecyclerView.ViewHolder(view) {
val tv_date=view.tv_date
}
}
My Adapter xml file "adapter_perday_sale.xml"
<?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" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:background="#color/black">
<TextView
android:id="#+id/tv_date"
android:layout_below="#+id/txt_today"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"
android:text="25 June, 2019"
android:textSize="23sp"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="8dp" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout1"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/tv_date">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:text="Orders"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginTop="0dp"
android:text="50"
android:layout_centerHorizontal="true"
android:textColor="#color/order_history_txt_color"
android:fontFamily="#font/segoeui"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:text="Revenue"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginTop="0dp"
android:text="£550"
android:layout_centerHorizontal="true"
android:textColor="#color/order_history_txt_color"
android:fontFamily="#font/segoeui"/>
</LinearLayout>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="#color/gray"
android:id="#+id/view_1"
app:layout_constraintTop_toBottomOf="#+id/linearLayout1"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
</android.support.constraint.ConstraintLayout>
Add this app:layout_constraintBottom_toBottomOf="parent" property to your recyclerview
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="#+id/btn_back"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?attr/selectableItemBackground"
android:onClick="OnClickPerDaySale"
android:padding="5dp"
android:src="#drawable/ic_background" />
<TextView
android:id="#+id/home_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Signup"
android:textColor="#000"
android:textSize="18sp"
android:visibility="gone" />
<TextView
android:id="#+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:text="SKIP"
android:textColor="#000"
android:textSize="18sp"
android:visibility="gone" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="#+id/TV_signupScreenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="10dp"
android:text="Per Day Sale Details"
android:textColor="#AD4E4E"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_perday_sale"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TV_signupScreenText" />
</androidx.constraintlayout.widget.ConstraintLayout>
In my fragment, I have two linear layout with the same tag "result". Inside theses linear layouts, I have another layout with the tag "toggle" and a button with the tag "toggleButton".
I want that : when I click on my button, it toggle the linear layout "toggle" of its linear layout parent "result"
But when I do this :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val root = rootLayoutResultSearch
root.findViewWithTag<Button>("toToggleButton")
val buttonToggle = view.findViewWithTag<Button>("toToggleButton")
buttonToggle.setOnClickListener{
Log.i(TAG, "click")
val toggle = view.findViewWithTag<LinearLayout>("toToggle")
if(toggle.visibility == View.GONE){
toggle.visibility = View.VISIBLE
}else{
toggle.visibility = View.GONE
}
}
}
it's working only for my first linear layout "result". When I click on my second button, it's not doing anything
Here the xml of the layouts "result" (I only post the first, the second is the exact same one)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="result"
android:contentDescription="result">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="35dp"
android:background="#drawable/whit_bg_and_shadow"
android:tag="visibleNotChangeable"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche1"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche1"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche2"
android:textFontWeight="500"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche2"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/jourCircuRecherche" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/fillJourCircuRecherche" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bus" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:background="#drawable/dark_blue_rectangle"
android:text="3"
android:textColor="#color/colorWhite" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/horaire" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:text="#string/fillTempsRecherche" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="35dp"
android:background="#drawable/gray_bg"
android:visibility="gone"
android:tag="toToggle"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche1"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche1"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche2"
android:textFontWeight="500"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche2"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/jourCircuRecherche" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/fillJourCircuRecherche" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bus" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:background="#drawable/dark_blue_rectangle"
android:text="3"
android:textColor="#color/colorWhite" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/horaire" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:text="#string/fillTempsRecherche" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/ToggleSearchResult"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/arrow_down_blue_circle"
android:layout_gravity="center"
android:layout_marginTop="-20dp"
android:tag="toToggleButton"
/>
</LinearLayout>
Okay, I've thrown together a quick idea of what I think you want to accomplish. It excludes all the error and consistency checking one would normally want to do.
package com.example.toggler
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val list = arrayListOf<View>()
root_View.findViewsWithText(list, "toToggleButton", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION)
list.forEach { button ->
button.setOnClickListener { v: View ->
val viewParent = v.parent
if (viewParent is LinearLayout) {
val taggedView = viewParent.findViewWithTag<View>("toggle")
taggedView.visibility = when {
taggedView.visibility == View.GONE -> View.VISIBLE
else -> View.GONE
}
}
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root_View"
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="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:contentDescription="toToggleButton"
android:tag="toToggleButton"
android:text="toggle"
tools:ignore="HardcodedText" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#F44336"
android:tag="toggle" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:contentDescription="toToggleButton"
android:tag="toToggleButton"
android:text="toggle"
tools:ignore="HardcodedText" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#9C27B0"
android:tag="toggle" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:contentDescription="toToggleButton"
android:tag="toToggleButton"
android:text="toggle"
tools:ignore="HardcodedText" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFEB3B"
android:tag="toggle" />
</LinearLayout>
</LinearLayout>
[EDIT]
Or, if you prefer to use tags:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
root_View.findViewsWithTag("toToggleButton").forEach { button ->
button.setOnClickListener { v: View ->
val viewParent = v.parent
if (viewParent is LinearLayout) {
val taggedView = viewParent.findViewWithTag<View>("toggle")
taggedView.visibility = when {
taggedView.visibility == View.GONE -> View.VISIBLE
else -> View.GONE
}
}
}
}
}
}
private fun ViewGroup.findViewsWithTag(tag: String): Sequence<View> {
return sequence {
for (index in 0 until childCount) {
val child = getChildAt(index)
when (child) {
is ViewGroup -> yieldAll(child.findViewsWithTag(tag))
is View -> if (child.tag == tag) yield(child)
}
}
}
}