i have two activities each contain its own textview any one help me that when i click the one textview text the text goes to the another textview of the second activity please any on help me in this problem
In activity A.
TextView aTv = findViewById(R.id.aTextView);
String aText = aTv.getText().toString();
Intent i = new Intent(bClass.class); // your other activity
i.putExtra("myText", aText);
startActivity(i);
In the onCreate of Activity B:
String aText = this.getIntent().getStringExtra("myText");
TextView bTv = findViewById(R.id.bTextView);
bTv.setText(aText);
Is that what you were looking to do? I'm into a few beers so there may be a few typo's but that's the idea. :)
Related
Newbie android/java dev here. How can I pass custom lists via intent with loop?
The problem is the list of "Restaurants" is displayed in the firstactivity.
A "Details/Menu" button under the restaurant name will be clicked and I want to display the "Food Menu" and other relevant information of that specific restaurant via array.
It should be passed from firstactivity to the secondactivity with a loop (I think), so that it will use just few textviews.
The button should pass these data according to the Restaurant clicked.
Name of the Restaurant
URL of the restaurant drawable profile photo
URL of the restaurant drawable cover photo
The Title of the Food Category (*PROBLEM)
The lists of food in that specific category and its price (*PROBLEM)
My question is, how to pass the data automatically and how to make this code better and short? Thank you so much for the help!
Here is the sample of firstactivity.
First Activity Screenshot
Here is the java code of First Activity:
public void bluecafedetails (View dineview) {
Intent intent = new Intent(DineActivity.this, DineDetailsActivity.class);
String TitleofDine = "Blue Cafe";
String AddressofDine = "2nd Floor, VTC, Gogon, Virac, Catanduanes";
String OpenHoursofDine = "8AM - 10PM / Everyday";
String DineDelivery = "Yes";
String MenuTitle = "All Day Breakfast";
intent.putExtra("DineTitle", TitleofDine);
intent.putExtra("DineAddress", AddressofDine);
intent.putExtra("DineOpenHours", OpenHoursofDine);
intent.putExtra("DineDelivery", DineDelivery);
intent.putExtra("MenuTitle", MenuTitle);
intent.putExtra("image",R.drawable.blue_cafe_logo);
this.startActivity(intent);
}
Here is the sample of secondactivity.
Second Activity Screenshot
Here is the java code in the second activity so far:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dine_details);
TextView dineTitle = (TextView)findViewById(R.id.dinetitle);
TextView dineAddress = (TextView)findViewById(R.id.dineaddress);
TextView dineDelivery = (TextView)findViewById(R.id.acceptsdeliveryanswer);
TextView dineOpenHours = (TextView)findViewById(R.id.openhours);
TextView dineMenuTitle = (TextView)findViewById(R.id.menutitle);
Intent anotherActivityIntent = getIntent();
Bundle extras = getIntent().getExtras();
String DineTitle= anotherActivityIntent.getStringExtra("DineTitle");
String DineAddress= anotherActivityIntent.getStringExtra("DineAddress");
String DineOpenHours= anotherActivityIntent.getStringExtra("DineOpenHours");
String DineDelivery= anotherActivityIntent.getStringExtra("DineDelivery");
String MenuTitle= anotherActivityIntent.getStringExtra("MenuTitle");
int imageId = getIntent().getIntExtra("image", 0);
ImageView imageView = (ImageView) findViewById(R.id.dinelogo);
imageView.setImageResource(imageId);
dineTitle.setText(DineTitle);
dineAddress.setText(DineAddress);
dineDelivery.setText(DineDelivery);
dineOpenHours.setText(DineOpenHours);
dineMenuTitle.setText(MenuTitle);
this.setTitle(DineTitle);
I have an activity in foreground.
when i press a button "OK" on the activity, the same activity must come on top with new fields in it.
I changed the button text from OK to CANCEL in onclick handler. It worked fine.
But user cant see that that a new page is loaded .
I am new to android, can someone hint me?
Sounds like you need to read the documentation
But as a quick start you should build a new intent to start your Activity. You can pass data between your Activities using putExtra methods on your Intent:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, YourActivityName.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
When you start your Activity you can retrieve the data stored inside the Intent using the following:
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
You can then display the message (or any other data field) inside your View:
textView.setText(message);
Does the following work? (Place it in your onClick method)
TextView txtView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(newText)
where newText is the new String that you need to place in textView and R.id.textView1 is the reference to your TextView in your layout.
Intent i = getIntent();
categorie_label = i.getStringExtra("categorie_label");
TextView txtrank = (TextView) findViewById(R.id.catid);
txtrank.setText(categorie_label);
Why the code below returning a NullPointerException ? Please help
Your activity probably did not find the TextView with the specified ID in it´s layout and thus it is null. You should check if it is contained in that layout.
I have two activities namely MainActivity and DisplayMessageActivity. MainActivity is a app launcher and the first UI which contains only two components : EditText and Button .
And DisplayMessageActivity has only one component TextView with default string defined. I just want to change/set the TextView's text value which was typed in EditText field and when the Button is clicked.
TextView textView = (TextView) findViewById(R.id.typed_text);
returns null pointer in DisplayMessageActivity which was navigated by MainActivity's intent call. I could able to set and get the text in MainActivity without a problem. Please help me :)
#codeMagic : Thanks :) Here is the xml
`
<TextView
android:id="#+id/typed_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/typed_text"/>
`
The onCreate method in DisplayMessageActivity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
//TextView textView = new TextView(this); - This works
TextView textView = (TextView) findViewById(R.id.typed_text); // null pointer
textView.setTextSize(40);
textView.setText(message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
When you start DisplayMessageActivity, use intent.putExtra() to pass on the input from your EditText.
http://developer.android.com/training/basics/firstapp/starting-activity.html
This is probably exactly what you need!
I am passing a value for username in one activity through XML. I want to recall the value in the next screen, like, 'Hello/Welcome, '. How is it possible using GetExtra and PutExtra?
Pls. find the below code, which I have tried for the same.
For storing the value of username in one activity:
Intent myActivity2 = new Intent(this,Activity1.class);
myActivity2.putExtra("nickname", R.id.mynickname3);
startActivity(myActivity2);
For recalling the value in other activity:
Bundle extras = getIntent().getExtras();
String val = extras.getString("nickname");
TextView tv=new TextView(this);
tv.setText(val);
If you want to reuse a username, use sharedPreference.
It is not a good idea to pass a userName to another activity then use that.Field like userName may need to use again and again.
In the first activity, you are passing in an integer value (R.id.mynickname3). Instead you need to give it a String. Use this instead:
myActivity2.putExtra("nickname", getString(R.id.mynickname3));
As a side note, you are trying to get a Bundle unnecessarily. Try this:
String val = getIntent().getStringExtra("nickname");
TextView tv = new TextView(this);
if (val != null)
{
tv.setText(val);
}
You're nearly right.
It is not enough to just pass the id of the EditText. You have to use below code -
((EditText)findViewById(R.id.mynickname3)).getText().toString();
int putExtra. Everything else should be fine.
UPDATE: Don't forget to set your TextView as contentView in OnCreate via:
setContentView(..)
Just a silly mistake in your code:
myActivity2.putExtra("nickname", R.id.mynickname3);
Where you have written ID of the EditText (nickname), instead it should be as a:
txtNickName = (EditText) findViewById (R.id.mynickname3);
myActivity2.putExtra("nickname", txtNickName.getText().toString());