SHOW:
|
|
- or go back to the newest paste.
| 1 | activity_main.xml | |
| 2 | ====================== | |
| 3 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 4 | android:layout_width="match_parent" | |
| 5 | android:layout_height="match_parent" | |
| 6 | android:orientation="vertical" | |
| 7 | android:layout_margin="16dp"> | |
| 8 | ||
| 9 | <!-- place for logo --> | |
| 10 | <LinearLayout | |
| 11 | android:layout_width="match_parent" | |
| 12 | android:layout_height="match_parent" | |
| 13 | android:layout_weight="1" | |
| 14 | android:orientation="vertical" | |
| 15 | android:gravity="center"> | |
| 16 | <TextView | |
| 17 | android:layout_width="wrap_content" | |
| 18 | android:layout_height="wrap_content" | |
| 19 | android:text="MY TASK" | |
| 20 | android:textSize="64sp"/> | |
| 21 | </LinearLayout> | |
| 22 | ||
| 23 | <!-- place for user input --> | |
| 24 | <LinearLayout | |
| 25 | android:layout_width="match_parent" | |
| 26 | android:layout_height="match_parent" | |
| 27 | android:layout_weight="1" | |
| 28 | android:orientation="vertical" | |
| 29 | android:gravity="center"> | |
| 30 | <EditText | |
| 31 | android:layout_width="match_parent" | |
| 32 | android:layout_height="wrap_content" | |
| 33 | android:hint="@string/userName" | |
| 34 | android:id="@+id/txtUser"/> | |
| 35 | <EditText | |
| 36 | android:layout_width="match_parent" | |
| 37 | android:layout_height="wrap_content" | |
| 38 | android:hint="@string/userPass" | |
| 39 | android:inputType="textPassword" | |
| 40 | android:layout_marginTop="20dp" | |
| 41 | android:id="@+id/txtPass"/> | |
| 42 | </LinearLayout> | |
| 43 | ||
| 44 | <!-- place for buttons--> | |
| 45 | <LinearLayout | |
| 46 | android:layout_width="match_parent" | |
| 47 | android:layout_height="match_parent" | |
| 48 | android:layout_weight="1" | |
| 49 | android:orientation="horizontal" | |
| 50 | android:gravity="top"> | |
| 51 | <Button | |
| 52 | android:layout_width="match_parent" | |
| 53 | android:layout_height="wrap_content" | |
| 54 | android:text="@string/register" | |
| 55 | android:textColor="#ffffff" | |
| 56 | android:background="#009fff" | |
| 57 | android:textSize="22sp" | |
| 58 | android:layout_weight="1" | |
| 59 | android:layout_margin="20dp" | |
| 60 | android:id="@+id/btnRegister"/> | |
| 61 | <Button | |
| 62 | android:layout_width="match_parent" | |
| 63 | android:layout_height="wrap_content" | |
| 64 | android:text="@string/login" | |
| 65 | android:textColor="#ffffff" | |
| 66 | android:background="#009fff" | |
| 67 | android:textSize="22sp" | |
| 68 | android:layout_weight="1" | |
| 69 | android:layout_margin="20dp" | |
| 70 | android:id="@+id/btnLogin"/> | |
| 71 | </LinearLayout> | |
| 72 | ||
| 73 | </LinearLayout> | |
| 74 | ||
| 75 | ||
| 76 | activity_register.xml | |
| 77 | ======================= | |
| 78 | <?xml version="1.0" encoding="utf-8"?> | |
| 79 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 80 | android:orientation="vertical" android:layout_width="match_parent" | |
| 81 | android:layout_height="match_parent" | |
| 82 | android:layout_margin="16dp"> | |
| 83 | ||
| 84 | <LinearLayout | |
| 85 | android:layout_width="match_parent" | |
| 86 | android:layout_height="match_parent" | |
| 87 | android:layout_weight="2" | |
| 88 | android:orientation="vertical" | |
| 89 | > | |
| 90 | <TextView | |
| 91 | android:layout_width="wrap_content" | |
| 92 | android:layout_height="match_parent" | |
| 93 | android:text="LOGO" | |
| 94 | android:textSize="62sp" | |
| 95 | android:layout_gravity="center" | |
| 96 | /> | |
| 97 | </LinearLayout> | |
| 98 | <LinearLayout | |
| 99 | android:layout_width="match_parent" | |
| 100 | android:layout_height="match_parent" | |
| 101 | android:layout_weight="1" | |
| 102 | android:orientation="vertical" | |
| 103 | android:layout_margin="30dp"> | |
| 104 | <EditText | |
| 105 | android:layout_width="match_parent" | |
| 106 | android:layout_height="match_parent" | |
| 107 | android:id="@+id/regUserName" | |
| 108 | android:hint="Enter a user name..." | |
| 109 | android:layout_weight="1" | |
| 110 | android:gravity="bottom"/> | |
| 111 | <EditText | |
| 112 | android:layout_width="match_parent" | |
| 113 | android:layout_height="match_parent" | |
| 114 | android:id="@+id/regPass1" | |
| 115 | android:hint="Enter your password...." | |
| 116 | android:layout_weight="1" | |
| 117 | android:gravity="bottom" | |
| 118 | android:inputType="textPassword"/> | |
| 119 | <EditText | |
| 120 | android:layout_width="match_parent" | |
| 121 | android:layout_height="match_parent" | |
| 122 | android:id="@+id/regPass2" | |
| 123 | android:hint="Re-Enter your password..." | |
| 124 | android:layout_weight="1" | |
| 125 | android:gravity="bottom" | |
| 126 | android:inputType="textPassword" | |
| 127 | /> | |
| 128 | </LinearLayout> | |
| 129 | <LinearLayout | |
| 130 | android:layout_width="match_parent" | |
| 131 | android:layout_height="match_parent" | |
| 132 | android:layout_weight="2" | |
| 133 | android:orientation="horizontal"> | |
| 134 | <Button | |
| 135 | android:layout_width="match_parent" | |
| 136 | android:layout_height="match_parent" | |
| 137 | android:text="@string/register" | |
| 138 | android:textColor="#ffffff" | |
| 139 | android:background="#009fff" | |
| 140 | android:textSize="32sp" | |
| 141 | android:layout_weight="1" | |
| 142 | android:layout_margin="10dp" | |
| 143 | android:id="@+id/btnRegRegister"/> | |
| 144 | <Button | |
| 145 | android:layout_width="match_parent" | |
| 146 | android:layout_height="match_parent" | |
| 147 | android:text="cancel" | |
| 148 | android:textColor="#ffffff" | |
| 149 | android:background="#009fff" | |
| 150 | android:textSize="32sp" | |
| 151 | android:layout_weight="1" | |
| 152 | android:layout_margin="10dp" | |
| 153 | android:id="@+id/btnRegCancel"/> | |
| 154 | </LinearLayout> | |
| 155 | </LinearLayout> | |
| 156 | ||
| 157 | android manifest (example only) | |
| 158 | ====================================== | |
| 159 | <?xml version="1.0" encoding="utf-8"?> | |
| 160 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| 161 | package="com.example.teacher.mytasks"> | |
| 162 | ||
| 163 | <application | |
| 164 | android:allowBackup="true" | |
| 165 | android:icon="@mipmap/ic_launcher" | |
| 166 | android:label="@string/app_name" | |
| 167 | android:roundIcon="@mipmap/ic_launcher_round" | |
| 168 | android:supportsRtl="true" | |
| 169 | android:theme="@style/AppTheme"> | |
| 170 | <activity android:name=".MainActivity"> | |
| 171 | <intent-filter> | |
| 172 | <action android:name="android.intent.action.MAIN" /> | |
| 173 | ||
| 174 | <category android:name="android.intent.category.LAUNCHER" /> | |
| 175 | </intent-filter> | |
| 176 | </activity> | |
| 177 | <activity android:name=".RegisterActivity"/> | |
| 178 | </application> | |
| 179 | ||
| 180 | </manifest> | |
| 181 | ||
| 182 | ||
| 183 | RegisterActivity.java | |
| 184 | ========================= | |
| 185 | ||
| 186 | import android.content.Context; | |
| 187 | import android.os.Bundle; | |
| 188 | import android.support.annotation.Nullable; | |
| 189 | import android.support.v7.app.AppCompatActivity; | |
| 190 | import android.view.View; | |
| 191 | import android.widget.Button; | |
| 192 | import android.widget.EditText; | |
| 193 | import android.widget.Toast; | |
| 194 | ||
| 195 | /** | |
| 196 | * Created by teacher on 12/21/2017. | |
| 197 | */ | |
| 198 | ||
| 199 | public class RegisterActivity extends AppCompatActivity {
| |
| 200 | Context context; | |
| 201 | EditText regUser,regPass1,regPass2; | |
| 202 | Button btnRegister,btnCancel; | |
| 203 | UtlUserSP userUtil=new UtlUserSP(); | |
| 204 | ||
| 205 | @Override | |
| 206 | protected void onCreate(@Nullable Bundle savedInstanceState) {
| |
| 207 | super.onCreate(savedInstanceState); | |
| 208 | setContentView(R.layout.activity_register); | |
| 209 | setPointer(); | |
| 210 | } | |
| 211 | ||
| 212 | private void setPointer() {
| |
| 213 | this.context=this; | |
| 214 | userUtil.setContext(context); | |
| 215 | regUser=findViewById(R.id.regUserName); | |
| 216 | regPass1=findViewById(R.id.regPass1); | |
| 217 | regPass2=findViewById(R.id.regPass2); | |
| 218 | btnCancel=findViewById(R.id.btnRegCancel); | |
| 219 | btnCancel.setOnClickListener(new View.OnClickListener() {
| |
| 220 | @Override | |
| 221 | public void onClick(View view) {
| |
| 222 | finish(); | |
| 223 | } | |
| 224 | }); | |
| 225 | btnRegister =findViewById(R.id.btnRegRegister); | |
| 226 | btnRegister.setOnClickListener(new View.OnClickListener() {
| |
| 227 | @Override | |
| 228 | public void onClick(View view) {
| |
| 229 | //check if password o.k. | |
| 230 | if (!regPass1.getText().toString().equals(regPass2.getText().toString())) | |
| 231 | {
| |
| 232 | Toast.makeText(context, "Password not match", Toast.LENGTH_SHORT).show(); | |
| 233 | return; | |
| 234 | } | |
| 235 | if (regPass1.getText().toString().length()<3) | |
| 236 | {
| |
| 237 | Toast.makeText(context, "Password must be 3 letter minimum", Toast.LENGTH_SHORT).show(); | |
| 238 | return; | |
| 239 | } | |
| 240 | if(!userUtil.registerUser(regUser.getText().toString(),regPass1.getText().toString())) | |
| 241 | {
| |
| 242 | Toast.makeText(context, "Error in user registration....", Toast.LENGTH_SHORT).show(); | |
| 243 | return; | |
| 244 | } | |
| 245 | Toast.makeText(context, "new user is add...", Toast.LENGTH_SHORT).show(); | |
| 246 | finish(); | |
| 247 | } | |
| 248 | }); | |
| 249 | ||
| 250 | } | |
| 251 | } | |
| 252 | ||
| 253 | ||
| 254 | ||
| 255 | MainActivity.java | |
| 256 | ====================== | |
| 257 | import android.content.Context; | |
| 258 | import android.content.Intent; | |
| 259 | import android.support.v7.app.AppCompatActivity; | |
| 260 | import android.os.Bundle; | |
| 261 | import android.view.View; | |
| 262 | import android.widget.Button; | |
| 263 | import android.widget.EditText; | |
| 264 | ||
| 265 | public class MainActivity extends AppCompatActivity {
| |
| 266 | EditText txtUser, txtPass; | |
| 267 | Button btnLogin, btnRegister; | |
| 268 | Context context; | |
| 269 | ||
| 270 | @Override | |
| 271 | protected void onCreate(Bundle savedInstanceState) {
| |
| 272 | super.onCreate(savedInstanceState); | |
| 273 | setContentView(R.layout.activity_main); | |
| 274 | setPointer(); | |
| 275 | } | |
| 276 | ||
| 277 | private void setPointer() {
| |
| 278 | this.context = this; | |
| 279 | txtUser = findViewById(R.id.txtUser); | |
| 280 | txtPass = findViewById(R.id.txtPass); | |
| 281 | btnLogin = findViewById(R.id.btnLogin); | |
| 282 | btnRegister = findViewById(R.id.btnRegister); | |
| 283 | ||
| 284 | btnRegister.setOnClickListener(new View.OnClickListener() {
| |
| 285 | @Override | |
| 286 | public void onClick(View view) {
| |
| 287 | Intent myIntent=new Intent(context,RegisterActivity.class); | |
| 288 | startActivity(myIntent); | |
| 289 | } | |
| 290 | }); | |
| 291 | } | |
| 292 | } | |
| 293 | ||
| 294 | ||
| 295 | ||
| 296 | ||
| 297 | UtlUserSp.java | |
| 298 | ===================== | |
| 299 | package com.example.teacher.mytasks; | |
| 300 | ||
| 301 | import android.content.Context; | |
| 302 | import android.content.SharedPreferences; | |
| 303 | import android.widget.Toast; | |
| 304 | ||
| 305 | /** | |
| 306 | * Created by teacher on 12/21/2017. | |
| 307 | */ | |
| 308 | ||
| 309 | public class UtlUserSP implements UserAble {
| |
| 310 | Context context; | |
| 311 | ||
| 312 | public UtlUserSP() { }
| |
| 313 | ||
| 314 | @Override | |
| 315 | public void setContext(Context context) {
| |
| 316 | this.context=context; | |
| 317 | } | |
| 318 | ||
| 319 | @Override | |
| 320 | public boolean registerUser(String userName, String userPass) {
| |
| 321 | //User Exists | |
| 322 | if (userExists(userName)) {
| |
| 323 | Toast.makeText(context, "user already exists...", Toast.LENGTH_SHORT).show(); | |
| 324 | return false; | |
| 325 | } | |
| 326 | //save user and password | |
| 327 | ||
| 328 | //declaration of shared preference | |
| 329 | SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
| |
| 330 | ||
| 331 | //declaration of shared preference editor | |
| 332 | SharedPreferences.Editor editor = prefs.edit(); | |
| 333 | ||
| 334 | //put data inside, we use hash map, so we need KEY,VALUE (k,v) | |
| 335 | editor.putString(userName, userPass); | |
| 336 | ||
| 337 | //sending data to my shared preferences file. | |
| 338 | editor.commit(); | |
| 339 | return true; | |
| 340 | } | |
| 341 | ||
| 342 | @Override | |
| 343 | public boolean userExists( String userName) {
| |
| 344 | //declaration of shared prefernces | |
| 345 | SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
| |
| 346 | ||
| 347 | //get user pass | |
| 348 | String spPass=prefs.getString(userName,"na"); | |
| 349 | ||
| 350 | //return if users exists, if not (na) return false | |
| 351 | return !spPass.equals("na");
| |
| 352 | } | |
| 353 | ||
| 354 | @Override | |
| 355 | public boolean checkUser( String userName, String userPass) {
| |
| 356 | return false; | |
| 357 | } | |
| 358 | } |