I have a borderless button on in one of my layouts. When it is clicked, I want the intent to transfer both the button's tag and the text it contains. I am having trouble doing this.
This is what I have for when the button is clicked:
public void openGoalWeek (View view) {
Intent intent = new Intent(this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
}
This is what I have in the ViewGoal class:
public class ViewGoal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_goal);
// make sure running on honeycomb or higher for actionbar API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// get the message from the intent
Intent intent = getIntent();
String message = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE).toString();
String tag = intent.getBundleExtra(MainActivity.EXTRA_TAG).toString();
String text = "null";
if (tag == "year_tag") {
DatabaseHandler db = new DatabaseHandler(this);
Goal goal = db.getYearGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "month_tag") {
DatabaseHandler db = new DatabaseHandler(this);
MonthGoal goal = db.getMonthGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "week_tag") {
DatabaseHandler db = new DatabaseHandler(this);
WeekGoal goal = db.getWeekGoal(message);
text = goal._title + "\n" + goal._description;
}
// create the text view
TextView textView = new TextView(this);
textView.setTextSize(10);
textView.setText(text);
// display the content
setContentView(textView);
}
}
It throws an error at me and I am not sure why. Any help is appreciated!
why don't you just do this, just put your EXTRA instead of using bundle:
...
Intent intent = new Intent(this, ViewGoal.class);
intent.putExtra( EXTRA_MESSAGE, button.getText().toString());
intent.putExtra( EXTRA_TAG, button.getTag().toString());
....
Later on in the other activity:
....
// get the message from the intent
Intent intent = getIntent();
String id = intent.getStringExtra(EXTRA_MESSAGE);
String name = intent.getStringExtra(EXTRA_TAG);
....
In onClick button, instead of this used classname.this
Some of my errors are removed by this. Try if it helps you.
public void openGoalWeek (View view) {
Intent intent = new Intent(className.this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
Related
I know there are few questions similar to mine and I have tried all the suggested solutions mentioned in the existing questions however it's still not working. Pretty sure I'm doing something wrong logically but unable to figure out where. Please point me to right direction.
Spinner activity code method
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
// TODO Auto-generated method stub
Intent main = new Intent (this, MainActivity.class);
Bundle myBundle = new Bundle();
String chosenAppType = appliancestypespinner.getSelectedItem().toString();
myBundle.putString("appliance type spinner",chosenAppType);
main.putExtra("chose appliance type", myBundle);
startActivity(main);
}
TextView Activity (getting the spinner value as string in textview (tvApplianceType = textview )
Bundle myBundle = this.getIntent().getExtras();
if(myBundle == null)
{
return;
}
String str_recieved_appType = myBundle.getString("appliance type spinner");
if (str_recieved_appType != null)
{
tvApplianceType.setText(str_recieved_appType);
}
}
Just to add that I'm also passing the value of Edittext present from the Spinner activity to the same textview that i'm passing the spinner value to that EditText to TextView is working fine. Is it not working because i'm using the same textview for both operations however either operation need to work at one time :/. So either edittext to textView OR spinner to textview.
Edittext Code
String str_appliance_type = et_input_Appliance_Type.getText().toString().trim();
if (!str_appliance_type.equals(""))
{
data.add(str_appliance_type );
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data );
appliancestypespinner.setAdapter(aa);
//passing edittext value to MainActivity class
Bundle carrier = new Bundle();
Intent i = new Intent (this, MainActivity.class);
i.putExtra("appliance type",str_appliance_type);
startActivity(i);
}
Passing Value fro Editext to textview
//getting the edittext value from inputAppliance class
Bundle carrier = getIntent().getExtras();
if(carrier == null)
{
return;
}
String str_recieved = carrier.getString("appliance type");
if (str_recieved != null)
{
tvApplianceType.setText(str_recieved);
}
String str = appliancestypespinner.getSelectedItem().toString(); // I assume this line is proper.
Intent i = new Intent (this, MainActivity.class);
Bundle b = new Bundle();
b.putString("your_key", str);
i.putExtras(b);
startActivity(i);
In MainActivity
Bundle b = getIntent().getExtras();
String s = b.getString("your_key");
your_textView.setText(s);
Intent main = new Intent (this, MainActivity.class);
main.putExtra("SELECTED_DATA", appliancestypespinner.getSelectedItem().toString());
startActivity(main);
In Another Activity:
String data=this.getIntent.getStringExtra("SELECTED_DATA");
Try using Intent:
Spinner Activity:
Intent calculate = new Intent(getApplicationContext(),Second.class);
calculate.putExtra("carry_name", entername.getText().toString());
On Second Activity:
Intent intent = getIntent();
String name = intent.getStringExtra("carry_name");
congrates.setText("Congratulation " + name);
Change this
main.putExtra("chose appliance type", myBundle);
startActivity(main);
to
main.putExtras(myBundle);
startActivity(main);
in My Main Activity I am pasing data using the data using intent using the putextra()
method
private void editHandler()
{
//send the details to another form
if (itemID > 0)
{
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtra("bundle", values);
startActivity(intent);
}
in the next Activity I am receiving that intent
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addor_edit);
todoNote = (EditText)findViewById(R.id.txt_todoNote);
todoDate = (EditText) findViewById(R.id.txt_dateTODO);
todoTime = (EditText) findViewById(R.id.txt_timeTODO);
todoalarm =(ToggleButton) findViewById(R.id.toggle_alarm);
alarmEnable = (ImageView) findViewById(R.id.img_alarmEnable);
canceltodo = (Button) findViewById(R.id.btn_cancelTODO);
maketodo = (Button) findViewById(R.id.btn_makeTODO);
//Receiving intent
Bundle bundle = getIntent().getBundleExtra("bundle");
getValuesForEdit(bundle);
todoalarm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(todoalarm.getText().equals("ON"))
{
alarmEnable.setImageResource(R.drawable.dark_alarm);
alarm = 1;
}
else
{
alarmEnable.setImageResource(0);
alarm = 0;
}
}
});
maketodo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addnewTODO();
}
});
}
private void getValuesForEdit(Bundle bundle)
{
// if the Edit Button is pressed get all values from listView
ID = bundle.getInt("id");
todoNote.setText(bundle.getString("text"));
todoDate.setText(bundle.getString("date"));
todoTime.setText(bundle.getString("time"));
alarm = bundle.getInt("alarm");
if (alarm == 1)
{
todoalarm.setText("ON");
alarmEnable.setImageResource(R.drawable.dark_alarm);
}
}
and The application crashes
am I doing wrong with intents?? which is the right way to pass data between
activities?? suggestions and advises are needed...
thanks!!
Intent intent = new Intent(this,AddorEdit.class);
intent.putExtra("text",singleItem.getText());
intent.putExtra("date", singleItem.getDate());
intent.putExtra("time", singleItem.getDate());
intent.putExtra("id", singleItem.getItemID());
intent.putExtra("alarm", singleItem.getAlarm());
startActivity(intent);
//While get this data:
ID = getIntent().getIntExtra("id");
todoNote.setText(getIntent().getStringExtra("text"));
todoDate.setText(getIntent().getStringExtra("date"));
todoTime.setText(getIntent().getStringExtra("time"));
alarm = getIntent().getIntExtra;
Try this
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtras(values);
startActivity(intent);
and
Bundle bndl = this.getIntent().getExtras()
I have activity A where it has a ADD button when I click on the button it displays the list of users with check boxes in Second Activity and when I select the users and click the conform button it should display in all the users Activity A.I am using adapter to display the users.
from this button I am getting the list of checked values
private void checkButtonClick()
{
conform = (Button)findViewById(R.id.Confirm);
conform.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Namelist> stateList = contactadapteruser.listexample;
Bundle b=null;
Namelist state;
for(int i=0;i<stateList.size();i++)
{
state = stateList.get(i);
b = new Bundle();
b.putString("selected",state.getName());
if(state.isSelected())
{
responseText.append("\n" + state.getName());
}
}
Intent i= new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
}
});
}
from this button I am getting the I am retrieving the values
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2 = getIntent();
Bundle b1 = i2.getExtras();
String name1 = b1.getString("selected");
tweet.setText(name1+",\t");
}
});
My problem I am unable to get the users which are checked.
Can any one help me .
use start activity for result to start your second activity.
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, 1);
from second activity before closing you can send
Intent return_intent = new Intent();
return_intent.putExtra("your_user_list",user_list);
setResult(RESULT_CODE,return_intent);
finish();
Now in your FirstActivity override onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_CODE){
// GET YOUR USER LIST HERE AND USE IT FOR YOUR PURPOSE.
user_list=data.getExtra("your_user_list");
}
}
}
You can use broadcast sender and receiver also for your case. But above method is good for your task.
ArrayList<Namelist> stateList = contactadapteruser.listexample;
// convert your ArrayList to a String[]
String[] arr = new String[stateList.size()];
for (int i = 0; i < arr.length; i++)
{
arr[i] = stateList.get(i).getName();
}
Then using this question as a source, put the String array in your intent:
Bundle b = new Bundle();
b.putStringArray("selected", arr);
Intent i = new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
Then to read the String array from your other activity:
Bundle b = getIntent().getExtras();
String[] array = b.getStringArray("selected");
I'm working on an app where I type date in the EditText and it will send the date(which is typed in) to another activity and display as a TextView. I had created a button above the text box.
Below are the 2 activities of sending and getting the date.
PersonalInfo.class
these are the codes for sending the date to another activity
Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent dateIntent = new Intent();
dateIntent.setClass(PersonalInfo.this, Create_Events.class);
dateIntent.putExtra("passDate", "Date_var_here");
PersonalInfo.this.startActivity(dateIntent);
}
});
Create_Events.class
Codes for getting the date from first activity, and it'll display the date as textview
Intent dateIntent = this.getIntent();
/* Obtain String from Intent */
if(dateIntent !=null)
{
String strDate = dateIntent.getExtras().getString("passDate");
TextView txtDate = (TextView) findViewById(R.id.txtDate);
txtDate.setText(strDate);
}
Use this to get the text from your EditText:
dateIntent.putExtra("passDate", idEditText.getText().toString());
EditText et = (EditText) findViewById(R.id.my_edit_text);
String theText = et.getText().toString();
// To pass it to another Activity you use an Intent. Example...
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_label", theText);
startActivity(i);
In the new Activity (in onCreate()), you get the Intent and retrieve the String...
public class MyNewActivity extends Activity {
String uriString;
#Override
protected void onCreate(...) {
...
Intent i = getIntent();
uriString = i.getStringExtra("text_label");
}
}
You enter wrong intent name in second activity.
String strDate = **dateIntent**.getExtras().getString("passDate");
Final code is
Intent dateIntent = this.getIntent();
/* Obtain String from Intent */
if(dateIntent !=null)
{
String strDate=getIntent.getStringExtra("passDate");
TextView txtDate = (TextView) findViewById(R.id.txtDate);
txtDate.setText(strDate);
}
Hi am newbie to java and android.
Assume function demo() from the screenone is going to display some values on Textview which is on the same screen(screenone).
But i need to display that resultant value to the next screen ie.(screen two)
public void demo(){
{
.....
.....
}
So i have included these line to
screenoneActivity.java
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
demo();
ScreentwoActivity.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
txtName.setText(name);
I did these things so far.I don't know how to do transfer data from demo() function to the next screen.
Can anyone give me clues or ideas to achieve this.
Thanks a lot!..
You need to send some value into the putExtra methods parameters to be able to get something out of it.
In your first activity(A):
Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);
In your second activity(B):
Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");
Write the below code in demo() function:
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
In nextScreen.putExtra("",""); provide some key and value like:
nextScreen.putExtra("name","ABC");
Now in SecondActivity, write:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
Bundle bundle = i.getExtras();
txtName.setText(bundle.getString("name"));
In ScreenoneActivity
Intent act2=new Intent(this,Activity2.class);
act2.putExtra("A",a);
startActivity(act2);
In ScreentwoActivity class
Intent i = getIntent();
Bundle extras = getIntent().getExtras();
int a = extras.getInt("A");
txtName.setText(a);
in onCreate :
Bundle extras = getIntent().getExtras();
String value;
if (extras != null)
{
value= extras.getString("key");
}
https://stackoverflow.com/questions/10752501/how-can-we-go-to-next-page-in-android/10752516#10752516
google it is very basic .....
android using intent....
Vogella Ariticle
in activity 1-
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
startActivity(i);
In activity 2 - in onCreate finction
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}