android - How to set corner radius programmatically? - android

I have two ListViews (leftList, rightList). I also have one TextView which I use as row view in both of them.
I have a rectangle drawable shape and set it as background to the TextView.
I would like to change this shape and have rounded corners on the left.
What I tried :
GradientDrawable gradientDrawable = new GradientDrawable();
// gradientDrawable.setCornerRadius(30);
((GradientDrawable)gradientDrawable.mutate()).setCornerRadius(30);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewHolderPattern.digits.setBackground(gradientDrawable);
}
I have created a new layout in drawable with the right corner radius set and set that to the textView with setBackgroundRescource but still didn't work.
The TextView that I use as items in both listViews
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/digitsTextView"
android:textSize="15dp"
android:paddingTop="7dp"
android:paddingBottom="8dp"
android:fontFamily="monospace"
android:textColor="#color/selected_items"
android:background="#drawable/digital_text_shape">
</TextView>
Shape layout digital_text_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/orange" />
<solid android:color="#color/orange" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="20dp"
android:topRightRadius="0dp"
/>
<padding
android:bottom="0dp"
android:left="20dp"
android:right="0dp"
android:top="0dp" />
</shape>
Left list and Right list
<!-- Left ListView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:id="#+id/left_listView"
android:divider="#android:color/transparent"
android:dividerHeight="0.1sp"
android:choiceMode="singleChoice"
>
</ListView>
</LinearLayout>
<!-- Right ListView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<ListView
android:id="#+id/right_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="0.1sp"
android:choiceMode="singleChoice"
>
</ListView>
</LinearLayout>

Here example for how to create GradientDrawable shape programmatically.
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.RED);
shape.setStroke(3, Color.YELLOW);
For change the radius for all corners of the gradient.
shape.setCornerRadius(15);
For Change the radius for specific corners of the gradient.
shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
You can use this drawable as a background as below :
view.setBackgroundDrawable(shape);

The answer of #Rohit Suthar in Kotlin, with dp converted to pixel, and color resources:
private val STROKE_WIDTH_PX = 2.dpToPx
private val CORNER_RADIUS_PX = 5.dpToPx.toFloat()
val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setColor(ContextCompat.getColor(context, R.color.pink))
shape.setStroke(STROKE_WIDTH_PX, ContextCompat.getColor(context, R.color.black))
// choose either cornerRadius or cornerRadii
shape.cornerRadius = CORNER_RADIUS_PX
shape.cornerRadii = floatArrayOf(
// top left
0f,
0f,
// top right
CORNER_RADIUS_PX,
CORNER_RADIUS_PX,
// bottom right
CORNER_RADIUS_PX,
CORNER_RADIUS_PX,
// bottom left
0f,
0f
)
view.background = shape
To convert dp to px:
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()

Related

How to create a custom shape button background?

I am trying to make a custom background for my button(simple) but it is not displaying properly. it only displays the following output.
Create Room(btn_create_room_bg) :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<corners
android:bottomLeftRadius="360dp"
android:topLeftRadius="360dp" />
<solid android:color="#color/colorAccent" />
</shape>
</item>
</layer-list>
Join Room(btn_join_room_bg):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners
android:bottomRightRadius="360dp"
android:topRightRadius="360dp" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
</layer-list>
Button code:
Here are my two-button codes in a linear layout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="300dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="300dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginEnd="8dp"
android:layout_weight="5"
android:background="#drawable/btn_create_room_bg"
android:fontFamily="#font/open_sans_bold"
android:text="#string/landing_btn_create_room"
android:textAllCaps="false"
android:textColor="#color/colorWhite" />
<Button
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_weight="5"
android:background="#drawable/btn_join_room_bg"
android:fontFamily="#font/open_sans_bold"
android:text="#string/landing_btn_join_room"
android:textAllCaps="false"
android:textColor="#color/colorWhite" />
</LinearLayout>
I need an output like this.
You can build a custom CornerTreatment with the MaterialButton.
You can extend the default CutCornerTreatment:
public class FullCutCornerTreatment extends CutCornerTreatment {
protected static final float ANGLE_LEFT = 180;
public FullCutCornerTreatment() {
super();
}
#Override
public void getCornerPath(
#NonNull ShapePath shapePath, float angle, float interpolation, float radius) {
shapePath.reset(0, 2*radius * interpolation, ANGLE_LEFT, 180 - angle);
shapePath.lineTo(
(float) (Math.sin(Math.toRadians(angle)) * radius * interpolation),
(float) (Math.sin(Math.toRadians(90 - angle)) * radius * interpolation));
}
}
Then just apply the CornerTreatment to the Button:
MaterialButton materialButton = findViewById(R.id....);
FullCutCornerTreatment customCutCornerTreatment = new FullCutCornerTreatment();
materialButton.setShapeAppearanceModel(materialButton.getShapeAppearanceModel().toBuilder()
//Standard rounded corner with corner radius=50%
.setTopLeftCorner(CornerFamily.ROUNDED,new RelativeCornerSize(0.5f))
.setBottomLeftCorner(CornerFamily.ROUNDED,new RelativeCornerSize(0.5f))
//Square angle
.setTopRightCorner(CornerFamily.ROUNDED,0)
//Full cut corner
.setBottomRightCorner(customCutCornerTreatment)
.setBottomRightCornerSize(new RelativeCornerSize(0.5f))
.build());
Just a note about new RelativeCornerSize(0.5f): It changed in 1.2.0-beta01. Before it was new RelativeCornerSize(50)).

EditText and Horizontal Line

When we type an input value by using EditText, I want a horizontal line to be placed below my input like phone book application in all mobile phones.
Enter Phone : ___________
I want my input to be placed on this line. How can I do this by using EditText ?
Try this
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp">
<EditText
android:id="#+id/Prac"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Practice"
android:inputType="text"
android:textSize="12sp" />
</android.support.design.widget.TextInputLayout>
You have to set width of EditText from match_parent to wrap_content line below code
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
You have to set width to either match_parent or to fixed width like 100dp
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
As everyone answer said set width and height to wrap_content this is the right answer but still their is another way to achieve this lets have a look.
First create a drawable file (myline.xml) which create the line.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
// You can change width according to your need
android:width="1dp"
android:color="#000000" />
</shape>
</item>
Now in your layout.xml use myline.xml in edit text background.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Phone: "
android:textColor="#000"
android:textSize="18dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/myline"
android:textColor="#000"
android:textSize="18dp" />
</LinearLayout>
And its done see the output in screenshot below.
You can Try with CustomEditText Like Below code:
package com.cj.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* Created by E069099 on 7/20/2017.
*/
public class CustomEdiTextWithLine extends EditText {
private Rect rect;
private Paint paint;
public CustomEdiTextWithLine(Context context, AttributeSet attrs) {
super(context, attrs);
rect = new Rect();
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(2F);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setTextSize(20);
}
#Override
protected void onDraw(Canvas canvas) {
//start at the vertical center of the textview
float top = (getHeight() + getPaddingTop() - getPaddingBottom())/2;
//start at the left margin
float left = getPaddingLeft();
//we draw all the way to the right
float right = getWidth() -getPaddingRight();
//we want the line to be 2 pixel thick
float bottom = getHeight()- getPaddingBottom();
canvas.drawLine(0,bottom,right,bottom,paint);
super.onDraw(canvas);
}
}
one method is,
make backgroundtint is black colour and set hint like below code
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:backgroundTint="#000000"
android:hint=" " />
other method is,
add a view with black background below the editText and background of the editText is make it null.

How to add dynamically circular image view with some specified background color

I am working on an instant chat application.I have to implement following screen.
In the screenshot you can see we have a circular image view where we are displaying the profile pic of the user.On profile pic we have a small circle having green color indicating that the user is online.I am implementing the screen using following xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/padding10">
<FrameLayout
android:id="#+id/frame"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/margin10">
<com.almabay.almachat.circularImageView.CircularImageView
android:id="#+id/imgContact"
android:layout_width="80dp"
android:layout_height="80dp "
android:layout_gravity="bottom|left" />
<com.almabay.almachat.circularImageView.CircularImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="top|right"
android:background="#78dd63" />
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin10"
android:layout_toRightOf="#+id/frame"
android:orientation="vertical"
android:padding="#dimen/padding5">
<TextView
android:id="#+id/txtNam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Neeraj"
android:textSize="15sp" />
<TextView
android:id="#+id/stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin10"
android:text="Welocme to Almachat" />
</LinearLayout>
</RelativeLayout>
But i am getting the screen as is given below:
Here you can see that green color is not displayed in circle .It is displayed in square.However I have set the color in the design ,in actual practice i want to set it as green using java code when the user will be online .I have taken a circular image view .Still the background color is shown in square.Please guide me how can i fix the issue.
Solved the Issue:
Create a drawable named circle_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="270"
android:endColor="#color/green_color"
android:startColor="#color/green_color" />
<stroke
android:width="2dp"
android:color="#color/while_color"></stroke>
</shape>
Used it in the background of an imageview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/padding10">
<com.almabay.almachat.circularImageView.CircularImageView
android:id="#+id/imgContact"
android:layout_width="80dp"
android:layout_height="80dp "
android:layout_alignParentLeft="true" />
<ImageView
android:id="#+id/online"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignRight="#+id/imgContact"
android:layout_alignTop="#+id/imgContact"
android:background="#drawable/circle_green" />
It is working for me now.
You need to create a CustomImageView like this...
public class CircularImageView extends ImageView {
public CircularImageView(Context context,AttributeSet attributeSet){
super(context,attributeSet);
}
#Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
Bitmap finalBitmap;
if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
false);
else
finalBitmap = bitmap;
Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
finalBitmap.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(finalBitmap.getWidth() / 2 + 0.7f,
finalBitmap.getHeight() / 2 + 0.7f,
finalBitmap.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(finalBitmap, rect, rect, paint);
return output;
}
}
And use the imageview class in your xml layout like this...
<com.xxxx.xxx.xxxx.CircularImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/std_img"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"/>
you can do something like this :
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imgUserImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/user_temp" />
<ImageView
android:id="#+id/imgOnlineIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgUserImage"
android:layout_alignTop="#+id/imgUserImage"
android:layout_marginLeft="#dimen/margin_small"
android:src="#drawable/online_indicator" />
</RelativeLayout>
This is just an example from my app.You can modify it accordingly and it serves your purpose.
* i used a small green image to show as online indicator.
You can use the powerful Constraint Layout.
The advantages are:
Consistency in different layout sizes.
Simple to implement.
You can animate the dot =).
Create a drawable named circle_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="270"
android:endColor="#color/green_color"
android:startColor="#color/green_color" />
<stroke
android:width="2dp"
android:color="#color/while_color"></stroke>
</shape>
Align the online dot on your ImageView within the Constraint Layout.
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="#dimen/avatar_profile_height"
android:layout_height="#dimen/avatar_profile_height"
android:layout_marginTop="#dimen/margin_medium"
android:src="#drawable/avatar_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/online"
android:layout_width="12dp"
android:layout_height="12dp"
android:background="#drawable/circle_green"
android:contentDescription="#string/online_status"
app:layout_constraintCircle="#id/profile_image"
app:layout_constraintCircleAngle="150"
app:layout_constraintCircleRadius="#dimen/avatar_profile_radius" />
Ignore the warning that the ImageView is not constrained.
Finally, in order to animate, do the following:
view.button_home.setOnClickListener {
//1
val layoutParams = view.online.layoutParams as ConstraintLayout.LayoutParams
val startAngle = layoutParams.circleAngle
val endAngle = startAngle + 360
//2
val anim = ValueAnimator.ofFloat(startAngle, endAngle)
anim.addUpdateListener { valueAnimator ->
//3
val animatedValue = valueAnimator.animatedValue as Float
val layoutParam = view.online.layoutParams as ConstraintLayout.LayoutParams
layoutParam.circleAngle = animatedValue
view.online.layoutParams = layoutParam
//4
view.online.rotation = (animatedValue % 360 - 150)
}
//5
anim.duration = 2000
//6
anim.interpolator = LinearInterpolator()
anim.start()
}

Draw custom line between two elements in TableLayout Android

I have an activity with events organised in a timeline. But it looks ugly.
I want to design a more beautiful timeline like this one.
Is there any simple way or a library to draw lines between elements like in my example?
<ScrollView
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/text_data"
android:layout_above="#+id/button_trimite"
android:id="#+id/scroll_timeline"
android:layout_marginBottom="7dp"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/timelineTable"
>
</TableLayout>
</ScrollView>
This is my xml. But my TableLayout is generated dynamically because I need to sort my events.
for (final Event e : events) {
if(e.getDate().equals(dataComp)) {
//tablerow with event entry
final TableRow row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
if (indexForDrawable % 2 == 0)
row.setBackgroundResource(R.drawable.marcaj_event_albastru);
else
row.setBackgroundResource(R.drawable.marcaj_event_portocaliu);
TextView txtEvent = new TextView(getActivity());
txtEvent.setText(" "+ e.getHour() +"-"+e.getType()+"-"+e.getTitle());
txtEvent.setTextColor(Color.BLACK);
txtEvent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, trEvent);
txtEvent.setTypeface(Typeface.create(tf, Typeface.BOLD));
row.addView(txtEvent);
row.setClickable(true);
final String date = e.getDate(), hour = e.getHour(), title = e.getTitle(),
type = e.getType(), descriere = e.getDescriere();
final int finalResource = resource;
final int finalIndexForDrawable = indexForDrawable;
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
row.setBackground(getActivity().getResources().getDrawable(finalResource));
showPopup2(date, hour, type, title, descriere, row, finalIndexForDrawable);
}
});
timelineTable.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
indexForDrawable++;
}
else {
//tablerow with date
final TableRow row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView txtEvent = new TextView(getActivity());
// txtEvent.setText("\n" + dataSplit1[0]+months.indexOf(dataSplit11));
txtEvent.setText("\n" + e.getDate().substring(0, 5));
txtEvent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, trDate);
row.addView(txtEvent);
timelineTable.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
dataComp = e.getDate();
//tablerow with event entry
final TableRow row3 = new TableRow(getActivity());
row3.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
if (indexForDrawable % 2 == 0)
row3.setBackgroundResource(R.drawable.marcaj_event_albastru);
else
row3.setBackgroundResource(R.drawable.marcaj_event_portocaliu);
TextView txtEvent3 = new TextView(getActivity());
txtEvent3.setText(" "+ e.getHour() +"-"+e.getType()+"-"+e.getTitle());
txtEvent3.setTextColor(Color.BLACK);
txtEvent3.setTextSize(TypedValue.COMPLEX_UNIT_DIP, trEvent);
txtEvent3.setTypeface(Typeface.create(tf, Typeface.BOLD));
row3.addView(txtEvent3);
row3.setClickable(true);
final String date3 = e.getDate(), hour3 = e.getHour(), title3 = e.getTitle(),
type3 = e.getType(), descriere3 = e.getDescriere();
timelineTable.addView(row3, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
indexForDrawable++;
}
You may have to create your own custom adapter but I am using array adapter for your reference. Also giving item layout for list view, hope you will manage your code accordingly.
items.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="#android:color/black" />
<View
android:id="#+id/view1"
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_centerVertical="true"
android:layout_marginLeft="7dp"
android:background="#drawable/dot" />
</RelativeLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
dot.xml which is a drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="1dp"
android:color="#android:color/black" />
<solid android:color="#android:color/white" />
And in acivity you can use adapter like this:
list.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.textView1, items));
Hope this helped!
If you just want a line displayed i recommend you to create a Drawable for this.
Heres a little example:
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/line">
</LinearLayout>
and the line.xml Drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="3dp">
<shape >
<stroke android:width="1dp" android:color="#android:color/holo_purple"/>
</shape>
</item>
<item android:left="4dp">
<shape>
<solid android:color="#ffffff"/>
</shape>
</item>
</layer-list>
The Layer-list may also be changed to use up additional Drawables as the ones you are already using.
An example using draw-9 might look like this:
line.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<nine-patch android:src="#drawable/point" android:dither="true"/>
</item>
<!-- <item android:left="3dp">
<shape >
<stroke android:width="1dp" android:color="#android:color/holo_purple"/>
</shape>
</item>
<item android:left="4dp">
<shape>
<solid android:color="#ffffff"/>
</shape>
</item> -->
</layer-list>
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:background="#drawable/line" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:background="#drawable/line"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_gravity="center_horizontal"
android:background="#drawable/line" />
</LinearLayout>
and my point.9.png
to apply a draw-nine-patch you must mark the parts to be streched with black color on the borders.

Rounded corners in imageView [duplicate]

This question already has answers here:
How to make an ImageView with rounded corners?
(58 answers)
Closed 9 years ago.
This is my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:padding="3dp" >
<ImageView
android:id="#+id/img"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="fitCenter"
/>
<TextView
android:id="#+id/name"
android:layout_width="208dp"
android:layout_height="75dp"
android:paddingLeft="15dp"
android:paddingTop="15dp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:src="#drawable/ash_arrow" />
</LinearLayout>
How can i show my ImageView with rounded corners?
Use below code. It may helps you -
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Pass your Bitmap image into this method and get that as rounded corner.
ash_arrow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--Background with solid color-->
<solid android:color="#color/colorWhite"/>
<!--background with gradient-->
<!--<gradient-->
<!--android:centerColor="#fff"-->
<!--android:centerX="50%"-->
<!--android:centerY="50%"-->
<!--android:endColor="#f0f"-->
<!--android:gradientRadius="100"-->
<!--android:startColor="#f00"-->
<!--android:type="radial"/>-->
<!--Stoke with dashed line-->
<!--<stroke-->
<!--android:width="8dp"-->
<!--android:color="#f00"-->
<!--android:dashGap="8dp"-->
<!--android:dashWidth="8dp"/>-->
<!--Stroke normal-->
<stroke
android:width="1dp"
android:color="#color/willow_grove"/>
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp"/>
</shape>
What if you try to set a background to your ImageView, defining a Shape in xml that would only contain a "corner" Attribute? Something like that:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="20dip" />
</shape>

Categories

Resources