-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO 02 - Question 01.txt
279 lines (231 loc) · 10.6 KB
/
CO 02 - Question 01.txt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
Register Form Page
------------------------------------------------------------------- XML Code -------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<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:paddingHorizontal="20dp"
android:id="@+id/main_layout"
android:paddingVertical="10dp"
tools:context=".SharedPreferencesQues05Activity">
<EditText
android:id="@+id/fullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_marginTop="30dp"
android:hint="Full Name"/>
<EditText
android:id="@+id/emailid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:inputType="textEmailAddress"
android:hint="Email ID"/>
<RadioGroup
android:id="@+id/gender_radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/female_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Female"/>
<RadioButton
android:id="@+id/others_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Others"/>
</RadioGroup>
<TextView
android:id="@+id/gender_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/register_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"/>
</LinearLayout>
------------------------------------------------------------------- Java Code -------------------------------------------------------------------
package com.example.loginformques01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class SharedPreferencesQues05Activity extends AppCompatActivity {
EditText fullname, emailid, password;
RadioGroup gender_radioGroup;
RadioButton male_gender, female_gender, others_gender;
Button register_btn;
TextView gender_error;
LinearLayout main_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences_ques05);
fullname= findViewById(R.id.fullname);
emailid= findViewById(R.id.emailid);
password= findViewById(R.id.password);
gender_radioGroup= findViewById(R.id.gender_radioGroup);
male_gender= findViewById(R.id.male_gender);
female_gender= findViewById(R.id.female_gender);
others_gender= findViewById(R.id.others_gender);
register_btn= findViewById(R.id.register_btn);
gender_error= findViewById(R.id.gender_error);
main_layout= findViewById(R.id.main_layout);
register_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
main_layout.clearFocus();
gender_error.setText("");
String password_regex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$";
String fullname_text= fullname.getText().toString();
String emailid_text= emailid.getText().toString();
String password_text= password.getText().toString();
int gender_selected= gender_radioGroup.getCheckedRadioButtonId();
if(fullname_text.equals("")){
fullname.requestFocus();
fullname.setError("Please enter fullname !!");
}
else if(fullname_text.length() < 3){
fullname.requestFocus();
fullname.setError("Fullname should be more than 2 characters !!");
}
else if(emailid_text.equals("")){
emailid.requestFocus();
emailid.setError("Please enter email-id !!");
}
else if(!Patterns.EMAIL_ADDRESS.matcher(emailid_text).matches()){
emailid.requestFocus();
emailid.setError("Please enter a valid email-id !!");
}
else if(gender_selected < 0){
gender_error.setText("Select anyone of the gender option !!");
}
else if(!password_text.matches(password_regex)){
password.requestFocus();
password.setError("Password should contain - \na digit must occur at least once\na lower case letter must occur at least once\nan upper case letter must occur at least once\na special character like @#$%^&+=\nNo blank spaces allowed\natleast 6 characters");
}
else{
fullname.setError("");
emailid.setError("");
gender_error.setText("");
password.setError("");
SharedPreferences pref= getSharedPreferences("register_data", MODE_PRIVATE);
SharedPreferences.Editor pref_edit= pref.edit();
pref_edit.putString("reg_fullname",fullname_text);
pref_edit.putString("reg_emailid",emailid_text);
pref_edit.putString("reg_password",password_text);
pref_edit.putInt("reg_gender",gender_selected);
pref_edit.apply();
Intent intent= new Intent(getApplicationContext(),SharedPreferencesResultPageActivity.class);
startActivity(intent);
}
}
});
}
}
####################################################################################################################################################
User Details Result Page
------------------------------------------------------------------- XML Code -------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
android:padding="10dp"
tools:context=".SharedPreferencesResultPageActivity">
<TextView
android:id="@+id/fullname_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Name : "/>
<TextView
android:id="@+id/emailid_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Name : "/>
<TextView
android:id="@+id/gender_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Gender : "/>
<TextView
android:id="@+id/password_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Password : "/>
</LinearLayout>
------------------------------------------------------------------- Java Code -------------------------------------------------------------------
package com.example.loginformques01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class SharedPreferencesResultPageActivity extends AppCompatActivity {
TextView fullname_result, emailid_result, gender_result, password_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences_result_page);
fullname_result= findViewById(R.id.fullname_result);
emailid_result= findViewById(R.id.emailid_result);
gender_result= findViewById(R.id.gender_result);
password_result= findViewById(R.id.password_result);
SharedPreferences pref= getSharedPreferences("register_data", MODE_PRIVATE);
String name= pref.getString("reg_fullname","Sample Name");
String email= pref.getString("reg_emailid","Sample Email");
String password= pref.getString("reg_password","Sample Password");
int gender_id= pref.getInt("reg_gender",0);
String gender;
if(gender_id==0)
gender= "Male";
else if(gender_id==1)
gender= "Female";
else if(gender_id==2)
gender= "Others";
else
gender="Sample Gender";
fullname_result.setText(name);
emailid_result.setText(email);
gender_result.setText(gender);
password_result.setText(password);
}
}