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()
Related
I have made a form and connected it to a firebase database. It has an EditText in which you write something(e.x a suggestion) and then you press the button to submit it in the firebase database. But how can I refresh the whole xml file so I can write a new suggestion. I have tried this at first
setContentView(R.layout.activity_suggestions);
But after that, the xml file was refreshed but I couldn't submit a new suggestion.
and then I tried this which works but its really annoying because if I press the back button it gets me back to the same page
Intent reportBugIntent = new Intent(ReportBug.this, ReportBug.class);
startActivity(reportBugIntent);
Is the any other way I can do that?
You just need to clear editText content on submit button click.
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Save content to firebase database
// clear the editext
editText.setText("");// where edit text is your suggestion edit text
}
}
There are! How about refreshing Activity: Reload activity in Android
The current code is useless because it will just return you to the current page, instead, you may wanna finish the current Activity by finish() method then doing such thing.
So:
finish();
startActivity(getIntent());
Or:
// Refresh main activity upon close of dialog box
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish(); //
As mentioned on the above link.
Okay my title may not be the most descriptive, but basically what I want is this:
I have 2 buttons and 2 activities. the buttons are in ActivityA the ActivityB contains only one TextView that sets it's text based on the button that was pressed and the message it carries using the intent. For example
buttonA is set to change the text in AactivityB to "I am a boy" while
buttonB is set to change the same TextView In the same AactivityB to "I am a girl".
how do I receive this intent in ActivityB and set the text without using 2 TextViews.
Please I'm texting from my phone so I can't really post code because the system would start bugging me with indenting.
I'll appreciate any ans. thanks
here it is, the buttons I'm using are menu buttons in the overflow:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.aboutGospel:
Intent intent = new Intent(WebViewActivity.this, AboutActivity.class);
intent.putExtra("gospel", "hi im gospel");
startActivity(intent);
return true;
case R.id.aboutPeters:
Intent tent = new Intent(WebViewActivity.this, AboutActivity.class);
tent.putExtra("peters", "hi im peters");
startActivity(tent);
return true;
}
return super.onOptionsItemSelected(item);
what I'll need now is this: the 2nd activity has just one textview in it. I'll like to set the text in that text view based on which menu item was clicked. please I'll love it if you help me with that, because this is just a test app, I'm building something larger and this function will help save time instead of creating multiple layouts or text view. thanks a bunch in anticipation.
You can pass the data on the intent and you can get the data from the intent.
The intent you are passing will carry the data on the same key say gender so you can get the data from the extra with the same key.
Example -
You can get the data from extra in ActivityB as
String gender = getIntent().getStringExtra("gender");
You can send the data on the intent as from ActivityA as-
Intent intent = new Intent(ActivityA.this,ActivityB.class);
intent.putExtra("gender","I am a male.")//Female for the another button.
startActivity(intent);
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
I'm developing a Data Entry system wherein the user can save, search and update data. In this activity, I'm using EditTexts, spinners and a button (to search for Clinic Names). When the user is finish filling those EditTexts and spinners, he can search for Clinic using the Button. This button will trigger the intent to another activity.
The other activity has a EditText(with TextFilters and can search for Clinic) and a ListView which shows the results upon search. When the result shows up, the user can click the ListView Items and that will trigger to the previous activity and will fill-out the Clinic Name to the EditText assigned to it.
My problem is when the user clicked the ListView item, and goes to the previous activity, those data from the EditTexts and Spinners were cleared up!
I want to happen is that, when I search for Clinic Name and goes to the another activity, those data that was already filled-outwill not be cleared or lost.
MainActivity.java
//Getting clinic address
txt_DClinic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(getApplicationContext(), SearchClinicActivity.class);
startActivity(myIntent);
}
});
SearchClinicActivity.java
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
TextView tv_clinicId = (TextView) view.findViewById(R.id.textviewId);
String clinicID = tv_clinicId.getText().toString();
Intent intent = new Intent(view.getContext(), MainActivity.class);
intent.putExtra("clinic_id", clinicID);
intent.putExtra("signal", "2");
intent.putExtra("from", "SearchClinic");
intent.putExtra("LoginOrSearch", "ImSearchClinic");
finish();
view.getContext().startActivity(intent);
}
});
When you are clicking on list item and opening intent. set flag to intent.
intent.setFlag(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
and see if its working or not.
if its not than try one thing is that. you are passing extras in intent. at that time pass the values for edittext and item number of spinner. and in onCreate method of Main activity set text of edittext with that extra. and set selected value for spinner.
edtTxt.settext(getIntent().getExtras().get("Text"));
sp.setSelection(getIntent().getExtras().get("Value"));
Hope it Helps!!!
Call setSaveEnabled(true) or add android:saveEnabled="true" to layout of Views you want data retained for.
MyEditText.setSaveEnabled(true);
or
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:saveEnabled="true"/>
Same for Spinner , or any other View you need to retain data of.
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