Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make horizontal line in Android programmatically

Tags:

java

android

I am trying to fill linear layout programmatically like this

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,      
            LayoutParams.WRAP_CONTENT
    );
    params.setMargins(dpToPx(6), 0, 0, 0);
    kac.setLayoutParams(params);
    LinearLay.addView(kac);

and I want to add (after every TextView (like kac)) a horizontal line like this one I have in my xml

 <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#B3B3B3" 
             android:padding="10dp"
            />
like image 411
Deron Avatar asked Jan 13 '14 18:01

Deron


People also ask

How do I add a line in Android?

This example demonstrates how do I draw a line in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you put line through text on Android?

If you want to show a strike-through text you can do it programming using PaintFlags. You can set paint flags Paint. STRIKE_THRU_TEXT_FLAG to a TextView and it will add a strike-through to the text. TextView textView = (TextView) findViewById(R.

What is Android divider?

A divider is a thin line that groups content in lists and layouts. design_services Design integration_instructions Implementation. Android.


2 Answers

View v = new View(this);
v.setLayoutParams(new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT,      
        5
));
v.setBackgroundColor(Color.parseColor("#B3B3B3"));

LinearLay.addView(v);
like image 128
vipul mittal Avatar answered Oct 11 '22 11:10

vipul mittal


I recommend creating a drawable containing the line and setting it as a background to your textview. You would avoid unnecessary views.

Sample drawable could be:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#LINE_COLOR"/>
            <padding
                android:bottom="1dp"
                />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#BACKGROUND_COLOR"/>
        </shape>
    </item>
</layer-list>

And you can set it by

textView.setBackgroundResource(R.drawable.resource)
like image 26
balysv Avatar answered Oct 11 '22 12:10

balysv