giving error in validation in android studio - android

While running this code error is coming in the logact section.I guess its minor error so its difficult for me to search the error.I tried various things but it didn't worked for me.I don't know where my code went wrong.
this is my NewUserActivity .java code
public class NewUserActivity extends AppCompatActivity {
EditText name;
EditText email;
EditText phone;
EditText usname;
EditText passsword;
Button register;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_user_login);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
usname = (EditText) findViewById(R.id.usname);
passsword = (EditText) findViewById(R.id.passsword);
register= (Button) findViewById(R.id.textView2);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String NAME = name.getText().toString().trim();
String EMAIL = email.getText().toString().trim();
String PHONENO =phone.getText().toString().trim();
String username = usname.getText().toString().trim();
String password = passsword.getText().toString().trim();
if (NAME.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter name",Toast.LENGTH_SHORT).show();
else if(EMAIL.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter correct email address",Toast.LENGTH_SHORT).show();
else if(PHONENO.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter correct phone number",Toast.LENGTH_SHORT).show();
else if(username.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter Username",Toast.LENGTH_SHORT).show();
else if(username.length()<8|| username.length()>15)
Toast.makeText(getApplicationContext(),"Username must contain atleast 8 characters",Toast.LENGTH_SHORT).show();
else if(password.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter Password",Toast.LENGTH_SHORT).show();
else if(password.length()<6|| password.length()>10)
Toast.makeText(getApplicationContext(),"Password must contain 6 characters",Toast.LENGTH_SHORT).show();
else {
}
}
});
}
}
this is my xnl file for the same.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
tools:context=".NewUserActivity"
android:background="#drawable/ic_launcher_background"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/textView4"
android:layout_width="276dp"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="106dp"
android:layout_marginBottom="581dp"
android:text="REGISTRATION "
android:textColor="#color/WhiteSmoke"
android:textSize="30dp" />
<EditText
android:layout_height="26dp"
android:layout_width="297dp"
android:layout_marginBottom="487dp"
android:layout_marginEnd="65dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/name"/>
<TextView
android:id="#+id/textView5"
android:layout_width="218dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="135dp"
android:layout_marginBottom="530dp"
android:drawableLeft="#drawable/ic_action_name"
android:text="NAME"
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="195dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="161dp"
android:layout_marginBottom="439dp"
android:drawableLeft="#drawable/ic_action_mail"
android:text="EMAIL ID"
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:layout_height="26dp"
android:layout_width="297dp"
android:layout_marginBottom="399dp"
android:layout_marginEnd="67dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/email"/>
<TextView
android:id="#+id/textView7"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="209dp"
android:layout_marginBottom="351dp"
android:drawableLeft="#drawable/ic_action_phone"
android:text="PHONE NO."
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:layout_width="297dp"
android:layout_height="26dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="63dp"
android:layout_marginBottom="306dp"
android:id="#+id/phone"/>
<TextView
android:layout_height="30dp"
android:layout_width="195dp"
android:textSize="20dp"
android:textColor="#color/WhiteSmoke"
android:text="USERNAME"
android:layout_marginBottom="256dp"
android:layout_marginEnd="160dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/textView8"
android:drawableLeft="#drawable/ic_action_username"/>
<EditText
android:layout_height="26dp"
android:layout_width="297dp"
android:layout_marginBottom="205dp"
android:layout_marginEnd="63dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/usname"/>
<TextView android:layout_height="30dp" android:layout_width="195dp"
android:textSize="20dp" android:textColor="#color/WhiteSmoke"
android:text="PASSWORD" android:layout_marginBottom="160dp"
android:layout_marginEnd="156dp" android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" android:id="#+id/textView9"
android:drawableLeft="#drawable/ic_action_password"/>
<EditText
android:layout_width="297dp"
android:layout_height="26dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="62dp"
android:layout_marginBottom="113dp"
android:id="#+id/passsword"/>
<androidx.cardview.widget.CardView
android:layout_height="56dp" android:layout_width="348dp"
android:layout_marginBottom="40dp" android:layout_marginEnd="20dp"
android:layout_alignParentBottom="true" android:layout_alignParentEnd="true"
app:cardElevation="10dp" app:cardCornerRadius="30dp"
app:cardBackgroundColor="#color/DeepPink" android:layout_marginRight="28dp"
android:layout_marginStart="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true">
<RelativeLayout android:layout_height="73dp" android:layout_width="373dp"
android:layout_marginBottom="100dp" android:layout_marginEnd="50dp">
<Button
android:id="#+id/register"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="129dp"
android:layout_marginBottom="17dp"
android:fontFamily="sans-serif"
android:freezesText="true"
android:text="REGISTER"
android:background="#android:color/transparent"
android:textColor="#color/Black"
android:textSize="24dp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
There is error in this section of my code.
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String NAME = name.getText().toString().trim();
String EMAIL = email.getText().toString().trim();
String PHONENO =phone.getText().toString().trim();
String username = usname.getText().toString().trim();
String password = passsword.getText().toString().trim();

You are using Wrong id of button which will led to the null pointer exception
Change this
register= (Button) findViewById(R.id.textView2);
to
register= (Button) findViewById(R.id.register);
To know about null pointer exception check this
What is a NullPointerException, and how do I fix it?
You can use View binding to replaces all findviewbyId
https://developer.android.com/topic/libraries/view-binding

name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
usname = (EditText) findViewById(R.id.usname);
passsword = (EditText) findViewById(R.id.passsword);
register= (Button) findViewById(R.id.textView2);
here check the register button id you add textView2 id here your mistake

Related

page is not getting intended when button is clicked

So basically when I will click the register button it should direct me to the next page that is the three_section page.I too wrote the code for this also I have actually added validations to the page,But the button is getting clicked but it is not taking me next page.I mean next page is not getting opened.There is no error in the logcat section coming .It means my code is running ..But the page is not getting intended i.e. there is somewhere some bug.I tried solving but I am mot gettting it.
this is my new_user_register.java code
public class new_user_register extends AppCompatActivity {
EditText name1;
EditText email1;
EditText usname1;
EditText password1;
Button register;
ToastManager toastManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_user_register);
name1 = (EditText) findViewById(R.id.name1);
email1 = (EditText) findViewById(R.id.email1);
usname1 = (EditText) findViewById(R.id.usname1);
password1 = (EditText) findViewById(R.id.password1);
register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String NAME = name1.getText().toString().trim();
String EMAIL = email1.getText().toString().trim();
String username = usname1.getText().toString().trim();
String password = password1.getText().toString().trim();
String emailPattern = "^[a-zA-Z0-9+_.-]{3,32}+#[a-zA-Z0-9.-]{2,32}+$";
boolean isAtLeastOneFieldNotEmpty = !NAME.isEmpty()
|| !EMAIL.isEmpty()
|| !username.isEmpty()
|| !password.isEmpty();
toastManager = new ToastManager(new_user_register.this);
// You have 5 registration fields that users must fill up.
final int totalRegistrationField = 5;
// This will increase by 1 when a field is valid
int validRegistrationFieldCount = 0;
if (isAtLeastOneFieldNotEmpty) {
// NAME VALIDATION
if (NAME.isEmpty()) {
toastManager.addToast("ENTER NAME", ToastManager.Duration.LENGTH_SHORT);
} else if (!((NAME.length() > 3) && (NAME.length() < 15))) {
toastManager.addToast("NAME IS TOO SHORT.IT MUST BE BETWEEN 3-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
} else if (!NAME.matches("[a-zA-Z ]+")) {
toastManager.addToast("ONLY ALPHABETS ALLOWED", ToastManager.Duration.LENGTH_SHORT);
} else {
++validRegistrationFieldCount;
}
// EMAIL VALIDATION
if (EMAIL.isEmpty()) {
toastManager.addToast("ENTER EMAIL-ID", ToastManager.Duration.LENGTH_SHORT);
} else if (!(EMAIL.matches(emailPattern))) {
toastManager.addToast("INVALID EMAIL", ToastManager.Duration.LENGTH_SHORT);
} else {
++validRegistrationFieldCount;
}
// USERNAME VALIDATION
if (username.isEmpty()) {
toastManager.addToast("ENTER USERNAME", ToastManager.Duration.LENGTH_SHORT);
} else if (!((username.length() > 6) && (username.length() < 15))) {
toastManager.addToast("USERNAME IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
} else {
++validRegistrationFieldCount;
}
// PASSWORD VALIDATION
if (password.isEmpty()) {
toastManager.addToast("ENTER PASSWORD", ToastManager.Duration.LENGTH_SHORT);
} else if (!((password.length() > 6) && (password.length() < 15))) {
toastManager.addToast("PASSWORD IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
} else {
++validRegistrationFieldCount;
}
} else {
toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
}
if (validRegistrationFieldCount != totalRegistrationField) {
// Finally show all toast all screen
toastManager.show();
} else {
// All registration fields are valid, then go to another screen.
Intent p = new Intent(new_user_register.this, three_sections.class);
startActivity(p);
}
}
});
}
}
this is my new_user_register.xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".loginpage"
android:background="#drawable/ic_launcher_background">
<TextView
android:layout_width="216dp"
android:layout_height="68dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginEnd="128dp"
android:layout_marginBottom="561dp"
android:text="NEW USER REGISTRATION"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold|italic"
android:typeface="sans" />
<TextView
android:id="#+id/textView"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="207dp"
android:layout_marginBottom="277dp"
android:drawableLeft="#drawable/username_foreground"
android:text="USERNAME"
android:textColor="#color/Purple"
android:textSize="20dp" />
<TextView
android:id="#+id/name"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="256dp"
android:layout_marginBottom="501dp"
android:drawableLeft="#drawable/username_foreground"
android:text="NAME"
android:textColor="#color/Purple"
android:textSize="20dp" />
<EditText
android:id="#+id/name1"
android:layout_width="291dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="80dp"
android:layout_marginBottom="434dp"
android:hint="ENTER NAME" />
<TextView
android:id="#+id/email"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="229dp"
android:layout_marginBottom="386dp"
android:drawableLeft="#drawable/password_foreground"
android:text="EMAIL ID"
android:textColor="#color/Purple"
android:textSize="20dp" />
<EditText
android:id="#+id/email1"
android:layout_width="289dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="87dp"
android:layout_marginBottom="326dp"
android:hint="ENTER EMAIL ID" />
<EditText
android:id="#+id/usname1"
android:layout_width="292dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="85dp"
android:layout_marginBottom="220dp"
android:hint="ENTER USERNAME" />
<TextView
android:id="#+id/t3"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="184dp"
android:layout_marginBottom="170dp"
android:drawableLeft="#drawable/password_foreground"
android:text="PASSWORD"
android:textColor="#color/Purple"
android:textSize="20dp" />
<EditText
android:id="#+id/password1"
android:layout_width="286dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="88dp"
android:layout_marginBottom="113dp"
android:hint="ENTER PASSWORD" />
<ImageView
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-15dp"
android:layout_marginBottom="525dp"
android:src="#drawable/imaagee" />
<ImageView
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-15dp"
android:layout_marginBottom="408dp"
android:src="#drawable/image" />
<ImageView
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-15dp"
android:layout_marginBottom="292dp"
android:src="#drawable/iimagee" />
<ImageView
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-15dp"
android:layout_marginBottom="176dp"
android:src="#drawable/imagee" />
<ImageView
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-15dp"
android:layout_marginBottom="61dp"
android:src="#drawable/imageee" />
<Button
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="210dp"
android:layout_marginBottom="43dp"
android:background="#android:color/transparent"
android:text="REGISTER"
android:textColor="#color/Purple"
android:textSize="25dp" />
</RelativeLayout>
this is my three_section xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".three_sections">
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="429dp"
android:alpha="0.4"
android:background="#color/Purple" />
<TextView
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-14dp"
android:layout_marginBottom="472dp"
android:freezesText="true"
android:text=" BEGINNERS CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="238dp"
android:alpha="0.4"
android:background="#color/DeepPink" />
<TextView
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-27dp"
android:layout_marginBottom="90dp"
android:text=" ADVANCED CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="47dp"
android:alpha="0.4"
android:background="#color/DarkBlue" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="0dp"
android:layout_marginBottom="315dp"
android:freezesText="true"
android:text=" INTERMEDIATE CALLIGRAPHY"
android:textSize="25dp" />
</RelativeLayout>
Your code is never go inside the intent block since your totalRegistrationField and validRegistrationFieldCount never match.
You've assigned value 5 to `totalRegistrationField, and you just increment the validRegistrationFieldCount 4 times.
All what you just need is change totalRegistrationField value to 4.
final int totalRegistrationField = 4;

Variable pass is already defined in method onclickregister(View)

I am trying to run a code for matching passwords. I do not understand the error message:
variable pass is already defined in method onclickregister(View)
CODE:
package com.example.admin.rent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Register extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void onclickregister(View view) {
if(view.getId()==R.id.register)
{
EditText name = (EditText)findViewById(R.id.name);
EditText sname = (EditText)findViewById(R.id.surname);
EditText email = (EditText)findViewById(R.id.email);
EditText mnum = (EditText)findViewById(R.id.mobileno);
EditText pass = (EditText)findViewById(R.id.pass);
EditText pass1 = (EditText)findViewById(R.id.pass1);
String namestr = name.getText().toString();
String snamestr = sname.getText().toString();
String emailstr = email.getText().toString();
String mnumstr = mnum.getText().toString();
String passstr = pass.getText().toString();
String pass1str = pass1.getText().toString();
if(!passstr.equals(pass1str))
{
//popup msg
Toast pass = Toast.makeText(Register.this , "Passwords don't match!", Toast.LENGTH_SHORT);
pass.show();
}
}
XML:
<?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="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.admin.rent.Register">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:hint="#string/name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="#string/surname"
android:ems="10"
android:id="#+id/surname"
android:layout_below="#+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/email"
android:layout_below="#+id/mobileno"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="#string/email"
android:layout_marginBottom="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass"
android:layout_below="#+id/email"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/password"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass1"
android:hint="#string/confirm_password"
android:layout_below="#+id/pass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="+91"
android:id="#+id/textView2"
android:layout_below="#+id/surname"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center|clip_vertical"
android:textSize="18dp"
android:layout_alignBottom="#+id/mobileno" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/mobileno"
android:layout_below="#+id/surname"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_alignParentEnd="true"
android:hint="#string/mobile_number"
android:layout_toRightOf="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginLeft="10dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/register2"
android:id="#+id/register"
android:layout_below="#+id/pass1"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:layout_marginTop="25dp"
android:textSize="25sp"
android:typeface="normal"
android:clickable="false"
android:width="170dp"
android:onClick="onclickregister" />
Rename the pass variable in that inner scope:
if(!passstr.equals(pass1str))
{
//popup msg
Toast pass = Toast.makeText(Register.this , "Passwords don't match!", Toast.LENGTH_SHORT);
pass.show();
}
to something else. The problem is that you already have a variable with that name defined in the outer scope:
EditText pass = (EditText)findViewById(R.id.pass);

How do I send an email with filled subject and email address?

I wrote an activity to send an email in the app. However, i can't find a way to set the recipient and subject in the edittext fill. How should I set the email at the beginning ??
my activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback_email);
emailAddress = (EditText) findViewById(R.id.email);
emailSubject = (EditText) findViewById(R.id.subject);
message = (EditText) findViewById(R.id.message);
sendEmailButton = (Button) findViewById(R.id.send_button);
sendEmailButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String toemailAddress = emailAddress.getText().toString();
String msubject = emailSubject.getText().toString();
String mmessage = message.getText().toString();
Intent emailApp = new Intent(Intent.ACTION_SEND);
emailApp.putExtra(Intent.EXTRA_EMAIL, new String[]{toemailAddress});
emailApp.putExtra(Intent.EXTRA_SUBJECT, msubject);
emailApp.putExtra(Intent.EXTRA_TEXT, mmessage);
emailApp.setType("message/rfc822");
startActivity(Intent.createChooser(emailApp, "Send Email Via"));
}
});
}
my xml file
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject"
android:textStyle="bold" />
<EditText
android:id="#+id/subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:hint="Subject"
android:inputType="text" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginTop="43dp"
android:text="To"
android:textStyle="bold" />
<EditText
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView2"
android:layout_toEndOf="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:hint="Email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/email"
android:layout_marginTop="15dp"
android:text="Message:"
android:textStyle="bold" />
<EditText
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:gravity="top"
android:hint="Your message here..."
android:inputType="textMultiLine"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/email" />
<Button
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#498b24"
android:paddingTop="50dp"
android:padding="5dp"
android:text="Send Message "
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46dp" />
How should I set the email at the beginning ??
I am not sure if this is what you want but you can set it in the xml as android:text="example#gmail.com" For example:
<EditText
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView2"
android:layout_toEndOf="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:text="example#gmail.com"
android:inputType="textEmailAddress" />

findViewByID in Android Studio not working with EditText

I can't seem to get findViewById() working with the EditText. It works fine with the button, but I think I am doing something wrong and I can't seem to find a solution anywhere. How does findViewById() work with EditText ?
Here is my class:
public class MainActivity extends ActionBarActivity {
Button sbutton;
EditText book, chapter, verse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
}
}
And here is my XML:
<TextView android:text="Please enter your favorite scripture" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/book1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/chapter1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/verse1"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/sbutton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book"
android:id="#+id/textView2"
android:layout_alignTop="#+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter"
android:id="#+id/textView3"
android:layout_below="#+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verse"
android:id="#+id/textView4"
android:layout_below="#+id/chapter"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
I know this might not be the nicest code because I'm brand new to Android Studio. I would appreciate a little help understanding how this method works and what I might be doing wrong here. Thanks!
You are doing it as "findViewByID()" but its findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
Correct method name is
findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewById(R.id.book1);
chapter = (EditText) findViewById(R.id.chapter1);
verse = (EditText) findViewById(R.id.verse1);

EditText prints a letter twice

I know this is kinda silly and weird question, but its really bugging me. As my post subject says, when I enter a letter in the edit text its printing twice. I tried to find the issue, but couldn't. Here is my code,
changecode.xml
<EditText
android:id="#+id/oldcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/changecodetxt"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:hint="Enter Old Pass Code"
android:inputType="number"
android:password="true" />
<EditText
android:id="#+id/newcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/oldcode"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:hint="Enter New Pass code"
android:inputType="number"
android:password="true" />
<EditText
android:id="#+id/renewcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/newcode"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:hint="Re-Enter New Pass code"
android:inputType="number"
android:password="true" />
<Button
android:id="#+id/savenewcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/button_style"
android:text="Update Code" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/savenewcode"
android:layout_below="#id/renewcode"
android:gravity="center" >
<ToggleButton
android:id="#+id/cc_togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textOff="123"
android:textOn="ABC!##" />
</LinearLayout>
In MainActivity.java
oldcode = (EditText) findViewById(R.id.oldcode);
newcode = (EditText) findViewById(R.id.newcode);
renewcode = (EditText) findViewById(R.id.renewcode);
savenewcode = (Button) findViewById(R.id.savenewcode);
cc_toggle = (ToggleButton) findViewById(R.id.cc_togglebutton);
cc_toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
boolean on = cc_toggle.isChecked();
if (on) {
oldcode.setInputType(InputType.TYPE_CLASS_TEXT);
newcode.setInputType(InputType.TYPE_CLASS_TEXT);
renewcode.setInputType(InputType.TYPE_CLASS_TEXT);
}
else {
oldcode.setInputType(InputType.TYPE_CLASS_NUMBER);
newcode.setInputType(InputType.TYPE_CLASS_NUMBER);
renewcode.setInputType(InputType.TYPE_CLASS_NUMBER);
}
}
});
Any kind of help or suggestion is much appreciated.

Categories

Resources