How to set margins for TextView programmatically?

AndroidTextview

Android Problem Overview


TextView tv1 = new TextView(this);		
tv1.setPadding(5, 0, 5, 0);
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
tv1.setBackgroundColor(Color.parseColor("#0099cc"));
tv1.setTextColor(Color.WHITE);
tv1.setTextSize(11);
tv1.setGravity(Gravity.LEFT | Gravity.BOTTOM);
tv1.setText("Test1");
ll.addView(tv1);

TextView tv2 = new TextView(this);		
tv2.setPadding(5, 0, 5, 0);
tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
tv2.setBackgroundColor(Color.parseColor("#0099cc"));
tv2.setTextColor(Color.WHITE);
tv2.setTextSize(11);
tv2.setGravity(Gravity.LEFT | Gravity.BOTTOM);
tv2.setText("Test2");
ll.addView(tv2);

As you can see, in this peace of code I set TextView's background color. What I want to do is I want to separate both of these TextView's from each other, so that their background colors would be separated by a line. I don't want them to connect. As I understand, it would be possible to do so, if I could set margins of TextView, but as I know, TextView's are not able to do so.

Android Solutions


Solution 1 - Android

set to LayoutParams.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
tv1.setLayoutParams(params);

Solution 2 - Android

It depends according to your parent view.

If you using LinearLayout on your textview as a parent view give params like below

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
tv1.setLayoutParams(params);

If you using RelativeLayout on your textview as a parent view give params like below

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
tv1.setLayoutParams(params);

Solution 3 - Android

For Kotlin use following code snippet

(textView.layoutParams as ConstraintLayout.LayoutParams).apply {
        marginStart=8.dpToPixels()
        topMargin=8.dpToPixels()
        marginEnd=8.dpToPixels()
        bottomMargin=8.dpToPixels()
    }

Change LayoutParams as per used layout. Thanks.

Solution 4 - Android

All these answers are great, but I was using ConstraintLayout, so here is code for that:

ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 10, 10, 10);
textview.setLayoutParams(params); // note that textview would be your instanced TextView object

Solution 5 - Android

This one should be tried

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
        params.setMargins(10,20,30,20);
        txt_gender.setLayoutParams(params);

Solution 6 - Android

Using Kotlin Extensions:

Here is simple extension for setting margins for textview.

fun View.setMargins(marginLeft: Int, marginTop: Int, marginRight: Int, marginBottom: Int) {
  val params: LinearLayout.LayoutParams =
    LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
  params.setMargins(margin.dp, 0, margin.dp, 0)
  this.layoutParams = params
}

For Int to dp conversion, here is another extension

val Int.dp: Int
  get() = (this * Resources.getSystem().displayMetrics.density).toInt()

You can call this extension to give horizontal margin 8dp as follows

yourTextView.setMargins(8, 0, 8, 0)

Solution 7 - Android

If you use Kotlin ex. in Adapter add like this:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val params = holder.tvNext.layoutParams as MarginLayoutParams
        params.bottomMargin=0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2205288View Question on Stackoverflow
Solution 1 - AndroiddmnlkView Answer on Stackoverflow
Solution 2 - AndroidEmre TekinView Answer on Stackoverflow
Solution 3 - AndroidGeek TanmoyView Answer on Stackoverflow
Solution 4 - AndroidElijah MockView Answer on Stackoverflow
Solution 5 - AndroidSamirView Answer on Stackoverflow
Solution 6 - AndroidSalman NazirView Answer on Stackoverflow
Solution 7 - AndroidMoriView Answer on Stackoverflow