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.
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 want to hide a button when it get intent from a specific class.
Is it possible?
For instance, i have classes named A_activity.class, B_activity.class and C_activity.class . In C_activity.class , if it get intent from A_activity.class, the button is visible. But if it get from B_activity.class, the button is invisible.
This is my source code
Intent i=getIntent();
buttonTTS = (AppCompatButton) findViewById(R.id.button);
buttonTTS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speakOut();
}
});
What should i do with my code?
You should do it like this. When you're creating the Intent from activity A you should add extra:
intent.putExtra("buttonVisible", true);
And from activity B
intent.putExtra("buttonVisible", true);
And in your activity C:
if (getIntent().getBooleanExtra("buttonVisible", false)){
buttonTTS.setVisibility(View.VISIBLE)
} else
buttonTTS.setVisibility(View.GONE);
From A_Activity putExtra("showButton", true) and from B_Activity putExtra("showButton", false)
Now in C_Activity, getExtras and hide and show button.
hope this will help you.
You can send a tag with the intent and then check the tag with the class value then you can make hide or show the button.
putExtra("tag","A_activity");
Now in class C you can check it by taking it in string and compare it by value.
String tag = getIntent().getStringExtra("tag");
if(tag.equals("A_activity")){
button.setVisibility(View.GONE);
}
else{
....
}
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.
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));
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"));