I'm new to the committee and I have problems with my code because I'm using TextInputLayout and I want my Password and Confirm Password must validate but whatever I do it still giving me problems to fix it.
Here is my activity_register.xml
<LinearLayout 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:orientation="vertical"
android:padding="16dp"
tools:context=".Register">
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_RegPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:errorEnabled="true"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_RegCfmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/text_input_RegPassword"
app:errorEnabled="true"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="101dp"
android:text="Register"
android:onClick="Reg"/>
</LinearLayout>
and this is my Register.java because I have tried to use equals but it won't work because when I want to type .getText is just invalid
public class Register extends AppCompatActivity {
private TextInputLayout textInputRegPassword;
private TextInputLayout textInputRegCfmPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_register);
textInputRegPassword = findViewById(R.id.text_input_RegPassword);
textInputRegCfmPassword = findViewById(R.id.text_input_RegCfmPassword);
}
private boolean RegisterPassword(){
String userReg = textInputRegPassword.getEditText().getText().toString().trim();
if(userReg.isEmpty()){
textInputRegPassword.setError("Enter Password");
return false;
} else {
textInputRegPassword.setError(null);
return true;
}
}
private boolean RegisterCfmPassword(){
String userReg = textInputRegCfmPassword.getEditText().getText().toString().trim();
if(userReg.isEmpty()){
textInputRegCfmPassword.setError("Enter Password");
return false;
} else {
textInputRegCfmPassword.setError(null);
return true;
}
}
public void Reg(View v){
if(!RegisterPassword() | !RegisterCfmPassword() ){
return;
}
}
}
Edited: I want my Password and Confirm Password Validate that both of them are the same and when I press the button, it just crash
private boolean Verify(){
if(CfmPassword.getText().toString().equals(Password.getText().toString())){
return true;
} else{
textInputRegCfmPassword.setError("Password Do Not Match");
return false;
}
}
public void Reg(View v){
if(!RegisterPassword() | !RegisterCfmPassword() | !Verify() ){
return;
}
}
Try this code
Register Class code
public class Register extends AppCompatActivity {
private TextInputLayout textInputRegPassword;
private TextInputLayout textInputRegCfmPassword;
private TextInputEditText inputRegCfmPassword;
private TextInputEditText inputRegPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_register);
textInputRegPassword = (TextInputLayout)findViewById(R.id.text_input_RegPassword);
textInputRegCfmPassword =(TextInputLayout) findViewById(R.id.text_input_RegCfmPassword);
inputRegCfmPassword =(TextInputEditText) findViewById(R.id.input_RegCfmPassword);
inputRegPassword =(TextInputEditText) findViewById(R.id.input_RegPassword);
}
private boolean RegisterPassword(){
String userReg = inputRegPassword.getText().toString().trim();
if(userReg.isEmpty()){
textInputRegPassword.setError("Enter Password");
return false;
} else {
textInputRegPassword.setError(null);
return true;
}
}
private boolean RegisterCfmPassword(){
String userReg = inputRegCfmPassword.getText().toString().trim();
if(userReg.isEmpty()){
textInputRegCfmPassword.setError("Enter Password");
return false;
} else {
textInputRegCfmPassword.setError(null);
return true;
}
}
private boolean Verify(){
if(inputRegPassword.getText().toString().equals(inputRegCfmPassword.getText().toString())){
return true;
} else{
textInputRegCfmPassword.setError("Password Do Not Match");
return false;
}
}
public void Reg(View v){
if(!RegisterPassword() || !RegisterCfmPassword() || !Verify() ){
return;
}
}
}
And xml code is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".Register">
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_RegPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:errorEnabled="true"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/input_RegPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_RegCfmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/text_input_RegPassword"
app:errorEnabled="true"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/input_RegCfmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="101dp"
android:onClick="Reg"
android:text="Register" />
</LinearLayout>
Hope this will be help you!
TextInputLayout is just a design layout. You should get user input from its child TextInputEditText.
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_RegPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:errorEnabled="true"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/text_input_edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
then in your activity define TextInputEditText:
TextInputEditText password = findViewById(R.id.text_input_edit_password);
and finally get user input:
password.getText().toString()
Related
so I am trying to create an activity, and then add onclick listeners to it, but it wont let me refer. So the moment I open this activity in my app, my app crashes, saying 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference'
I have no idea why this is happening. I have correctly names them in accordance with the xml file as well.
Please help.
public class SubtaskActivity extends AppCompatActivity {
EditText etSubtaskName;
Button btnDone;
RadioGroup radgrpPri, radgrpTime;
RadioButton radbtnPriHigh, radbtnPriMed, radbtnPriLow, radbtnTimeMore, radbtnTimeMed, radbtnTimeLess;
boolean priHigh, priMed, priLow, timeMore, timeMed, timeLess;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnDone = findViewById(R.id.btnDone);
radgrpPri = findViewById(R.id.radgrpPri);
radgrpTime = findViewById(R.id.radgrpTime);
radbtnPriHigh = findViewById(R.id.radbtnPriHigh);
radbtnPriMed = findViewById(R.id.radbtnPriMed);
radbtnPriLow = findViewById(R.id.radbtnPriLow);
radbtnTimeMore = findViewById(R.id.radbtnTimeMore);
radbtnTimeMed = findViewById(R.id.radbtnTimeMed);
radbtnTimeLess = findViewById(R.id.radbtnTimeLess);
etSubtaskName = findViewById(R.id.etSubtaskName);
radgrpPri.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radbtnPriHigh.isChecked())
{
priHigh = true;
priLow = false;
priMed = false;
}
else if (radbtnPriMed.isChecked())
{
priHigh = false;
priLow = false;
priMed = true;
}
else if (radbtnPriLow.isChecked())
{
priHigh = false;
priLow = true;
priMed = false;
}
}
});
radgrpTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radbtnTimeMore.isChecked())
{
timeMore = true;
timeMed = false;
timeLess = false;
}
else if (radbtnTimeMed.isChecked())
{
timeMore = false;
timeMed = true;
timeLess = false;
}
else if (radbtnTimeLess.isChecked())
{
timeMore = false;
timeMed = false;
timeLess = true;
}
}
});
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = etSubtaskName.getText().toString().trim();
Intent intent = new Intent(SubtaskActivity.this, TaskInfo.class);
intent.putExtra("subtaskName", name);
intent.putExtra("priHigh", priHigh);
intent.putExtra("priMed", priMed);
intent.putExtra("priLow", priLow);
intent.putExtra("timeMore", timeMore);
intent.putExtra("timeMed", timeMed);
intent.putExtra("timeLess", timeLess);
startActivity(intent);
}
});
}
}
XML File :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/it"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:background="#color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:orientation="vertical">
<TextView
android:id="#+id/tvSubtaskPriorityHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="#font/roboto"
android:text="#string/priority_of_subtask"
android:textColor="#B8AEAE"
android:textSize="16sp" />
<RadioGroup
android:id="#+id/radgrpPri"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radbtnPriHigh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:buttonTint="#color/red"
android:text="#string/high"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnPriMed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/yellow"
android:text="#string/medium"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnPriLow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/green"
android:text="#string/low"
android:textColor="#color/white" />
</RadioGroup>
<TextView
android:id="#+id/tvTimeWeightHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="#font/roboto"
android:text="#string/time_this_subtask_may_consume"
android:textColor="#B8AEAE"
android:textSize="16sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/floating_hint_time_minutes"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:hintTextAppearance="#style/FlotatingHintStyle">
<EditText
android:id="#+id/etSubtaskName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:ems="10"
android:fontFamily="#font/roboto"
android:hint="#string/name_your_subtask"
android:inputType="textPersonName"
android:maxLength="20"
android:textColor="#color/white"
android:textColorHint="#B8AEAE"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<RadioGroup
android:id="#+id/radgrpTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radbtnTimeMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:buttonTint="#color/red"
android:text="#string/more"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnTimeMed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/yellow"
android:text="#string/medium"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnTimeLess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/green"
android:text="#string/less"
android:textColor="#color/white" />
</RadioGroup>
</LinearLayout>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:gravity="center_horizontal"
android:text="#string/done"
app:backgroundTint="#color/orange_accent" />
</LinearLayout>
You didn't call in onCreate method setContentView(R.layout.youractivity). If you didn't, Android doesn't know what to render, so there are no views for you to provide.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivity);
}
just set your (xml)layout file to setContentView in onCreate()
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I have some issues creating a login activity.
Even though I put the right username (123456) and password (123456789).
I made sure to put the getText() method inside onLogin but I always get the message "login failed" and so I assume I don't get any data from EditText, and so I can't see what's wrong.
this login java class:
public class LoginActivity extends AppCompatActivity {
private EditText UsernameEt;
private EditText PasswordEt;
AlertDialog alertDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
UsernameEt=findViewById(R.id.username);
PasswordEt=findViewById(R.id.password);
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Login Status");
}
public void onLogin(View view) {
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
//BackgroundWorker backgroundWorker = new BackgroundWorker(this);
//backgroundWorker.execute(type, username, password);
if(username=="123456" && password=="123456789"){
alertDialog.setMessage("Login success");
alertDialog.show();
} else {
alertDialog.setMessage("Login failed");
alertDialog.show();
}
}
}
and this is the login xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/bg"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:drawableLeft="#drawable/ic_person_black_24dp"
android:ems="10"
android:hint="IPN"
android:inputType="textPersonName"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:drawableLeft="#drawable/ic_lock_black_24dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF" />
<Button
android:id="#+id/buttonLogin"
style="#style/DefaultButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginTop="16dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:background="#drawable/gradiant"
android:onClick="onLogin"
android:text="#string/login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout>
</ScrollView>
if(username.equals("123456") && password.equals("123456789"){
// login true
}
try this
username.getText().toString().contentEquals("123456") && password.getText().toString().contentEquals("123456789")
I'm using the umano SlidingUpPanel library and I used an imagebutton in the panel but nothing happens when I click the imagebutton, I tried the imagebutton in another activity which worked perfectly. "next" is supposed to be printed to the logs when the button is pressed. I've searched all over but can't find any solution
Here's my code:
#SuppressLint("Registered")
public class NowPlaying extends Activity {
ImageButton play, pause, play_main, pause_main, next, imgbtn;
private MusicService musicSrv;
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.now_playing);
play = findViewById(R.id.play_button);
imgbtn = findViewById(R.id.imgbtn);
imgbtn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ShowToast")
#Override
public void onClick(View v) {
Log.i("oladapos", "next");
}
});
SlidingUpPanelLayout now_playing =
findViewById(R.id.slideup_nowplaying);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playNext();
}
});
}
//play next
private void playNext(){
musicSrv.playNext();
}
//play previous
private void playPrev(){
musicSrv.playPrev();
}
#Override
public void start() {
}
#Override
public void pause() {
}
#Override
public int getDuration() {
return 0;
}
#Override
public int getCurrentPosition() {
return 0;
}
#Override
public void seekTo(int pos) {
}
#Override
public boolean isPlaying() {
return false;
}
#Override
public int getBufferPercentage() {
return 0;
}
#Override
public boolean canPause() {
return false;
}
#Override
public boolean canSeekBackward() {
return false;
}
#Override
public boolean canSeekForward() {
return false;
}
#Override
public int getAudioSessionId() {
return 0;
}
}
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/slideup_nowplaying"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".Fragments.songsFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/songs_cover"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:clickable="true"
android:orientation="horizontal"
android:focusable="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/songs_cover_one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:src="#drawable/songs_cover"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="202dp"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/linearLayout3"
app:layout_constraintStart_toEndOf="#+id/songs_cover_one"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/songs_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:lines="1"
android:text="#string/havana_camila_cabello_song"
android:textColor="#FFFFFF"
android:textSize="22sp" />
<TextView
android:id="#+id/songs_artist_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:text="#string/camila_cabello" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.974"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="horizontal">
<Button
android:id="#+id/play_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:focusable="true"
android:scaleType="centerInside"
android:src="#drawable/round_play_arrow_black_48dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/forword_button"
android:id="#+id/imgbtn"/>
</RelativeLayout>
</RelativeLayout>
Always getting this warning TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead. and not finishing Activity for First time. On Second time not getting an warning and activity finishing perfectly.
activity_login.xml
<LinearLayout
android:id="#+id/ll_login_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:visibility="visible">
<android.support.design.widget.TextInputLayout
android:id="#+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:hint="#string/email_phone"
android:paddingTop="48dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/password"
android:paddingBottom="16dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textPassword"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/button_login_social_margin"
android:layout_marginStart="#dimen/button_login_social_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="#string/login"
android:textColor="#android:color/black" />
</LinearLayout>
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final AppCompatEditText etEmailOrPh = (AppCompatEditText) findViewById(R.id.et_email);
final AppCompatEditText etPassword = (AppCompatEditText) findViewById(R.id.et_password);
final Button btnLogin = (Button) findViewById(R.id.btn_login);
assert etEmailOrPh != null;
assert etPassword != null;
assert btnLogin != null;
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String emailOrPhone = etEmailOrPh.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if(emailOrPhone.isEmpty()){
etEmailOrPh.setError(getResources().getString(R.string.email_phone_mandatory));
etEmailOrPh.requestFocus();
} else if(emailOrPhone.contains("#") && CommonUtil.isValidEmail(emailOrPhone)) {
etEmailOrPh.setError(getResources().getString(R.string.email_error));
etEmailOrPh.requestFocus();
} else if(password.isEmpty()) {
etPassword.setError(getResources().getString(R.string.password_mandatory));
etPassword.requestFocus();
} else {
SharedPreferences cache = LoginActivity.this.getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
final SharedPreferences.Editor preferenceEditor = cache.edit();
preferenceEditor.putInt(Constants.SHARED_PREF_ITEM_USER_ID, 1);
preferenceEditor.apply();
setResult(RESULT_OK);
finish();
}
}
});
}
}
Change this EditText
android.support.v7.widget.AppCompatEditText
to this
android.support.design.widget.TextInputEditText
Full Code :
<android.support.design.widget.TextInputLayout
android:id="#+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:hint="#string/email_phone"
android:paddingTop="48dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp"
/>
</android.support.design.widget.TextInputLayout>
Try not using
<android.support.v7.widget.AppCompatEditText
instead use
<EditText
So you will get:
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
After doing a bit of research on the issue i found this post:
EditText added is not a TextInputEditText. Please switch to using that class instead
let me know if it helps you.
I am getting a run time error "RemindMe is unfortunately stopped" on AddReminder activity when I passed a username from logging activity to Menu then attempt to add reminder activity, it unfortunately stops. Also logcat is not showing me anything (its empty).
Loging.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="match_parent"
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.champ.remindme2.Login"
android:background="#drawable/back">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/remind_me_logo"
android:contentDescription="Logo" />
<EditText
android:id="#+id/edtitem"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="#drawable/username_rounded_edited_text"
android:inputType="text"
android:hint="Username"
android:textAlignment="center"
android:layout_marginTop="37dp"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/password"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="#drawable/pass_rounded_edited_text"
android:inputType="text"
android:hint="Password"
android:textAlignment="center"
android:layout_below="#+id/edtitem"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/LoginButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
style="#style/ButtonText"
android:onClick="Login"
android:layout_below="#+id/password"
android:layout_alignLeft="#+id/password"
android:layout_alignStart="#+id/password"
android:layout_alignRight="#+id/password"
android:layout_alignEnd="#+id/password" />
<Button
android:id="#+id/txtSignup"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
style="#style/ButtonText"
android:onClick="Signup"
android:layout_below="#+id/LoginButton"
android:layout_alignLeft="#+id/LoginButton"
android:layout_alignStart="#+id/LoginButton" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/fb_loging"
android:layout_alignBottom="#+id/txtSignup"
android:layout_toRightOf="#+id/txtSignup"
android:layout_toEndOf="#+id/txtSignup" />
</RelativeLayout>
Loging.java
public class Login extends AppCompatActivity {
EditText ed1Username, ed2Pass;
String Username;
int counter=3;
Button LoginButton;
// TextView txtAttempts;
//private final int interval = 3000;
/*private Runnable runnable = new Runnable(){
public void run() {
LoginButton.setEnabled(true);
}
}; */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton= (Button)findViewById(R.id.LoginButton);
ed1Username=(EditText)findViewById(R.id.edtitem);
Username=ed1Username.getText().toString();
ed2Pass=(EditText)findViewById(R.id.password);
}
public void Login(View v){
if (ed1Username.getText().toString().equals("Admin") && ed2Pass.getText().toString().equals("123")){
Toast.makeText(Login.this, "Login Successful", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, Menu.class);
intent.putExtra("Username",Username);
startActivity(intent);
}
else {
Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_LONG).show();
counter--;
}
// txtAttempts.setText("Attempts Left: " + counter);
if (counter == 0) {
LoginButton.setEnabled(false);
// new Timer().schedule((TimerTask) runnable,interval);
}
}
public void Signup(View v) {
Intent intent = new Intent(this, signup.class);
startActivity(intent);
}
}
Menu.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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/back"
tools:context="com.example.champ.remindme2.Menu">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#drawable/remind_me_logo"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Reminder"
android:id="#+id/button"
android:onClick="addReminder"
android:layout_below="#+id/imageView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="49dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View List"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Shop"
android:id="#+id/button3"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View profile"
android:id="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
Menu.java
public class Menu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
public void addReminder(View v) {
Intent intent = new Intent(this, AddReminder.class);
String Username = intent.getExtras().getString("Username");
intent.putExtra("Username", Username);
startActivity(intent);
}
}
AddReminder.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="match_parent"
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.champ.remindme2.AddReminder"
android:background="#drawable/back">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:src="#drawable/remind_me_logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
<EditText
android:id="#+id/edtitem"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/rounded_edited_text"
android:inputType="text"
android:textAlignment="center"
android:layout_alignBottom="#+id/plusButton"
android:layout_alignLeft="#+id/imageView6"
android:text="Add Item"
android:layout_alignTop="#+id/plusButton" />
<Button
android:id="#+id/plusButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
style="#style/ButtonText"
android:onClick="plus"
android:layout_below="#+id/imageView6"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/NextButton"
android:layout_alignStart="#+id/NextButton" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="47dp" />
<Button
android:id="#+id/NextButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
style="#style/ButtonText"
android:onClick="AddEventPlace"
android:layout_alignTop="#+id/BackButton"
android:layout_alignRight="#+id/imageView6" />
<Button
android:id="#+id/BackButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
style="#style/ButtonText"
android:onClick="Back"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/edtitem"
android:layout_alignStart="#+id/edtitem" />
</RelativeLayout>
AddReminder.java
public class AddReminder extends AppCompatActivity {
String item;
//Passing Value Through String
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
EditText edtItem= (EditText)findViewById(R.id.edtitem);
item=edtItem.getText().toString();
}
public void AddEventPlace(View v){
Intent intent = new Intent(this, AddEventPlace.class);
String Username = intent.getExtras().getString("Username");
intent.putExtra("Username", Username);
intent.putExtra("item",item);
startActivity(intent);
}
}
Check my updated code, Hope it will help...
1) Updated Menu Activity -
public class Menu extends AppCompatActivity {
String username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
username = getIntent().getStringExtras("Username");
}
public void addReminder(View v) {
Intent intent = new Intent(this, AddReminder.class);
intent.putExtra("Username", username);
startActivity(intent);
}
}
2) Updated AddReminder Activity -
public class AddReminder extends AppCompatActivity {
String item;
String username;
//Passing Value Through String
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
EditText edtItem= (EditText)findViewById(R.id.edtitem);
item=edtItem.getText().toString();
username = getIntent().getStringExtras("Username");
}
public void AddEventPlace(View v){
Intent intent = new Intent(this, AddEventPlace.class);
intent.putExtra("Username", username);
intent.putExtra("item",item);
startActivity(intent);
}
}