I am trying to open a custom Dialog with an EditText.
The dialog is properly displayed at first, but shows a weird behaviour when the user selects text.
Normal state:
enter image description here
State when user selects Text:
enter image description here
My Java Code:
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AlertDialogStyle);
LayoutInflater inflater = activity.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_edit_description, null);
builder
.setView(dialogView)
.setPositiveButton("OK", (dialogInterface, i) -> {
})
.setNegativeButton("Abbrechen", (dialogInterface, i) -> {
});
Dialog dialog = builder.create();
dialog.show();
My Layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#color/color4"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Beschreibung bearbeiten"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:textAppearance="#style/AlertDialogStyle"
android:textColor="#color/colorWhite"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/descr_edit_text"
android:inputType="text"
android:hint="Beschreibung hinzufügen"
android:layout_width="match_parent"
android:longClickable="false"
android:layout_height="wrap_content"
android:textColorHint="#color/colorLightGrey3"
android:textColor="#color/colorWhite"
android:textAppearance="#style/myFont"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="4dp" />
</LinearLayout>
Related
I have the following code and only want my positive button to close if a certain condition is met, otherwise I want it to stay open with the same data:
protected void showInputDialog() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.phrase_input_layout, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setTitle("Create new phrase flashcard");
dialogBuilder.setPositiveButton("Done", (dialog, whichButton) -> {
SubmissionState state = addCardToDocument(dialogView);
if (state == SubmissionState.SUBMITTED_WITH_RESULTS_FOUND){
dialog.dismiss();
} else {
// still stay open here, but it still closes...
}
});
dialogBuilder.setNegativeButton("Cancel", (dialog, whichButton) -> Log.d("DEBUG", "Cancelled creating new phrase card."));
AlertDialog b = dialogBuilder.create();
b.show();
}
However, it appears no matter what, when I press the positive button, the dialogBuilder will close, regardless if it goes into the else statement or not; also, even if I comment out all of the code for the setPositiveButton it will still dismiss the dialog!
What am I doing wrong?
If it's relevant, here is my phrase_input_layout too:
<TextView
android:id="#+id/phrase_translate_radio_group_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="14dp"
android:layout_marginBottom="4dp"
android:text="#string/select_translation_mode"
android:textAppearance="#style/TextAppearance.AppCompat.Body2" />
<RadioGroup
android:id="#+id/phrase_translate_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/phrase_manual_translation"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/manual_translation" />
<RadioButton
android:id="#+id/eng_auto_translation"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/english_auto_translation" />
<RadioButton
android:id="#+id/swe_auto_translation"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/swedish_auto_translation" />
</RadioGroup>
<EditText
android:id="#+id/englishPhrase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="#string/english_phrase" />
<EditText
android:id="#+id/swedishPhrase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="#string/swedish_phrase" />
</LinearLayout>
You cannot do that by using the provided setPositiveButton method. If you set a listener using this method, then whenever the button is clicked the listener is invoked and then the dialog is dismissed. I don't see a way to override this behavior as AlertDialog internally uses an AlertController object that implements this behavior.
So what you can do is use custom buttons instead of the default ones.
For example, you could add two buttons at the bottom of phrase_input_layout xml.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/negativeButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<View
android:id="#+id/buttonSeparator"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/separator" />
<Button
android:id="#+id/positiveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Set click listeners for these buttons. Like so:
AlertDialog dialog;
protected void showInputDialog() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.phrase_input_layout, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setTitle("Create new phrase flashcard");
Button positiveButton = (Button) dialogView.findViewById(R.id.positiveButton);
Button negativeButton = (Button) dialogView.findViewById(R.id.negativeButton);
positiveButton.setText("Create new phrase flashcard");
positiveButton.setOnClickListener(view -> {
SubmissionState state = addCardToDocument(dialogView);
if (state == SubmissionState.SUBMITTED_WITH_RESULTS_FOUND){
dialog.dismiss();
} else {
}
});
negativeButton.setText("Cancel");
negativeButton.setOnClickListener(view -> {
dialog.dismiss()
});
dialog = dialogBuilder.create();
dialog.show();
}
I had problems with this when I was programming it. I thought I'd cured it by specifying a fixed percentage height for the body of the dialog. I thought this was bad form, because the user might have a slim display, or set large fonts, which would cut of the EditText box.
Anyway that solution has failed beyond the emulator.
It looks like Android is assigning a fixed height to a dialog, and if my custom title has devoured too much of this height, squeezing everything else off. Is this correct and how do I fix it?
Problem
public class GetUserNameDialogFragment extends DialogFragment {
final String TAG = "GetUserNameDialog";
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle);
//TODO getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getActivity().getLayoutInflater();
final View sunburst =inflater.inflate(R.layout.dialog_user_name, null);
builder.setView(sunburst);
builder.setCancelable(false)
.setPositiveButton("Let's go!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.wtf(TAG, "button press");
EditText name = (EditText) sunburst.findViewById(R.id.nameEditText);
String userName = name.getText().toString().trim();
//TODO needs to be validated
SharedPreferences sharedPref = getActivity().getSharedPreferences("userDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", userName );
editor.commit();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here's the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:srcCompat="#drawable/ic_ball_sunburst_classic"
android:background="#color/colorAccent"
/>
<LinearLayout
android:layout_width="250dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_height="125dp">
<EditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:hint="Enter your first name"/>
</LinearLayout>
</LinearLayout>
All help very much appreciated, the first impression of my app - what a disaster!
Why don't you use Dialog instead of AlertDialog ?
I built the example using a Dialog and it works perfectly
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_width=" 290dp"
android:layout_height="wrap_content"
android:background="#4CAF50">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/base"
android:background="#color/colorAccent"
/>
<EditText
android:textColor="#fff"
android:textColorHint="#fff"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your first name"/>
<Button
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="#+id/letsGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Let's Go"
android:textColor="#fff"
android:background="#android:color/transparent"/>
final Dialog dialog = new Dialog(context);
if(dialog.getWindow() != null){
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
dialog.setContentView(R.layout.dialog);
Button letsGO = (Button) dialog.findViewById(R.id.letsGo);
EditText nameEditText = (EditText) dialog.findViewById(R.id.nameEditText);
letsGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Lets GO!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.show();
I want to create a custom alertdialog with Spinner .So i used the following code.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ActivityThu.this);
alertDialog.setTitle("Thêm khoản thu");
//final LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// final View dialogView = layoutInflater.inflate(R.layout.thu_thong_bao_them, null);//Tạo dialogview từ layout thông báo
View dialogView = getLayoutInflater().inflate(R.layout.thu_thong_bao_them, null);
spinner=(Spinner)dialogView.findViewById(R.id.spdanhmuc_them);
MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());
List<String> lables = db.Load_danhmucthu();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
alertDialog.setView(dialogView);// đặt dialogview vào alertdialog
alertDialog.setIcon(R.drawable.add48x48);//đặt icon
alertDialog.setPositiveButton("Thêm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("Hủy",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
Layout for DialogAlert thu_thong_bao_them.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Danh mục"
android:id="#+id/tvdanhmuc_them"/>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tvdanhmuc_them"
android:layout_marginLeft="20dp"
android:id="#+id/spdanhmuc_them"
android:spinnerMode="dropdown">
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nội dung"
android:id="#+id/tvnoidung_them"
android:layout_below="#+id/tvdanhmuc_them"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
android:id="#+id/edtnoidung_them"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tvnoidung_them"
android:layout_below="#id/spdanhmuc_them"
android:layout_marginLeft="20dp"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ngày thu"
android:id="#+id/tvngaythu_them"
android:layout_below="#+id/tvnoidung_them"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
android:id="#+id/edtngaythu_them"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:inputType="text"
android:layout_toRightOf="#id/tvnoidung_them"
android:layout_below="#id/edtnoidung_them"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Số tiền"
android:id="#+id/tvsotien_them"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvngaythu_them"
/>
<EditText
android:id="#+id/edtsotien_them"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tvsotien_them"
android:layout_below="#id/edtngaythu_them"
android:layout_marginLeft="30dp"
android:inputType="text" />
</RelativeLayout>
Image Befor I call AlertDialog
Image After call alertdialog (data has been load into spinner but main_layout was replaced by layout of alertdialog and spinner in Dialog hasn't data)
Please help me.
After create custom view , set it to alert dialog view, don't set anything related to dialog like icon,positive buttons etc.
I wrote an application which have to call dialog to get name. I need the custom view in dialog so I used .setContentView(). But in android with api less 20 I have problems with displaying it.
This is calling dialog:
public void setName(View v) {
nameDialog = new Dialog(this);
//using our dialog
nameDialog.setContentView(R.layout.new_name);
EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
nameDialog.show();
}
the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
tools:context=".CreateNoteActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/form2_dialog_name_of_event"
android:id="#+id/textView11" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/form2_dialog_name"
android:maxLines="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_button"
android:id="#+id/button8"
android:onClick="onCancelDialog"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/okay_form2_dialog_button"
android:onClick="onFinishDialog" />
</LinearLayout>
</LinearLayout>
How it looks when api upper then 20:
http://i.imgur.com/pJ1oZky.png
Less then 20:
http://i.imgur.com/TJGEwXh.png
OK the answer is quite simple: Use the android.support.v7.app.AlertDialog
Your setName will have to look like this then:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.new_name, null);
builder.setView(dialogView);
EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
I guess you will have to play a little with your layout file because the buttons are quite a bit too far away but I think this will be just a little problem ;)
I have a DialogFragment with Buttons that look like this:
How can I change the style of the buttons to look like this?
I am extending DialogFragment, but would like to use the style that AlertDialog.Builder uses for the defaultPositive/negative buttons.
It is default interface of android 2.3 below. You need external library to make it's interface become android 4.0 above style. You can use this library to do that.
Try This
create a xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
<EditText
android:id="#+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="#string/username" />
<EditText
android:id="#+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="#string/password"/>
</LinearLayout>
then in onCreate()
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
then in the manifest
<activity android:theme="#android:style/Theme.Holo.Dialog" >