button within Alert Dialog not clickable - android

I have an Alert Dialog, within the layout for the dialog there is a button that I want to be clickable, however when I use a onclick listener the button still dosn't work.
My Alert Dialog is built up as such.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.distro_editor_popup, (ViewGroup) findViewById(R.id.layout_root));
txt_Name = (EditText) layout.findViewById(R.id.txt_LinuxName);
txt_Image = (EditText) layout.findViewById(R.id.txt_ImageName);
filemanger = (Button) layout.findViewById(R.id.fileselector);
txt_Name.setText(selected_Name);
txt_Image.setText(selected_Image);
final String oldName = selected_Name;
final String oldImage = selected_Image;
filemanger.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
}
});
alert.setView(layout);
alert.setPositiveButton(R.string.dialog_button_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String name = txt_Name.getText().toString();
String image = txt_Image.getText().toString();
// Make sure the user entered a name
if (name.equals("")) {
return;
}
if (!oldName.equals(name)) {
// Name was changed so we have to delete the old one from the profiles first!
profiles.remove(oldName);
}
if (!oldImage.equals(image)) {
// Image name has changed so we rename the mounts and config files
file_Rename(oldImage + ".mounts", image + ".mounts");
file_Rename(oldImage + ".config", image + ".config");
}
profiles.put(name, image);
lastSelected = name;
fillSpinner();
savePrefs();
}
});
alert.setNegativeButton(R.string.dialog_button_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
And then the XML layout for the dialog is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<RelativeLayout android:layout_alignBaseline="#+id/layout_root" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="8sp" android:id="#+id/RelativeLayout1">
<TextView
android:id="#+id/label_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/inputLabel_Name"
android:layout_centerVertical="true"/>
<EditText
android:id="#+id/txt_LinuxName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/label_Name"
android:hint="#string/hint_EnterName"
android:textColor="#FF111111" >
<requestFocus></requestFocus>
</EditText>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/layout_root"
android:layout_marginTop="8sp" >
<TextView
android:id="#+id/label_Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/launcher_Label_ImageName" />
<EditText
android:id="#+id/txt_ImageName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/fileselector"
android:layout_toRightOf="#id/label_Image"
android:textColor="#FF111111" >
<requestFocus></requestFocus>
</EditText>
<Button
android:id="#+id/fileselector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txt_ImageName"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="..." />
</RelativeLayout>

You aren't calling dismiss on the negative button:
alert.setNegativeButton(R.string.dialog_button_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
As for the positive, is your onClick getting fired? Put Log statements in and see if it is.

This is just a guess so it is probably wrong, but when you are setting your onClickListener, try replacing the line fileSelector.setOnClickListener(new OnClickListener) with fileSelector.setOnClickListener(new View.OnClickListener)

Related

NPE at android.text.Editable android.widget.EditText.getText()' on a null object reference

I have this NPE when I call getText() on EditText.
I have an alert and I set my layout as a view to an alert and I need to get the text from EditText.
Thank you for any help
Here is my code
LinearLayout view;
EditText input;
Button in;
Button gfxtr;
Button gfh;
private void numItem(int position) {
view = (LinearLayout) getLayoutInflater()
.inflate(R.layout.dialog, null);
input = (EditText) findViewById(R.id.input);
in = (Button) findViewById(R.id.in);
gfxtr = (Button) findViewById(R.id.gfxtr);
gfh = (Button) findViewById(R.id.gfh);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Введите новое количество");
alert.setView(view);
alert.setPositiveButton("ДА", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String qq = input.getText().toString();
}
});
alert.setNegativeButton("НЕТ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
XML:
<?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:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"></EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="in"
android:scaleType="fitCenter"
android:text="шт"></Button>
<Button
android:id="#+id/gfxtr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="gfxtr"
android:scaleType="fitCenter"
android:text="пачек"></Button>
<Button
android:id="#+id/gfh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="gfh"
android:scaleType="fitCenter"
android:text="пар"></Button>
</LinearLayout>
</LinearLayout>
I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.text.Editable android.widget.EditText.getText()' on a null
object reference
You don't call findViewById on view. Try it like this:
input = (EditText) view.findViewById(R.id.input);

ImageView setOnClickListener in a dialog

I create a custom dialog box with this method:
public Dialog onCreateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_rate, null))
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putBoolean("rate",false);
settingsEditor.apply();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=de.resper.pzcrettungsdienst"));
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putBoolean("rate",false);
settingsEditor.apply();
}
})
.setNeutralButton(R.string.later, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putInt("count",0);
settingsEditor.apply();
}
});
return builder.create();
}
I open the dialog like this:
Dialog dialog = onCreateDialog();
dialog.show();
This is my layout file:
<?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:layout_gravity="center"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/dialog_message"
android:id="#+id/textView15"
android:layout_gravity="center_horizontal"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center"
android:text="#string/resperLink"
android:layout_margin="20dp"
android:id="#+id/textView" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fbImg"
android:src="#drawable/fb_icon"
android:layout_margin="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gplusImg"
android:src="#drawable/gplus_icon"
android:layout_margin="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/twImg"
android:src="#drawable/tw_icon"
android:layout_margin="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pinImg"
android:src="#drawable/pin_icon"
android:layout_margin="5dp"/>
</LinearLayout>
</LinearLayout>
In the layout file are images, is it possible to set on the images in the layout "setOnClickListener"? The Buttons working, but I want to add some more "actions" for the images.
In the layout file are images, is it possible to set on the images in
the layout "setOnClickListener"?
yes it is possible. Keep a reference to the inflated view, and use this to findViewById
View view= inflater.inflate(R.layout.dialog_rate, null);
view.findViewById(R.id.yourid).setOnClickListener(...);
builder.setView(view)

How to build a custom Dialog "Button area"

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.

Android Null Pointer,where shoud I put the code for activity?

I set a swith card with three tab in the main activity,there is a listview in the third tab,click the listview will pop up time options dialog box.
public void onSetWarn(){
LayoutInflater factoryInflater = LayoutInflater.from(MainActivity.this);
final View dialogView = factoryInflater.inflate(R.layout.dialog, null);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("set")
.setView(dialogView)
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "set success!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
/*
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
*/
dialog.show();
}
where should i put this code
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
I trid several places,but all the NULL pointer errors.
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reminder the time"
/>
<TimePicker
android:id="#+id/TimePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remind way"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ring"
/>
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="shake"
/>
</LinearLayout>
</LinearLayout>
You should use dialogView.findViewById(R.id.TimePicker1) instead of this.findViewById(R.id.TimePicker1).That is because TimePicker1 exists in the view tree of dialogView (You load it from dialog.xml by LayoutInflator)

Saving EditText from a Dialog Box in android

Does anyone know how to save the text that the user will input after a dialog Box pops up?
I'm first inflating a view for the dialog Box, then letting the user input a string and displaying it somehow. Here's my code for the Dialog Box inflating. My question is How do I save the text that someone inputs into my field from the AlertDialog Box. My XML is at the bottom
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEXT_ENTRY:
LayoutInflater mInflater = LayoutInflater.from(this);
final View textEntryView = mInflater.inflate(
R.layout.alert_text_entry, null);
return new AlertDialog.Builder(AlarmList.this)
.setIcon(R.drawable.alert_icon)
.setTitle("Enter your name")
.setView(textEntryView)
.setPositiveButton("accept",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNegativeButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
}
return null;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:padding="12dp"
android:textSize="16sp" >
</TextView>
<EditText android:id="#+id/alarm_name_text"
android:layout_height="3dp"
android:text=""
android:layout_below="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_alignLeft="#+id/rowTextView"
android:layout_alignRight="#+id/rowTextView">
</EditText>
<CheckBox android:id="#+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true" android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
As mentioned in EditText docs, you can call getText() to get the text from the EditText view.

Categories

Resources