Storing editText to variables, Android Java [closed] - java

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?

Related

App crashes on initialization views in Dialog [duplicate]

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);

Get clicked TextView in ScrollView/LinearLayout?

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.

How to display the EditText fields text in the TextView

Of late I was just trying to create a mems geneator app, yes I had been following the new bostons and in that buckey created them using 2 fragments and here i just want to do in a single main activity, but I just can't figure out how to retrieve the text from the edit text field and set the text of Text view to it. I know it's pretty a newbie question but still I don't know how to code it so please help...
I had just imported some widgets,views,etc and done some modified the on create function and my on create function is:
public class MainActivity extends AppCompatActivity {
public static EditText topText;
public static EditText bottomtext;
public static TextView top;
public static TextView bottom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topText = (EditText) findViewById(R.id.topedit);
bottomtext = (EditText) findViewById(R.id.bottomedit);
top = (TextView) findViewById(R.id.top);
bottom = (TextView) findViewById(R.id.bottom);
Button myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View V) {
top.setText((CharSequence) topText);
bottom.setText((CharSequence) bottomtext);
}
}
);
}
Simply do this:
if(topText.getText()!=null){
top.setText(topText.getText().toString());
}
if(bottomtext.getText()!=null){
bottom.setText(bottomtext.getText().toString());
}
Try this to get text of the EditText field:
CharSequence text = topText.getText();
And set the text above for the textView:
top.setText(text);
Use this short example to understand the concept
store=ret.getText().toString();
show.setText(store);
Explanation
ret: is the name of the edit text field;
store: is used to hold anything gotten from ret (text field)
show.setText(store) displays the result in the textview
This question has already been answered:
"Use getText() on your EditText object".
Get Value of a Edit Text field
Next time do a quick search ;)

Getting value from EditText within an AlertDialog Builder

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.

Removing TextViews when i clicked backbutton

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.

Categories

Resources