3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Android/Kotlin]コードから背景色を変更する

Last updated at Posted at 2020-09-23

結論

val view: View = findViewById(R.id.constraint) // idを指定(synthetic使えば要らない)
view.background = getDrawable(R.color.colorAccent) // 背景色をcolorAccentに変更

全体

MainActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        constraint.background = getDrawable(R.color.colorAccent) // constraintの背景色をcolorAccentに変更

    }
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/constraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?