How to open another activity on button click? So now my app has one main window ( when you run the app ). Then there is a menu whit some buttons and when I click on some button is open another activity for this button.
So I have one form which user must enter his details but now I want to click on button continue and open another activity where he must enter some more info. Now when this button is clicked the info goes into database.
I know how to add more activities on normal page like main where I have only buttons but on this one I don't really know. Here is the code
public class Reservation extends Activity {
String Name;
String Email;
String Phone;
String Comment;
String DateTime;
String numberOfPeople;
private EditText editText1, editText3, editText2, editText4, txtDate, numberOfPeoples; //, txtTime;
private Button btnMenues, btnCalendar, btnTimePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation);
editText1 = (EditText) findViewById(R.id.personName);
editText3 = (EditText) findViewById(R.id.personPhone);
editText2 = (EditText) findViewById(R.id.personEmail);
editText4 = (EditText) findViewById(R.id.personComment);
txtDate = (EditText) findViewById(R.id.datePicker);
numberOfPeoples = (EditText) findViewById(R.id.numberOfPeoples);
btnCalendar = (Button) findViewById(R.id.btnCalendar);
//btnTimePicker = (Button) findViewById(R.id.btnTimePicker);
btnMenues = (Button) findViewById(R.id.continueWithReservation);
btnMenues.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Reservation.this, ReservationSecond.class);
intent.putExtra(Name, Name.getBytes().toString());
intent.putExtra(Email, Email.getBytes().toString());
intent.putExtra(Phone, Phone.getBytes().toString());
intent.putExtra(Comment, Comment.getBytes().toString());
intent.putExtra(DateTime, DateTime.getBytes().toString());
intent.putExtra(numberOfPeople, numberOfPeople.getBytes().toString());
startActivity(intent);
}
});
}
So what I can put in setOnClickListener to go on next activity?
Update:
If I change this
public void onClick(View v) {
Name = editText1.getText().toString();
Phone = editText3.getText().toString();
Email = editText2.getText().toString();
Comment = editText4.getText().toString();
DateTime = txtDate.getText().toString();
numberOfPeople = numberOfPeoples.getText().toString();
new SummaryAsyncTask().execute((Void) null);
}
to this
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Contacts.class);
startActivity(intent);
}
How to keep the information that user is added so far for next activity and save in DB after he finish with second activity?
You can use an Intent and then call startActivity() with that Intent:
myBtn.setOnClickListener() {
public void onClick() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
Android Docs give a pretty good explanation
EDIT
To address the question you gave in the comment, the easiest way to pass information between Activities is to use extras like:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(NAME, VALUE);
startActivity(intent);
There are multiple versions of putExtra to accomodate passing different types of values
Another way to store information would be to use SharedPreferences. Here is a simple example
You could also create a public class called, for example, Storage and have your data be represented as static member(s) of this class (accessed like Storage.MY_DATA) so it can be accessed from any point in your application.
Related
I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.
I'm trying to make the button (R.id.buttonring) call any phone number entered in the textview called tvTelefon. However, when i click that button, my phone is trying to call "w4126848130,511290,549" instead of the phone number entered in the textview. Any ideas how come? I don't receive any error messages so I'm clueless! Thanks!
public class Activity2 extends Activity {
public static final String SPARAD_DATA = "sparadData";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
SharedPreferences sp = getSharedPreferences(SPARAD_DATA, Context.MODE_PRIVATE);
TextView tvRubrik = (TextView)findViewById(R.id.textViewrubrik);
tvRubrik.setText(sp.getString("rubrik", "Rubrik saknas"));
TextView tvNamn = (TextView)findViewById(R.id.textViewnamn);
tvNamn.setText(sp.getString("namn", "Namn saknas"));
TextView tvText = (TextView)findViewById(R.id.textViewtext);
tvText.setText(sp.getString("text", "Text saknas"));
final TextView tvTelefon = (TextView)findViewById(R.id.textViewtelefon);
tvTelefon.setText(sp.getString("telefon", "Telefon saknas"));
TextView tvPris = (TextView)findViewById(R.id.textViewpris);
tvPris.setText(sp.getString("pris", "Pris saknas"));
Button r = (Button)findViewById(R.id.buttonring);
r.setOnClickListener (new View.OnClickListener() {
Intent call = new Intent(Intent.ACTION_DIAL);
#Override
public void onClick(View v){
call.setData(Uri.parse("tel:" +tvTelefon));
startActivity(call);
}
});
}
}
Slight change might help you:
call.setData(Uri.parse("tel:" +tvTelefon));
It should be:
call.setData(Uri.parse("tel:" +tvTelefon.getText().toString()));
you are passing an object reference, get the string you put into it.
Actually, how I understand your posted code you can just take the phone number straight from your preferences. Try this:
call.setData(Uri.parse("tel:" + sp.getString("telefon", "")));
Instead of:
call.setData(Uri.parse("tel:" +tvTelefon));
my problem is related to android application so far i was able to create 5 different activities with variables and onclick button to pass on next activity...
public class Abc extends Activity{
Button one2five;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA, tB, tC, tD, tE, tF;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
one2five = (Button) findViewById(R.id.btp1);
edtA = (EditText) findViewById(R.id.etA);
tA = edtA.getText().toString();
edtB = (EditText) findViewById(R.id.etB);
tB = edtB.getText().toString();
edtC = (EditText) findViewById(R.id.etC);
tC = edtC.getText().toString();
edtD = (EditText) findViewById(R.id.etD);
tD = edtD.getText().toString();
edtE = (EditText) findViewById(R.id.etE);
tE = edtE.getText().toString();
edtF = (EditText) findViewById(R.id.etF);
tF = edtF.getText().toString();
one2five.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openg2j = new Intent("com.sport.sport.G2J");
startActivity(openg2j);
}
});
}
}
Now with this as a base i created 4 more pages and total 26 different edittext fields.
My objective is:-
get all edit text in one calculate page.
perform claculation and post result in on One page.
Problem:-
get user text all decimal numbers
user get prompt if a field is left blank with highlighted field.
if a user press back previous activity should have previously entered values.
can any one help me to solve this issue..
thanks
monika
If you want to save the datas throughout the application then use "SHARED PEFERENCES"
Saving the values:
SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF",0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(String.valueOf(key), value);//store int values, can use string,arrays,etc
editor.commit();
Load the saved values:
SharedPreferences getProgramPrefs = this.getSharedPreferences(
"MY_SHARED_PREF", MODE_WORLD_READABLE);
int Index = getProgramPrefs.getInt(String.valueOf(key), -1);
and for this "a user press back previous activity should have previously entered values", you can try with finish() or by loading the preference values
finish();
With various methodology you can handle it
1- either define all the Field at application Level this will keep alive all the field alive untill and unless application is alive
Pros: Once defined use through out the application
cons: once app close data lost.
2- pass value in intent handle it in next activity and then combine field of previous and current activity and pass it on to next activity
Pros: use Fields in back and forth activity
cons: once app close data lost.
3- Save field in shared preferences and use it any time either app is alive or not
pros: data is saved once committed.
4- you can use ArrayList and add data in every current to next activity context switching
so its up to you with which scenario you want to use it
and if you want to have data in previous activity
you can override method onbackpressed and perform your functionality as accordingly
Sample code Using Method 2
Modified code in the question
public class Abc extends Activity{
Button one2five;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA, tB, tC, tD, tE, tF;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
one2five = (Button) findViewById(R.id.btp1);
edtA = (EditText) findViewById(R.id.etA);
tA = edtA.getText().toString();
edtB = (EditText) findViewById(R.id.etB);
tB = edtB.getText().toString();
edtC = (EditText) findViewById(R.id.etC);
tC = edtC.getText().toString();
edtD = (EditText) findViewById(R.id.etD);
tD = edtD.getText().toString();
edtE = (EditText) findViewById(R.id.etE);
tE = edtE.getText().toString();
edtF = (EditText) findViewById(R.id.etF);
tF = edtF.getText().toString();
one2five.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openg2j = new Intent("com.sport.sport.G2J");
openg2j .putExtra("field1Data", tA);
openg2j .putExtra("field2Data", tB);
openg2j .putExtra("field3Data", tC);
openg2j .putExtra("field4Data", tD);
openg2j .putExtra("field5Data", tE);
startActivity(openg2j);
}
});
}
this is how you can send data from current class to next Class
}
Edited :
in 2ndActivity you can get that data like ,
String field5Data = getIntent().getStringExtra("field5Data") ;
I think you have to create ArrayList<String> data = new ArrayList<String>(); for each activity when onClick(). Press "add all data" to ArrayList, pass this ArrayList into intent, put extra, on 2,3,4 activity you get previous activity data from getIntent() add your current activity data and pass on next activity and last activity do your calculation with previous and current data.
/**First Activity**/
ArrayList<String> data = new ArrayList<String>();
#Override
public void onClick(View v) {
data.clear();
data.add(tA);
--------------
--------------
data.add(tZ);
Intent intent = new Intent(SecondActivity.class, FirstActivity.this);
intent.putStringArrayListExtra("IN_PREVIOUS_DATA", data);
startActivity(intent);
}
/**Second Activity**/
ArrayList<String> data = new ArrayList<String>();
#Override
public void onClick(View v) {
data.clear();
data.addAll((ArrayList<String>)getIntent().getStringArrayListExtra("IN_PREVIOUS_DATA"));
data.add(tA);
--------------
--------------
data.add(tZ);
Intent intent = new Intent(ThirdActivity.class, SecondActivity.this);
intent.putExtra("IN_PREVIOUS_DATA", data);
startActivity(intent);
}
I am developing an android application. In that application I have some variety of products.
When user clicks on any product it will show images of the product. In that page only I have
one Button like Enquiry. When user clicks on that it will open a form page . In that form I have one field Product name. After completing this when user clicks on submit the details has been mailed to admin mail .
What I want is when user clicks on Enquiry Button it should open a form page with auto filled
product name .
Here is my code.
Lenovo.java:
Button order = (Button) findViewById(R.id.order);
order.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intentmenu = new Intent(view.getContext(),Order.class);
startActivityForResult(intentmenu,0);
}
});
}
Order.java:
final EditText name = (EditText)findViewById(R.id.username);
final EditText mail = (EditText)findViewById(R.id.email);
final EditText phone = (EditText)findViewById(R.id.phone);
final EditText product = (EditText)findViewById(R.id.product);
final String _name = name.getText().toString();
final String _mail = mail.getText().toString();
final String _phone = phone.getText().toString();
final String _product = product.getText().toString();
System.out.println(_name);
System.out.println(_mail);
System.out.println(_phone);
System.out.println(_product);
Button email = (Button) findViewById(R.id.Button01);
email.setOnClickListener(new OnClickListener(){
public void onClick(View v){
StringBuilder body = new StringBuilder();
body.append("Name: "+name.getText().toString());
body.append("\n\n\nMail: "+mail.getText().toString());
body.append("\n\n\nPhone: "+phone.getText().toString());
body.append("\n\n\nProduct: "+product.getText().toString());
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL ,new String[]
{"b.gadwantikar1#gmail.com","",});
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Customer Details");
i.putExtra(android.content.Intent.EXTRA_TEXT, body.toString());
startActivity(i);
finish();
}
});
}
}
If any body knows then help me out...
Thanks in Advance...
You can do as below:
Add new activity to your project
add the new activity to your Mainfest file.
add new XML file for the new activity into layout folder.
in the XML file define TextView and/or Edittext as your demand.
setContentView of your new activity in onCreate() method to be the new XML file.
Start the new Activity by the following code wich will have the data that will transfer to the new activity.
like the following:
Intent intent=new Intent(this,NewActivity.class)
intent.putExtra("Key","data_need_to_Be Transfer");
startActivity(intent);
and to retrive the data that already trsansfered to the new activity by doing:
Intent intent = getIntent();
String yourString=intent.getStringExtra("key");
and then you can assig the value of that string to Textview or EditText
I'm creating a password field. I want to input password and at once star new intent. Her is my code:
EditText passwd = (EditText) findViewById(R.id.passwd);
passwd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText ryt = (EditText) findViewById(R.id.editPasswd);
if(ryt.getText().toString().equals("123")) {
Intent intent = new Intent(first.this, second.class);
startActivity(intent);
System.exit(0);
}
}
}
I wondering how to change onClick method. Maybe some one had similar problem. Thanks in advance
final EditText passwd = (EditText) findViewById(R.id.passwd);
Witha final variable you would be able to access it from your inner class.
Erasing System.exit(0) might be a good start.