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?
asked Sep 17, 2013 at 9:17
Waqleh
10.2k10 gold badges69 silver badges104 bronze badges
5 Answers 5
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);
}
Waqleh
10.2k10 gold badges69 silver badges104 bronze badges
answered Sep 17, 2013 at 9:25
Sanket Shah
4,3823 gold badges23 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Chase Roberts
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.
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);
answered Sep 17, 2013 at 9:28
Piyush
18.9k5 gold badges35 silver badges64 bronze badges
Comments
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);
answered Sep 17, 2013 at 9:24
SilentKiller
6,9426 gold badges42 silver badges76 bronze badges
Comments
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
answered Sep 17, 2013 at 9:29
Hardik Joshi
9,50712 gold badges66 silver badges117 bronze badges
Comments
The best option if you already have an XML file is not to make changes in code but to use LayoutInflater
answered Sep 17, 2013 at 9:31
Maxim Efimov
2,7371 gold badge21 silver badges25 bronze badges
Comments
lang-java
android:layout_width="fill_parent"toandroid:layout_width="match_parent",, that depreciation will go..