checking for gmail in android studio using java - android

I'm working on a mobile application for an online learning center and it requires the user to register using their email address. I don't know how to verify that the email address entered by the user actually exists.
--- This is my XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<!--EditText which takes input
as Email from the user-->
<EditText
android:id="#+id/emailField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="16dp"
android:drawableStart="#drawable/ic_email_black_24dp"
android:drawablePadding="12dp"
android:hint="Email" />
<!--Button which when clicked validates
the entered email is valid or not-->
<Button
android:id="#+id/validateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/emailField"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:backgroundTint="#color/colorPrimary"
android:text="VALIDATE"
android:textColor="#android:color/white" />
</RelativeLayout>
--- And this is my Java code
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// EditText filed for Email
EditText etMail;
// Button to validate the Email address
Button bValidate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// register all the UI elements
// with their appropriate IDs
etMail = findViewById(R.id.emailField);
bValidate = findViewById(R.id.validateButton);
// handle the VALIDATE button to show the toast
// message whether the entered email is valid or not
bValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
emailValidator(etMail);
}
});
}
// the function which triggered when the VALIDATE button is clicked
// which validates the email address entered by the user
public void emailValidator(EditText etMail) {
// extract the entered data from the EditText
String emailToText = etMail.getText().toString();
// Android offers the inbuilt patterns which the entered
// data from the EditText field needs to be compared with
// In this case the entered data needs to compared with
// the EMAIL_ADDRESS, which is implemented same below
if (!emailToText.isEmpty() && Patterns.EMAIL_ADDRESS.matcher(emailToText).matches()) {
Toast.makeText(this, "Email Verified !", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Enter valid Email address !", Toast.LENGTH_SHORT).show();
}
}
}
--- But it only checks that the email is valid. I don't know if it's on the server or not.
Can this be done in Java code? If yes, please show me an example.

Related

Keep user in Activity

I was creating an app then a question come out:
how can i keep user in activity?
I've put two editTexts with input of int.
i want user be kept in activity until he/she puts valid numbers in edittexts.
any ideas?
The solution is very simple all you need to do is get the string values from the edit text box and have a condition in your setonclicklistener that checks the strings if they match to a certain value, if yes the intent from current activity will take you to next if not it will show an error on the edit text box (by using .setError()) and return.
Here is the Java Class Code:
package com.example.text_to_speech;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity);
EditText editText1,editText2;
Button button;
editText1=findViewById(R.id.ed1);
editText2=findViewById(R.id.ed2);
button=findViewById(R.id.check_input);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!editText1.getText().toString().equals("Don"))
{
Toast.makeText(ActivityA.this, "You Can't Leave this Activity, Input Correct Fields", Toast.LENGTH_SHORT).show();
editText1.setError("Input Correct values");
return;
}
if(!editText1.getText().toString().equals("Tom"))
{
Toast.makeText(ActivityA.this, "You Can't Leave this Activity, Input Correct Fields", Toast.LENGTH_SHORT).show();
editText2.setError("Input Correct values");
return;
}
Intent intent=new Intent(ActivityA.this,MainActivity.class);
startActivity(intent);
}
});
}
}
XML Code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityA">
<EditText
android:layout_margin="10dp"
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Input Data Here"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:layout_margin="10dp"
android:layout_below="#id/ed1"
android:id="#+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Input Data Here"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ed2"
android:id="#+id/check_input"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:text="Go to Next Activity"
/>
</RelativeLayout>
Output:

Firebase "no such instance field" error

As the title says, I have encountered that error when I tried working with Firebase database. I have tried numerous solutions found online but nothing seems to have worked for me. Below you can find my code and also, a brief information regarding on what I've tried so far before posting on stackOverflow.
The code is the following:
package com.example.vlad.restaurantorder;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.vlad.restaurantorder.Model.User;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rengwuxian.materialedittext.MaterialEditText;
public class LogIn extends AppCompatActivity {
EditText editPhone, editPassword;
Button btnLogIn;
FirebaseDatabase database;
DatabaseReference tableUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
btnLogIn = (Button)findViewById(R.id.btnLogInScreenLogIn);
editPhone = (MaterialEditText)findViewById(R.id.edtPhone);
editPassword = (MaterialEditText)findViewById(R.id.edtPassword);
//database
database = FirebaseDatabase.getInstance();
tableUser = database.getReference("User");
btnLogIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ProgressDialog progressDialog = new ProgressDialog(LogIn.this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
tableUser.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(editPhone.getText().toString()).exists()){
progressDialog.dismiss();
User user = dataSnapshot.child(editPhone.getText().toString()).getValue(User.class);
if(user.getPassword().equals(editPassword.getText().toString())){
Toast.makeText(getApplicationContext(), "You are logged in!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Log in failed!", Toast.LENGTH_SHORT).show();
}
}
else {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "User does not exists!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
}
What I have tried so far before posting here: Disabling proGuard and putting minifyEnable on false in build.gradle in debug{}; Restarting android studio, as I've read other people's posts who also encountered the problem said that this worked for them; On line where I used tableUser = database.getReference("User"); I also tried using tableUser = database.getReference().child("User");
Any help is much appreciated. Thank you very much
Later edit: adding the LogIn activity xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_screen_image"
tools:context="com.example.vlad.restaurantorder.LogIn">
<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/edtPhone"
android:hint="Phone Number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#android:color/white"
android:text="0293292442"
android:textColor="#android:color/white"
android:textSize="34sp"
android:inputType="phone"
app:met_floatingLabel="highlight"
app:met_baseColor="#android:color/white"
app:met_maxCharacters="11"
app:met_primaryColor="#android:color/white"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/edtPassword"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#android:color/white"
android:text="5678"
android:textColor="#android:color/white"
android:textSize="34sp"
android:inputType="textPassword"
app:met_floatingLabel="highlight"
app:met_baseColor="#android:color/white"
app:met_maxCharacters="11"
app:met_primaryColor="#android:color/white"
app:met_singleLineEllipsis="true"
/>
</LinearLayout>
<info.hoang8f.widget.FButton
android:id="#+id/btnLogInScreenLogIn"
android:text="#string/LogIn"
android:textColor="#android:color/white"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:buttonColor="#color/btnLogIn"
app:shadowColor="#android:color/black"
app:shadowEnabled="true"
app:shadowHeight="5dp"
app:cornerRadius="4dp"
/>
Later edit: LogCat image:
It seems your code should work just fine if there is no other warning/error showing up. The missing instance error/message might only be because it takes some time to read the data from Firebase.
After adding the ValueEventListener, the app will take some time to "listen" to or read the data from Firebase. So, add some log messages to each of the if/else block as below, recompile and fire up the app, open the Logcat view in Android Studio and then click the button btnLogIn on the app screen. Then wait (make sure internet is available to the device/emulator and Firebase Database is connected).
In Logcat window, you should see the Log messages show up depending on the if/else block that was entered in onDataChange. E.g..... D/ANY_TAG: entered onDataChange
Code with debug lines:
tableUser.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("ANY_TAG", "entered onDataChange");
if(dataSnapshot.child(editPhone.getText().toString()).exists()){
Log.d("ANY_TAG", "entered onDataChange/childExists");
progressDialog.dismiss();
User user = dataSnapshot.child(editPhone.getText().toString()).getValue(User.class);
if(user.getPassword().equals(editPassword.getText().toString())){
Log.d("ANY_TAG", "entered onDataChange/childExists/equalPassword");
Toast.makeText(getApplicationContext(), "You are logged in!", Toast.LENGTH_SHORT).show();
}
else{
Log.d("ANY_TAG", "entered onDataChange/childExists/unEqualPasswords");
Toast.makeText(getApplicationContext(), "Log in failed!", Toast.LENGTH_SHORT).show();
}
}
else {
Log.d("ANY_TAG", "entered onDataChange/childDoesNotExist");
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "User does not exists!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

Button Unresponsive

I have two rather basic questions I believe which needs answering:
1) When I run my emulator on the home screen my SignIn button is unresponsive and I am unsure why as I have tried alternative methods but whenever I click nothing happens and no error is showing. Code is shown below:
package com.techblogon.loginexample;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class HomeActivity extends Activity
{
Button btnSignIn,btnSignUp;
LoginDataBaseAdapter loginDataBaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create a instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get The Reference Of Buttons
btnSignIn=(Button)findViewById(R.id.buttonSignIN);
btnSignUp=(Button)findViewById(R.id.buttonSignUP);
// Set OnClick Listener on SignUp button
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Create Intent for SignUpActivity and Start The Activity
Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
startActivity(intentSignUP);
}
});
}
// Method to handleClick Event of Sign In Button
public void signIn(View V)
{
final Dialog dialog = new Dialog(HomeActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIN);
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(HomeActivity.this, "Welcome", Toast.LENGTH_LONG).show();
dialog.dismiss();
Intent ii=new Intent(HomeActivity.this,MainMenu.class);
startActivity(ii);
}
else
{
Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
#Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}
Could somebody provide me with the best sqlite database viewer for eclipse, I am looking to view the records of the database I have created on my emulator
My XML is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/picture" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello, Welcome"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonSignIN"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:text="Sign In" />
<Button
android:id="#+id/buttonSignUP"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:text="Sign Up" />
</LinearLayout>
you are missing an attribute in xml for buttonSignIN button
android:onClick="signIn"
Try this,
<Button
android:id="#+id/buttonSignIN"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:onClick="signIn"
android:text="Sign In" />
You need to understand the difference between setting clickListener in java code and setting a attribute from xml for the button click. These are two different ways in which you could achieve click events for any element.

Add a "Remember me" checkbox

I would like to have a Checkbox button to Remember User Id and Password. Can anyone please guide me in the right direction as to how to get it started?
I just built this into my app, here's the basic code and some explanation:
Basically the key here is SharedPreferences. You'll setup a SharedPreferences object and store your username and password after the user has entered them. Then, when they run the application again, the preferences will have their data stored and will then repopulate the login fields.
LoginActivity.java
package com.realsimpleapps.LoginTesting;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class LoginActivity extends Activity implements OnClickListener {
private String username,password;
private Button ok;
private EditText editTextUsername,editTextPassword;
private CheckBox saveLoginCheckBox;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ok = (Button)findViewById(R.id.buttonLogin);
ok.setOnClickListener(this);
editTextUsername = (EditText)findViewById(R.id.editTextUsername);
editTextPassword = (EditText)findViewById(R.id.editTextPassword);
saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
editTextUsername.setText(loginPreferences.getString("username", ""));
editTextPassword.setText(loginPreferences.getString("password", ""));
saveLoginCheckBox.setChecked(true);
}
}
public void onClick(View view) {
if (view == ok) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextUsername.getWindowToken(), 0);
username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();
if (saveLoginCheckBox.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", username);
loginPrefsEditor.putString("password", password);
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}
doSomethingElse();
}
}
public void doSomethingElse() {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
LoginActivity.this.finish();
}
}
The method at end, doSomethingElse() is your placeholder to go to the next step for your application. My doSomethingElse() method simply loads another activity.
Here's a basic xml file for the login page:
login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:padding="10dp" >
<EditText
android:id="#+id/editTextUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:hint="Username"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext" />
<EditText
android:id="#+id/editTextPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editTextUsername"
android:hint="Password"
android:inputType="textPassword"
android:imeOptions="actionDone" />
<CheckBox
android:id="#+id/saveLoginCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editTextPassword"
android:text="Save Login?"
android:textColor="#FFF" />
<Button
android:id="#+id/buttonLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/saveLoginCheckBox"
android:layout_marginTop="40dp"
android:text="Login" />
</RelativeLayout>
IMPORTANT: You'll likely want to encrypt the password before storing it in SharedPreferences. Details for that are beyond the scope of this question, but here is the code I used to do that: http://www.androidsnippets.com/encryptdecrypt-strings. You'll have to come up with some kind of key schema too.
This code has been tested on Android 2.1, SDK 7. Let me know how it works for you.
David
Seeing this answer i will consider what i want to do with this data. If you want to persist this data in local storage or doing this asyncronously.
loginPrefsEditor.commit();
Persists your data in local storage and block your UI Main Thread.
or
loginPrefsEditor.apply();
In this case makes an asyncronous work and it doesn´t block you main thread.

Android: View crashes when I have two EditText boxes with #+id defined in the xml

Another newbie question here. I'm trying to create a form which takes several text input fields from the user, however my view keeps crashing/failing to load with the following error reported in the log:
Window already focused, ignoring focus gain of:com.android.internal.view.IInputMethodClient$Stub$Proxy#628a9148
This is what my xml looks like (this crashes)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">"
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="top">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter contact details\n\nName:"/>
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:maxLength="30"
android:maxLines="1"
android:hint="#string/compose_name"></EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\nSurname:"/>
<EditText
android:id="#+id/surname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_below="#id/name"
android:maxLength="30"
android:maxLines="1"
android:hint="#string/compose_surname"></EditText>-->
<Button
android:id="#+id/new_contact_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
</ScrollView>
However if I remove just the line:
android:id="#+id/surname"
It works (well at least it loads the view, of course I can't access the content in the EditText field without creating an id for it).
The view also fails if I add #+id tags to both the TextView fields (but works if I add only one).
What's going on here? I thought you should be able to label multiple view fields in your UI (all the examples in my book let me do this)?
I'm using NetBeans to develop on.
EDIT: Adding Javacode:
package org.me.savingsdepositrecord;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class NewContact extends Activity
implements OnClickListener, View.OnKeyListener {
//private EditText nameField;
//private String name;
private Button submitButton;
EditText nameField = null;
EditText surnameField = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_contact);
nameField = (EditText) findViewById(R.id.name);
nameField.setOnKeyListener(this);
surnameField = (EditText) findViewById(R.id.surname);
submitButton = (Button) findViewById(R.id.new_contact_button);
// Set up click listeners for all the buttons
submitButton.setOnClickListener(this);
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String name = nameField.getText().toString();
String surname = surnameField.getText().toString();
// TODO: Save Nickname setting (strNicknameToSave)
return true;
}
return false;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.new_contact_button:
Intent i = new Intent(this, MainActivity.class);
finish();
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
Yes, you can label multiple fields. It might be a good to take out the android:layout_below attribute since that isn't supported in LinearLayout.
Otherwise, your Java code might be the culprit. Can you post it as well?
Edit: I also tested your code and found no errors, although I had to remove the '-->' and the #string references.

Categories

Resources