Divyesh Kalotra
Divyesh Kalotra

Reputation: 204

Relative layout params in kotlin

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!

Upvotes: 2

Views: 17224

Answers (4)

MRT
MRT

Reputation: 1610

This is Working for me

var tempSnack = Snackbar.make(view,"Your Text Here", Snackbar.LENGTH_LONG)
            var tempview = tempSnack.view
            var params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT)
            params.gravity = Gravity.TOP
            tempview.layoutParams = params
            tempSnack.show()

Upvotes: 0

BarmanInfo
BarmanInfo

Reputation: 392

var view: RelativeLayout =RelativeLayout(this)
view.layoutParams= RelativeLayout.LayoutParams(25,25)

Try This one ...

Upvotes: 1

Ankit Patidar
Ankit Patidar

Reputation: 2781

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.

Upvotes: 7

ChandraShekhar Kaushik
ChandraShekhar Kaushik

Reputation: 397

    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)

    }

Upvotes: 1

Related Questions