Bu dersimizde floating label ve doğrulama konusunu inceliyoruz.
Edittextlerimizi TextInputLayoutlarla sarmalayarak hint değerini kayan label olarak kullanabiliriz. Ayrıca java kodu ile yazacağımız kısıtlara göre hata kodları üretebiliriz. Uygulamalarımızda kullanıcıdan veri alırken kolay kullanımı ve estetik görünümüyle bu özellikleri kullanabiliriz.
Tüm projeyi indirmek için lütfen burayı tıklayın….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package com.example.emrealtunbilek.floatinglabelwithvalidation; public class MainActivity extends AppCompatActivity { private EditText etUsername, etPassword; private TextInputLayout inputLayoutName, inputLayoutPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); inputLayoutName = (TextInputLayout) findViewById(R.id.inputLayoutUsername); inputLayoutPassword = (TextInputLayout) findViewById(R.id.inputLayoutPassword); etUsername = (EditText) findViewById(R.id.etUsername); etPassword = (EditText) findViewById(R.id.etPassword); } private boolean dogrulaAd(){ if(etUsername.getText().toString().isEmpty()){ inputLayoutName.setError("Kullanıcı adı boş olmamalı"); return false; } else{ inputLayoutName.setErrorEnabled(false); return true; } } private boolean dogrulaSifre(){ String parola=etPassword.getText().toString().trim(); if(parola.length() <6){ inputLayoutPassword.setError("Parola min 6 karakter olmalı"); return false; }else{ inputLayoutPassword.setErrorEnabled(false); return true; } } public void girisDogrula(View view){ if(dogrulaAd() && dogrulaSifre()){ Toast.makeText(this, "Giriş yapıldı ", Toast.LENGTH_LONG).show(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<RelativeLayout 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.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" app:titleTextColor="@android:color/white"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="20dp" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/inputLayoutUsername"> <EditText android:id="@+id/etUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Kullanıcı Adı"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/inputLayoutPassword" > <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Parola" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:background="@color/colorPrimary" android:text="GIRIS" android:onClick="girisDogrula" android:textColor="@android:color/white"/> </LinearLayout> </RelativeLayout> |