I have been searching other stackoverflow questions and also looking through the android developer guide but I have not found an solution yet.. In my program I ask the user to enter in a homework assignment that they have just received. From there I take that message and I print it on a new screen. What I want to be able to do is save the previous messages entered in and print those out each time along with the new message. What I am having trouble is printing more than one message at a time. I used setContentView but it only prints one message. Does anyone have any advice? I have posted my code at the bottom:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
saveHomework(message);
// Set the text view as the activity layout
setContentView(textView);
}
public void saveHomework(String message)
{
String message2 = message;
TextView textView2 = new TextView(this);
textView2.setTextSize(40);
textView2.setText(message2);
setContentView(textView2);
}
Well, apparently you should preserve all previously entered messages in some place.
Your options:
If you are sure your activity process is alive all the time user can enter the messages, you can use some static variable of the activity and store there the messages in a list or array.
If your process can be closed you should store the messages in a filed using SharedPreferences.
Then, whereever you want to show the messages you should read them from where you have them saved and pass all together to setText(messages_list) call.
Related
Same as my last question, I'm building custom contacts app which saved the contacts on the app only.
I've encountered an issue from creating a contact and viewing it.
Whenever I click on "OK" on the new contact screen, instead of opening the activity with the input, I'm being pushed back to contact list.
I've seen countless of questions here about problem passing information from activity to another, yet it didn't help me.
I've switched from startActivity(intent) to startActivityForResult(moverIntent,1);
I've tried using putExtra() to each string instead of bundle.
I've tried to send only 1 string instead of 4 and still didn't work.
The AddNewActivity.java:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addcontact);
ok=findViewById(R.id.okIB);
not_ok=findViewById(R.id.notokIB);
fullName=findViewById(R.id.fullnameET);
nickName=findViewById(R.id.nicknameET);
email=findViewById(R.id.emailET);
phoneNum=findViewById(R.id.phoneET);
final String name,nick,emaill,phone;
name=fullName.getText().toString();
nick=nickName.getText().toString();
emaill=email.getText().toString();
phone=phoneNum.getText().toString();
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ok.setImageResource(R.drawable.okpress);
Intent moverIntent=new Intent(AddNewActivity.this,ContactActivity.class);
Bundle bundle=new Bundle();
bundle.putString("fullName",name);
bundle.putString("nickName",nick);
bundle.putString("email",emaill);
bundle.putString("phoneNum",phone);
moverIntent.putExtras(bundle);
startActivityForResult(moverIntent,1);
}
});
The ContactActivity.java:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//Intent get_info=getIntent(); //Previous attempt of getting the Bundle
Bundle old_info= getIntent().getExtras();
final String phone_num,full_name,nick_name,email_add;
phone_num=old_info.getString("phoneNum");
full_name=old_info.getString("fullName");
nick_name=old_info.getString("nickName");
email_add=old_info.getString("email");
pic_btn=findViewById(R.id.take_pic);
date_btn=findViewById(R.id.take_date);
call_btn=findViewById(R.id.take_call);
sms_btn=findViewById(R.id.send_sms);
phone=findViewById(R.id.contact_phoneTV);
name=findViewById(R.id.contact_nameTV);
nick=findViewById(R.id.nick_nameTV);
email=findViewById(R.id.emailTV);
phone.setText(phone_num);
name.setText(full_name);
nick.setText(nick_name);
email.setText(email_add);
Based on what I've learned and read here, I have expected that the activity ContactActivity to be open and display all the information from the AddNewActivity, what does happen is that I'm being thrown back to the list activity (didn't post it here).
Hi everyone.
I'm new in android and I'm working on an app in which I need to recall the same activity to enter the information of a variable amount of entities (passengers).
I have the following:
btnContinue3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (int i=0; i<Pssngr; i++){
passenger[i] = new
Intent(getApplicationContext(), Pasajeros.class);
startActivity(passenger[i]);
}
}
});
Pssngr is the amount of passsengers or entites that need a unique activity to get their information entered.
The trigger is the button then the activities will be called one by one following an array
I try this code but after clicking on the button the app crashed.
Please someone help me find a way to make this work.
thanks
It crashes because You are trying to start x number of Activities at once.
If You have to run new Activity for each of Passengers best in this scenario will be startActivityForResult
I beliver effect You trying to get is that user clicks on button just once and activities for each passenger will open one after another.
To do it in method onClick You will start only first activity, don't use loop.
You start consequently next activities in onActivitiyResult
In addition to what Gustek mentions above, a better way to approach this would be to have one activity and just pass the different values from the parent (PassengersAvitivty) activity through the intent as below:
final Intent intent = new Intent(PassengersAcitivty.this, PassengersEntityActivity.class);
intent.putExtra("PASSENGER_FIRSTNAME", passenger[i].getName());
intent.putExtra("PASSENGER_LASTNAME", passenger[i].getLastName());
intent.putExtra("PASSENGER_EMAIL", passenger[i].getEmail());
startActivity(intent);
and here is how you can retrieve them on your activity (PassengerEntityActivity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
firstname = extras.getString("PASSENGER_FIRSTNAME");
lastname = extras.getString("PASSENGER_LASTNAME");
email = extras.getString("PASSENGER_EMAIL");
}
else
{
//Log.v("NO VALUE", "OOPS");
}
I can clarify further if needed.
This is probably quite basic but I've only needed a use for a feature like this now. I have a button that carries out a calculation when I click on it. The output is a number. I want to put that number into a TextView on a different layout. On it's own page basically.
I can already get what I want on the same page. Just do the whole
TextView.setText();
Can anyone help me put the data onto it's own page? So when I click the button it carries out the calculation AND opens this new page to put the answer on it?
I tried putting the TextView in a new layout file and calling it via a findViewById but that gave me a force close.
Any solutions? Thanks.
EDIT: Here's the code, I'm trying to display the time on another page. See below
public void getWakeUpTime (View v) {
LocalTime localtime = new LocalTime();
LocalTime dt = new LocalTime(localtime.getHourOfDay(), localtime.getMinuteOfHour());
LocalTime twoHoursLater = dt.plusHours(2);
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
Text1.setText("Time: " + twoHoursLater.toString(formatter));
}
Right now it displays on the same page under the TextView Text1.
You can pass integer to next activity using this code:
String num;
Intent i = new Intent(this, NextActivity.class);
i.putExtra("value", num);
startActivity(i);
You can retrieve the data on next activity as shown below:
Intent i = getIntent();
String num = i.getStringExtra("value");
textview.setText("Number is: "+num);
So brother, here you want is that you have calculated a value in 1st page and you want to show it on the 2nd page am I right?
For this brother you need to pass some data(i.e. Bundle) while calling the next activity.
Here's a similar Application that I built few months back. It passes the message created in one activity to another. Hope this works for you.
The following code is from the first activity where message is created and it is bundled and sent to the next activity.
public void yourMethod(View view ){
// Fetching the message to be sent to the next activity.
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
// Remember this constant(or any string you can give). to be used on other activity.
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
And this is what I did on the onCreate Method of the Activity which was called by the previous Activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
// save the passed message as string so that it can be displayed anywhere in this activity.
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Hope My Program give you enough light to your solution. Hope that satisfies you.
Thanks brother.
You need to send the data as an Extra. Starting Another Activity has all the information you need.
Totally new and starting out just working through the app tutorials. I wanted to run an algorithm to test out how it runs. I have it working all perfectly in java but am not sure how to create a class instance correctly in android.
public class DisplayMessageActivity extends Activity {
myAlgorithm myAlg = new myAlgorithm();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
However as soon as run the program I get a force close. I take it this isn't the correct way to do it with Android?
TIA
I am trying to use putExtra() and getExtra() to send String Data from one activity to another, such that the retrieved string is to be displayed on a TextView and when running.
When i run the program i get a classCastException on onCreate() method.
I am new to android so any assistance will be appreciated.
Here is my sample code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sales);
//TextView
TextView model1 = (TextView)findViewById(R.id.model1);
TextView model2 = (TextView)findViewById(R.id.model2);
TextView model3 = (TextView)findViewById(R.id.model3);
//Bundle
Bundle bundle = getIntent().getExtras();
String Mod1 = bundle.getString("model1");
String Mod2 = bundle.getString("model2");
String Mod3 = bundle.getString("model3");
//setting values
model1.setText(Mod1);
model2.setText(Mod2);
model3.setText(Mod3);
}
Your widgets model1,model2,model3 are either not TextView or you do not pass strings to the intent that you pass to the new Activity. Also you could try to clean your projects, maybe your R.java file is messed up. You could also paste the LogCat or tell us which is the line that gives you the ClassCastException.