Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions app/src/main/java/com/training/simpleloginform/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Assignment add validation to edit text fields
*
* <p>
* if an edit text is empty, show a toast that says "edit text cannot be empty", where edit text can be username or password
*
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {

private EditText usernameEditText;
private AutoCompleteTextView usernameEditText;
private EditText passwordEditText;
private Button submitButton;
private TextView usernameDetailsTextView;
private TextView passwordDetailsTextView;
private Button resetButton;

Names names = new Names();

List<String> namesList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -35,10 +48,17 @@ protected void onCreate(Bundle savedInstanceState) {
submitButton = findViewById(R.id.submit_button);
resetButton = findViewById(R.id.reset_button);

namesList.addAll(names.getNames());

usernameEditText.addTextChangedListener(this);
usernameEditText.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, namesList));

submitButton.setOnClickListener(this);
resetButton.setOnClickListener(this);

}


@Override
public void onClick(View view) {
switch (view.getId()) {
Expand All @@ -48,11 +68,42 @@ public void onClick(View view) {

usernameDetailsTextView.setText(username);
passwordDetailsTextView.setText(password);

if(!namesList.contains(username)){
namesList.add(username);
}


break;
case R.id.reset_button:
usernameEditText.setText("");
passwordEditText.setText("");
setResetButton();
break;
}
}

public void setResetButton() {
usernameEditText.setText("");
passwordEditText.setText("");
usernameDetailsTextView.setText("");
passwordDetailsTextView.setText("");

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

usernameEditText.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, namesList));

}

@Override
public void afterTextChanged(Editable s) {


}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/training/simpleloginform/Names.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.training.simpleloginform;

import java.util.ArrayList;
import java.util.List;

public class Names {

List<String> names = new ArrayList<>();

public List<String> getNames() {

names.add("Desmond");
names.add("Dean");
names.add("Sidd");
names.add("Nahi");
names.add("Iliana");
names.add("Sulekha");
names.add("Damian");

return names;
}
}
11 changes: 6 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />

<EditText
<AutoCompleteTextView
android:id="@+id/user_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/general_margin"
android:hint="@string/username_edit_text_hint" />
android:hint="@string/username_edit_text_hint"
android:completionThreshold="1"/>

</LinearLayout>

Expand Down Expand Up @@ -55,17 +56,17 @@

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="@dimen/general_margin"
android:layout_marginTop="@dimen/general_margin"
android:layout_marginRight="@dimen/general_margin"
android:layout_marginLeft="@dimen/general_margin"
android:layout_weight="1"
android:background="@android:color/holo_green_dark"
android:text="@string/submit_button_string"
android:textStyle="bold" />
Expand Down