How to change background color

Asked

Viewed 16,937 times

0

I wanted to change the background color of an activity, I’ve seen some topics here but without success. When selecting in the spinner the activity color will be changed to black, or white. How do I change colors?

  • this question has been answered in the Soen http://stackoverflow.com/questions/20032594/android-need-to-change-the-spinner-background-color

  • @Thallyson, it’s not the same thing. EN OS’s question changes the Spinner background, not Activity’s.

  • Yes, I don’t want to change the spinner, but the background of the activity

  • I tried to put Linearlayout li=(Linearlayout)findViewById(R.layout.activity_main); li.setBackgroundColor(Color.RED); li.invalidate(); But unsuccessfully hangs, and the nullPointException

  • If the swap is static (a fixed number only), you can create a Theme that extends the application theme, where only the attribute varies android:background, according to the choice (SharedPreferences p.e). I usually use the theme solution because I offer only two pro user themes in the app settings. How does it give NPE? the li is null? Include the error code and the stacktrace if you can.

2 answers

0

or using rgb colors in Linearlayout

<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
**android:background="#ffffff"**>

0


You can do it that way

view

Add an id to your view and then take the view in your Activity and set the color activity

  • To create blocks of code, you can use the button {} above the edit box.

  • vlw, I didn’t know that

  • Perfect! How to animate the change? I saw a topic on the Transictiondrawable, however I could not, if I can complement your answer how to animate, I thank you very much

  • I tried to put this: Transitiondrawable Transition = (Transitiondrawable) view.getBackground(); Transition.startTransition(1000); however from this error: E/Androidruntime(1689): FATAL EXCEPTION: main 09-18 22:39:48.560: E/Androidruntime(1689): Process: with.example.exercicio, PID: 1689 09-18 22:39:48.560: E/Androidruntime(1689): java.lang.Classcastexception: android.graphics.drawable.Colordrawable cannot be cast to android.graphics.drawable.Transitiondrawable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.