Reputation: 681
i'm creating a listview array from of LinearLayout programmatically and i want to make rounded corners so i created a drawable resource like this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="40dp"
/>
<padding
android:top="2dp"
android:bottom="2dp"
android:left="2dp"
android:right="2dp"/>
</shape>
The code of the LinearLayout is this
LinearLayout linearLayout= new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layout= new LinearLayout.LayoutParams(
200,
100
);
layout.setMargins(20,20,20,20);
linearLayout.setLayoutParams(layout);
linearLayout.setBackgroundColor(Color.BLUE);
linearLayout.setBackgroundResource(R.drawable.rounded);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("Test selected","");
v.setBackgroundColor(Color.RED);
return false;
}
});
The problem is that the shape of the elements is not rounded. Where is my mistake?? Thanks to every one sorry for my english.
Upvotes: 0
Views: 2351
Reputation: 859
To make the layout having round corner:
You have to define a xml file having properties of round corner having name rounded.xml in res/drawable (You can save any name)
shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:endColor="#000000"
android:startColor="#000000"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
Define your activity as:
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearLayout= new LinearLayout(getApplicationContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
//Use framelayout.layoutparams not linearlayout.layoutparams
FrameLayout.LayoutParams layout= new FrameLayout.LayoutParams(200,100);
//Use setContentView to show this layout in your activity
setContentView(linearLayout,layout);
layout.setMargins(50,50,50,50);
linearLayout.setLayoutParams(layout);
linearLayout.setBackgroundColor(Color.BLUE);
linearLayout.setBackgroundResource(R.drawable.rounded);
linearLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("Test selected","");
v.setBackgroundColor(Color.RED);
return false;
}
});
I have checked with:
FrameLayout.LayoutParams layout= new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
And the color of layout is set to black in rounded.xml
And the output is:
Upvotes: 1