I have a code that uses putextra method to push data from activity to another, I want to push different values using the same key
code:
String int_value = "int_value";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondScreen.class);
intent.putExtra(int_value , 0);
startActivity(intent);
}
});
Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondScreen.class);
intent.putExtra(int_value , 1);
startActivity(intent);
}
});
And in the next activity:
int value;
String int_value = "int_value";
View myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_screen);
Intent intent = getIntent();
int temp = intent.getIntExtra(int_value, value);
myLayout = findViewById(R.id.myLayout);
switch (value){
case 0:
myLayout.setBackgroundResource(R.drawable.a);
Log.e("VALUE" , String.valueOf(value));
break;
case 1:
myLayout.setBackgroundResource(R.drawable.b);
Log.e("VALUE" , String.valueOf(value));
break;
}
But the background always changes to a.jpg although I passed 0 & 1 (or a least I thought I did...)
what is the problem here?
just change value in switch to temp
switch ( temp){
case 0:
myLayout.setBackgroundResource(R.drawable.a);
Log.e("VALUE" , String.valueOf(value));
break;
case 1:
myLayout.setBackgroundResource(R.drawable.b);
Log.e("VALUE" , String.valueOf(value));
break;
}
because you are saving the value inside temp
int temp = intent.getIntExtra(int_value, value);
Related
From the first activity I would like to go to the second, then from the second to the third. In the third activity I would like to enter the name in EditText, then after pressing the button, go to the first activity and at the same time send the information entered in the third activity.
Unfortunately, after pressing the button in the third activity, instead of returning to the first activity, I return to the second activity. Was the first activity killed? What could I do to ensure that the information is correct for the first activity? This is my code:
First:
public class MainActivity extends AppCompatActivity {
TextView textViewInformation;
Button button_GoToSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);
button_GoToSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
String name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}
Second:
public class Second extends AppCompatActivity {
Button button_GoToThird;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button_GoToThird = findViewById(R.id.button2);
button_GoToThird.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Second.this, Third.class);
startActivity(i);
}
});
}
}
Third:
public class Third extends AppCompatActivity {
EditText editText_Data;
Button button_SendData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
just remove finish(); thats it
The reason that it goes to the second activity is because of this:
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish(); //This kills the current activity.
}
You should do:
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
startActivityForResult(i, RESULT_OK, bundle);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
Bundle bundle = i.getExtras();
String name = bundle.getString("name");
textViewInformation.setText(name);
}
}
When you call finish, it just kills the current activity. If you want to go back to the first activity, just start a new activity for the first activity and pass the data in a Bundle.
If you want to have more of a stack concept, you can use Fragments.
I have two Edittexts in MainActivity: etFrom and etTo.
When clicked etFrom goes to ListFrom activity and etTo to ListTo activity.ListFrom and ListTo activities contain Listview and after choosing an item it comes back to Main activity and displays results in edittexts. I use MainActivity.putExtra("To",v); and etTo.setText(getIntent().getStringExtra("To")); to get data from an activity.
After clicking on first edittext, choosing an item from Listview and coming back to Main Activity, when I do the same for second edittext the first edittext becomes empty. And the same when I do first one, second one becomes empty.First Second
ListTo Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_to);
etSearchTo=(EditText) findViewById(R.id.etSearchTo);
listViewTo=(ListView) findViewById(R.id.listViewTo);
initList();
listViewTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
Intent intent=new Intent(ListTo.this,SearchRide.class);
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
intent.putExtra("To",v);
startActivity(intent);
}
});
ListFrom Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_from);
listView=(ListView) findViewById(R.id.listviewFrom);
editText=(EditText) findViewById(R.id.etsearchFrom);
initList();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
Intent intent=new Intent(ListFrom.this,SearchRide.class);
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
intent.putExtra("From",v);
startActivity(intent);
}
});
SearchRide as Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_ride);
etDate=(EditText) findViewById(R.id.etDate);
TextView tFrom=(TextView) findViewById(R.id.tFrom);
TextView tTo=(TextView) findViewById(R.id.tTo);
etFrom=(EditText) findViewById(R.id.etFrom);
etTo=(EditText) findViewById(R.id.etTo);
Button bSearch=(Button) findViewById(R.id.bSearch);
ImageView iplussign=(ImageView) findViewById(R.id.iplussign);
ImageView iminussign=(ImageView) findViewById(R.id.iminussign);
final TextView tNumber=(TextView) findViewById(R.id.tNumber);
etTo.setText(getIntent().getStringExtra("To"));
tNumber.setText(String.valueOf(p));
iplussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p<=9) {
p++;
tNumber.setText(String.valueOf(p));
}
}
});
iminussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p>=2) {
p--;
tNumber.setText(String.valueOf(p));
}
}
});
etFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ListFrom=new Intent(SearchRide.this,ListFrom.class);
startActivity(ListFrom);
}
});
etFrom.setText(getIntent().getStringExtra("From"));
etTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ToList =new Intent(SearchRide.this,ListTo.class);
startActivity(ToList);
}
});
etTo.setText(getIntent().getStringExtra("To"));
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerDialog dialog= new DatePickerDialog(SearchRide.this,R.style.MyDatePickerStyle,listener,calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.MONTH),calendar.get(Calendar.YEAR));
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dialog.show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
use startActivityForResult() to start ListFrom and ListTo Activities and the receive the result from these activities in OnActivityResult() method.
SearchRide as Main Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_ride);
etDate=(EditText) findViewById(R.id.etDate);
TextView tFrom=(TextView) findViewById(R.id.tFrom);
TextView tTo=(TextView) findViewById(R.id.tTo);
etFrom=(EditText) findViewById(R.id.etFrom);
etTo=(EditText) findViewById(R.id.etTo);
Button bSearch=(Button) findViewById(R.id.bSearch);
ImageView iplussign=(ImageView) findViewById(R.id.iplussign);
ImageView iminussign=(ImageView) findViewById(R.id.iminussign);
final TextView tNumber=(TextView) findViewById(R.id.tNumber);
tNumber.setText(String.valueOf(p));
iplussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p<=9) {
p++;
tNumber.setText(String.valueOf(p));
}
}
});
iminussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p>=2) {
p--;
tNumber.setText(String.valueOf(p));
}
}
});
etFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ListFrom = new Intent(SearchRide.this,ListFrom.class);
startActivityForResult(ListFrom,0);
}
});
etTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ToList = new Intent(SearchRide.this,ListTo.class);
startActivityForResult(ToList,1);
}
});
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerDialog dialog= new DatePickerDialog(SearchRide.this,R.style.MyDatePickerStyle,listener,calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.MONTH),calendar.get(Calendar.YEAR));
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dialog.show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if(resultCode == Activity.RESULT_OK){
etFrom.setText(data.getStringExtra("From"));
}
} else if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
etTo.setText(data.getStringExtra("To"));
}
}}
ListFrom code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_from);
listView=(ListView) findViewById(R.id.listviewFrom);
editText=(EditText) findViewById(R.id.etsearchFrom);
initList();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
Intent intent = new Intent();
intent.putExtra("From",v);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
ListTo code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_to);
etSearchTo=(EditText) findViewById(R.id.etSearchTo);
listViewTo=(ListView) findViewById(R.id.listViewTo);
initList();
listViewTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
Intent intent = new Intent();
intent.putExtra("To",v);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
Here is what you can do. Put both from and to values as extra when starting an activity and return them both as result. This way you will be able to remember it. I am assuming you just do not want to use static variables.
// in ListTo activity
intent.putExtra("To",v);
intent.putExtra("From", getIntent().getStringExtra("From"));
// in ListFrom activity
intent.putExtra("From",v);
intent.putExtra("To", getIntent().getStringExtra("To"));
// in Main activity
etTo.setText(getIntent().getStringExtra("To"));
etFrom.setText(getIntent().getStringExtra("From"));
// when starting ListFrom activity
Intent ListFrom=new Intent(SearchRide.this,ListFrom.class);
intent.putExtra("From", getIntent().getStringExtra("From"));
intent.putExtra("To", getIntent().getStringExtra("To"));
startActivity(ListFrom);
// when starting ListTo activity
Intent ToList =new Intent(SearchRide.this,ListTo.class);
intent.putExtra("From", getIntent().getStringExtra("From"));
intent.putExtra("To", getIntent().getStringExtra("To"));
startActivity(ToList);
PS: I have not tested the above code. You can experience null pointer errors, but it should give you the general idea.
So I have this code and it is working. My problem is that how can I pass and show the result in new activity. I set the onclick of the 3 checkbox with selectItinerary
public class topPawikan extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toppawikan);
final_text = (TextView)findViewById(R.id.textView);
final_text.setEnabled(false);
}
public void selectItinerary(View view){
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId())
{
case R.id.checkBox1:
if (checked)
{selection.add("Maglibot");}
else
{selection.remove("Maglibot");}
break;
case R.id.checkBox2:
if (checked)
{selection.add("Kumain");}
else
{selection.remove("Kumain");}
break;
case R.id.checkBox3:
if (checked)
{selection.add("Umupo");}
else
{selection.remove("Umupo");}
break;
}
}
//button
public void finalSelection(View view){
String final_itinerary = "";
for (String Selections : selection)
{
final_itinerary = final_itinerary + Selections + "\n";
}
final_text.setText(final_itinerary);
final_text.setEnabled(true);
}
}
Put params into the new Intent. Start new activity on button click.
Intent intent = new Intent(topPawikan.this, SecondActivity.class);
Bundle b = new Bundle();
b.putString("selection", final_text.getText().toString());
intent.putExtras(b);
startActivity(intent);
Then get the params in your new Activity:
Bundle b = getIntent().getExtras();
String selection = b.getString("selection");
I tried looking on other questions but nothing seems to work. Trying to pass the value stored in sUsername from activity Username.java to MainActivity.java (username).
This is Username.java:
public class Username extends ActionBarActivity implements View.OnClickListener {
EditText eUsername;
Button login;
String sUsername;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_username);
eUsername = (EditText) findViewById (R.id.username);
sUsername = eUsername.getText().toString();
login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername); //first argument is the name of the string being passed
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.id.login:
loginClick();
break;
}
}
and this is MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String username = bundle.getString("containsUsername");
WebView listOfSongs = (WebView) findViewById (R.id.webview);
String url = "http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user="+username+"&api_key=68f82cd7b37e7b23800c2025066531c9&format=json";
listOfSongs.loadUrl(url);
}
your loginCheck Method should be like this:
private void loginClick() {
sUsername = eUsername.getText().toString();
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername); //first argument is the name of the string being
startActivity(intent);
}
As in on create your edittext might be empty.
Please check your edittext is empty or not. Chnage your method longClick.
private void loginClick(){
if(!sUsername.isEmpty()) {
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername);
startActivity(intent);
} else {
Toast.makeText(Username.this, "Please enter username.", Toast.LENGTH_LONG).show();
}
}
I'm having issues with my view controller in my android application. I'll admit I'm a noob at android programing please forgive me here is my issue and photos of the application. Basically i just what to press the button and go to the related page and i can't quite figure it out.
http://imgur.com/aVpqUCZ
package com.Apps.jarredapp;
public class ViewController extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button btnSearchStudent = (Button) findViewById(R.id.button1);
Button btnNewStudent = (Button) findViewById(R.id.button2);
Button btnLegalInfo = (Button) findViewById(R.id.button3);
btnSearchStudent.setOnClickListener(this);
btnNewStudent.setOnClickListener(this);
btnLegalInfo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(this, SearchStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button2: {
Intent intent = new Intent(this, NewStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button3: {
Intent intent = new Intent(this, LegalInfoActivity.class);
startActivity(intent);
break;
}
}
}
{
Button searchStudentButton;
Button newStudentButton;
Button legalButton;
searchStudentButton = (Button) findViewById(R.id.button1);
searchStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
newStudentButton = (Button) findViewById(R.id.button2);
newStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
legalButton = (Button) findViewById(R.id.button3);
legalButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
You have written two onclicklisteners for one button, use only one then it will work