I don't know how transfer my variable in android - 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

Related

saving/printing text 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.

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

Android: Text not being applied to Card

So I'm trying to create an app where the user will go enter some text in two edittext fields and when a submit button is pressed, it will close the edit window and then add a new card to the list on the main activity. I've set up all my code and everything works without error. However, no cards are added and when the application starts, there's one card which has no text anywhere.
My Code:
MainActivity.java:
//The following lines of code are in onCreate() and setContentView() has been
//called
//Get the intent that launched the activity
Intent i = getIntent();
//Get the strings from the intent
String sig = i.getStringExtra("signature");
String lMessage = i.getStringExtra("long_message");
//Init mCardView
mCardView = (CardUI) findViewById(R.id.cardsview);
//Allow swipe to delete for cards
mCardView.setSwipeable(true);
//Add new card with the two extra strings from the edit text fields
mCardView.addCard(new MyCard(sig, lMessage));
//Draw the cards
mCardView.refresh();
AddText.java
//The following lines of code are in onCreate() and setContentView() has been
//called
//Find edit text's and button
EditText name = (EditText) findViewById(R.id.enter_name);
EditText message = (EditText) findViewById(R.id.enter_message);
button = (Button) findViewById(R.id.add_signature);
//Get the string from the edit text...whatever it may be.
final String signature = name.getText().toString();
final String longMessage = message.getText().toString();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make a new intent to start main activity
Intent intent = new Intent(getApplication(), YearbookMain.class);
//Put extras into intent
intent.putExtra("signature", signature);
intent.putExtra("long_message", longMessage);
//Start intent
startActivity(intent);
}
});
Again, I'm not getting any crashes it's just that the card is not being edited. I've tried everything I can think of so I'm hoping someone can think of something else!
Thanks

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

Categories

Resources