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: Output of Above code

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 ...

Share:
13,189

Related videos on Youtube

Divyesh Kalotra
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, 2022

Comments

  • Divyesh Kalotra
    Divyesh Kalotra almost 2 years

    I am trying to set relative layout params programmatically. I can set params 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
      Pavneet_Singh over 6 years
      probably =RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRA‌​P_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT) then param.addRule(...)
    • Divyesh Kalotra
      Divyesh Kalotra over 6 years
      i know but in kotlin how can i declare it .. thats what i want
    • Pavneet_Singh
      Pavneet_Singh over 6 years
      declare it where?, you can show your java code the way you want it in kotlin
    • Divyesh Kalotra
      Divyesh Kalotra over 6 years
      sorry friend i am directly coding in kotlin not in java .
    • Pavneet_Singh
      Pavneet_Singh over 6 years
      how can i declare it .. thats what i want please explain?
    • Divyesh Kalotra
      Divyesh Kalotra over 6 years
      means i want declare param variable as Layoutparams. and want to set in textview
    • Divyesh Kalotra
      Divyesh Kalotra over 6 years
  • Ankit Patidar
    Ankit Patidar over 6 years
    Thanku soo much divyesh
  • SMBiggs
    SMBiggs about 4 years
    Nice and simple, just like how we used to do it in java. Thanks