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);
Related
I am doing a project in Android Studio. I will have 2/3 activities at most. On main activity I will have some buttons. There will be a second activity. So for each button that second activity will pop up and show some more buttons or imageButton but they will be different. The buttons on the second activity will also be meant for different actions but all of it will be on a third activity. The third activity will have some images and a text view. So image view and text view will show different data from the "XML resource" for different buttons. Any ideas how I can do it?I am using Android Studio.
Can it be done using base Adapter?
Also when all the data are stored on sql database, what different thing do I need to do?
Thank You!
When you go from main activity to the second activity, you must be using intent.
You can add extra info in your intent to to modify accordingly using putExtra()
Then in your second activity, you can simply retrieve the extra info and make changes accordingly.
in main activity,
//your first button
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load first set of images");
startActivity(newintent);
}
});
//your second button
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load second set of images");
startActivity(newintent);
}
});
in second activity,
String extrainfo=getIntent().getStringExtra("details");
if(extrainfo.equals("load first set of images"))
{
//make your changes accordingly
}
else if(extrainfo.equals("load second set of images"))
{
//make your changes accordingly
}
else
{
//make according changes
}
This is how you can achieve it.
Hope this helps!
I'm new programming on Android and i stacked in (i think) a common problem.
I have two activities and a CheckBox In one of those. What i need is when the CheckBox is selected and i click a button that changes to the second activity display the notifications.
What you can do is probably send a specific value if a specific checkbox is checked. like 1 for the 1st and 2 for the second you can include the string of check boxes checked in a string eg: 1246
After you use intent to call the other activity
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra( strName,"strval");
i.startActivity();
On the other side you could retrive the value inside strName to find out which checkboxes are checked
Intent intent = getIntent();
String xyz = intent.getStringExtra("strName");
check if str xyz contains (1)..if it does perform the action you want else check if it contains "2"
if(xyz.contains(1))
{
//perform task 1
}
else if(xyz.contains(2))
{
//perform task 2
}
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()
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 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