6
5

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 5 years have passed since last update.

【Android】ボタンの色や形を動的に変える

Posted at

##ボタンの色を動的に変える方法
###Drawableを用意して、ボタンに色や形を適用する

※まず適当なDrawableを作成し、drawableディレクトリに保存する

color_sample.xml
<?xml version="1.0" encoding="utf-8"?>  
<shape android:shape="rectangle">
    <solid android:color="#000000" />
    <corners android:radius="5dp" />
</shape>
java
//リソースからボタンのリソースを取得(ここではbtn_sampleとする)
Button btn = (Button)findViewById(R.id.btn_sample);
//リソースから作成したDrawableのリソースを取得
Drawable btn_color = ResourcesCompat.getDrawable(getResources(), R.drawable.color_sample, null);
//ボタンにDrawableを適用する
btn.setBackground(btn_color);
6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?