How Set Background Drawable Programmatically in Android

How set background drawable programmatically in Android

layout.setBackgroundResource(R.drawable.ready); is correct.

Another way to achieve it is to use the following:

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready) );
} else {
layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
}

But I think the problem occur because you are trying to load big images.
Here is a good tutorial how to load large bitmaps.

UPDATE:
getDrawable(int ) deprecated in API level 22



getDrawable(int ) is now deprecated in API level 22.
You should use the following code from the support library instead:

ContextCompat.getDrawable(context, R.drawable.ready)

If you refer to the source code of ContextCompat.getDrawable, it gives you something like this:

/**
* Return a drawable object associated with a particular resource ID.
* <p>
* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
* drawable will be styled for the specified Context's theme.
*
* @param id The desired resource identifier, as generated by the aapt tool.
* This integer encodes the package, type, and resource entry.
* The value 0 is an invalid identifier.
* @return Drawable An object that can be used to draw this resource.
*/
public static final Drawable getDrawable(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
return ContextCompatApi21.getDrawable(context, id);
} else {
return context.getResources().getDrawable(id);
}
}

More details on ContextCompat

As of API 22, you should use the getDrawable(int, Theme) method instead of getDrawable(int).

UPDATE:

If you are using the support v4 library, the following will be enough for all versions.

ContextCompat.getDrawable(context, R.drawable.ready)

You will need to add the following in your app build.gradle

compile 'com.android.support:support-v4:23.0.0' # or any version above

Or using ResourceCompat, in any API like below:

import android.support.v4.content.res.ResourcesCompat;
ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);

Setting android:background / android:src programmatically

Use setImageResource() to set android:src to your ImageButton

setImageResource() Sets a drawable as the content of this ImageView.

SAMPLE CODE

btnBack.setImageResource(R.drawable.ic_camera);

Use setBackgroundResource() to set android:background to your ImageButton

SAMPLE CODE

btnBack.setBackgroundResource(R.color.colorAccent);

How to set background to views programmatically?

view.setBackground(getResources().getDrawable(R.drawable.division_line))
instead of
view.setBackgroundDrawable(...)

and keep in mind that first 2dp isn't much and you are adding a view with no layout properties so its size is 0px x 0px

you can do so in code:
view.setLayoutParams(new AbsListView.LayoutParams(300, 300));

How to create android shape background programmatically?

I've created a library which can help to create drawables programmatically.

See here: DrawableToolbox.

With DrawableToolbox, you can create it by:

Drawable drawable = new DrawableBuilder()
.rectangle()
.solidColor(0xffe67e22)
.bottomLeftRadius(20) // in pixels
.bottomRightRadius(20) // in pixels
// .cornerRadii(0, 0, 20, 20) // the same as the two lines above
.build();

how to set drawable background of edit text programmatically

You should retrieve the background drawable using ContextCompat.getDrawable method:

edit_text.setBackground(ContextCompat.getDrawable(this, R.drawable.background1));

Programmatically setting TextView background

You have to use

getResources().getDrawable(R.drawable.cellborder);

which will return a Drawable.

If you use findViewById() it will try to find a View in the View Hierarchy and return that. A View is not a Drawable, so you can't cast it.

Programmatically change button background drawable onClick


private boolean isButtonClicked = false; // You should add a boolean flag to record the button on/off state

protected void onCreate(Bundle savedInstanceState) {
......
Button star = (Button) findViewById(R.id.buttonStar);
star.setOnClickListener(new OnClickListener() { // Then you should add add click listener for your button.
@Override
public void onClick(View v) {
if (v.getId() == R.id.buttonStar) {
isButtonClicked = !isButtonClicked; // toggle the boolean flag
v.setBackgroundResource(isButtonClicked ? R.drawable.btn_star_on : R.drawable.btn_star_off);
}
}
});
}

Programmatically create textview background from drawable in Android

You need to use setBackgroundResource() method.

For example :

best_deals.setBackgroundResource(R.drawable.headerradius);

More about setBackgroundResource().



Related Topics



Leave a reply



Submit