Android Save Contact by click button event - java

I want to make a button that when its clicked it creates and saves all the person's information such as name, email and phone # into the contact list without opening the contact form or hitting save, code is in Java.
EditText name;
EditText phone;
Button addContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
name = findViewById(R.id.editTextTextPersonName);
phone = findViewById(R.id.editTextPhone2);
addContact = findViewById(R.id.buttonDone);
addContact.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(!name.getText().toString().isEmpty() && !phone.getText().toString().isEmpty()){
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, name.getText().toString());
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone.getText().toString());
if(intent.resolveActivity(getPackageManager()) !=null){
startActivity(intent);
}else{
Toast.makeText(MainActivity2.this, "Try again", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity2.this, "Try again", Toast.LENGTH_LONG).show();
}
}
}
);
}
}

You'll need to use the ContactsProvider. You'll see how to use it and some examples.

Related

How to make two "intent" in android studio code not processing at the same time?

In the code below (log in code), I have one button that if it clicked, it will start two intent. One intent is to use to open A class and the other is to use to send a string value to B class. But when the button clicked, the second intent not just sending the string value but also open the B class. The code supposedly to just send the string value to B class and open A class.
How to fix this?
package com.example.project;
public class login extends AppCompatActivity {
DatabaseReference reff;
EditText kode, pass;
Button masuk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FirebaseApp.initializeApp(this);
kode = (EditText)findViewById(R.id.kode);
pass = (EditText)findViewById(R.id.pass);
masuk = (Button)findViewById(R.id.masuk);
masuk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(TextUtils.isEmpty(kode.getText().toString()) || TextUtils.isEmpty(pass.getText().toString())){
Toast.makeText(login.this, "isi kode alat atau password", Toast.LENGTH_LONG).show();
}
else {
final String value = kode.getText().toString().trim();
reff = FirebaseDatabase.getInstance().getReference().child(value);
final String password = pass.getText().toString().trim();
// Read from the database
reff.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String passin = dataSnapshot.child("pass").getValue().toString();
if (passin.equals(password)){
Intent intent = new Intent(login.this, MainActivity.class);
startActivity(intent);
Intent send = new Intent(login.this, addmember.class);
send.putExtra("kode", value);
startActivity(send);
}
else if(!passin.equals(password)){
Toast.makeText(login.this, "password salah", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
}
});
}
Here, you can't start activity because you are trying to start two activities at same time. This is not possible and if possible this approach is not too good. In safe side, you can use EventBus library that used for pushing event as data and subscriber that receive that data. This is the right way to do what you want here.

How can I add a data to the another activity in a Button form

I'm making an android POS and I have a problem to add a data pass to other activity. When the user is adding a new product, the product will show on the Menu activity in the Button form and has a value whatever the user input in the name and price.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addproduct);
POSDB = new DatabaseHelper(this);
prdName = (EditText)findViewById(R.id.PrdName);
prdPrice = (EditText) findViewById(R.id.PrdPrice);
btnAdd = (Button) findViewById(R.id.BtnPrdAdd);
btnBack = (Button) findViewById(R.id.BtBack);
AddProduct();
}
public void AddProduct(){
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = prdName.getText().toString().trim();
String price = prdPrice.getText().toString().trim();
if(prdName.length() != 0 && prdPrice.length() != 0)
{
boolean ADD = POSDB.addData(name,price);
if(ADD == true)
{Toast.makeText(Addproduct.this, " The product has been added", Toast.LENGTH_LONG).show();}
else
{Toast.makeText(Addproduct.this, " Something Went Wrong", Toast.LENGTH_LONG).show();}
}
else
{Toast.makeText(Addproduct.this, " Please fill up the Textfield", Toast.LENGTH_LONG).show();}
}
});
}
That depends on how do you structure the Menu Activity to get the data and how it shows it.
For example, If you are using a RecyclerView you can set it to show the data in a list of buttons, so when a new product added to the database it will be shown by RecyclerView in the button form.

EditText data not being passed over without button click

I am building an app where you can upload images to my company server
My problem is that I have a login screen with Email, Password and Client ID(in LoginActivity)
Now The data you enter there is passed to another Activity(CameraActivity) and that is used to build a URL with Uri.Builder.
The data is being saved within the edittext boxes BUT it is only being passed(using intents) to the other activity on the button click so, you have to go back to the login screen and click the button to reSubmit the data for every upload and everytime you start the app(The Data is stored within the textboxes but it is not being passed to the other activity without the button click), for the URI.Builder to work and give the entered information to the URL.
I want the client to enter that information ONCE and click the Button ONCE and then it is stored and used everytime regardless of the app being exited etc.
Login Activity, This activity has the edittext boxes and the entered information is being saved but not passed to the other activity without the button click
public class LoginActivity extends AppCompatActivity {
String ID = "id";
String EMAIL = "email";
String PASS = "pass";
private SharedPreferences mPreference;
EditText email, password, id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
id.setText(mPreference.getString(ID, ""));
email.setText(mPreference.getString(EMAIL, ""));
password.setText(mPreference.getString(PASS, ""));
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EMAIL =email.getText().toString();
PASS =password.getText().toString();
ID =id.getText().toString();
mPreference.edit().putString(ID,ID).apply();
mPreference.edit().putString(EMAIL, EMAIL).apply();
mPreference.edit().putString(PASS, PASS).apply();
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", ID);
intent.putExtra("email", EMAIL);
intent.putExtra("password", PASS);
startActivity(intent);
}
});
}
}
As suggested in the question comment, SharedPreferences is a good option.
SharedPreferences getPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
void setEmail(Context context, String email) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putString("EMAIL", email);
editor.apply();
}
String getEmail(Context context) {
return getPreferences(context).getString("EMAIL", "");
}
Above is an example of how you can put things and retrieve things from the shared preferences. Since you have more than an email to store (user ID, etc...) you could even store an object as a gson so you would have to call only one function. But of course this is your call.
You can start the CameraActivity if data exist in SharedPreferences.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.emailtext);
password=findViewById(R.id.pwdtext);
id=findViewById(R.id.clientid);
Button loginBtn=findViewById(R.id.button);
mPreference = PreferenceManager.getDefaultSharedPreferences(this);
//Check if data exist
if(mPreference.contain(ID))
{
Intent intent=new Intent(LoginActivity.this,
CameraActivity.class);
intent.putExtra("clientId", mPreference.getString(ID, ""));
intent.putExtra("email", mPreference.getString(EMAIL, ""));
intent.putExtra("password", mPreference.getString(PASS, ""));
startActivity(intent);
}
//Rest of the code
}
Cheers :)

Password - String comparison

My app consists of an Activity, which asks for a String password, in order to launch a second Activity. I am using an EditText for input and a Button for verification. If the Button is pressed, the app should check whether the inserted password, (let's assume it's "ABC") matches the password in the array. If not, set the inserted password to a red color.
public class Login extends AppCompatActivity{
Button mButton;
EditText mEdit;
String [] mArray;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mButton = (Button)findViewById(R.id.anmelden_button);
mEdit = (EditText)findViewById(R.id.password);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
String [] mArray = getResources().getStringArray(R.array.password);
if (mArray.equals(mEdit.getText().toString())){
Intent intent = new Intent(this, secondactivity.class);
startActivity(intent);
}else{
mEdit.setTextColor(Color.RED);
}
}
}
My problem is, that when I insert "ABC" and press the Button, nothing happens at all.
You need to loop through all the passwords first, then check to see if you found what you wanted.
Your onclick listener should something look like this:
mButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
String [] mArray = getResources().getStringArray(R.array.password);
// loop to check all passwords
for (String s : mArray) {
if (s.equals(mEdit.getText().toString())) {
// found the password
Intent intent = new Intent(this, secondactivity.class);
startActivity(intent);
return;
}
}
mEdit.setTextColor(Color.RED);
}
});
Put your password checking code inside the onClickListener
Loop through each element of the array, checking it against the value in your EditText

SharedPreferences value always returns true?

I have an Activity in which I am trying to implement a kind of auto-login. In my login activity, I have this:
sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE);
User.setUid(sharedPref.getInt("UID", 1));
Boolean al = sharedPref.getBoolean("AUTOLOGIN", false);
...
if (al) {
Log.i("AUTOLOGIN", "Go!");
Gui.createAlert(context, context.getString(R.string.loading));
Intent i = new Intent(context, CityActivity.class);
context.startActivity(i);
}
...
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
Ajax.AjaxListener callback = new Ajax.AjaxListener() {
int uid = Integer.parseInt(userInfo.optString("user_id", "1"));
String sid = user.optString("sid", "");
String k = json.optString("k", "");
Boolean al = autoLogin.isChecked();
SharedPreferences.Editor e = sharedPref.edit();
e.putInt("UID", uid).putBoolean("AUTOLOGIN", al).putString("SID", sid).apply();
Gui.createAlert(context, context.getString(R.string.loading));
Intent i = new Intent(context, CityActivity.class);
};
});
I've only been able to test this on the emulator, and I can't seem to get the SharedPreferences file contents to show in Android Device Monitor, but every time the app loads, it does the autologin routine. Even when I shut down the emulator completely and restart everything the autologin flag seems to persist. I'm pretty new to Java/Android programming, but coming from a PHP and JavaScript background, it's not hard to pick up. I'm just stumped as to why the AUTOLOGIN SharedPreference key always seems to return true when checked. Is there an example of implementing autologin with SharedPreferences?
I should note that I tried to use a database originally, but scrapped the idea because of difficulty of use and the minimal data required right now.
Try This Hope This Will Help You.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void login(View v){
SharedPreferences spf=getSharedPreferences("myprfs",Context.MODE_PRIVATE);
String name=spf.getString("uname", "no value");
String pass=spf.getString("pass", "no value");
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
if(et1.getText().toString().equalsIgnoreCase(name) && et2.getText().toString().equalsIgnoreCase(pass))
{
Intent i=new Intent();
i.setComponent(new ComponentName(getApplicationContext(), WelcomeActivity.class));
startActivity(i);
}
}
public void register(View v){
Intent i=new Intent();
i.setComponent(new ComponentName(getApplicationContext(), RegistrationActivity.class));
startActivity(i);
}
}
RegistrationActivity.java
public class RegistrationActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
public void register(View v){
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
EditText et3=(EditText)findViewById(R.id.editText3);
SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
SharedPreferences.Editor spe=spf.edit();
spe.putString("uname", et1.getText().toString());
spe.putString("pass", et2.getText().toString());
spe.putString("dob", et3.getText().toString());
spe.commit();
finish();
}
}

Categories