saving/printing text Android - android

I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.
I've tried this code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.

Try this
public void onClick(View v) {
textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
}

Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.
You have 2 Options:
textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());
And Second you can use SpanableStringBuilder
SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
sb.append("\n").append(editEmail.getText());
The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.

Related

I don't know how transfer my variable in android

so sorry for trouble.
I want to make a search Google. First time i have create edittext and button. In button it is create un link for access in browser.
I had a errror when add text in link. The variable mEdit does not match the written
Help me, please.
button = (Button) findViewById(R.id.button);
mEdit = (EditText) findViewById(R.id.editText2);
mEdit.setMovementMethod(LinkMovementMethod.getInstance());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://www.google.com/search?q="+mEdit;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
You need to call
String url = "https://www.google.com/search?q=" + mEdit.getText().toString();
to get the text inside the edittext

How to display inputted text in a TextView after clicking a button in Android studio?

So I am experimenting with Android Studio. I am stuck in the part where I want my "Submit" button to display the user inputted text underneath the page.
Button submit is the button.
"name" and "email" are the EditText variables a.k.a text fields
My TextView variable is outputText
So after the user enters random stuff in both text fields and click submit, I want my outputText to display both texts that were inputted in 2 separate lines
ex:
John Smith
john.smith#gmail.com
The code below is my .java code for the activity. I need to know how to initialize the new intent in a way that lets me do the 4 lines below it. Thanks.
public void buttonOnClick(View v){
Button submit = (Button) v;
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i =
new Intent(...?...);
startActivity(i);
}
});
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.textView);
textout.setText(editName.getText()+" "+editEmail.getText());
}
I want my outputText to display both texts that were inputted in 2
separate lines
Use <br> and Html.fromHtml between text to add new line:
String strText=(String)editName.getText()+"<br>"+(String)editEmail.getText();
textout.setText(Html.fromHtml(strText));
how to initialize the new intent in a way that lets me do the 4 lines
below it...
Intent i =new Intent(CurrentActivityName.this,TargetActivityName.class);
startActivity(i);
For print in next line
textout.setText(editName.getText()+"\n"+editEmail.getText());
For intent
Intent i =new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
Ok I was able to accomplish what I wanted without an Intent initialized. Here's the code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}

How can I change the text of a button at runtime on Android?

I want to write a simple example where I can change the text of a button at run time.
Basically, I am extracting the text from "EditText" view and trying to populate the contents of the button "OnClick" event.
However, the system crashes.
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Button btn = (Button) findViewById(R.id.button_message);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
btn.setText(message);
}
What am I doing wrong?
a) Should I be registering an Listener on the button for this purpose?
b) Is this another "Activity" and should I be creating an Intent for this?
Button btn = (Button) findViewById(R.id.button_message);
EditText editText = (EditText) findViewById(R.id.edit_message);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String message = editText.getText().toString();
btn.setText(message);
}
});
Inside the onclick events u get the string from edittext and set it to the button it will work

how to set a value to an xml element in java code in android?

I have a edit_text field in activity_X.xml. I have a button B in activty_X.xml. On clicking button B i need to assign a value to edit_text field. How to do?
you should try something on your own then post any question if you are getting any problem in that...anyways here's how you can implement this
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
EditText editText1 = (EditText) findViewById(R.id.textidinxml);
editText1.setText("Some text to show on edittext");
}
});

How to populate the EditText with the button click?

I want to get the values in the button to be populated in the edit text box. i.e I am creating a login page in which the pin has to entered I have given a set of numbers as buttons, when I click the button the corresponding values has to be populated in the edit text box( just like the ATM action).
I am now using:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
String text=(String) b1.getText();
pincode.setText(text);
}
});
with the above code I am getting the values populated, but I have a set of buttons, so is there any simplified way to get the values populated?
*The values currently replacing the previous one , I need multiple values in the edit text like if we press buttons 1,2,3 (values) the values in the edit text should be 123 *
To do this, in your activity implements OnClickListener
then add this listener to all your button as
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
and write onClick event as
public void onClick(View v) {
Button b1 = (Button)v;
editText.setText(editText.getText().toString()+b1.getText().toString());
}
and you are done :)
on your button onClick you got to do this
button1.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"1");
});
button2.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"2");
});
this way your buttons will work
onClick(View v)
{
editText.setText(editText.getText() + v.getText());
//plz adjust casting and spanned as per requirement
}
You can add text like this:
String a = "random text";
input = your_edit_text_box;
input.setText(a);
Link: http://developer.android.com/reference/android/widget/EditText.html#setText%28java.lang.CharSequence,%20android.widget.TextView.BufferType%29
Button btnNO = (Button) findViewById(R.id.btn);
EditTExt edtText = (EditText) findViewById(R.id.editText);
btnNO.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text= btnNo.getText();
edtText.setText(text);
// or direct use like, edtText.setText(btnNo.getText());
}
});

Categories

Resources