AlertDialog Layout below the buttons - android

I have my own layout for an AlertDialog, and if I use setPositiveButton everything works. But when i use setItems, my layout is shown below the item buttons.
How can I show my custom layout on top?
Here is my code:
private void selectImage(){
final CharSequence[] items = { TAKE_PICTURE, FROM_GALLERY, CANCLE};
AlertDialog.Builder builder = new AlertDialog.Builder(Add_Object.this);
LayoutInflater inflater = this.getLayoutInflater();
View content = inflater.inflate(R.layout.alert_dialog, null);
builder.setView(content);
((TextView) content.findViewById(R.id.dialogTitle)).setText(R.string.addPictureTitle);
builder.setItems(items, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals(TAKE_PICTURE)) {
captureImage();
} else if (items[item].equals(FROM_GALLERY)) {
chooseFromGallery();
} else if (items[item].equals(CANCLE)) {
dialog.dismiss();
}
}
});
builder.show();
}
}
And my layout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:textSize="25sp"
android:textColor="#color/main_color"/>
<View
android:layout_width="match_parent"
android:id="#+id/dialogDivider"
android:layout_below="#id/dialogTitle"
android:layout_height="2dp"
android:background="#color/main_color" />
<TextView
android:id="#+id/dialogText"
android:layout_below="#+id/dialogDivider"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:textColor="#color/darkgrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Thanks in advance.

You can use builder.setCustomTitle(content) instead of setView(content), but this will add a separator line between your layout and the item buttons...
EDIT:
As an alternative, you can add the list to your custom layout, e.g. with an TextView like this:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/take_picture"
android:layout_below="#id/dialogText"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="26sp"
android:onClick="takePicture"/>
and define the takePicture method in your MainActivity:
public void takePicture(View view) {
Log.i("MainActivity", "take picture");
//cancel the dialog
dialog.dismiss();
}
the dialog is saved in a class variable, and initialized in selectImage():
private void selectImage(){
final CharSequence[] items = { "TAKE_PICTURE", "FROM_GALLERY", "CANCLE"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View title = inflater.inflate(R.layout.alert_dialog, null);
builder.setView(title);
((TextView) title.findViewById(R.id.dialogTitle)).setText("picture title");
((TextView) title.findViewById(R.id.take_picture)).setText(items[0]);
//init dialog
dialog = builder.create();
dialog.show();
}

Related

Trying to set an OnClickListener on a button in a DialogFragment

I am trying to set an onclicklistener for a button in my dialog. However, when my dialog loads, it crashes, saying Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference on the line date = (Button) v.findViewById(R.id.DateButton);. I have the following dialog:
public class EventsDialog extends DialogFragment {
private Button date;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.event_dialog, container, false);
date = (Button) v.findViewById(R.id.DateButton);
// set onclicklistener
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OpenDate();
}
});
return v;
}
#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.event_dialog, null));
return builder.create();
}
public void OpenDate() {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "datepicker");
}
}
And the layout:
android.support.constraint.ConstraintLayout 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="#dimen/mr_controller_volume_group_list_max_height"
android:layout_height="match_parent"
android:background="#dfdfdf"
android:orientation="vertical">
<TextView
android:id="#+id/eventsHeader"
android:layout_width="0dp"
android:layout_height="46dp"
android:background="#color/colorAccent"
android:text="Add a New Event!"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:textColor="#android:color/white" />
<EditText
android:id="#+id/EventName"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="7dp"
android:hint="Event Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/eventsHeader" />
<Button
android:id="#+id/DateButton"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/EventName" />
<Button
android:id="#+id/TimeButton"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/DateButton" />
<EditText
android:id="#+id/EventLocation"
android:layout_width="0dp"
android:layout_height="46dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:hint="Location"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TimeButton" />
<EditText
android:id="#+id/EventDetails"
android:layout_width="0dp"
android:layout_height="186dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Details"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/EventLocation" />
<ImageButton
android:id="#+id/SaveButtonEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/EventDetails"
app:srcCompat="#android:drawable/ic_menu_save" />
</android.support.constraint.ConstraintLayout>
you are inflating your layout two times. Do inflate it in onCreateDialog() method and do something like below and remove onCreateView() method:
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.event_dialog, null,false);
builder.setView(view);
builder.setTitle("Your Title");
date = (Button) view.findViewById(R.id.DateButton);
// set onclicklistener
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OpenDate();
}
});
return builder.create();
I have used DialogFragment on my project, and I had wrote it by
the following way.
Here is my AlertDFragment.Java
public class AlertDFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
// Set Dialog Icon
.setIcon(R.drawable.androidhappy)
// Set Dialog Title
.setTitle("Alert DialogFragment")
// Set Dialog Message
.setMessage("Alert DialogFragment Tutorial")
// Positive button
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do something else
}
})
// Negative Button
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do something else
}
}).create();
}
}
And the output is,

Add dynamically components to a Custom Dialog in Android

I'd need your help to do something in Android, in the following the use case.
I have created a custom dialog in Android, whose layout is:
dialog_threshold.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp">
<ImageView
android:src="#drawable/ic_media_route_on_03_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/set_target" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_threshold" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout_threshold">
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/targetSelected"
android:layout_weight="1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/threshold_operator_spinner"
android:layout_weight="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/threshold_val"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
And in the Activity, when I click on a specific button, I have something like this, to create the custom dialog:
setThreshold_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(DiseaseActivity.this);
//Inflate the custom layout
View mView = getLayoutInflater().inflate(R.layout.dialog_threshold, null);
//Set title for dialog
mBuilder.setTitle("Set thresholds for your target");
//Define the spinner inside your custom layout
final Spinner mSpinner = (Spinner) mView.findViewById(R.id.spinner_threshold); //because it doesn't exist in the main layout, but only in the custom layout
//Define the ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DiseaseActivity.this,
android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.threshold_choices));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
String ThresholdSelection = mSpinner.getSelectedItem().toString();
}
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if ((dialog2 != null) && dialog2.isShowing() && !mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
} else {
dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//Set the positive and negative button for the custom dialog
mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
disease.setThreshold(mSpinner.getSelectedItem().toString());
if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
Toast.makeText(DiseaseActivity.this,
mSpinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT)
.show();
setInterval_button.setEnabled(true);
}
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mBuilder.setView(mView);
dialog2 = mBuilder.create();
dialog2.show();
dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
});
What I want to do now is to add dynamically other components (like EditText or TextView) to the above Dialog before is it shown.
How can I do it?
Thanks in advance!
just create your custom view >> find the parent(in which viewgroup you want to add) >>> and add it. eg:-
mView.addView(new TextView(ActivityContext),LayoutParamss);
mBuilder.setView(mView);

Buttons in Custom Dialog won't respond

I'm trying to create a custom dialog for the settings of my application, the problem is, that the custom buttons, EditText & the spinner don't respond to user activity. I hope someone can help.
Here is my Code:
public class SettingsDialog extends DialogFragment {
private EditText editText;
private Spinner ipSpinner;
private MainActivity mainActivity;
private Settings settings;
private int selectedIP;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_settings, null);
mainActivity = (MainActivity) getActivity();
settings = mainActivity.getSettings();
selectedIP = settings.getSelectedIP();
editText = (EditText) dialogView.findViewById(R.id.edit_text_ip);
ipSpinner = (Spinner) dialogView.findViewById(R.id.spinner_ip);
ImageButton imageButton = (ImageButton) dialogView.findViewById(R.id.image_button_save_ip);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
});
ipSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
selectedIP = position;
editText.setText(settings.getIP(selectedIP));
mainActivity.showToast(Integer.toString(position));
settings.settingsSave();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
and my xml for the dialog:
<LinearLayout 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:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP-address"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="#+id/spinner_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/spinner_ip_visual"
android:entryValues="#array/spinner_ip_values" />
<EditText
android:id="#+id/edit_text_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8" />
<ImageButton
android:id="#+id/image_button_save_ip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_done_black_24dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unit"
android:textAllCaps="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/button_unit_celsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Celsius"
android:textAllCaps="true" />
<RadioButton
android:id="#+id/button_unit_fahrenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenheit"
android:textAllCaps="true" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickGenerateRandomData"
android:text="Generate random data"
android:textAllCaps="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickDropDatabase"
android:text="delete weatherdata"
android:textAllCaps="true" />
Thanks in advice
You are inflating dialogView and then you set listeners for the views that are in dialogView, however when you set the view for dialog builder, you inflate the new view.
This is wrong.
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
This is correct
builder.setView(dialogView)

How can i make my alert dialogue into a custom alert in android

How can i make my alert dialogue into a custom alert in android.please help me to display my dialogue box in a custom alert. my code is pasted here.
the code contains an out put of 10 list view. and when click on item in the list it could be alerted..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of planet names.
final String[] planets = new String[] { "Allu", "Abin", "Bibin", "Aswathy",
"Jibin", "Saran", "Jobin", "Neethu","ammu","Ram"};
final ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
/*// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );*/
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here
alert.setMessage("Edit Your Name Here"); //Message here
final EditText input = new EditText(context);
input.setText((String)planetList.get(position));
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String srt = input.getEditableText().toString();
Toast.makeText(context,srt, Toast.LENGTH_LONG).show();
planetList.set(position, srt);
listAdapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}); //End of alert.setNegativeButton
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
}
}
That's a simple way to make a popup dialog with a custom xml layout, It will surely work:
Make an xml file for your dialog. Set android:layout_width="wrap_content" and android:layout_height="wrap_content" or any other size. You can also set a background for your layout.
For example this is an xml layout for a popup window:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/black" >
<TextView
android:id="#+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="title"
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/details_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Enter your name here:"
android:textColor="#color/white"
android:layout_weight="1"
android:layout_gravity="center"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center"
android:layout_marginRight="8dp"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="#+id/editText"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
style="?android:attr/buttonBarStyle" >
<Button
android:id="#+id/cancel_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:onClick="cancel"
android:textColor="#color/white"
style="?android:attr/buttonStyle" />
<Button
android:id="#+id/exit_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"
android:onClick="ok"
android:textColor="#color/white"
style="?android:attr/buttonStyle" />
</LinearLayout>
</LinearLayout>
And that's how you show the popup:
private void showPopup(final Activity context) {
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup_layout);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutPopup = layoutInflater.inflate(R.layout.layout_popup, viewGroup);
popup = new PopupWindow(context);
popup.setContentView(layoutPopup);
popup.setWidth(LayoutParams.WRAP_CONTENT);
popup.setHeight(LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
popup.showAtLocation(layoutPopup, Gravity.CENTER, 0, 0);
}
You can use simple onClick methods for buttons in your popup dialog:
public void cancel(View v){
Toast.makeText(getApplicationContext(), "Canceled",
Toast.LENGTH_LONG).show();
popup.dismiss();
}
public void ok(View v){
Toast.makeText(getApplicationContext(), "Done",
Toast.LENGTH_LONG).show();
popup.dismiss();
}
You can also read this for another way of creating custom dialogs.
public class MyDialog extends DialogFragment {
public static MyDialog getInstance() {
MyDialog dialog = new MyDialog ();
Bundle bundle = new Bundle();
bundle.putInt("your key", "save something");
dialog.setArguments(bundle);
return dialog;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder vBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout."your custom layout", null);
yourEditText = (TextView) view.findViewById(R.id."your edit text id");
Bundle bundle = getArguments();
if(bundle!=null){
... do something
}
vBuilder.setView(view);
return vBuilder.create();
}
}
To open this dialog
MyDialog.getInstance().show(getFragmentManager(), "your TAG");
Why can't you use dialog?
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);//create an xml and use it.
dialog.setCancelable(true);
passwordEmail = (EditText) dialog
.findViewById(R.id.dialog_txt_name);
dialog.show();
Instead of this :
final EditText input = new EditText(context);
input.setText((String)planetList.get(position));
alert.setView(input);
Write this :
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout."your custom layout", null);
alert.setView(view);

How to set Title,Icon for custom dialog in android

I've a class which extends Dialog in android.It shows custom dialog well.But i couldn't set icon & title for that dialog.How could i do that?
My code:
public class helpDialog extends Dialog implements OnClickListener {
Button okButton;
String Description;
TextView text;
public helpDialog(Context context, String desc) {
super(context);
Description=desc;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.custom);
text=(TextView)findViewById(R.id.text);
text.setText(Description);
okButton = (Button) findViewById(R.id.submit);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}
}
My custom.xml:
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textColor="#000000"
android:gravity="center"
android:text="Help alert for iDispatch"
android:textSize="18dp" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textColor="#FFFFFF" />
USE THIS ONE
private AlertDialog AskOption()
{
AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this)
//set message, title, and icon
.setTitle("Confirm Delete")
.setMessage("Do you want to Delete Employee "+listview_array[1]+" ?")
.setIcon(R.drawable.dialog_warning)
.setPositiveButton(resource.getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
if(dh.selectEmployeeTypeById(EmpId).size()>0)
{
//CODE
}
dialog.dismiss();
}
})
.setNegativeButton(resource.getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myQuittingDialogBox;
}
for custom dialog a small example layout this is my custom dialog
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".StaffTimeClock" >
<TextView android:id="#+id/img"
android:layout_width="fill_parent"
android:text="#string/selectjob"
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
android:background="#203C56"
android:padding="10dp"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:cacheColorHint="#222000" />
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#000"/>
<LinearLayout android:id="#+id/lin00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal">
<Button
android:layout_weight="0.1"
android:contentDescription="#string/ok"
android:id="#+id/okBtn"
android:layout_gravity="center"
android:layout_margin="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"/>
<Button
android:layout_weight="0.1"
android:contentDescription="#string/ok"
android:id="#+id/selectBtn"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/selectall"/>
<Button
android:layout_weight="0.1"
android:contentDescription="#string/cancel"
android:id="#+id/cancelBtn"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"/>
</LinearLayout>
</LinearLayout>
this is the code call the dialog box
private void showPopUp(int eid)
{
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("");
LayoutInflater inflater = getLayoutInflater();
final View PopupLayout = inflater.inflate(R.layout.jobselection, null);
helpBuilder.setView(PopupLayout);
final AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
okbtn = (Button)PopupLayout.findViewById(R.id.okBtn);
caneclbtn = (Button)PopupLayout.findViewById(R.id.cancelBtn);
selectallbtn = (Button)PopupLayout.findViewById(R.id.selectBtn);
clearallbtn = (Button)PopupLayout.findViewById(R.id.clearallBtn);
jobList = (ListView)PopupLayout.findViewById(R.id.list);
mylist = new ArrayList<HashMap<String, String>>();
for(int i=0;i<Punchedjobs.size();i++)
{
map = new HashMap<String, String>();
map.put("name", Punchedjobs.get(i));
mylist.add(map);
}
sd = new SimpleAdapter(StaffTimeClock.this,mylist,R.layout.jobslist,
new String[]{"name"},new int[]{R.id.jobText});
jobList.setAdapter(sd);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
\\code
}
});
caneclbtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
helpDialog.dismiss();
}
});
selectallbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
}
);
}

Categories

Resources