How to pass edittext value to another activity's textview - java

i want to send value from one activity's edit-text to another activity's text-view.I have written following code but when i am running the program than it is showing nothing.so can you help on this...
Home.java
Intent in = new Intent(Home.this, Splash.class);
String Data= UserName.getText().toString();
in.putExtra("text",Data);
startActivity(in);
splash.java
Intent in = new Intent(Splash.this, SearchData.class);
UserWelcome.setText(in.getStringExtra("text"));
startActivity(in);
here username is the edittext and userwelcome is the textview.

You should change in splash
UserWelcome.setText(in.getStringExtra("text"));
to
UserWelcome.setText(getIntent().getStringExtra("text"));

In splash.java do this..
UserWelcome.setText(getIntent.getStringExtra("text"));

Just replace this in splash.java
Intent in = new Intent(Splash.this, SearchData.class);
UserWelcome.setText(in.getStringExtra("text"));
startActivity(in);
with
UserWelcome.setText(getIntent().getStringExtra("text"));

Related

I am trying to put a intent to my app so i can navigate

I am trying this intent so I can navigate from one activity to another activity but I'm getting this error `
final Button btnAdd = findViewById(R.id.addEmp);
btnAdd.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(v.getId() == R.id.addEmp){
Intent intent = new
Intent(getCallingActivity(),AddEmployee.class);
startActivity(intent);
}
}
});`
there is a red line under
(getCallingActivity(),AddEmployee.class);
there error says
cannot resolve constructor
Is anything wrong with this
(getCallingActivity(),AddEmployee.class);
statement?
You need to change your line from
Intent intent = new Intent(getCallingActivity(),AddEmployee.class);
to this:
Intent intent = new Intent(YourActivityName.this,AddEmployee.class);
OR
Intent intent = new Intent(getApplicationContext(),AddEmployee.class);
Edit
Whats wrong with getCallingActivty()?
getCallingActivity() returns ComponentName while intent constructor requires Context as a first argument.
Hope this will work

Passing data from one activity to another and then printing

The problem I am having is that it prints out Null on the second activity and not the actual username that is entered. Is the data being passed to the second activity correctly? Does the second activity need more code? Sorry but not the best at programming.
I have this code in my main class
if (username.getText().toString().equals("batman") &&
password.getText().toString().equals("Joker")) {
Toast.makeText(MainActivity.this, "Username and
password is correct", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.example.*******.loginpage.User");
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class));
This is the code inside my second class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Intent intent = getIntent();
String username = getIntent().getStringExtra("username");
TextView textView = (TextView) findViewById(R.id.textView4);
textView.setText("Welcome" + " " + username );
The problem is your intent in your first class
Intent intent = new Intent("com.example.*******.loginpage.User"); <-- have created an intent
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class)); <-- but using new Intent
You have created an intent but you passing new intent. Use your created Intent instead of passing new Intent.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",String.valueOf(username));
startActivity(intent);
EDIT
Instead using String.valueOf(username) you must use username.getText(), because String.valueOf(username) is method to translate your object to String.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText());
startActivity(intent);
Two problems here.
First one is that you have to pass the intent where you put your extra instead of creating new one to startActivity, like
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText().toString());
startActivity(intent);
Second problem is that username looks like editText, String.valueOf won't pass actual value, use username.getText().toString() like i mentioned in code.

Show text on other activity than the button clicked

I know how to show text with a button click on the same page, but my question is (since I couldn't find anything on Google): Is it possible when you click a button, that the text shows up on another activity?
Yes, you can
In your FirstActivity execute this when button is clicked:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data","Messsage to be sent");
startActivity(intent);
In your SecondActivity inside onCreate():
String someData = getIntent().getStringExtra("data");
yourTextView.setText(someData);
If you need pass values between the activities you use this:
String name="aaaa";
Intent intent=new Intent(Main_Activity.this,Other_Activity.class);
intent.putExtra("name", name);
startActivity(intent);
And this code to recovery data on new Activity:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
You can use Intent for this. Intent is used to move on other activity from first activity.
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");

IllegalStateException: Exception when I try to jump to my SignUp sheet

I am getting an exception at second startActivity(i) statement. My first startActivity(i) statement works fine.
public void onBClick(View v){
EditText a = (EditText) findViewById(R.id.TFUsername);
String str = a.getText().toString();
if(v.getId() == R.id.BLogin){
Intent i = new Intent(MainActivity.this, new_activ_java.class);
i.putExtra("username",str+',');
startActivity(i);
} else if(v.getId() == R.id.BforSignUp){
Intent i = new Intent(MainActivity.this, Signup.class);
startActivity(i); // I am getting an exception here
}
}
Please make sure Signup is an Activity declared in the Android Manifest. As a convention, append Activity at the end of the class name to indicate that it is indeed an Activity, for instance, SignUpActivity.
Make sure that the new activity you are trying to start is listed in Android Manifest.xml file.

Passing extra data from an Activity to an Intent

I am calling an Activity using an Intent, and I need to pass variables to that Activity when is initialized. On iOS you can use a custom initialization using the initWithNibName method. How can a similar thing be achieved on Android?
Here is my code that creates an Intent...
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
startActivity(myIntent);
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.putExtra("mystring",strValue)' <<---put String here
startActivity(myIntent);
and in second Activity...
String str = getIntent.getExtras().getString("mystring");<<get string in second class
and check this
How do I pass data between Activities in Android application?
You can put extra data into the Intent...
myIntent.putExtra("exampleString","This is some extra data");
myIntent.putExtra("exampleNumber",1234);
When you call the Intent, it starts the Activity. in one of the main methods of the Activity, like onCreate(), you can access the Intent and get the extras from it, like so...
Intent callingIntent = getIntent();
String exampleString = callingIntent.getStringExtra("exampleString");
int exampleNumber = callingIntent.getIntExtra("exampleNumber");
You can set extras to the intent:
myIntent.putStringExtra("First key", 1);
myIntent.putStringExtra("Second key", "some string");
And then get it in the new activity
Int extraInt = getIntent().getIntExtra("First key");
String extraString = getIntent().getStringExtra("Second key");
See more in the Intent docs
This can be done with intent extras. For example:
int variable = 6;
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.PutExtra("stringLabel", variable);
startActivity(myIntent);

Categories

Resources