Relative layout params in kotlin
13,189
Solution 1
It work for me::
var param: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,100);
param.setMargins(500,12,0,0)
val i:Int=1
var temp : TextView = TextView(this);
temp.id=i
temp.layoutParams = param ;
temp.text= "TextView\nadfadsfasdfadsfasdf\nadfadsfasdfa\nadfadsfasdf"+i
Log.e(ContentValues.TAG, "onCreate: " + i)
See here ouput Generated by it:
Thanks and let me know if you need more.
Solution 2
lateinit var r1: RelativeLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
r1= RelativeLayout(this)
var rlp : RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
}
Solution 3
var view: RelativeLayout =RelativeLayout(this)
view.layoutParams= RelativeLayout.LayoutParams(25,25)
Try This one ...
Related videos on Youtube
Author by
Divyesh Kalotra
Hello i am Divyesh and i am Android developer .I am not going to office for Work since i left the java developer job. I used to came Stackoverflow and know what kind of problems people facing and i tried to solve of them.i just love coding and learn new things and making them applicable to our real world.thank you
Updated on June 04, 2022Comments
-
Divyesh Kalotra almost 2 years
I am trying to set
relative layout params
programmatically. I can setparams
using java but not know how to accomplish using kotlin. So what I have tried is as follows :MainActivity class
class MainActivity : AppCompatActivity() { lateinit var context : Context override fun onCreate(savedInstanceState: android.os.Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) context=this asdasd.text="finaly done"; var param:RelativeLayout.LayoutParams =//don't know what to do here val i:Int=1 var temp :TextView = TextView(this); temp.id=i temp.layoutParams = param ; temp.text= "TextView"+i Log.e(ContentValues.TAG, "onCreate: " + i) } }
Any help is appreciated!
-
Pavneet_Singh over 6 yearsprobably
=RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
thenparam.addRule(...)
-
Divyesh Kalotra over 6 yearsi know but in kotlin how can i declare it .. thats what i want
-
Pavneet_Singh over 6 yearsdeclare it where?, you can show your java code the way you want it in kotlin
-
Divyesh Kalotra over 6 yearssorry friend i am directly coding in kotlin not in java .
-
Pavneet_Singh over 6 years
how can i declare it .. thats what i want
please explain? -
Divyesh Kalotra over 6 yearsmeans i want declare
param
variable asLayoutparams
. and want to set intextview
-
Divyesh Kalotra over 6 yearsLet us continue this discussion in chat.
-
-
Ankit Patidar over 6 yearsThanku soo much divyesh
-
SMBiggs about 4 yearsNice and simple, just like how we used to do it in java. Thanks