I am new to Kotlin programming. I didn't have any problems in other layouts, but now I need to use relativeLayout. I will create hundreds of buttons and their positions will be relative to each other I have to do this programmatically. I was able to find java examples but I couldn't find many resources in Kotlin because addRule is not here. Can you give me a small example of this?
Thanks in advance.
1 Answer
Thanks for ansver. I solved the addRule problem. now i see only one button at start instead of 1000 button, i think i have problem assigning id. Here is my simple code. i just want programmatically add years at top line between 1000-2000. After that, I will add the beginning and end of an event in 1378 to a bottom line as button. so i can assign dates easily, i chose relativeLayout for this.
val buttonParam = LayoutParams( 100, 100 ) var counter:Int=1000 while (true) { var btnId:Int=counter val myButton = Button(this) myButton.id=btnId buttonParam.addRule(RelativeLayout.RIGHT_OF, btnId-1) myButton.text = btnId.toString() myButton.layoutParams = buttonParam binding.rl.addView(myButton) counter=counter +1 if(counter==2000) break }
Edit: İ solve my problem. the problem is i create 1 buttonParam. Now i am creating parameter for every buttons.
addRuleis also there. As a matter of fact, most (or even all) Android classes that you refer to in your kotlin code are actually Java classes. There's no difference in what's available in kotlin and java. Maybe show your code so we might see what's going wrong