Reputation: 379
I want to dynamically add a variable amount of Buttons to a dialog. So because the amount of buttons isn't fix I can't add them to the layout file.
This is how I tried it:
private void oeffne_dialog ( String[] prediction_array ) {
//GestureAnyWhere gestureAnyWhere = null;
// Activity activity = gestureAnyWhere.get_activity ();
// TODO: bessere Lösung finden, als das Flag setzen zu müssen. Falscher Context
Dialog dialog = new Dialog ( getApplicationContext () );
dialog.getWindow ().setType ( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT );
Log.d ( "dialog", "dialog " + dialog + "|" + HintergrundService.this /*+ "|" + activity*/ );
dialog.setContentView ( R.layout.multiple_predictions_layout );
dialog.setTitle ( "Bitte die zu startende Anwendung auswählen" );
// Button button_id = (Button)dialog.findViewById ( R.id.button_ID );
Button button;
for ( int i = 0 ; i < prediction_array.length ; i++ ) {
/*
button_id = new Button ( getApplicationContext () );
button_id.setText ( prediction_array [i] );
*/
Log.d ( "aufrufe", "aufrufe " + i + prediction_array[ i ] );
button = new Button ( getApplicationContext () );
button.setText ( prediction_array[ i ] );
button.setId ( i );
}
dialog.show ();
}
But with this code no Button will appear in the dialog.
Thanks for helping.
Upvotes: 1
Views: 1147
Reputation: 1911
Here's a custom Dialog, with a custom layout and a cancel and save button.
You can set up also gravity on the device's screen (bottom) and define the width and height of the dialog.
private void showDialog(final String scanContent, final String currentTime, final String currentDate) { LayoutInflater linf = LayoutInflater.from(this); final View inflator = linf.inflate(R.layout.dialog_barcode_result_dialog, null);
final Dialog dialog = new Dialog(this, android.R.style.Theme_DeviceDefault_Light_Dialog);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
dialog.getWindow().setLayout(375, 350);
window.setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.dialog_barcode_result_dialog);
dialog.setTitle(getResources().getString(R.string.dialog_title));
dialog.setContentView(inflator);
final Button save = inflator.findViewById(R.id.saveBtn);
final Button cancel = inflator.findViewById(R.id.cancelBtn);
final TextView message = inflator.findViewById(R.id.dialog_message_text);
message.setText(scanContent);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}
the dialog layout xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:minWidth="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:layout_marginBottom="10dp"
android:id="@+id/dialog_message_text"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<Button
android:id="@+id/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel" />
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_save" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Upvotes: 0
Reputation: 12605
You just need to inflate the view first and get the ViewGroup from that View that you want to add childs into.
Dialog dialog = new Dialog ( MainActivity.this );
dialog.getWindow ().setType ( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT );
View view = getLayoutInflater().inflate(R.layout.empty_basket, null);
dialog.setTitle ( "Bitte die zu startende Anwendung auswählen" );
Button button;
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
for ( int i = 0 ; i < 10 ; i++ ) {
Log.d ( "aufrufe", "aufrufe " + i);
button = new Button ( MainActivity.this );
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
button.setText (String.valueOf(i));
button.setId (i);
ll.addView(button);
}
dialog.setContentView (view);
dialog.show ();
Don't forget to add the permission TYPE_SYSTEM_ALERT
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Upvotes: 2