i have a relative view with different images each time i click on an image i want a unique text to be sent to another activity. i have been able to achieve this for the first image but i cant get the second image to send the text to next activity.
my code for the first-activity:
b1 = (ImageView) findViewById(R.id.native_amala);
b2 = (ImageView) findViewById(R.id.native_fufu);
b3 = (ImageView) findViewById(R.id.native_jollof);
b4 = (ImageView) findViewById(R.id.native_ofada);
b5 = (ImageView) findViewById(R.id.native_porridge);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("Amala",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("fufu",foodName1.getText().toString());
startActivity(intent2);
}
});
}
My second activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_cart);
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("Amala");
foodView.setText(str);
intent.putExtra(String key, String value) is the format.
Have a unique key for all putExtra functions and use that key in getStringExtra
You should use Same KEY .
Intent putExtra (String name,
Parcelable[] value)
intent2.putExtra("value",foodName1.getText().toString());
And
Bundle bundle=getIntent().getExtras();
String str=bundle.getString("value");
foodView.setText(str);
You basically need to understand how putExtra works.
You can put some value with keys, for e.g.
intent.putExtra("Amala",foodName1.getText().toString());
Here, Amala is the key and foodName1.getText().toString() is the value.
Then in your next activity you can retrieve the value from key.
i.e. you can do,
String str = intent.getStringExtra("Amala");
In your click listener of b2 ImageView, you are mentioning the key fufu but in your second activity you are getting Amala.
You need to keep common key for all click listeners and then retrieve data from that key in second activity.
You need to keep common key for all click listeners like this way to send data
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("data",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("data",foodName1.getText().toString());
startActivity(intent2);
}
});
now receiv e data in other activty like this
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("data");
foodView.setText(str);
You using the key is fufu.
intent2.putExtra("fufu",foodName1.getText().toString());
So u get the value then used that key.so change getStringExtra key name change "amala" to "fufu"
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("fufu");
foodView.setText(str);
For Sending Some Text form one Activity to another activity
You can Simply use putExtra() function
putExtra(Sting Key, String value);
Let say you have String variable
String str = "Hello";
After when go to next Next Activity
Intent Intent = new Intent(getApllicationContext(),Your_Activity.class);
intent.putExtra("Hello");
startActivity(intent);
When you want this variable in Your_Activity then simply in onCreate() method
Intent intent = getIntent();
String str = intent.getStringExtra("Hello");
Use this
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("VALUES", someValues);
startActivity(intent);
Access that intent on next activity
String s = getIntent().getStringExtra("VALUES");
Related
Full Error:-
> androidx.appcompat.widget.AppCompatEditText{39b5cb6VFED..CL. .F....... 188,641-977,812 #7f080097 app:id/edittext aid=1073741824}
This image will show you that error...
https://i.stack.imgur.com/ZpVnh.png
Detail:-
i got this error when i was passing data from one activity to another in android.
Activity 1st Code:-
EditText e1;
e1 = (EditText) findViewById(R.id.edittxt);
public void jumpbutton(View view) {
Intent i1 = new Intent(this,ThirdActivity.class);
i1.putExtra("user",e1.toString());
startActivity(i1);
}
Activity 2nd Code:-
TextView txt1;
txt1 = (TextView) findViewById(R.id.txtvw);
Bundle bd1 = getIntent().getExtras();
String st1 = bd1.getString("user");
txt1.setText(st1);
i successfully solve this error, by adding this
.getText()
into the first activity code.
Like this:-
public void jumpto3(View view) {
Intent i1 = new Intent(this,ThirdActivity.class);
i1.putExtra("user",e1.getText().toString());
startActivity(i1);
}
Then my error is solved.
After code is edit:-
you can see the result in this image
On the first Activity
public void openActivity2(){
EditText editEmail =findViewById(R.id.textEmail) ;
Bundle bundle = new Bundle();
Intent i = new Intent(this, MainActivity2.class);
bundle.putString("emailvalue", editEmail.getText().toString());
i.putExtras(bundle);
startActivity(i);
}
Then in your second Activity u can get the value of EditText like that
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = getIntent();
String text = i.getStringExtra("emailvalue" );
System.out.println(text);
}
});
you are missing .getText().toString() somewhere. find that out and fix it.
I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)
You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}
I'm trying to make a button use some data from another activity, I did seach, but I didn't find out how.
The onClickListener doesn't work.
Here is the code inside my onCreate:
super.onCreate(savedInstanceState);
setContentView(R.layout.show_message);
Intent intent = getIntent();
String msg = intent.getStringExtra("MESSAGE");
ScrollTextView scrolltext = (ScrollTextView) findViewById(R.id.scroll_text);
scrolltext.setText(msg);
scrolltext.setTextSize(220);
scrolltext.startScroll();
super.onResume();
DialogFragment mDialog = new GeneralDialogFragment();
mDialog.show(getSupportFragmentManager(), "Dialog TAG");
ImageButton mImageButton = (ImageButton)findViewById(R.id.image_button);
mImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//I'm sure this is not the to make this button works. What code should I put here to pass this data to the ScrollTextView?
scrolltext.setText(msg);
scrolltext.setTextSize(220);
scrolltext.startScroll();
}
});
This two lines that you wrote are just used to get information from the previous activity that was executing
Intent intent = getIntent();
String msg = intent.getStringExtra("MESSAGE");
If you want to use data from another activity at any time, you have to save the data in the previous activity with these lines:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("msg", "MESSAGE");
editor.commit();
and call it on your activity like this:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String yourmessage = prefs.getString("msg", null);
if (restoredText != null) {
String name = prefs.getString("msg", "No name defined");//"No name defined" is the default value.
}
I have a button on an activity called "HomePage". When you click the button, I want it to setText to a TextView called "weaponTitle" on a separate activity, called ItemSelection. Though, when I run the setText, it gives the error:
Attempt to invoke virtual method void android.widget.TextView.setText(java.lang.CharSequence) on a null object reference
So this means it can't find the TextView "weaponTitle". Is there a good way to do fix this? I have made sure I set up everything correctly too!
Here is the sliver of code I forgot to share!
new displayPrices(weaponTitle, "Genuine Freedom Staff");
try this
Firstclass
Intent intent = new Intent(getApplicationContext,SecondActivity.class);
intent.putExtra("KEY",value);
intent.putExtra("KEY",VALUE);
startActivity(intent)
Second Activity
Intent intent =getIntent();
confirm_txt =(TextView)findViewById(R.id.txt);
String txt_put =intent.getStringExtra("KEY");
confirm_tId.setText(txt_put);
Inside oncrete of your HomePage Activity
Button yourbutton = (Button) findViewById(R.id.your_button_id);
yourbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(HomePage.this, ItemSelection.class);
intent.putExtra("weapontitle",value);
startActivity(intent);
}
});
Inside ItemSelection Activity's oncreate
Intent intent =getIntent();
String txt =intent.getStringExtra("weapontitle");
TextView weaponTitle = (TextView) findViewById(R.id.textview_weaponTitle);
weaponTitle.setText(txt);
go through this way,
pass this from your first activity,
Intent i = new Intent(Login.this, ChangePasswordActivity.class);
i.putExtra("savedUser", savedUser);
Log.e("username", "--->>" + savedUser);
startActivity(i);
in second activity , get this as below
Intent extras = getIntent();
savedUser = extras.getStringExtra("savedUser");
Log.e("savedUser", "--->>" + savedUser);
Now you can set text as you got it on other activity.
etUsername.setText(userName);
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("weaponTitle","Genuine Freedom Staff");
startActivity(intent);
In the second activity you do:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String weaponName = bundle.getString("weaponTitle");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(weaponName);
I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.