This question already has answers here:
findviewbyid returns null in a dialog
(7 answers)
Closed 3 years ago.
I'm trying to open Dialog window while clicking on some text view in recyclerview element.
Opening a window works perfectly if I don't try to initialize the texts view.
If I do, then the app crashes.
in my guestMessage setOnClickListener i'm open a Dialog.
In onClick i'm initialize TextView that should contain the texts (name and message).
#Override
public void onBindViewHolder(#NonNull final GuestViewHolder holder, final int position) {
final Guest guest = guests.get(position);
holder.guestName.setText(guests.get(position).getGuestName());
//holder.guestMessage.setText(guests.get(position).getGuestMessage());
holder.guestPhoneNumber.setText(guests.get(position).getGuestPhoneNumber());
holder.personsCount.setText(guests.get(position).getPersonsCount());
holder.guestMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog messageDialog = new Dialog(context);
TextView name = messageDialog.findViewById(R.id.messageTitlePopUp);
TextView message = messageDialog.findViewById(R.id.messageInPopup);
messageDialog.setContentView(R.layout.message_dialog_popup);
name.setText(guests.get(position).getGuestName());
message.setText(guests.get(position).getGuestMessage());
messageDialog.show();
}
});
}
You are initialising views before setting up content view on dialog, which cause the issue.
To solve issue, just change the initialisation order for your dialog code.
final Dialog messageDialog = new Dialog(context);
messageDialog.setContentView(R.layout.message_dialog_popup);
TextView name = messageDialog.findViewById(R.id.messageTitlePopUp);
TextView message = messageDialog.findViewById(R.id.messageInPopup);
Related
I have in my program a scroll view with a linearlayout inside of it. I add dynamically TextView's to the linearlayout and I have no way to know how much TextView's I'll end up with. When a certain TextView is clicked I need to get it's text. Any idea what is the best way to do it?
Thanks in advance.
I've tried to add a listener to the text view but I am not sure how to get the text. I saw in some posts that you can do a listener to the LinearLayour/ScrollView though I am not sure what is the best option.
This happenes every time a message is added:
TextView messageText = new TextView(RecordedMessagesScreen.this);
messageText.setText(content);
messageText.setClickable(true);
messageText.setOnClickListener(RecordedMessagesScreen.this.textViewListener);
RecordedMessagesScreen.this.messagesLayout.addView(messageText);
this is the listener:
this.textViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("message", ***NEED TO GET THE TEXT***)
}
};
At the class level declare a String variable:
private String text = "";
and a View.OnClickListener variable:
private View.OnClickListener listener;
Initialize the listener in onCreate() of your activity like this:
listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v;
text = tv.getText().toString();
}
};
and every time you create a new TextView set the listener:
textView.setOnClickListener(listener);
This way the variable text each time you click a TextView will get the clicked TextView's text.
You can customize the code inside onClick() yo suit your needs.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have a function which creates a button, which has an onClick event to bring up a custom dialog which just shows some information from the functions parameters. The dialog has two buttons on it as well, one to close the dialog, and one to add information to a file.
When I try to set onClick events for those buttons, the app crashes, and the error I get is a NullPointerException which says that I am trying to invoke a virtual method on a null object reference.
If I comment out the part where I set the onClickEventListener code for both buttons, then the dialog appears as normal, with the buttons on it.
Note: context is a variable declared in the class. It is simply Context context = this
Code is below:
public void addButton(String text, int id, String areas, String details, String notes) {
Button button = new Button(this);
final String title = "Add "+text;
final String dName = text;
final String dAreas = areas;
final String dDetails = details;
final String dNotes = notes;
button.setText(text);
button.setTextColor(ContextCompat.getColor(context, R.color.buttonText));
button.setTextSize(32);
button.setId(id);
if (isEven(id+1)) {
button.setBackgroundResource(R.drawable.buttonshapeother);
} else {
button.setBackgroundResource(R.drawable.buttonshape);
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Make custom dialog
final Dialog dialog = new Dialog(context);
Button add = (Button) dialog.findViewById(R.id.btnAddExer);
Button cancel = (Button) dialog.findViewById(R.id.btnCancel);
dialog.setContentView(R.layout.popup_exercise);
dialog.setTitle(title);
// Set the custom components now
TextView tName = (TextView) dialog.findViewById(R.id.lblNameData);
TextView tAreas = (TextView) dialog.findViewById(R.id.lblAreaData);
TextView tDetails = (TextView) dialog.findViewById(R.id.lblDetailsData);
TextView tNotes = (TextView) dialog.findViewById(R.id.lblNotesData);
tName.setText(dName);
tAreas.setText(dAreas);
tDetails.setText(dDetails);
tNotes.setText(dNotes);
// Add functions to buttons
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (addExercise(dName, dAreas, dDetails, dNotes)) { // Add exercise to user's workout
Toast.makeText(context, "Exercise was added to your workout", Toast.LENGTH_LONG).show();
dialog.dismiss(); // Close dialog
} else {
Toast.makeText(context, "There was an error adding your exercise", Toast.LENGTH_LONG).show();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss(); // Close dialog
}
});
dialog.show(); // Actually show the dialog
}
});
LinearLayout lay = (LinearLayout) findViewById(R.id.innerLay);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lay.addView(button, params);
}
public boolean isEven(int num) {
if ((num&1) == 0) {
return true;
} else {
return false;
}
}
Because you are trying to find button view before setting the layout to it. So try like this:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup_exercise);
dialog.setTitle(title);
Button add = (Button) dialog.findViewById(R.id.btnAddExer);
Button cancel = (Button) dialog.findViewById(R.id.btnCancel);
This question already has answers here:
Android - Using Custom Font
(21 answers)
Closed 7 years ago.
I have alertDialog with items,
this is the code:
final CharSequence[] options = { "Back","continue"};
AlertDialog.Builder builder = new AlertDialog.Builder(myActivity.this);
builder.setTitle(customTitle);
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Back"))
{
//do something;
}
else if (options[item].equals("continue"))
{
dialog.dismiss();
}
}
});
builder.show();
I have custom font in my assets folder, how can I connect the item's font to this font?
I saw many answers but there is no answer for itmes in dialog, all answers was about the title or text uses by textView,
I need to change the font of my items, someone can help me?
To do this you use alert builder to build your alert. You then get the TextView from this alert and then you set the typeface for the alert.
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/yourfont.ttf");
textView.setTypeface(face);
My problem is I have a button and that button is doing create new textview but that textviews removing when i click back button. How I saved textviews in activity?
My java sourcecodes here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notlar);
Button btnNotEkle = (Button) findViewById(R.id.btnNotEkle);
final EditText etNot = new EditText(NotEkle.this);
final LinearLayout layoutNotlar = (LinearLayout) findViewById(R.id.layoutNotlar);
final TextView tv1 = (TextView) findViewById(R.id.tvnotOrtalama);
etNot.setInputType(InputType.TYPE_CLASS_NUMBER);
AlertDialog.Builder notEkle = new AlertDialog.Builder(NotEkle.this);
notEkle.setTitle("Notunuz");
notEkle.setView(etNot);
//Positive button
notEkle.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
tvNot = new TextView(NotEkle.this);//girelen not burdaki textview e yazdırılacak.
girilenNot = etNot.getText().toString();//Girilen notu alıyoruz
tvNot.setText(girilenNot);//girilen notu textviewa veriyoruz
notTopla += Integer.parseInt(girilenNot);//Notları topluyoruz
layoutNotlar.addView(tvNot);
count = layoutNotlar.getChildCount();
dersOrtalamaYazdir=String.valueOf(dersOrtalama());
tv1.setText("Ders Ortalamanız : "+dersOrtalamaYazdir);
dialog.cancel();
}
});
final AlertDialog notEkleCreate = notEkle.create();
btnNotEkle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
notEkleCreate.show();
}
});
}
}
Try giving your TextView objects ids.
You need to know that when you click back button - by default your activity is destroyed so all views are removed.
When you are adding new TextView you should add information about this TextView (like the text itself) to some list declared as field in your activity.
Then you can save this list when activity is recreated see: onSaveInstanceState/nRestoreInstanceState
You can also pass this list back or to new activity so that they can take actions based on this list.
Following my understanding your TextView had been created inside Dialog and after you press back button the dialog dismisses and all views you created inside will be removed and you can't access it from your Activity.
You may try to create TextView in onCreate, pass and in Dialog just call setText. I hope this is the answer you're looking for.
Cheers.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I posted this question before, however i didnt explain myself good. I will have the other question removed. Here is my current situation:
I have a regular xml page with a textView which when clicked opens a popup dialog. This dialog contains 2 editText. Currently my code (OnClick – Done button) gets the value of both edit texts and puts them into the single TextView. However when i open the pop-up again, instead of the two strings being listed in its own editText (Where each string was originally inputted) the combined string which was stored in the text view appears in one edit text. The issue is that although i’m getting the strings from 2 different editText’s and storing them into one textView. I cannot get each string back individually. I understand that i may have to store the string from each editText into variables and then i can use the variables to show the strings combined in the textView (and the editText – when i open the popup dialog again) How would i go about this? Thank for your help
The code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton;
EditText getInput;
EditText getInput2;
String myvalue = "";
String myvalue2 = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showPopUpButton = (TextView) findViewById(R.id.buttonShowPopUp);
showPopUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showPopUp3(); }
});
}
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");
LayoutInflater inflater = getLayoutInflater();
View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
helpBuilder.setView(checkboxLayout);
getInput = (EditText) checkboxLayout.findViewById(R.id.editText1);
getInput2 = (EditText) checkboxLayout.findViewById(R.id.editText2);
getInput.setText(myvalue);
getInput2.setText(myvalue2);
helpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
myvalue = getInput.getText().toString();
showPopUpButton.setText(myvalue + ", " + myvalue2);
}
});
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
}
First of all you need string to save your EditText value in it declare it like this
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton; //NEW
EditText getInput; //NEW
EditText getInput2; //NEW
// declare string to save the dialog edittext
String myValue = "" ;
then you need to show the last value of dialog in the EditText so try this :
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");
LayoutInflater inflater = getLayoutInflater();
View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
getInput = (EditText) checkboxLayout.findViewById(R.id.editText1); //MISTAKE
getInput2 = (EditText) checkboxLayout.findViewById(R.id.editText2); //MISTAKE
getInput.setText(showPopUpButton.getText()); //New to keep the text in the editText when done is pressed
getInput2.setText(getInput2.getText()); //New test
// here set the my value to edit text , note firs time will be empty
getInput.setText(myValue)
and last thing when you click in done button in dialog you need to save the EditText value like that :
#Override
public void onClick(DialogInterface dialog, int which){
//showPopUpButton.setText(getInput.getText() + ", " + getInput2.getText());//NEW
//showPopUpButton.setText(value) ;
// save the edit text value into myvalue string
myvalue = getInput.getText().toString();
}
});
feed me back
This is fairly straightforward.
Create the variables to placehold your strings.
String inputText;
Where applicable, get and set.
inputText = editText.getText().toString();
textView.setText(inputText);
Did I understand this correctly? Is this what you are trying to accomplish?