i am new to Andriod and programming. i am trying to make a to do list app, which will contain a listView, Button(add) in the main layout. when i click add i want it to go to another activity which will contain a editText and a add button. when i click the add button i want to update the list in my main activity. Now i was able to get the information from the second activity but when i try to add it in my list it over writes it.
How can i update my list as soon as my main activity appears again.
Here's what i have so far :
MainActivity class :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.bAdd);
list = (ListView) findViewById(R.id.lvList);
addItems(); // i think this is the error but i dont know how to fix it.
alList = new ArrayList<String>();
aaList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alList);
list.setAdapter(aaList);
}
public void clickAdd(View v) { //when clicking the add button(add)
Intent intent = new Intent(MainActivity.this, AddItem.class);
startActivity(intent);
}
private void addItems(){
String s = getIntent().getStringExtra("item");
aaList.add(s);
aaList.notifyDataSetChanged();
}
AddItem class :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
et = (EditText) findViewById(R.id.etAdd);
bt = (Button) findViewById(R.id.badd);
}
public void add(View v){ //when clicking the add button(bt)
edit = et.getText().toString();
Intent intent = new Intent(AddItem.this, MainActivity.class);
intent.putExtra("item", edit);
startActivity(intent);
}
Can you please tell me where and why i am going wrong? Thank You
First of all, you should use startACtivityForResult
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
Then in your second Activity
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
And back in your fist one:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
and then, your addItem should do the trick:
private void addItems(String s){
aaList.add(s);
aaList.notifyDataSetChanged();
}
Related
So I want this button and TextView and LinearLayout to be invisible until a button is pressed on another activity, but when I return to MainActivity(the activity with the button and textview and linearlayout) the stuff is still invisible.
Thank you in advance.
MainActivity.java
textView.findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);
ToggleButton button=findViewById(R.id.button);
button.setVisibility(View.INVISIBLE);
LinearLayout alarmLayout=findViewById(R.id.alarmLayout);
alarmLayout.setVisibility(View.INVISIBLE);
String value=getIntent().getStringExtra("buttonStatus");
if(value.equals("Visible")){
button.setVisibility(View.VISIBLE);
alarmLayout.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
AlarmFrequency.java
Button create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(alarmFrequency.this,
MainActivity.class);
intent.putExtra("buttonStatus","Visible");
I made little changes to your code to run it on my pc.
MainActivity.java
textView=findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);
//make textView visible
Intent i= getIntent();
String value = i.getStringExtra("buttonStatus");
if (i!=null && value!=null && value.equals("Visible")) {
textView.setVisibility(View.VISIBLE);
}
//button to go to second activity
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
SecondActivity.java
Button create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("buttonStatus", "Visible");
startActivity(intent);//launch main activity again
}
});
This makes the textView visible in MainActivity.
But if you press back button to go back to MainActivity you will see no change.
When your application starts, the application stack has only MainActivity in it. Lets call it MainActivity1.
Then, you press a button to go to SecondActivity. Your application stack contents are now :- MainActivity1 / SecondActivity
Then when you launch MainActivity again from SecondActivity MainActivity2 get in the stack.
Stack becomes MainActivity1 / SecondActivity / MainActivity2.
Changes are visible in MainActivity2, not in MainActivity1. If you use back button to go back to MainActivity1, textView will still be invisble.
Also note that, MainActivity1 was launched from another intent, and MainActivity2 was launched from different intent in SecondActivity.
Edit:-
If you want changes in your MainActivity1, you need to use startActivity for result. Following code changes will be required:-
MainActivity.java
textView=findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i,100);
}
});
//outside onCreate
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null)
{
String value = data.getStringExtra("buttonStatus");
if(value!=null && value.equals("Visible")) {
Log.d("Debug", "i am here.");
textView.setVisibility(View.VISIBLE);
}
}
}
SecondActivity.java
Button create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("buttonStatus", "Visible");
setResult(RESULT_OK, intent);
finish();
}
});
Read more about this changes here how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android
Your if(value.equals("Visible")) is in onCreate? It should be under onResume otherwise it will only execute once when onCreate is called.
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView. This is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
Button button;
ListView listView;
String name;
private static final int REQUEST_CODE = 1;
ArrayAdapter<String> adapter;
ArrayList<String> nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button2);
listView = findViewById(R.id.CarList);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
});
nameList = new ArrayList<String>();
nameList.addAll(Arrays.asList(name));
adapter = new ArrayAdapter<String>(this, R.layout.element, nameList);
listView.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent i){
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
name = i.getStringExtra("name");
}
}
}
And this is my SecondActivity:
public class SecondActivity extends AppCompatActivity {
EditText editText;
Button button2;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText = findViewById(R.id.editText);
button2 = findViewById(R.id.button);
}
public void finish() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView.
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
This is simply because you've incorrectly thinking that Activity.finish() is always called whenever you close the activity. But it never be called by the Android. Take a look for this Lifecyle picture from Activity-lifecycle concepts:
you can see that only onStop() then onDestroy() is called when activity is closed.
You need to call the finish() method manually to send your intent. Or, the better way, create a method that only build and set the intent for the result. Something like this:
private void prepareResult(String name) {
Intent i = new Intent();
i.putExtra("name", name);
setResult(RESULT_OK, i);
}
then call it whenever you want to close your activity:
String name = editText.getText().toString();
prepareResult(name);
finish();
Or you can override the onBackPressed() to also handing the back pressed, like the following:
#Override
public void onBackPressed() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
// The default implementation simply finishes the current activity
// see the documentation.
super.onBackPressed();
}
I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)
You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}
I have two activities that I would like to send data between. I would like to send two strings from the second activity to the first, where the strings will be manipulated and applied to the code. Here is the intent I have in the second activity to send the strings to the first activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});
And here is the intent I have in the first acitivity to receive the data:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_messages);
request_code = 1;
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.but);
assert button != null;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(TextMessages.this,Settings.class);
startActivityForResult(intent, request_code);
Intent a = new Intent(TextMessages.this,Settings.class);
startActivityForResult(a, recode);
}
});
Intent pass = getIntent();
String address = pass.getStringExtra("value");
String string2 = pass.getStringExtra("cost");
However, the app always crashes when I start it. So I can not use any of the data. Thanks in advance to any help I may receive.
In the first activity you should get your strings from onActivityResult. You shouldn't use startActivity in second activity.
Checkout this link for more information
https://developer.android.com/training/basics/intents/result.html
In your second activity you should use setResult method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
setResult(Activity.RESULT_OK,pass);
finish();
}
});
In your first activity you should get Intent data inside the onActivityResult method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_code) {
if(resultCode == Activity.RESULT_OK){
String value=data.getStringExtra("value");
String cost=data.getStringExtra("cost");
//handle value and cost.
}
}
}
I think it's because you missed out to put the target Activity class when you instantiate your intent. It should be like this:
Intent pass = new Intent(TargetActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
Hope it helps.
EDIT: But I agree with Abdullah. Since the first activity started the second activity, use onActivityResult to pass values to the first activity.
My guess is, the app starts into the second code block you posted. There is no value for String address = pass.getStringExtra("value"); or String string2 = pass.getStringExtra("cost"); when you start your app - only when the second activity sends these to the first activity. I would recommend finding a way to determine if its the second activity creating the first activity or creating a method to set these values in the first activity and call it in the second activity. Also, your second activity should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent(SecondActivity.this, FirstActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});
I am creating an android application that uses data from buttons clicked to generate an answer stored in the shared preferences. I am having trouble finding the necessary intent code to save the information when the user clicked on the button so it can be used to calculate the answer. As you can see from the code when the user clicks the button it will generate the next activity.Thanks!
Button button18 = (Button) findViewById(R.id.button18);
button18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I need to store the results then move to the next activity shown below?
Intent i = new Intent(EnterAgeActivity.this, EnterWeightActivity.class);
startActivity(i);
// TODO Auto-generated method stub
}
});
`
Send the data with the intent.
#Override
public void onClick(View v) {
//I need to store the results then move to the next activity shown below?
Bundle data = new Bundle();
data.putString(key, value);
data.putInt(key, value);
Intent i = new Intent(EnterAgeActivity.this, EnterWeightActivity.class);
i.putExtra("data", data);
startActivity(i);
}
EnterWeightActivity oncreate:
#Override
public void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if(intent.hasExtras) {
Bundle data = intent.getExtras();
// Your data
}
}