I created an AlertDialog such that when you press a Button, the Dialog pops up and a layout appears with EditTexts. However, I created the layout in the actual code rather than in the xmlfile. For some reason, when the AlertDialog pops up, it's not able to find the EditText field and gives me a NullPointerException.
//private Lecture lecture;
private LectureManager lectureManager;
public void addWork(View view) {
LinearLayout layout = new LinearLayout(this);
EditText weight = new EditText(this);
EditText mark = new EditText(this);
mark.setInputType(InputType.TYPE_CLASS_NUMBER);
weight.setInputType(InputType.TYPE_CLASS_NUMBER);
weight.setId(99);
mark.setId(100);
layout.addView(mark);
layout.addView(weight);
AlertDialog.Builder addwork = new AlertDialog.Builder(this);
addwork.setView(layout);
addwork.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText eweight = (EditText) findViewById(99);
EditText emark = (EditText) findViewById(100);
String coursename = ecoursename.getText().toString();
And for some reason, I'm getting a NullPointerException at the "EditText weight" line. I believe that maybe it's not finding anything with ID 99 and that the EditText might be out of scope? Thanks in advance!
Actually, when you make these calls:
EditText eweight = (EditText) findViewById(99);
EditText emark = (EditText) findViewById(100);
you're calling the findViewById() method of your Activity, not the AlertDialog. In order to retrieve the views from the Dialog, you can use something like this, inside onClick():
EditText eweight = (EditText) ((AlertDialog)dialog).findViewById(99);
EditText emark = (EditText) ((AlertDialog)dialog).findViewById(100);
Hope this helps.
Try this..
Use final for both EditText while initilization like below
final EditText weight = new EditText(this);
final EditText mark = new EditText(this);
Then you can get the text from EditText in PositiveButton
String weight_txt = weight.getText().toString().trim();
String mark_txt = mark.getText().toString().trim();
No need to set id for EditText also no need findViewById.
i think you need to do this
EditText eweight = (EditText) view.findViewById(99);
and view is , what you have passed in your addWork() method parameter.
Related
This is what I want and can't do. After I choose a date, I need to open a dialog with information about the date.
Can anyone help me with the code for this?
This is the code to open the method:
private void abrirPopUpAlSeleccionarFecha(){
//defino los objetos de mi popup
EditText txtNombrePersona;
EditText txtEdadPersona;
EditText txtFechaSeleccionada;
EditText txtIdeasRegalo;
Button btnCerrarPopUp;
txtNombrePersona = (EditText) findViewById(R.id.txtNombrePersona);
txtEdadPersona = (EditText) findViewById(R.id.txtEdadPersona);
txtFechaSeleccionada = (EditText) findViewById(R.id.txtFechaEscogida);
txtIdeasRegalo = (EditText) findViewById(R.id.txtIdeasRegalo);
btnCerrarPopUp = (Button) findViewById(R.id.buttonClosePopUp);
dialogoPopUpFechaSeleccionada.setContentView(R.layout.fecha_seleccionada_popup);
dialogoPopUpFechaSeleccionada.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
EditText txtNombrePersona;
EditText txtEdadPersona;
EditText txtFechaSeleccionada;
EditText txtIdeasRegalo;
Button btnCerrarPopUp;
txtNombrePersona = (EditText)view.findViewById(R.id.txtNombrePersona);
txtEdadPersona = (EditText) view.findViewById(R.id.txtEdadPersona);
txtFechaSeleccionada = (EditText) view.findViewById(R.id.txtFechaEscogida);
txtIdeasRegalo = (EditText) view.findViewById(R.id.txtIdeasRegalo);
btnCerrarPopUp = (Button) view.findViewById(R.id.buttonClosePopUp);
builder.setView(view);
builder.setCancelable(true);
final Dialog dialog = builder.create();
dialog.show();
Hi I want to get user name and user gender using alert dialog so I add:
AlertDialog.Builder user= new AlertDialog.Builder(this);
user.setTitle("New Student");
user.setMessage("What is your Name?");
final RadioGroup genderRG= new RadioGroup(this);
RadioButton radiomr = new RadioButton(this);
radiomr.setText("Mr");
// radiomr.setId(1);
genderRG.addView(radiomr);
RadioButton radiomiss = new RadioButton(this);
radiomiss.setText("Miss");
// radiomiss.setId(2);
genderRG.addView(radiomiss);
user.setView(genderRG);
final EditText input = new EditText(this);
user.setView(input);
user.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
// get data
}
});
user.setNegativeButton("CANCEL", null);
user.create().show();
But when I run it, It just gives me an EditText.
Please! How can I get Gender and Name in same AlertDialog using radio button or spinner.
You should create your own layout and set it via setView().
You are instead inflating just the EditText, this is why you have just that in the dialog.
In any case it is better to use DialogFragment to create your own dialog version.
More here https://developer.android.com/reference/android/app/DialogFragment
I have an activity with 3 edittexts and a button.
I have a second activity with 2 textviews.
When button is clicked i want two random edittexts values from activity1 to replace the text on textviews on activity2.
I managed to do that, but not randomly. How can I make it random?
Here is the first activity.
final EditText et = (EditText) findViewById(R.id.editText);
final EditText et1 = (EditText) findViewById(R.id.editText2);
final EditText et2 = (EditText) findViewById(R.id.editText3);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
Intent i = new Intent(MainActivity.this, postavke.class);
i.putExtra("prvi", et.getText().toString());
i.putExtra("drugi", et1.getText().toString());
i.putExtra("treci", et2.getText().toString());
startActivity(i);
}
});
}
Here is the second activity.
TextView tv = (TextView) findViewById(R.id.asdf);
tv.setText(getIntent().getExtras().getString("prvi"));
TextView dr = (TextView) findViewById(R.id.asdg);
dr.setText(getIntent().getExtras().getString("drugi"));
In your second activity:
String[] texts = new String[]{
getIntent().getExtras().getString("prvi"),
getIntent().getExtras().getString("drugi"),
getIntent().getExtras().getString("treci"),
};
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Random random = new Random();
tv.setText(texts[random.nextInt(3)]);
dr.setText(texts[random.nextInt(3)]);
Or, for unique values:
First activity:
final EditText et = (EditText) findViewById(R.id.editText);
final EditText et1 = (EditText) findViewById(R.id.editText2);
final EditText et2 = (EditText) findViewById(R.id.editText3);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
ArrayList<String> texts = new ArrayList<>();
texts.add(et.getText().toString());
texts.add(et1.getText().toString());
texts.add(et2.getText().toString());
Intent i = new Intent(MainActivity.this, postavke.class);
i.putExtra("texts", texts);
startActivity(i);
}
});
}
Second activity:
ArrayList<String> texts = getIntent().getExtras().getStringArrayList("texts");
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Collections.shuffle(texts);
tv.setText(texts.get(0));
dr.setText(texts.get(1));
Add your Strings(Eg : getIntent().getExtras().getString("prvi")) from previous Activity to a ArrayList.
Then do simply Collection.shuffle(your_list);
Get items from ArrayList & set it to your TextViews.
Declare an array that will contain the values:
public String[] randomAnswers = new String[3]; //3 because you said you have 3 random strings
In your onCreate() add the values to the array:
randomAnswers[0] = getIntent().getExtras().getString("prvi");
randomAnswers[1] = getIntent().getExtras().getString("drugi");
randomAnswers[2] = getIntent().getExtras().getString("treci");
Select two random values:
private static final Random RANDOM = new Random(); //in java.util package
Inside onCreate:
final int randomInt1 = RANDOM.nextInt(3); //nextInt(int) goes from 0 to int-1, considering arrays start at index=0 we want to get a random number from: 0, 1, 2
int randomInt2;
do {
randomInt2 = RANDOM.nextInt(3);
} while (randomInt1 == randomInt2);
final String firstRandomString = values[randomInt1];
final String secondRandomString = values[randomInt2];`
you can use some thing like this the edited form of #Joe Frostick answere:
String[] texts = new String[]{
getIntent().getExtras().getString("prvi"),
getIntent().getExtras().getString("drugi"),
getIntent().getExtras().getString("treci"),
};
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Random random = new Random();
texts.remove(random.nextInt(3));
tv.setText(texts[0]);
dr.setText(texts[1]);
Thanks for the help everyone, I really didn't expect so many answers in not much time. You're the best!
Right, I know there's alot of similar questions. I've researched on stackoverflow as well as on the internet about this but still stumped.
This code is in a fragment.
...
private Context context = getActivity();
public void Dialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// 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
View mView = inflater.inflate(R.layout.dialog, null);
alertDialogBuilder.setView(mView);
EditText a = (EditText) mView.findViewById(R.id.a);
EditText b = (EditText) mView.findViewById(R.id.b);
EditText c = (EditText) mView.findViewById(R.id.c);
//a.setText("abc");
//b.setText("xyz");
//c.setText("123");
strA = a.getText().toString();
strB = b.getText().toString();
final String strC = c.getText().toString();
}
This should be a typical approach to getting the view of the inflated layout and using it to access the elements inside the view, but it's not working no matter what I tried, I just could not get strA, strB and strC values using getText().toString().
//a.setText("abc");
//b.setText("xyz");
//c.setText("123");
But, if I uncomment the above 3 lines, the values get sent across. And I can receive them inside strA, strB, strC. Why is this so? I don't get it at all.
Any help greatly appreciated, thank you!
You are trying to get values at the time you initialize the dialog. you need to get values after an action, like a button click
alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
strA = a.getText().toString();
strB = b.getText().toString();
}
});
Have a look at this display textview in alert dialog
Looks to me like you're checking the values of the EditTexts as soon as they're created. Which of course the value is going to be null every time since your user hasn't had time to type anything. I think you need a SubmitButton or something similar that checks the EditTexts in its onClickListener. Though I'll admit I've never inflated a view into an alert dialog.
Try like this.
MyDialogue is your Activity name.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MyDialogue.this);
// Get the layout inflater
LayoutInflater inflater = MyDialogue.this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View mView = inflater.inflate(R.layout.dialog, null);
alertDialogBuilder.setView(mView);
final EditText a = (EditText) mView.findViewById(R.id.a);
final EditText b = (EditText) mView.findViewById(R.id.b);
final EditText c = (EditText) mView.findViewById(R.id.c);
a.setText("abc");
b.setText("xyz");
c.setText("123");
alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
final String strA = a.getText().toString();
final String strB = b.getText().toString();
final String strC = c.getText().toString();
Log.e("tag", strA + " " + strB + " " + strC );
}
});
alertDialogBuilder.show();
The layout is not yet initiated when you are trying to set the values, you need to call AlertDialog alertDialog = alertDialogBuilder.create(); and then alertDialog.show(); or just alertDialogBuilder.show() before you can edit the textView content.
I'm trying to create a calculator for Android using Eclipse. This is my code for the function 'cal', 'cal' is executed when a button is clicked, But clicking on the button closes the application, I tried an other button with different function and it works fine. Can anyone point out the mistake that I've done?
public void cal( View view ){
EditText op = (EditText) findViewById(R.id.editText2);
EditText n1 = (EditText) findViewById(R.id.editText1);
EditText n2 = (EditText) findViewById(R.id.editText3);
EditText res = (EditText) findViewById(R.id.textView1);
String sn1 = n1.getText().toString();
String sn2 = n2.getText().toString();
String sres;
String sop;
int in1 = Integer.parseInt(sn1);
int in2 = Integer.parseInt(sn2);
int ires;
sop = op.getText().toString();
if(sop == "+"){
ires = in1 + in2;
sres = Integer.toString(ires);
res.setText(sres);
}
}
There is no exception in this.
I think you forgot to set the activity layout which causes your application force close.
or
Problem here
EditText res = (EditText) findViewById(R.id.textView1);
Are you sure your editText name is textView1 in your layout.
If it textView then do like this.
TextView res = (TextView) findViewById(R.id.textView1);