Skip to content

Instantly share code, notes, and snippets.

@oha-yashi
Last active April 9, 2021 19:32
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 oha-yashi/694a2952b126ffa51c0ade8dd750dfe0 to your computer and use it in GitHub Desktop.
Save oha-yashi/694a2952b126ffa51c0ade8dd750dfe0 to your computer and use it in GitHub Desktop.
androidDialogSnackbar
package com.example.testapplication.dialogAndSnackbar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.snackbar.Snackbar;
public class DialogAndSnackbarActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_and_snackbar);
String[] strings = new String[]{"hoge", "fuga", "piyo"};
final View v = this.getLayoutInflater().inflate(R.layout.dialog_button_close, null);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Dialog with Snackbar")
.setSingleChoiceItems(strings, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Snackbar.make(v, strings[i]+" was selected", Snackbar.LENGTH_SHORT).show();
}
})
.setView(v)
.create();
v.findViewById(R.id.dialog_button).setOnClickListener(view -> dialog.dismiss());
dialog.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".dialogAndSnackbar.DialogAndSnackbarActivity"
android:id="@+id/coordinator>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="start">
<Button
android:id="@+id/dialog_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:text="閉じる"
android:textColor="@color/design_default_color_primary"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment