Android アプリ開発メモ(Java)

Programming
公開: 2022-05-31

参考情報

デベロッパー ガイド(公式)
初めてのアプリを作成する(公式)

ボタンの Click イベント

【Android Studio】ボタンのクリックイベント 3つの書き方

  • XML で onClick 属性をつける
  • OnClickListener を実装する
  • 匿名クラスを使う

アクションバー(画面上部のタイトルバーのようなもの)の文字列を変更

【Android Studio】アクションバーの文字列を変更する方法

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setTitle("テキスト");
}

TextView に背景色を設定

How to change TextView background color in android

ウィジェット(ボタンなどの画面要素)の表示/非表示をプログラムで切り替え

  • View.INVISIBLE ウィジェットが非表示になるだけでレイアウトは変わらない
  • View.GONE ウィジェットが存在しないかのようにレイアウトが調整される
Button btn = findViewById(R.id.btn_a010);
btn.setVisibility(View.INVISIBLE); 
btn.setVisibility(View.GONE);

画面遷移と画面間のデータ引き継ぎ

  • 画面遷移:startActivity(Intent)
  • データ設定:Intent.putExtra(key, value)
// 起動する側
Intent intent = new Intent(this, M000Activity.class);
intent.putExtra(EXTRA_DATA, "あいうえお");
startActivity(intent);

// 起動される側(onCreate)
Intent intent = getIntent();
String data = intent.getStringExtra(MainActivity.EXTRA_DATA);

テーマでフォントサイズを指定する

// theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SilverLifeAndroidJava" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ・・・・
        <!-- Customize your theme here. -->
        <item name="android:textSize">30sp</item>
    </style>
</resources>