diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..d51122f --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +AndroidFundamentalsMai2021 \ No newline at end of file diff --git a/app/src/main/java/com/magdamiu/androidfundamentalsmai2021/MainActivity.java b/app/src/main/java/com/magdamiu/androidfundamentalsmai2021/MainActivity.java index ab7184b..7c6f37e 100644 --- a/app/src/main/java/com/magdamiu/androidfundamentalsmai2021/MainActivity.java +++ b/app/src/main/java/com/magdamiu/androidfundamentalsmai2021/MainActivity.java @@ -3,7 +3,9 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; +import android.text.TextUtils; import android.util.Log; +import android.util.Patterns; import android.view.View; import android.webkit.WebView; import android.widget.AdapterView; @@ -21,6 +23,8 @@ public class MainActivity extends AppCompatActivity { private static final String ANDROID_URL = "https://developer.android.com/"; private EditText editTextName; + private EditText editTextEmail; + private EditText editTextPhone; private Button buttonDisplayGreetings; private TextView textViewGreetings; @@ -33,20 +37,22 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - /*setContentView(R.layout.activity_main);*/ - /*setContentView(R.layout.sample);*/ - // setContentView(R.layout.scrollview_sample); - // setContentView(R.layout.webview_sample); + /* setContentView(R.layout.activity_main); + setContentView(R.layout.sample); + setContentView(R.layout.code_challenge_c2); + setContentView(R.layout.scrollview_sample); + setContentView(R.layout.webview_sample); setContentView(R.layout.spinner_sample); + setContentView(R.layout.code_challenge2_c3); */ + setContentView(R.layout.code_challenge1_c3); - setAndroidVersions(); + /* setAndroidVersions(); initialiseSpinnerAdapter(); setSpinnerAdapter(); - handlingSpinnerListener(); + handlingSpinnerListener(); */ - //loadUrl(); - - // initViews(); + // loadUrl(); + initViews(); displayLogs(); } @@ -60,6 +66,7 @@ private void setAndroidVersions() { androidVersions.add("kitkat"); } + // step 2: initialise adapter for our spinner // ArrayAdapter is a default adapter private void initialiseSpinnerAdapter() { @@ -89,6 +96,7 @@ public void onNothingSelected(AdapterView adapterView) { }); } + // webview load url private void loadUrl() { webView = findViewById(R.id.webViewSample); @@ -96,11 +104,14 @@ private void loadUrl() { webView.loadUrl(ANDROID_URL); } + // init views for implementing a first simple form private void initViews() { editTextName = findViewById(R.id.editTextName); buttonDisplayGreetings = findViewById(R.id.buttonDisplayGreetings); textViewGreetings = findViewById(R.id.textViewGreetings); + editTextEmail = findViewById(R.id.editTextEmail); + editTextPhone = findViewById(R.id.editTextPhone); } private void displayLogs() { @@ -128,4 +139,28 @@ public void displayGreetingsOnClick(View view) { Toast.makeText(MainActivity.this, getString(R.string.error_insert_name), Toast.LENGTH_LONG).show(); } } + + // Course 3: Code Challenge 1 + public static boolean isValidEmail(CharSequence target) { + return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches()); + } + + public static boolean isValidPhone(CharSequence phone) { + return (!TextUtils.isEmpty(phone) && Patterns.PHONE.matcher(phone).matches()); + } + + public void displaySubmitOnClick(View view) { + String inputName = editTextEmail.getText().toString(); + if (!isValidEmail(inputName)) { + editTextEmail.setError(getString(R.string.error_insert_email)); + Toast.makeText(MainActivity.this, getString(R.string.error_insert_email), + Toast.LENGTH_LONG).show(); + } + inputName = editTextPhone.getText().toString(); + if (!isValidPhone(inputName)) { + editTextPhone.setError(getString(R.string.error_insert_phone)); + Toast.makeText(MainActivity.this, getString(R.string.error_insert_phone), + Toast.LENGTH_LONG).show(); + } + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/paint.png b/app/src/main/res/drawable/paint.png new file mode 100644 index 0000000..a6a6092 Binary files /dev/null and b/app/src/main/res/drawable/paint.png differ diff --git a/app/src/main/res/layout/code_challenge1_c3.xml b/app/src/main/res/layout/code_challenge1_c3.xml new file mode 100644 index 0000000..5d1cdab --- /dev/null +++ b/app/src/main/res/layout/code_challenge1_c3.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + +