Question

Hello I am trying to set the background image on the LinearLayout, first I need to make it rounded that I made using shape tag and calling setBackgroundResource to make it rounded but now how should I set any drawable also at run time on same LinearLayout

layout_rounded.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="3dip" android:color="#B1BCBE" />
    <corners android:radius="10dip"/>
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>

and I did this

linearLayout.setBackgroundResource(R.drawable.layout_rounded);

Here to set the background on the LinearLayout then I have to call the setBackgroundDrawable but after calling this, it set the drawable but again it unset the shape of the LinearLayout.

I don't have that drawable in res, that background I am getting from URL then trying to set

Can you please look how to achieve this ?

Thanks in advance

Was it helpful?

Solution 2

Finally I did this by simply making the bitmap to rounded shape. Now there is no need to give that LinearLayout shape to rounded because the bitmap I am setting to the LinearLayout is of rounded shape that is making LinearLayout shape to rounded.

import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.BitmapShader;
import android.graphics.RectF;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;

public class RoundedCornersDrawable extends BitmapDrawable {

  private final BitmapShader bitmapShader;
  private final Paint p;
  private final RectF rect;
  private final float borderRadius;

  public RoundedCornersDrawable(final Resources resources, final Bitmap bitmap) {
    super(resources, bitmap);
    bitmapShader = new BitmapShader(getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    final Bitmap b = getBitmap();
    p = getPaint();
    p.setAntiAlias(true);
    p.setShader(bitmapShader);
    final int w = b.getWidth();
    final int h = b.getHeight();
    rect = new RectF(0, 0, w, h);
    borderRadius = 0.15f * Math.min(w, h);
  }

  @Override
  public void draw(final Canvas canvas) {
    canvas.drawRoundRect(rect, borderRadius, borderRadius, p);
  }
}

This is which is helpful for me and worked for what I was looking.

OTHER TIPS

Use Shape in android to make the rounder corners

create the xml file named it as roundcorner.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#33DDFF" />
        <corners android:radius="4dp" />
    </shape>

In your ImageButton add this attribute android:background="@drawable/roundcorner"

<ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageButton"
                android:layout_marginTop="57dp"
                android:src="@drawable/friends"
                android:background="@drawable/roundcorner"
                android:padding="1dp"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/imageButton2"
                android:layout_marginRight="62dp" />

OR

Use image view set android:src = "your image" , android:background= "your shape "and android:clickable="true"

Try this way. Hope this will help you.

Note that I have used FrameLayout here instead of LinearLayout, in order to achieve your requirement.

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</FrameLayout>

MainActivity.java

public class MainActivity extends Activity {

    FrameLayout frame;
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_main);
        frame = (FrameLayout) findViewById(R.id.frame);
        frame.setBackgroundResource(R.drawable.layout_round); // set your rounded background here
        frame.setForeground(getResources().getDrawable(R.drawable.ic_launcher)); // set your background image here
    }   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top