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" >
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();
}
My app displays an AlertDialog
page1WalkthroughDialogBuilder = new AlertDialog.Builder(activity, R.style.Widget_AppCompat_Light_ActionBar);
page1WalkthroughDialogBuilder
.setCancelable(false)
.setView(page1WalkthroughDialogLayout)
.setPositiveButton(getString(R.string.goto_next), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
page2Walkthrough.show();
dialog.cancel();
}
})
.setNegativeButton(getString(R.string.exit_dialog), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
manager.beginTransaction().show(LaunchActivity.editProfileFragment).commit();
dialog.cancel();
}
});
page1Walkthrough = page1WalkthroughDialogBuilder.create();
that uses the following layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rel_layout"
android:layout_height="match_parent"
android:background="#drawable/park_with_people"
mlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
android:text="#string/welcome_hdr"
android:textSize="35sp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp" />
<uomini.com.wegrokstrasbourg.TextViewWithImages
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/heading"
android:text="#string/welcome_text"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp" />
</RelativeLayout>
Everything displays just fine except that the buttons have a white background:
How can I change the color of the background?
You should create a style and then apply it in the Builder constructor:
<style name="MyTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">#color/myColor</item>
</style>
and then :
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyTheme)
...
...
.create();
I'm building a simple AlertDialog with custom layout from XML. Here is how it's supposed to look (taken from Eclipse): It's simply 4 columns:
Here is the xml of the image above:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="#+id/colorpicker_dialog_color1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFF"
android:tag="0xFFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFDD66"
android:tag="0xFFDD66" />
<ImageView
android:id="#+id/colorpicker_dialog_color3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#66CCFF"
android:tag="0x66CCFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#B6C0D2"
android:tag="0xB6C0D2" />
</LinearLayout>
Here is the code of the costume Alert Dialog:
public void showColorPickerDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater
.inflate(R.layout.color_picker_dialog, null);
ImageView clr1 = (ImageView) layout
.findViewById(R.id.colorpicker_dialog_color1);
clr1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// .. code
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("cccc");
builder.setView(layout)
.setCancelable(true)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
}).show();
}
The problem is that in my dialog I dont see any column.
Try using Views instead of ImageViews in your layout. The problem using ImageViews in your case is that since you do not specify the src attribute for the ImageView, it will display a 0x0-sized image, which leaves your views invisible.
I am trying to design AlertDialog, but I can't get rid of the black surrounding (As indicated by the red line), and the orange surrounding (As indicated by the blue line).
This is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/darker_gray" >
<TextView
android:src="#drawable/save_as"
android:id="#+id/label_save_as"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/save_as" />
<EditText
android:id="#+id/filename"
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/filename" />
</LinearLayout>
This is the code:
AlertDialog.Builder saveAsBuilder = new AlertDialog.Builder(this);
LayoutInflater saveAsInflater = this.getLayoutInflater();
saveAsBuilder.setView(saveAsInflater.inflate(R.layout.saveas, null))
.setNeutralButton(R.string.saveAsImg, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// do something
}
})
.setPositiveButton(R.string.saveAsSkt, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
});
d = saveAsBuilder.create();
d.setCanceledOnTouchOutside(true);
d.show();
How can I get rid of those?
BTW , I am using API 10.
Once try to change like this and try..
AlertDialog.Builder saveAsBuilder = new AlertDialog.Builder(this,android.R.style.Theme_Translucent);
setting the theme for the dialog to remove the black spaces
with using dialog..
Dialog dialog=new Dialog(getApplicationContext());
dialog.setContentView(R.layout.yourlayout);
for this you have to create two buttons in layout and implement listeners for that..
I'm trying to get a custom Dialog to work. It should have no Title, a basic TextMessage and my Custom Layout where normally Buttons appear.
I tried to accomplish this with an AlertDialog.Builder, extending Dialog, call methods on the Dialog and still not get the expected result.
This is the layout, I Like to use as a custom ButtonArea (layout/dialog_footer):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/dialog_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/never_show_again"
android:textColor="#color/black"
android:visibility="gone"
android:background="#color/dialog_button_background"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/dialog_checkbox"
android:layout_alignWithParentIfMissing="true"
android:orientation="horizontal"
android:background="#color/dialog_button_background"
android:paddingBottom="-10dip">
<Button
android:id="#+id/dialog_btn_positive"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/ok"
android:gravity="center"
android:visibility="gone"
/>
<Button
android:id="#+id/dialog_btn_negative"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/btn_cancel"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
The simplest try goes like this:
AlertDialog.Buiilder builder = new AlertDialog.Builder(context);
builder.setMessage("my Message");
Dialog dialog = builder.create();
//builder has `setView for Message or setCustomTitle, but no setCustomFooter
LayoutInflater inflater = LayoutInflater.from(context);
View footer = inflater.inflate(R.layout.dialog_footer);
//either NullPointer Exception
dialog.addContentView(footer, footer.getLayoutParams);
//or AndroidRuntimeException (requestFeature() must be called befoire adding content)
dialog.addContentView(footer, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
If I try to emulate the "MessageArea" by putting a TextView on Top of my Layout it works, but has very ugly black borders at the bottom (I guess its the custom Dialog-Theme).
Whats the best way to keep the "look and feel" of a System Dialog, but replace the Buttons with my own View and handle anything myself?
Hello friend please check this post it may be helpful for your problem
Android - Custom AlertDialog Background Color
Here which i have build my own Custom Edit Dialog for my app see the Layout code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Name: "
android:textColor="#fff" />
<EditText
android:id="#+id/txtDelName"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Contact No :"
android:textColor="#fff" />
<EditText
android:id="#+id/txtDelAge"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ems="10" >
<requestFocus />
</EditText>
</TableRow>
<TableRow android:layout_height="wrap_content" >
<Spinner
android:id="#+id/spinDiagDept"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_span="2"
android:prompt="#string/Rel_prompt"
android:visibility="visible" />
</TableRow>
</TableLayout>
And here is the alert dialog code
public class Alerts {
public static AlertDialog ShowEditDialog(final Context con,final Emergencydb emp)
{
AlertDialog.Builder b=new AlertDialog.Builder(con);
b.setTitle("Emergency Contact Details");
LayoutInflater li=LayoutInflater.from(con);
View v=li.inflate(R.layout.editdialog, null);
b.setIcon(android.R.drawable.ic_input_get);
b.setView(v);
final TextView txtName=(TextView)v.findViewById(R.id.txtDelName);
final TextView txtAge=(TextView)v.findViewById(R.id.txtDelAge);
final Spinner spin=(Spinner)v.findViewById(R.id.spinDiagDept);
Utilities.ManageDeptSpinner(con, spin);
for(int i=0;i<spin.getCount();i++)
{
long id=spin.getItemIdAtPosition(i);
if(id==emp.getDept())
{
spin.setSelection(i, true);
break;
}
}
txtName.setText(emp.getName());
txtAge.setText(String.valueOf(emp.getAge()));
b.setPositiveButton("Modify", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
emp.setName(txtName.getText().toString());
emp.setAge(String.valueOf(txtAge.getText().toString())); emp.setDept((int)spin.getItemIdAtPosition(spin.getSelectedItemPosition()));
try
{
#SuppressWarnings("rawtypes")
DatabaseHelper db=new DatabaseHelper(con);
db.UpdateEmp(emp);
Toast.makeText(getBaseContext(), "Modified Successfully", Toast.LENGTH_SHORT).show();
}
catch(Exception ex)
{
}
}
});
b.setNeutralButton("Delete", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
#SuppressWarnings("rawtypes")
DatabaseHelper db=new DatabaseHelper(con);
db.DeleteEmp(emp);
}
});
b.setNegativeButton("Cancel", null);
return b.create();
//diag.show();
}
protected static Context getBaseContext() {
// TODO Auto-generated method stub
return null;
}
public static void ShowEmpAddedAlert(
android.view.View.OnClickListener onClickListener) {
// TODO Auto-generated method stub
}
}
with the help of Aleks G I managed to get it work:
the code flow goes like this:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("MyMessage");
LayoutInflater inflater = LayoutInflater.from(context);
View footerView = inflater.inflate(R.layout.apo_plus_dialog_footer, null);
AlertDialog alertDialog = builder.create();
alertDialog.setView(footerView, 0,0,0,0);
if (!hasTitle){
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
alertDialog.show();
just in case someone wants to have a similar Dialog behavior than me.