I am trying to make a drawing app but when I am setting the brush size seek and try to make a text view to show its size it is always returning null I tried multiple ways but it didn't succeed so please can any one help me fix the poblems in this code so the text shows the seekbar progress text
class MainActivity : AppCompatActivity() {
private var ibBrush: ImageView? = null
private var tvBrushSize: TextView? = null
private var sbBrushSize: SeekBar?= null
override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ibBrush = findViewById(R.id.ib_brush)
ibBrush?.setOnClickListener{
changeBrushSize()
}
}
private fun changeBrushSize(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
val brushDialog = Dialog(this)
brushDialog.setContentView(R.layout.brush_size_dialog)
tvBrushSize = findViewById(R.id.tv_brush_size)
sbBrushSize = findViewById(R.id.sb_brush_size)
tvBrushSize?.text = sbBrushSize?.progress.toString()
brushDialog.show()
}
}
Why tvBrushSize is returning null while I am setting it to findViewById() to the correct id in xml
*here is the 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="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/brush_size"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/linear"/>
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<SeekBar
android:id="#+id/sb_brush_size"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10"
android:max="30"
/>
<TextView
android:id="#+id/tv_brush_size"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/_0" />
</LinearLayout>
</LinearLayout>
You should inflate the dialog view first using LayoutInflater.inflate(layoutId) and then use view.findViewById(id) like this snippet:
private fun changeBrushSize(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
val brushDialog = Dialog(this)
val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
sbBrushSize = dialogView.findViewById(R.id.sb_brush_size)
tvBrushSize?.text = sbBrushSize?.progress.toString()
brushDialog.show()
}
you need to bind views from dialog, so manage it like this;
brushDialog.setContentView(R.layout.brush_size_dialog)
val dialogView = layoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
the dialog with your view is not yet existed, when you are trying to refer it with findViewById()
You can inflate view first, then access it as follows
val brushDialog = Dialog(this)
val view = layoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(view)
tvBrushSize = view.findViewById(R.id.tv_brush_size)
sbBrushSize = view.findViewById(R.id.sb_brush_size)
tvBrushSize?.text = sbBrushSize?.progress.toString()
brushDialog.show()
or call brushDialog.show() first and then access views with findViewById()
that also should work
You should inflate the view first
val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
then try to use
dialogView.findViewById(...)
Related
everyone. I'm trying to display a line chart in a full-screen activity. Unfortunately I can't do that. I always get to see my activity_main.xml layout. I also have my LineChart in it, within a CardView. Everything works fine in this activity. But as soon as I switch to activity_details_vitali.xml via a button, I don't see a line chart, just my main_activity. I think I have a wrong binding here. However, I can't find the error
class DetailsVitali : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details_vitali)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setLineChartData()
}
private fun setLineChartData() {
val linevalues = ArrayList<Entry>()
linevalues.add(Entry(20f, 0.0F))
linevalues.add(Entry(30f, 3.0F))
linevalues.add(Entry(40f, 2.0F))
linevalues.add(Entry(50f, 1.0F))
linevalues.add(Entry(60f, 8.0F))
linevalues.add(Entry(70f, 10.0F))
linevalues.add(Entry(80f, 1.0F))
linevalues.add(Entry(90f, 2.0F))
linevalues.add(Entry(100f, 5.0F))
linevalues.add(Entry(110f, 1.0F))
linevalues.add(Entry(120f, 20.0F))
linevalues.add(Entry(130f, 40.0F))
linevalues.add(Entry(140f, 50.0F))
val linedataset = LineDataSet(linevalues, "First")
//We add features to our chart
linedataset.color = resources.getColor(R.color.purple_200)
linedataset.circleRadius = 5f
linedataset.setDrawFilled(true)
linedataset.valueTextSize = 10F
linedataset.fillColor = resources.getColor(R.color.purple_500)
linedataset.setMode(LineDataSet.Mode.CUBIC_BEZIER);
//We connect our data to the UI Screen
val data = LineData(linedataset)
binding.getTheGraph.data = data
binding.getTheGraph.setBackgroundColor(resources.getColor(R.color.white))
binding.getTheGraph.animateXY(2000, 2000, Easing.EaseInCubic)
}
layout
<?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="horizontal"
tools:context=".DetailsVitali">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
app:cardCornerRadius="10dp"
android:elevation="20dp"
android:layout_gravity="center_horizontal">
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/graphDetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
EDIT
And I also don't have access to the ID of my LineChart in the DetailsVitali.class. binding.ID.data = data is the ID is from the activity_main.xml.
From your comment that your desired layout is defined in activity_details_vitali.xml, you should be loading ActivityDetailsVitaliBinding, not ActivityMainBinding.
And as mentioned by #ARiF, you can remove the first call to setContentView since you are overwriting it with the second call to setContentView.
open class BaseActivity : AppCompatActivity() {
private var doubleBackToExitPressedOnce = false
private lateinit var mProgressDialog: Dialog
override fun onCreate(savedInstanceState: Bundle?) {
val binding = ActivityBaseBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
}
fun showProgressDialog(text: String) {
mProgressDialog = Dialog(this)
mProgressDialog.setContentView(R.layout.dialog_progress)
mProgressDialog.tv_progress_text.text = text
mProgressDialog.show()
}
}
//This is my XML file for custom Dialog
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/tv_progress_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="#string/pleaseWait"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/progressBar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my code(kotlin) and im using view binding. I'm not able to access view objects from custom dialog xml file in function showProgressDialog. However, i have tried to make a binding instance for custom dialog in fun showProgressDialog but that is also not working. Can anyone tell me, what is the problem and how i should access the view objects form custom Dialog xml file. setContentView is also of no use.
For using view binding with your custom dialog, you have to inflate your custom dialog with its generated methods, you cannot directly use it.
As you haven't written the name of your custom dialog layout file, I assume it to be customDialog.xml. Now you can inflate and show this custom dialog using this.
fun showProgressDialog(text: String) {
mProgressDialog = Dialog(this)
val customDialogBinding = CustomDialogBinding.inflate(layoutInflater, binding.root, false)
mProgressDialog.setContentView(customDialogBinding.root)
customDialogBinding.tv_progress_text.text = text
mProgressDialog.show()
}
I've created a basic layout in Android in my activity_main.xml for what will eventually be a custom view for a button. It is a relative view with three circles stacked on top of eachother inside it aligned in the centre. The circles change size when the user interacts with them.
I now want to replicate the whole thing purely in Kotlin code, as it will be loaded using data in a database.
I can't figure it out. Heres the manifest...
<?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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/myContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/button3"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
app:srcCompat="#drawable/circle"
app:tint="#E60505" />
<ImageView
android:id="#+id/button2"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
app:srcCompat="#drawable/circle"
app:tint="#FFB700" />
<ImageView
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="false"
android:layout_centerInParent="true"
app:srcCompat="#drawable/circle" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Then in Kotlin, all I can find about is LayoutParams for width and height, but where do I set all this stuff for the Relative Layout?
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"
which I presume I want to do something like:
class MyContainer : RelativeLayout () {
var button1 = MyButtonClass(someArgs)
var button2 = MyButtonClass(someArgs)
var button3 = MyButtonClass(someArgs)
init {
/// can't find out how to do this bit in Kotlin...
///something like....
this.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
this.layoutConstraintBottomToBottomOf = PARENT
this.layoutConstraintHorizontalBias = 0.5
///etc...
//then add the circle buttons
this.addView(button3)
this.addView(button2)
this.addView(button1)
}
}
Am I even vagueley on the right track? Any pointers would be really appreciated.
EDIT: Following first answer, I can set layout attributes, but my app is just blank when i try to reproduce my original manifest tree in code. Have I applied this correctly?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val appContainer = findViewById<ConstraintLayout>(R.id.appContainer)
var buttonContainer = ButtonContainer(this.applicationContext)
appContainer.addView(buttonContainer)
}
}
class ButtonContainer (context: Context) : RelativeLayout(context) {
var button1 = CustomButton(context)
val buttonLayoutParams by lazy { ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
startToStart = ConstraintLayout.LayoutParams.PARENT_ID
topToTop = ConstraintLayout.LayoutParams.PARENT_ID
endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
horizontalBias = 0.5F
}
}
override fun onFinishInflate() {
super.onFinishInflate()
this.layoutParams = buttonLayoutParams
this.addView(button1)
}
}
class CustomButton(context: Context) : ImageView(context) {
val layoutParams by lazy { RelativeLayout.LayoutParams(60, 60).apply {
addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
}
}
override fun onFinishInflate() {
super.onFinishInflate()
setLayoutParams(layoutParams)
setBackgroundResource(squibcircle)
}
}
You need to cast LayoutParams as ConstraintLayout.LayoutParams to set those properties like below
class ButtonContainer(context: Context) : RelativeLayout(context) {
var button1 = CustomButton(context)
val buttonLayoutParams by lazy { ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
startToStart = ConstraintLayout.LayoutParams.PARENT_ID
topToTop = ConstraintLayout.LayoutParams.PARENT_ID
endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
horizontalBias = 0.5F
}
}
override fun onFinishInflate() {
this.layoutParams = buttonLayoutParams
this.addView(button1)
}
}
I am using tab layout with custom views.
The issues I am facing are the following -
1) The text is being cut off with small screen devices as you can see here -
small device -
big device -
2) the second issue I am facing is as you can see, the background does not go wrap content on "Chats" tabs. It does through wrap the "Market" tab title as it is longer. I need it to wrap quallity with short and long texts.
here is my xml -
<androidx.appcompat.widget.Toolbar
android:id="#+id/activity_dashboard_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="#color/transparent">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:src="#drawable/user_default_image"
app:civ_border_color="#color/black"
app:civ_border_width="1dp" />
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="#+id/activity_dashboard_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/activity_dashboard_toolbar"
app:tabIndicator="#null" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/activity_dashboard_viewpager"
android:layout_width="match_parent"
android:layout_height="600dp"
app:layout_constraintTop_toBottomOf="#+id/activity_dashboard_tablayout" />
private fun initTabLayoutAndViewPager() {
setSupportActionBar(toolbar)
supportActionBar?.title = null
viewpager.adapter = DashboardViewPagerAdapter(this)
TabLayoutMediator(tabLayout, viewpager, TabLayoutMediator.TabConfigurationStrategy { _, _ -> }).attach()
val chatsView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
val callsView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
val walletView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
val marketView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
(chatsView as TextView).text = pageTitles[0]
(callsView as TextView).text = pageTitles[1]
(walletView as TextView).text = pageTitles[2]
(marketView as TextView).text = pageTitles[3]
chatTextView = chatsView
callsTextView = callsView
walletTextView = walletView
marketTextView = marketView
tabLayout.getTabAt(0)?.customView = chatTextView
tabLayout.getTabAt(1)?.customView = callsTextView
tabLayout.getTabAt(2)?.customView = walletTextView
tabLayout.getTabAt(3)?.customView = marketTextView
viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
when(position) {
DashboardTabs.CHATS.type -> {
ViewPagerUtils.setSelectedTab(chatTextView, callsView, walletView, marketView)
}
DashboardTabs.CALLS.type -> {
ViewPagerUtils.setSelectedTab(callsView, chatTextView, walletView, marketView)
}
DashboardTabs.WALLET.type -> {
ViewPagerUtils.setSelectedTab(walletView, callsView, chatTextView, marketView)
}
DashboardTabs.MARKET.type -> {
ViewPagerUtils.setSelectedTab(marketView, callsView, walletView, chatTextView)
}
}
}
})
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_medium"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textStyle="bold"
android:textSize="18sp"
android:maxLines="1"
tools:text="Chats">
</TextView>
edit -
After resolving the issue with the answer provided here I am stuck a gravity issue -
how can I make all tabs in the center? tried both gravity and layout_gravity and none helped me.
make tab layout mode scrollable app:tabMode="scrollable" or app:tabMode="auto" like below
<com.google.android.material.tabs.TabLayout
android:id="#+id/activity_dashboard_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:layout_constraintTop_toBottomOf="#+id/activity_dashboard_toolbar"
app:tabIndicator="#null" />
I need to draw dynamic buttons inside a foreach loop that retrieve data from my anko sqlite, the foreach only enter once and breaks and only draw one button in my layout, what I doing wrong? my code is this:
fun loadZones (ctx: Context, update: String, view: View, layout: LinearLayout) {
val zonesParser = rowParser{idzone: Int, zone: String -> Pair(idzone, zone)}
for (it in ctx.database.use {
select("tableplan")
.distinct()
.column("idzone")
.column("zone")
.orderBy("zone")
.parseList(zonesParser)
}) {
val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
val btnZone = layoutInflater.inflate(R.layout.zones_item, null) as MaterialButton
btnZone.text = it.second
btnZone.id = it.first
layout.addView(btnZone, layoutParams)
Log.e("PAIR", "FIN DEL CICLO")
continue
}
}
The data that retrieves from my query is this:
(2, LARRY)
(1, MADISON)
That's my activity, I need to draw the buttons in "lytZonesButtons" id
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".TablePlanFragment">
<com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:elevation="2dp"
tools:targetApi="lollipop" app:liftOnScroll="true">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarTablePlan"
style="#style/com.madison.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="#string/table_title_module">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal"
android:background="#color/orangeLighter"
android:gravity="center_vertical"
android:padding="5dp" android:id="#+id/lytZonesButtons" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="112dp"
android:padding="5dp">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc_tableplan"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
and that's my button template that I called "zones_item":
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" style="#style/com.madison.AppButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle2"
tools:text="MADISON"
tools:targetApi="lollipop"
android:layout_margin="5dp"
/>
EDIT: I found the solution!
I don't now why my layout instance in the twice iteration of my loop throws NullPointerException but not shows in the log cat, my solution was put the loop code in onCreateView function, this is the code:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.activity_tableplan, container, false)
val iActivity = (activity as AppCompatActivity)
iActivity.setSupportActionBar(view.toolbarTablePlan)
iActivity.supportActionBar?.setDisplayShowTitleEnabled(true)
// view.rc_tableplan.setHasFixedSize(true)
// val gridLayoutManager = GridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false)
// view.rc_tableplan.layoutManager = gridLayoutManager
val response = loadTablePlan(this.context!!, "no")
if (response.trim().toUpperCase() == "SUCCESS") {
val zonesParser = rowParser{idzone: Int, zone: String -> Pair(idzone, zone)}
for (zone in this.context!!.database.use {
select("tableplan")
.distinct()
.column("idzone")
.column("zone")
.orderBy("zone")
.parseList(zonesParser)
}) {
val layout:LinearLayout = view.lytZonesButtons
layout.let {
val btnZone = layoutInflater.inflate(R.layout.zones_item, layout, false) as MaterialButton
btnZone.text = zone.second
btnZone.id = zone.first
btnZone.requestLayout()
layout.addView(btnZone)
Log.e("PAIR", "FIN DEL CICLO")
}
}
}
return view
}
Thanks a lot for all people that tried help me, some admin can close my question please.
The hint is only one button is showing. Your trying to inflate the same view twice in the same spot.
You need to add an empty linearlayout in your xml. And in your loop change the buttonz..
var btnZone = findViewById(R.layout.btnZone)
button.text = "Pair"
btnZone.addView(button, layoutParams)
That's not the exact code (and probably not even the right syntax) but it shows you how you need to modify your loop.
Basicly you were attempting to inflate the same instance of the same view. When really your not inflating any views this way your just adding views.
Note
If you have a linearlayout in your xml when you add another button view to it it will add it below it. If you set the layout orientation to horizontal the button view then gets added beside the other one.
here's a link to an example.
Sorry I would make sure my code matched your code and variables with proper syntax but I am at work.