user2340490
user2340490

Reputation: 137

How to create a vertical line programmatically?

I want to create a layout like below with LinearLayout:

enter image description here

The problem is that I see all the components except the red vertical line. How can I add this line programmatically?

Here is the my code:

public class TextLinear extends LinearLayout {

        mainLinearLayout= new LinearLayout(mcontext);
        linearSocial= new LinearLayout(mcontext);
        linearSocial.setOrientation(LinearLayout.HORIZONTAL);

        lLayout = new LinearLayout(mcontext);
        tViewTitle = new TextView(mcontext);
        tvMessageFire = new TextView(mcontext);

        viewDivider = new View(mcontext);
        viewDivider.setLayoutParams(new LayoutParams(5,LayoutParams.WRAP_CONTENT));
        viewDivider.setBackgroundColor(Color.RED);

        img0=new ImageView(mcontext);
        img1=new ImageView(mcontext);

        linearSocial.addView(img0);
        linearSocial.addView(img1);
        linearSocial.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT));

        lLayout.addView(tViewTitle);
        lLayout.addView(tvMessageFire);
        lLayout.addView(linearSocial,rightGravityParams);

        mainLinearLayout.addView(lLayout);

        this.addView(mainLinearLayout);
        this.addView(viewDivider);
    }

Full code here: http://pastebin.com/gxySsKZ1

Upvotes: 1

Views: 2459

Answers (2)

Ahd Radwan
Ahd Radwan

Reputation: 1100

With XML you can use View with 1 pixel width

<View 
android:layout_width="1dp"
android:layout_height="wrap_content " />

Upvotes: -1

vilpe89
vilpe89

Reputation: 4702

I think the problem is that viewDivider wraps the views height and because there is nothing inside it the height is 0 px and therefor it's invisible. Try to replace the LayoutParams.WRAP_CONTENT with 120 or LayoutParams.MATCH_PARENT.

Upvotes: 3

Related Questions