1

I would like to do this vertical line programmatically

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

This is how I create the view:

View view = new View(getActivity());

How can I add the width, height and background parameters?

4
  • You can inflate the view and then add it to the parent. Also you can set the parameters using LayoutParams . Commented Sep 17, 2013 at 9:18
  • yes but how every time i try to use LayoutParams I get a message about deprecation Commented Sep 17, 2013 at 9:19
  • 1
    Try using match_parent instead of fill_parent. May be because of fill_parent you are getting the deprecation message, Commented Sep 17, 2013 at 9:21
  • change android:layout_width="fill_parent" to android:layout_width="match_parent",, that depreciation will go.. Commented Sep 17, 2013 at 9:21

5 Answers 5

4

Try this

View v = new View(activityContext);
v.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT), GetPixel(1, activityContext));
v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));

and

// To convert pixels to dp units
public int GetPixel(float f , Context context)
    {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return (int)((f * displayMetrics.density) + 0.5f);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Your method is called GetPixel (implying that the input is dp and output is dp), but your comment says that input is pixels and output is dp.
1

you can also use this:

View mView = new View(Context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
myLInearLayout.setOrientation(LinearLayout.VERTICAL);
mView .setLayoutParams(params);
mView.setBackgroundResource(android.R.color.white);
myLInearLayout.addView(mView);

Comments

0

Heres an example :

View mView = new View(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
mView.setBackgroundResource(android.R.color.black);
mRelativeLayout.addView(mView, params);

Comments

0

Let me explain it with example.

Here's xml file which contains LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"  android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

Your JAVA file.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        float dpi = 0;
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        dpi = (float) (dm.densityDpi / 160.0);

        LinearLayout linear = new LinearLayout(
                StackOverflowAnswerDemoActivity.this);
        LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
                (int) (1 * dpi));
        linear.setBackgroundColor(Color.WHITE);
        linear.setLayoutParams(params);
        ll.addView(linear);
}

enter image description here

Comments

0

The best option if you already have an XML file is not to make changes in code but to use LayoutInflater

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.