Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Custom Component View w/Rounded Corners
    primarykey
    data
    text
    <p>I'm trying to create a View with rounded corners (and a background color of choice) that I can reuse with different background colors; hard to explain, so here's my code:</p> <p><em>/app/src/com/packagename/whatever/CustomDrawableView.java</em></p> <pre><code> package com.packagename.whatever; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.drawable.PaintDrawable; import android.util.AttributeSet; import android.view.View; public class CustomDrawableView extends View { private PaintDrawable mDrawable; int radius; private void init(AttributeSet attrs) { TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.RoundedRect); radius = a.getInteger(R.styleable.RoundedRect_radius, 0); } public CustomDrawableView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); mDrawable = new PaintDrawable(); } protected void onDraw(Canvas canvas) { mDrawable.setCornerRadius(radius); mDrawable.draw(canvas); } } </code></pre> <p>Here's the XML to display the custom component: <em>/app/res/layout/test.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ny="http://schemas.android.com/apk/res/com.packagename.whatever" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:padding="10dp"&gt; &lt;com.packagename.whatever.CustomDrawableView android:id="@+id/custom" android:layout_width="200dp" android:layout_height="200dp" android:background="#b80010" ny:radius="50" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>I'm wanting the red box to have 50px rounded corners, but as you'll see, it does not:</p> <p><img src="https://i.stack.imgur.com/7BSGR.png" alt="Red box without rounded corners"></p> <p>The idea is that I could easily change the background color in the XML and automatically have a nice View with rounded corners, without having to create multiple drawables.</p> <p>Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload