Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created October 29, 2019 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/adbd346ce8ba901f5db018fc21f18606 to your computer and use it in GitHub Desktop.
Save codingwithsara/adbd346ce8ba901f5db018fc21f18606 to your computer and use it in GitHub Desktop.
AlertDialog Customize 2-2
package jp.codeforfun.customalertdialog;
import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// カスタムレイアウトの用意
LayoutInflater layoutInflater = getLayoutInflater();
View customAlertView = layoutInflater.inflate(R.layout.custom_alert_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customAlertView);
// タイトルの変更
TextView title = customAlertView.findViewById(R.id.title);
title.setText("こんにちは!");
// メッセージの変更
TextView message = customAlertView.findViewById(R.id.message);
message.setText("ここにメッセージを入力します。ここにメッセージを入力します。ここにメッセージを入力します。");
final AlertDialog alertDialog = builder.create();
// ボタンの設定
Button alertBtn = customAlertView.findViewById(R.id.btnPositive);
alertBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ボタンを押した時の処理を書く
// ダイアログを閉じる
alertDialog.dismiss();
}
});
// ダイアログ表示
alertDialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment