how to move data between 2 activities - android

i have a first screen in my app that the user enter his name in an editText.Then, when the user presses the button "ok",the app is going to a new activity.I would like to get the text from the first activity and move it to the second.For example,if the user fills the edittext with the name "kostas",when he goes to the second activity,to appear a textView writing "Hello kostas"..
i have tried to use putExtra, but i m thinking that i m doing it in a wrong way.In the first class i m using this
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent newActivity = new Intent(view.getContext(),home.class);
newActivity.putExtra("NAME", name);
startActivity(newActivity);
}
});
in order to move the name into my next activity "home".but then i dont know how to get it there...
and then in my new "home" activity i m using this:
Bundle extras = getIntent().getExtras();
String Name = extras.getString("NAME");

First, in your onClick Handler I would extract the text from the editText box and place the text into the intent.
Second, to debug the problem I would enable LogCat viewing
Third, I would log the actual values being passed (in Act 1) and extracted (
in Act 2) using a call such as:
Log.d(TAG,name);
Hope that helps,
JAL

Related

How to refresh the same activity with new text when button is pressed

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.

How to pass the value of a pressed button

I was wondering how to pass the value of a button to another activity. I have a screen with 7 buttons, and each button shows different data from the SQLitedatabase. So I was wondering how I can implement that in the activity so that I can select different data from the database, dependable which button was clicked in the previous activity. I know I have to use Bundles and Intents, but I cannot find how I can implement that so that the Activity knows which button was clicked and which data to select. Thanks!
How to pass the value of a pressed button
Buttons don't really have a "value". You could use the button's ID, though:
public void onClick(View v) {
startActivity(new Intent(this, YourOtherActivity.class)
.putExtra(YourOtherActivity.EXTRA_SOME_KEY, v.getId()));
}
This will pass the widget ID of the Button that was clicked in an extra (named YourOtherActivity.EXTRA_SOME_KEY) to YourOtherActivity. YourOtherActivity, in onCreate() or elsewhere, could call getIntent().getIntExtra(EXTRA_SOME_KEY, -1)) to retrieve the widget ID of the button that was clicked, then use a switch statement or some such to route your behavior as needed.
Set it with
Intent myIntent = new Intent(this, ACTIVITY.class);
myIntent.putExtra("buttonValue", BUTTON VALUE);
startActivity(myIntent);
Get it with
getIntent().getStringExtra("buttonValue")
Button Text
String buttonText = button.getText().toString()

Change Value of TextView, ImageView, ImageButton when i press button in other Activity

I am developing one simple app but i have problem.
The problem I have is that two activities, Activity1, have 26 button A-Z and Activity2 have textView,ImageView. I don't know how to set the button with default value.
Question
How can I change the value of textView, ImageView, and button in Activity2 whenever I press any button in Activity1?
Question
if i click buttonA how can i change texView display from "TextView" to "A" and imageView to other Image can u guys help me?
In simple way you can pass the value within intent like
Intent Activity2Intent = new Intent(this, Activity2.class);
Activity2Intent.putExtra("BUTTON_TEXT", yourText);
Activity2Intent.putExtra("IMAGE_VIEW", yourText);
startActivity(Activity2Intent);
Else if the data is needed later you can store that in SqliteDatabase
UPDATE
You should also need to extract the values passed from the bundle on Activity2.
You can use SharedPreferences or Intent for passing the value from Activity A and retrive the value in next Activity .
In Activity A:::
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do something and save the value and pass it using intent
// pass the button name or use boolean is_state= true;
}
});
In Activity B:::
By using getIntent() retrive the button name and change theTextViewandImageView` as per your requirement

Android - Save text and launching an Activity with the same button

I'm currently working in an Activity which saves text from three EditViews and constructs a SQL query. After that, the query is given to another activity to search and display the results.
Right now I've got two buttons, one to save the query, inside an onClickListener and another button to start the second activity:
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//First the data from the editviews is saved
EditText searchName = (EditText) findViewById(com.pedro.books.R.id.search_name);
search_name = searchName.getText().toString();
searchName.setText("");
EditText searchAut = (EditText) findViewById(com.pedro.books.R.id.search_aut);
search_aut = searchAut.getText().toString();
searchAut.setText("");
EditText searchYea = (EditText) findViewById(com.pedro.books.R.id.search_yea);
search_yea = searchYea.getText().toString();
searchYea.setText("");
//here i construct the query
});
And with an onClick in the xml code, I start the activity with another button:
public void ReadResults(View view){
//The query is given to the ReadActivity to display the results
Intent intent = new Intent(this, ReadActivity.class);
intent.putExtra(ReadActivity.EXTRA_QUERY, query);
startActivity(intent);
}
I've tried with the same button for both without changing anything and it obviously doesn't work, and I've also tried to start the activity inside the onClickListener, but I got this error: "The constructor Intent(new View.OnClickListener(){}, Class) is undefined"
Is there a way to start the activity inside the onClickListener or to stop the second activity to start until the query is saved?
Thanks in advance!!
if you are putting the intent in the onClick of a button you cannot use this you need to use YourActivity.this to properly get context.
this in your button is your OnClickListener like the error says
Where/How do you call your ReadResults method? On a side note - does it need to be public? If you call it inside the clickListener handler then that's wrong.
1) Extract your code out of the clickListener handler and have it into a private method within your activity class.
2) Your clickListener should only call that private method.
3) You could have your 3 editboxes as memebers of your activity and instantiate them onCreate() instead of getting them each time you click the button, it's expensive parsing the UI if not necessary.

Using dynamically created buttons in android to take user to another activity with dynamically generated content

I have a list of buttons in one of my activities that are dynamically generated, and I was wondering how i would get one of those buttons to, when clicked, open another activity and display text based on which button in the list was clicked.
I generate the buttons using a for loop (I've ommited details relating to TextViews in the loop for easier reading, it also used some variables defined elsewhere)
for (int i = 0; i < N; i++) {
// create a new Button
final Button rowButton = new Button(this);
// Set properties of rowButton
rowButton.setText("See Recipe");
rowButton.setId(RecipeArray.get(i));
// add the Button to the LinearLayout
myLinearLayout.addView(rowButton);
// save a reference to the Button for later
myButtons[i] = rowButton;
}
The buttons represent a certain recipe and when clicked they should take the user to a new activity "HowToMake" and generate a textview with the information relating to that recipe only. They are stored in an array at the bottom of the code snippet "myButtons[i] = rowButton" But I'm not sure how I would use this.
Thanks for any help.
You'll have to add an onclicklistener to each button as you add it. Then in the onclick event, you can call the startActivity method of your current Activity. When you create your Intent which will open the new activity, you can add "extras" to it (in other words, you can add some extra data which will be passed into the new activity). New data, such as the ID of the recipe that you want to open :)
Example (Untested, but should be along the right lines):
rowButton.setTag("the unique ID by which you can read back your recipe");
rowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(YourActivity.this, TheViewRecipeClass.class);
intent.putExtra("id", v.getTag());
startActivity(intent);
}
}

Categories

Resources