--alert dialog--

activity_main.xml

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open alert dialog"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="check"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
showDialog();
}
});
}
private void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("alert");
builder.setMessage("wanna finish?");
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
String msg = "you have put yes";
textView.setText(msg);
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn) {
String msg = "you have put no";
textView.setText(msg);
}
});
builder.setNeutralButton("cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
String msg = "you have put cancel";
textView.setText(msg);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}

--progress bar--

activity_main.xml

<ProgressBar
android:id = "@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text = "check message"
android:layout_weight="1"/>
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="close"
android:layout_weight="1"/>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.setProgress(40);

Button button = findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("checking the progress");
dialog.show();
}
});
Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(dialog != null){
dialog.dismiss();
}
}
});
}
}

'android studio' 카테고리의 다른 글

Recycler View  (0) 2019.08.13
custom dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (0) 2019.08.13
방향전환시 상태값 저장  (0) 2019.08.12
xml을 이용한 focus event  (0) 2019.08.12

+ Recent posts