Android set window background programmatically

Android change windowBackground programmatically on application start

I’m well conscious how to set the splash of an activity in Android using the theme in manifest and the tag windowBackground.

A client recently showed up asking to «change the splash screen accordingly to some events during the day». I’m almost sure that it cannot be done, but I decided to give it a shot with this code:

Obviously this isn’t working.

Someone already tried to accomplice this or has a better idea?

Create a fake activity or fragment to show as splash screen will be easy, but will left that unpleasant white (or black depending on the theme) flash at the application start.

This question is about feasibility of changing the splash screen programmatically with the same result as hardcoding it up in the manifest.

3 Answers 3

What if you remove the Splash and simulate it by creating an Activity in fullscreen that shows your desired Image for n seconds?

A slightly better/earlier place to select the theme would be in onApplyThemeResource (called before onCreate ):

However, this doesn’t solve the problem of the white/black flash at the application start.

Our way around this is to set a neutral theme (only background color, which is the same as the theme background in all the variations) as android:theme in AndroidManifest.xml , and then switch to the right variation in onApplyThemeResource , which in our case is just some logos over the colored background.

Result: the app «flashes» a plain color background (instead of black/white, until Android has initialized the app), and then shows the logos on this background (depending on the state decided in onApplyThemeResource ) while we do our app initialization.

This is not perfect, but for us it’s good enough — maybe it helps someone else.

Источник

How to set background color of a View

I’m trying to set the background color of a View (in this case a Button).

I use this code:

It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?

Читайте также:  Как добавить иконку android studio

21 Answers 21

You made your button transparent. The first byte is the alpha.

When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc. What you want to do is change the color of the existing background resource.

Experiment with PorterDuff.Mode.* for different effects.

Several choices to do this.

Set background to green:

Set background to green with Alpha:

Set background to green with Color.GREEN constant:

Set background to green defining in Colors.xml

or the longer winded:

You can set the hex-color to any resource with:

The code does not set the button to green. Instead, it makes the button totally invisible.

Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.

The correct hex value is 0xFF00FF00 for full opacity green. Any Alpha value between 00 and FF would cause transparency.

For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:

and what is the correct way to change the background color on any View?

On any View ? What you have is correct, though you should drop the invalidate() call.

However, some Views already have backgrounds. A Button , for example, already has a background: the face of the button itself. This background is a StateListDrawable , which you can find in android-2.1/data/res/drawable/btn_default.xml in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.

In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button .

Источник

Android — set layout background programmatically

I have noticed that the setBackground method for the RelativeLayout object is targeted for API 16 (Android 4.1) and higher, but my application has the target API 8 and I cannot use it.

Is there any alternative solution for this problem (besides marking the class/method with TargetApi(16) or changing the target API in the manifest)?
Thank you!

Edit: Eclipse was buggy and it showed me the same error for setBackgroundDrawable but now it seems to work. Thank you for your help.

Читайте также:  Найти видеоплеер для андроид

3 Answers 3

  • .setBackgroundColor(int) (if you’re setting it to a color)
  • .setBackgroundDrawable(Drawable) (if you’re setting it to a Drawable type; this is deprecated, and was replaced by .setBackground(Drawable) )
  • .setBackgroundResource(int) (for setting a resource from R.java )

If you use the second one, make sure to do a conditional check on your API version:

. and mark it with @TargetApi(16) and @SuppressWarnings(«deprecation») .

It depends. If you want to set a color as the background, use setBackgroundColor() . For a Drawable, you can use the now deprecated method setBackgroundDrawable() for APIs below 16, and setBackground() for API 16 devices. You can also use setBackgroundResource() for setting a resource as the background.

Note that while a lot of methods are marked as deprecated, I’m yet to come across one that has actually been removed. So while you could use the deprecated method even in API 16, I’d recommend setting your target API to 16 and using an if else to switch between the methods, depending on the API version the device is running.

Источник

How to set background color of a View

I’m trying to set the background color of a View (in this case a Button).

I use this code:

It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?

21 Answers 21

You made your button transparent. The first byte is the alpha.

When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc. What you want to do is change the color of the existing background resource.

Experiment with PorterDuff.Mode.* for different effects.

Several choices to do this.

Set background to green:

Set background to green with Alpha:

Set background to green with Color.GREEN constant:

Set background to green defining in Colors.xml

or the longer winded:

You can set the hex-color to any resource with:

The code does not set the button to green. Instead, it makes the button totally invisible.

Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.

The correct hex value is 0xFF00FF00 for full opacity green. Any Alpha value between 00 and FF would cause transparency.

For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:

and what is the correct way to change the background color on any View?

On any View ? What you have is correct, though you should drop the invalidate() call.

Читайте также:  Android device iot hub

However, some Views already have backgrounds. A Button , for example, already has a background: the face of the button itself. This background is a StateListDrawable , which you can find in android-2.1/data/res/drawable/btn_default.xml in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.

In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button .

Источник

Setting android:background / android:src programmatically

I need to set two attributes like in a xml:

As you see, there is a background and an src attribute. How do I set BOTH programmatically?

I only know of one: Which one is it? And what is the other one?

9 Answers 9

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

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

SAMPLE CODE

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

SAMPLE CODE

you can do it by this code:

and call initView method in your onCreate

setImageResource() is for android:src . ImageButton inherits it from ImageView.

setBackgroundResource() sets the background — it differs from setBackground() by taking a ressource id (as int) as input.

I am fairly sure setImage() is the method setting the ‘src’ attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable() .

You can set image to ImageView programatically in Android please use bellow like of code.

android:src is set with setImageResource()

android:background is set with setBackgroundResource()

In code

ImagResource will be on top of the BackgroundResource.

There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.

If your only goal is to change it in the code then take a look at this.

Источник

Оцените статью