How to get the specified Product name in EditText box? - android

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

Related

Android studio getting data from 1 activity sending it to another

I am trying to make a button send data from a number input from one activity to another and display that number. When I click the add button it does not change the value and it displays as null.
//EXTRA_NUMBER is initalized under class header as
//public static final String EXTRA_NUMBER = "com.example.operationhydration.EXTRA_NUMBER";
public void addWater()
{
EditText editText = (EditText) findViewById(R.id.water_input);
double number = Double.parseDouble(editText.getText().toString());
Intent intent = new Intent(this, Progress.class);
intent.putExtra(EXTRA_NUMBER,number);
}
//Code in class I am trying to send it to
Intent intent = getIntent();
String number = intent.getStringExtra(AddWater.EXTRA_NUMBER);
TextView textGoal = (TextView) findViewById(R.id.goal_text);
textGoal.setText("" + number);
EDIT:
I got the variable to display but how do I make it stay if I change pages and come back.

Passing inputs to a new intent

hello there i'm a new beginner in android development. I tried several times to figure out my issue but i couldn't please help me.I created a simple login screen once the user enters the username and password I simply want to display it in the next activity. I used putExtra and getExtra but the values are null all the time.
This is my code
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
data = (TextView)findViewById(R.id.txt);
Intent i = this.getIntent();
u = i.getStringExtra("username");
p = i.getStringExtra("password");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
You've Passed the Data to the next Intent Correctly using PutExtra but in the new intent when you've used getStringExtra you've used another String variable which is completely different from what you've passed.Try this example it should work.You should use i.getStringExtra("uname"); and i.getStringExtra("pass"); instead of what you have passed.
public class Data1 extends AppCompatActivity {
EditText username;
EditText password;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data1);
username = (EditText)findViewById(R.id.edusername);
password = (EditText)findViewById(R.id.edpassword);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
}
});
}
}
public class Data2 extends AppCompatActivity {
TextView data;
String u;
String p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data2);
data = (TextView)findViewById(R.id.txt);
// Intent i = getIntent();
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
}
}
You a passing the value using
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
and getting value using
u = i.getStringExtra("username");
p = i.getStringExtra("password");
here you must be use same key as you passed in intent so you have to use "uname" instead of "username" and "pass" instead of "password"..
example :
FirstActivity.java
i.putExtra("key", "value");
secondActivity.java
i.getStringExtra("key");
You are getting null values because strings are not same in put and get method.And values are always stored in same key.So change your code in get method like as-
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");`

Open new class/activity on button click in android

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.

android pass the username and password in edittext of another activity

I am developing android application with login. When the user logged in i want to pass both username and password to another activity in another edittext. The passed username and password should be displayed in the another edittext of another activity. the password should look as it is (not revealing characters). I know that it may sound like weird but is that possible? Just give me an answer i know you might ask why should i display this thing to another edittext but i have my own purpose.
This is what i need to pass to another edittext
inputusername = (EditText) findViewById(R.id.loginUsername);
inputPassword = (EditText) findViewById(R.id.loginPasswod);
imgLogin1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String username = inputusername.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(username, password);
// check for login response
try {
if (json.getString(KEY_PSUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_PSUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_PUSERNAME), json.getString(KEY_PUID), json_user.getString(KEY_PCREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), PatientHomeActivity.class);
dashboard.putExtra(KEY_PUSERNAME,username);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
EDIT
Here's where i have passed the data
private static final String TAG_PUSERNAME = "username";
private static final String TAG_PASSWORD = "password";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Start and bind the imService
**/
startService(new Intent(Login.this, IMService.class));
setContentView(R.layout.login_screen);
setTitle("Login");
Bundle i = getIntent().getExtras();
Button loginButton = (Button) findViewById(R.id.login);
cancelButton = (Button) findViewById(R.id.cancel_login);
usernameText = (EditText) findViewById(R.id.userName);
usernameText.setText(i.getString(TAG_PUSERNAME));
passwordText = (EditText) findViewById(R.id.password);
passwordText.setText(i.getString(TAG_PASSWORD));
That's the next activity where the data should be passed.
Use Bundle to pass the value from current activity to next acivity
Current Activtiy to pass data
Intent i = new Intent (BundleActivity.this,datapass.class);
Bundle b =new Bundle();
b.putString("Username","your Username editext value");
b.putString("Password","your Password editext value");
i.putExtras(b);
startActivity(i);
In Next activity to receive data
Bundle b = getIntent().getExtras();
String Username= b.getString("Username");
String Password= b.getString("Password");
For passing data from onr activity to other you can use intents or shared preference.
For setting password as non revealing character in edit textbox :Set this android:inputType="phone" to editbox of your second activity in which you are showing password in Xml Layout OR from your activity you can also dynamically set this as userPassword.setInputType(129)(it will make text into password ).Hope it will help.

set text in textview with text from an edittext in another class

Im completely new to Android so apologies if this is a silly question. But my problem is this:
I have created a number of classes for each of the pages in my app and I would like to update the text in a textview of a particular class from the text in an edittext field from another class.
To be more specific, I have a login page and I want the username (input by the user in an edittext box) to be transferred to a textfield in the logged in page. Currently I am trying to achieve this by using a click listener for the log in button in the log in page:
public void sign_in_click(View view) {
EditText tv1 = (EditText) findViewById(R.id.txt_input_uname);
String username=tv1.getText().toString();
LoginDetails unamede=new LoginDetails();
unamede.setuname(username);
Intent myIntent = new Intent(view.getContext(), customer.class);
startActivityForResult(myIntent, 0);
}
So the click listener initialises a new class variable I have defined in another class like so:
public class LoginDetails {
public String uname;
public void setuname(String username){
uname=username;
}
public String getuname(){
return uname;
}
}
and it gives uname the username from the edittext box in the login page.
Then I have in the logged in page under oncreate:
LoginDetails unamed= LoginDetails();
String username=unamed.getuname();
tv1.setText(username);
but the text in the textview box doesnt get anything written to it. Now I wont be surprised if I'm doing this completely wrong but any advice would be much appreciated. thanks
What i would suggest is put the user's log in information into a SharedPreference. To transfer the date to another activity.
For example...
SharedPreferences myPrefs = getSharedPreferences("USER_INFO", 0);
myPrefsEdit.putString("USER_ID", username=tv1.getText().toString(););
//This is the username you get from edittext
myPrefsEdit.putString("PASSWORD", password);
//this is the user password you get from edittext
myPrefsEdit.commit();
In your next activity.
Get reference to your SharePreference like...
SharedPreferences info = getSharedPreferences("USER_INFO, 0);
String username = info.getString("USER_ID", "DOESNT EXIST");
String userpassword = info.getString("PASSWORD", "DOESNT EXIST");
This should do it
You could try putting the username to activity intent extra (when you don't want to persist it in the shared preferenced right now):
Intent myIntent = new Intent(view.getContext(), customer.class);
myIntent.putExtra("uname", username);
startActivityForResult(myIntent, 0);
And then in onCreate:
tv1.setText(getIntent().getStringExtra("uname"));

Categories

Resources