7
7

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: ボタンに押された感を出してみる

7
Posted at
  • 条件によってボタンの色が変わるユースケース
  • 今回は色ですがイメージとかも可能

色をDrawable化

public static Android.Graphics.Drawables.ColorDrawable ToDrawable(
  this Android.Graphics.Color color){

  return  new Android.Graphics.Drawables.ColorDrawable (color);
}

アルファ値を変えた色を背景色として押された効果を作る


using System.Linq;
...

public static Android.Graphics.Drawables.StateListDrawable ToButtonPressEffect(
  this Android.Graphics.Color color, byte alpha=200
){

  var pressed = new Android.Graphics.Color (
    color.R, color.G, color.B, alpha);

  var effect = new  Android.Graphics.Drawables.StateListDrawable ();
  effect.AddState (
    new int[]{ Android.Resource.Attribute.StatePressed }, pressed.ToDrawable ());

  effect.AddState (
    Android.Util.StateSet.WildCard.ToArray (), color.ToDrawable ());
  return effect;
}

Buttonの背景にエフェクトを設定する

...
var ok_button = new Button (this) {   // this = Activity
  Background=ok_button_color.ToButtonPressEffect()
};
7
7
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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?