setText from a class for another - android

I am really new to android. And my English speaking is not good. So,I was going to get a username using an EditText and set it to a TextView But the problem is that the EditText is in the first class (MyActivity),and the TextView is in the second class(MyAcyivity2).
I did everything such as FindViewById and....
but when I set on a click listener :
Textview1.setText(EditText1.getText())
and open the app, by clicking on button it says: Unfortunately app has stopped.
what to do?

In MyActivity :
EditText et = (EditText) findViewById(R.id.editText1);
On button click, inside onClickListener :
void onClick(View view) {
Intent intent = new Intent(MyActivity.this, MyActivity2.class);
intent.putExtra("myString", et.getText().toString());
startActivity(intent);
}
In MyActivity2 inside onCreate()
String myString = getIntent().getStringExtra("myString");
TextView tv = findViewById(R.id.textView1);
tv.setText(myString);

Related

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 to create another button in android dynamically

As the title states, I am looking to find out how to create a button dynamically when another button in another activity is pressed. This is being done with the Android SDK.
Basically, I have two activities, MainActivity and SecondaryActivity, within the SecondaryActivity you enter some information, title, text, id, so on and so forth. When you click the "Save" button it sends some, information to MainActivity(not the issue). As well as sending the information, I need to create an entirely new button within the MainActivity.
Any suggestions on how this should be accomplished?
Thanks.
Edit 1
public void CreateNewButton(View view)
{
LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
TextView newTextView = new TextView(this);
int id = rand.nextInt(100);
int newId;
newTextView.setVisibility(View.VISIBLE);
newTextView.setId( R.id.new_button + id );
newTextView.setText("New Item");
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(getBaseContext(), SecondActivity.class);
startActivity(intent);
}
});
lineLayout.addView(newTextView);
}
This code generates the new TextView( Decided to change it up ) but now the issue I have, is newTextView.setText(); needs to get the text from the other activity
newTextView.setText(i.getData().toString());
putting this in the CreateNewButton(View view) methods causes an error since technically there is no data in the field that it is trying to grab from.
The problem at hand is I need to create the new TextView field WITH the name of the new account that has yet to be created. If that makes any sense.
I'm going to assume you want to add this button to a LinearLayout:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
Button button = new Button(this);
button.setText("I'm a button!");
// add whatever other attributes you want to the button
linearLayout.addView(button);

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

editing a global variable

Hi im new to android and I have a program that has a global variable define and it works, so I can set it and get it in every activity, BUT it dosnt like to be changed in an on click listener. I made it so on the screen there is an edittext and when someone presses a button I want the edittext text to be put into the global variable. here is my code:
Button SiteButton = (Button) findViewById(R.id.SiteButton);
SiteButton.setOnClickListener(new View.OnClickListener() {
TextView textviewS = (TextView) findViewById(R.id.SiteIdT);
EditText edittextS = (EditText) findViewById(R.id.SiteIdE);
TextView textviewB = (TextView) findViewById(R.id.BusIdT);
EditText edittextB = (EditText) findViewById(R.id.BusIdE);
public void onClick(View v) {
textviewS.setText(edittextS.getText());
((Global) this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) this.getApplication()).setgVehicleId(textviewB.getText().toString());
}
});
but the getApplication() part is showing an error. can anyone help?
You should refer to your activity this, since View.OnClickListener doesn't have such a method:
// Bad code! read below
((Global) MyActivityClassName.this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) MyActivityClassName.this.getApplication()).setgVehicleId(textviewB.getText().toString());
By the way, how do you cast the return from getApplication() to Global? You will get a class cast exception there.

Categories

Resources