11
11

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アプリで画面にふわっとメッセージを表示する方法

Last updated at Posted at 2014-05-11

Androidアプリで画面にふわっとメッセージを表示する方法

androidで以下のようにふわっとメッセージを表示する機能をtoast(トースト)を使用して実装してみる。
無題.png

使いどころとしては保存ボタン等を押下したときに「保存しました」などの簡易メッセージを表示するときに役に立つのでメモ。

■実装方法

onCreateメソッド内等に以下を追記することで実装可能。

// イメージビュー(これを設定すると画像を表示できる)
ImageView iv1 = new ImageView(context);
iv1.setImageResource( R.drawable.ic_launcher_template3 );

// メッセージビュー(ここでメッセージを設定)
TextView tv = new TextView(context);
tv.setTextSize(20.0f);
tv.setTextColor(Color.WHITE);
tv.setText( "おすきなメッセージを記述" );

// リニアレイアウト上に、作成したビューを並べる
LinearLayout ll = new LinearLayout(context);
ll.setOrientation( LinearLayout.HORIZONTAL );
ll.setGravity( Gravity.CENTER );
ll.addView(iv1);
ll.addView(tv);

// トーストを作成し、今作ったビューをセットする
Toast toast = new Toast(context);
toast.setView(ll);
toast.setGravity( Gravity.CENTER, 0, 0 );
toast.setDuration(Toast.LENGTH_LONG);

View v = toast.getView();
v.setBackgroundColor(Color.rgb(30, 30, 30));

//ここで表示
toast.show();

上記記述でtoast.show()を実行した時点でメッセージが表示される。

以上、参考まで。


簡単家計簿 Androidアプリ

11
11
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
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?