Parham Ghazanfari
Parham Ghazanfari

Reputation: 11

Writing custom LinearLayout for android

I have a custom layout that draws a transparent rounded rectangle beneath its children. The problem is when I try to add it to my xml file, it doesn't show up. Also, when I try to add parameters to it (i.e. android:layout_width) the popup shows that none of them are available. The same thing happens to any child views I add. Can anyone help?

public class RoundRectLayout extends LinearLayout 
{ 
 private RectF shape;
 public RoundRectLayout(Context context)
 {
  super(context);
  LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.settings, this);
  shape = new RectF();
 }

 public RoundRectLayout(Context context, AttributeSet attrs)
 {
  super(context, attrs);
  LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.settings, this);
  shape = new RectF();
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh)
 {
  shape = new RectF(0, 0, w - 5, h - 5);
  super.onSizeChanged(w, h, oldw, oldh);
 }

 @Override
 protected void dispatchDraw(Canvas canvas)
 {
  Paint temp = new Paint();
  temp.setAlpha(125);
  canvas.drawRoundRect(shape, 10f, 10f, temp);
  super.dispatchDraw(canvas);
 }
}

Upvotes: 1

Views: 13335

Answers (3)

dinigo
dinigo

Reputation: 7448

This tuto really helped me with yourp roblem

First of all you have to define the fields you want to pass to your custom layout in a file res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="MyLayout">
        <attr name="text" format="string" />
    </declare-styleable>
</resources>

Now prepare your constructor to recive those attributes

public RoundRectLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.MyLayout);
    CharSequence s = a.getString(R.styleable.RoundRectLayout_text);
    if (s != null) {
        mText = s.toString();
    }
    a.recycle();
}

Then add a namespace to your layout.xml so that android konws were to look for your custom layout. "mi.pakage" is the java package were your custom layout is.

Once you have your namespace and your class just call the atributes you set in attrs.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/mi.pakage"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff">

    <com.example.MyLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        myapp:text="My Special Text String"/>

</RelativeLayout>

But if you just want to draw a rectangle you can use the method sugested in the other answer

Upvotes: 1

fedj
fedj

Reputation: 3452

I think it is the plugin which fails but you can specify android:layout_*, it will be took into account. Anyway if you just want to draw a round rect layout, follow Marco's instructions

Upvotes: 0

Marco
Marco

Reputation: 26

If you just want a rounded rectangle background for your LinearLayout, you can define a drawable shape in an xml file in "res/drawable/":

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#50000000"/>    
    <stroke android:width="3dp"
            android:color="#ffffffff"/>
    <padding android:left="5dp"
             android:top="5dp"
             android:right="5dp"
             android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"
             android:bottomLeftRadius="7dp" 
             android:topLeftRadius="7dp"
             android:topRightRadius="7dp"/> 
</shape>

Then you set the layout's background to the name of your drawable xml file. If you named it round_border.xml, set the background to:

<LinearLayout
   android:background="@drawable/round_border"
   ...

The transparency is set in the following...

<solid android:color="#50000000"/>    

First 2 digits denotes transparency, last 6 digits is the colour.

Upvotes: 8

Related Questions