· 5 years ago · Feb 28, 2020, 01:54 PM
1Calculation
2activity_main.xml
3
4<?xml version="1.0" encoding="utf-8"?>
5<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
6 xmlns:app="http://schemas.android.com/apk/res-auto"
7 xmlns:tools="http://schemas.android.com/tools"
8 android:layout_width="match_parent"
9 android:layout_height="match_parent"
10 tools:context=".MainActivity">
11 <TextView
12 android:id="@+id/textView"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:layout_alignParentStart="true"
16 android:layout_alignParentTop="true"
17 android:layout_marginStart="36dp"
18 android:layout_marginTop="100dp"
19 android:text="Number 1"
20 android:textColor="@color/black"
21 android:textSize="25dp" />
22 <TextView
23 android:id="@+id/textView2"
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 android:layout_below="@id/textView"
27 android:layout_alignStart="@+id/textView"
28 android:layout_marginTop="100dp"
29 android:text="Number 2"
30 android:textColor="@color/black"
31 android:textSize="25dp" />
32 <EditText
33 android:id="@+id/txtnum1"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:layout_alignParentTop="true"
37 android:layout_marginStart="56dp"
38 android:layout_marginTop="98dp"
39 android:layout_toEndOf="@id/textView"
40 android:ems="10"
41 android:inputType="textPersonName" />
42 <EditText
43 android:id="@+id/txtnum2"
44 android:layout_width="wrap_content"
45 android:layout_height="wrap_content"
46 android:layout_below="@id/txtnum1"
47 android:layout_alignStart="@id/txtnum1"
48 android:layout_marginTop="96dp"
49 android:ems="10"
50 android:inputType="textPersonName" />
51 <Button
52android:id="@+id/button1"
53 android:layout_width="250dp"
54 android:layout_height="50dp"
55 android:layout_below="@id/textView2"
56 android:layout_centerHorizontal="true"
57 android:layout_marginTop="86dp"
58 android:text="ADD"
59 android:textColor="@color/black" />
60 <TextView
61 android:id="@+id/textView3"
62 android:layout_width="wrap_content"
63 android:layout_height="wrap_content"
64 android:layout_alignStart="@id/button1"
65 android:layout_alignParentTop="true"
66 android:layout_marginStart="43dp"
67 android:layout_marginTop="-1dp"
68 android:text="ADDITION"
69 android:textColor="@color/black"
70 android:textSize="40dp" />
71</RelativeLayout>
72
73MainActivity.java
74
75package com.example.add;
76import androidx.appcompat.app.AppCompatActivity;
77import android.os.Bundle;
78import android.view.View;
79import android.widget.Button;
80import android.widget.EditText;
81import android.widget.Toast;
82public class MainActivity extends AppCompatActivity {
83 EditText edit1,edit2;
84 Button btn;
85 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 setContentView(R.layout.activity_main);
89 edit1=(EditText)findViewById(R.id.txtnum1);
90 edit2=(EditText)findViewById(R.id.txtnum2);
91 btn=(Button)findViewById(R.id.button1);
92 btn.setOnClickListener(new View.OnClickListener() {
93 @Override
94 public void onClick(View v) {
95 String num1=edit1.getText().toString();
96 String num2=edit2.getText().toString();
97 int a=Integer.parseInt(num1);
98 int b=Integer.parseInt(num2);
99 int c=a+b;
100
101Toast.makeText(getApplicationContext(),String.valueOf(c),Toast.LENGTH_LONG).show();
102 }
103 });
104 }
105}
106
107Create an Android App Working with TextView (Scroll View).
108activity_main.xml
109
110<?xml version="1.0" encoding="utf-8"?>
111<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
112 xmlns:app="http://schemas.android.com/apk/res-auto"
113 xmlns:tools="http://schemas.android.com/tools"
114 android:layout_width="match_parent"
115 android:layout_height="match_parent"
116 tools:context=".MainActivity">
117 <TextView
118 android:id="@+id/textView"
119 android:layout_width="wrap_content"
120 android:layout_height="wrap_content"
121 android:text="Text And Scroll View"
122 android:textAlignment="center"
123 android:layout_marginTop="65dp"
124 android:textStyle="bold"
125 android:fontFamily="serif"
126 android:textSize="25dp"
127 android:layout_alignParentTop="true"
128 android:layout_centerHorizontal="true"
129 android:textColor="@color/Black"/>
130 <ScrollView
131 android:layout_width="match_parent"
132 android:layout_height="250dp"
133 android:layout_centerHorizontal="true"
134 android:layout_centerVertical="true" >
135 <TextView
136 android:id="@+id/textView2"
137 android:layout_width="match_parent"
138 android:layout_height="wrap_content"
139 android:text="@string/text"
140 android:textSize="25dp"
141 android:textColor="@color/Black"
142 android:fontFamily="cursive"/>
143 </ScrollView>
144</RelativeLayout>
145
146
147strings.xml
148
149<resources>
150 <string name="app_name">Text and ScrollView</string>
151 <string name="text">Gandhi was the youngest child of his father’s fourth wife.
152His father—Karamchand Gandhi, who was the dewan (chief minister) of Porbandar, the
153capital of a small principality in western India (in what is now Gujarat state)
154under British suzerainty—did not have much in the way of a formal education. He
155was, however, an able administrator who knew how to steer his way between the
156capricious princes, their long-suffering subjects, and the headstrong British
157political officers in power.
158Gandhi’s mother, Putlibai, was completely absorbed in religion, did not care much
159for finery or jewelry, divided her time between her home and the temple, fasted
160frequently, and wore herself out in days and nights of nursing whenever there was
161sickness in the family. Mohandas grew up in a home steeped in Vaishnavism—worship
162of the Hindu god Vishnu—with a strong tinge of Jainism, a morally rigorous Indian
163religion whose chief tenets are nonviolence and the belief that everything in the
164universe is eternal. Thus, he took for granted ahimsa (noninjury to all living
165beings), vegetarianism, fasting for self-purification, and mutual tolerance between
166adherents of various
167 The educational facilities at Porbandar were rudimentary; in the primary
168school that Mohandas attended, the children wrote the alphabet in the dust with
169their fingers. Luckily for him, his father became dewan of Rajkot, another princely
170state. Though Mohandas occasionally won prizes and scholarships at the local
171schools, his record was on the whole mediocre. One of the terminal
172</string>
173</resources>
174
175
176colors.xml
177
178<?xml version="1.0" encoding="utf-8"?>
179<resources>
180 <color name="colorPrimary">#008577</color>
181 <color name="colorPrimaryDark">#00574B</color>
182 <color name="colorAccent">#D81B60</color>
183 <color name="Black">#000000</color>
184</resources>
185
186
187Create an Android App for Android Activity LifeCycle.
188activity_main.xml
189
190<?xml version="1.0" encoding="utf-8"?>
191<androidx.constraintlayout.widget.ConstraintLayout
192xmlns:android="http://schemas.android.com/apk/res/android"
193 xmlns:app="http://schemas.android.com/apk/res-auto"
194 xmlns:tools="http://schemas.android.com/tools"
195 android:layout_width="match_parent"
196 android:layout_height="match_parent"
197 tools:context=".MainActivity">
198 <TextView
199 android:layout_width="wrap_content"
200 android:layout_height="wrap_content"
201 android:text="Hello World!"
202 app:layout_constraintBottom_toBottomOf="parent"
203 app:layout_constraintLeft_toLeftOf="parent"
204 app:layout_constraintRight_toRightOf="parent"
205 app:layout_constraintTop_toTopOf="parent"
206 android:textSize="22dp"
207 android:fontFamily="serif"
208 android:textStyle="bold"/>
209</androidx.constraintlayout.widget.ConstraintLayout>
210
211
212
213MainActivity.java
214
215package com.example.lifecycle;
216import androidx.appcompat.app.AppCompatActivity;
217import android.os.Bundle;
218import android.widget.Toast;
219public class MainActivity extends AppCompatActivity {
220 @Override
221 protected void onCreate(Bundle savedInstanceState) {
222 super.onCreate(savedInstanceState);
223 setContentView(R.layout.activity_main);
224 Toast.makeText(this,"onCreate invoked",Toast.LENGTH_LONG).show();
225 }
226 @Override
227 protected void onStart() {
228 super.onStart();
229
230Toast.makeText(this,"onStart invoked",Toast.LENGTH_LONG).show();
231 }
232 @Override
233 protected void onResume() {
234 super.onResume();
235 Toast.makeText(this,"onResume invoked",Toast.LENGTH_LONG).show();
236 }
237 @Override
238 protected void onPause() {
239 super.onPause();
240 Toast.makeText(this,"onPause invoked",Toast.LENGTH_LONG).show();
241 }
242 @Override
243 protected void onStop() {
244 super.onStop();
245 Toast.makeText(this,"onStop invoked",Toast.LENGTH_LONG).show();
246 }
247 @Override
248 protected void onRestart() {
249 super.onRestart();
250 Toast.makeText(this,"onRestart invoked",Toast.LENGTH_LONG).show();
251 }
252 @Override
253 protected void onDestroy() {
254 super.onDestroy();
255 Toast.makeText(this,"onDestroy invoked",Toast.LENGTH_LONG).show();
256 }
257}
258
259
260
261Create an Android App for AutoComplete TextView.
262activity_main.xml
263
264<?xml version="1.0" encoding="utf-8"?>
265<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
266 xmlns:app="http://schemas.android.com/apk/res-auto"
267 xmlns:tools="http://schemas.android.com/tools"
268 android:layout_width="match_parent"
269 android:layout_height="match_parent"
270 tools:context=".MainActivity">
271 <TextView
272 android:id="@+id/textView"
273 android:layout_width="wrap_content"
274 android:layout_height="wrap_content"
275 android:layout_margin="20dp"
276 android:layout_weight="0"
277 android:text="Select Language"
278 android:textColor="@color/Black"
279 android:textSize="25dp"/>
280 <AutoCompleteTextView
281 android:id="@+id/autoCompleteTextView"
282 android:layout_width="wrap_content"
283 android:layout_height="wrap_content"
284 android:layout_weight="1"
285 android:layout_margin="10dp"
286 android:text="AutoCompleteText"/>
287</LinearLayout>
288
289
290MainActivity.java
291
292package com.example.autocomplete;
293import androidx.appcompat.app.AppCompatActivity;
294import android.graphics.Color;
295import android.os.Bundle;
296import android.widget.ArrayAdapter;
297import android.widget.AutoCompleteTextView;
298public class MainActivity extends AppCompatActivity {
299 String[]language = {"C","C++","C#","Python","Java",".NET","Android","PHP"};
300 @Override
301 protected void onCreate(Bundle savedInstanceState) {
302 super.onCreate(savedInstanceState);
303 setContentView(R.layout.activity_main);
304
305ArrayAdapter<String>adapter = new
306ArrayAdapter<String>(this,android.R.layout.select_dialog_item,language);
307 AutoCompleteTextView actv =
308(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
309 actv.setThreshold(1);
310 actv.setAdapter(adapter);
311 actv.setTextColor(Color.CYAN);
312 }
313}
314
315
316
317Create an Android App that demonstrate the Use of Option Menu.
318activity_main.xml
319
320<?xml version="1.0" encoding="utf-8"?>
321<androidx.constraintlayout.widget.ConstraintLayout
322xmlns:android="http://schemas.android.com/apk/res/android"
323 xmlns:app="http://schemas.android.com/apk/res-auto"
324 xmlns:tools="http://schemas.android.com/tools"
325 android:layout_width="match_parent"
326 android:layout_height="match_parent"
327 tools:context=".MainActivity">
328 <TextView
329 android:layout_width="wrap_content"
330 android:layout_height="wrap_content"
331 android:text="Hello World!"
332 app:layout_constraintBottom_toBottomOf="parent"
333 app:layout_constraintLeft_toLeftOf="parent"
334 app:layout_constraintRight_toRightOf="parent"
335 app:layout_constraintTop_toTopOf="parent" />
336</androidx.constraintlayout.widget.ConstraintLayout>
337
338
339
340MainActivity.java
341
342package com.example.menu;
343import androidx.appcompat.app.AppCompatActivity;
344import android.content.Intent;
345import android.os.Bundle;
346import android.view.Menu;
347import android.view.MenuInflater;
348import android.view.MenuItem;
349public class MainActivity extends AppCompatActivity {
350 @Override
351 protected void onCreate(Bundle savedInstanceState) {
352 super.onCreate(savedInstanceState);
353 setContentView(R.layout.activity_main);
354 }
355 @Override
356 public boolean onCreateOptionsMenu(Menu menu){
357 MenuInflater menuInflater = getMenuInflater();
358 menuInflater.inflate(R.menu.menu_main, menu);
359 return true;
360 }
361@Override
362 public boolean onOptionsItemSelected(MenuItem item) {
363 switch (item.getItemId()) {
364 case R.id.fycs:
365 startActivity(new Intent(MainActivity.this, fycs.class));
366 return true;
367 case R.id.sycs:
368 startActivity(new Intent(MainActivity.this, sycs.class));
369 return true;
370 case R.id.tycs:
371 startActivity(new Intent(MainActivity.this, tycs.class));
372 return true;
373 default:
374 return super.onOptionsItemSelected(item);
375 }
376 }
377}
378
379
380menu_main.xml
381
382<?xml version="1.0" encoding="utf-8"?>
383<menu xmlns:android="http://schemas.android.com/apk/res/android">
384 <item
385 android:id="@+id/fycs"
386 android:title="FYCS" />
387 <item
388 android:id="@+id/sycs"
389 android:title="SYCS" />
390 <item
391 android:id="@+id/tycs"
392 android:title="TYCS" />
393</menu>
394
395
396activity_fycs.xml
397
398<?xml version="1.0" encoding="utf-8"?>
399<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
400 xmlns:app="http://schemas.android.com/apk/res-auto"
401 xmlns:tools="http://schemas.android.com/tools"
402 android:layout_width="match_parent"
403 android:layout_height="match_parent"
404 android:gravity="center"
405 tools:context=".fycs">
406 <TextView
407 android:layout_width="wrap_content"
408 android:layout_height="wrap_content"
409 android:text="FYCS"
410 android:textSize="25dp"
411 android:textAlignment="center"
412 android:textColor="@color/colorPrimaryDark"
413android:fontFamily="monospace"
414 android:textStyle="bold" />
415</RelativeLayout>
416activity_sycs.xml
417
418<?xml version="1.0" encoding="utf-8"?>
419<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
420 xmlns:app="http://schemas.android.com/apk/res-auto"
421 xmlns:tools="http://schemas.android.com/tools"
422 android:layout_width="match_parent"
423 android:layout_height="match_parent"
424 android:gravity="center"
425 tools:context=".sycs">
426 <TextView
427 android:layout_width="wrap_content"
428 android:layout_height="wrap_content"
429 android:text="SYCS"
430 android:textSize="25dp"
431 android:textStyle="bold"
432 android:textColor="@color/colorPrimaryDark"
433 android:textAlignment="center"
434 android:fontFamily="monospace" />
435</RelativeLayout>
436
437
438activity_tycs.xml
439
440<?xml version="1.0" encoding="utf-8"?>
441<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
442 xmlns:app="http://schemas.android.com/apk/res-auto"
443 xmlns:tools="http://schemas.android.com/tools"
444 android:layout_width="match_parent"
445 android:layout_height="match_parent"
446 android:gravity="center"
447 tools:context=".tycs">
448 <TextView
449 android:layout_width="wrap_content"
450 android:layout_height="wrap_content"
451 android:text="TYCS"
452 android:textAlignment="center"
453 android:textColor="@color/colorPrimaryDark"
454 android:textSize="25dp"
455 android:textStyle="bold"
456 android:fontFamily="monospace" />
457</RelativeLayout>
458
459
460
461
462Create an Android App to Show Notification.
463activity_main.xml
464<?xml version="1.0" encoding="utf-8"?>
465<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
466 xmlns:app="http://schemas.android.com/apk/res-auto"
467 xmlns:tools="http://schemas.android.com/tools"
468 android:layout_width="match_parent"
469 android:layout_height="match_parent"
470 tools:context=".MainActivity">
471 <Button
472 android:layout_width="wrap_content"
473 android:layout_height="wrap_content"
474 android:id="@+id/bt_notification"
475 android:text="Notification"
476 android:layout_centerInParent="true"/>
477</RelativeLayout>
478
479
480MainActivity.java
481package com.example.notification;
482import androidx.appcompat.app.AppCompatActivity;
483import androidx.core.app.NotificationCompat;
484import android.app.NotificationManager;
485import android.app.PendingIntent;
486import android.content.Context;
487import android.content.Intent;
488import android.os.Bundle;
489import android.view.View;
490import android.widget.Button;
491public class MainActivity extends AppCompatActivity {
492 Button btNotification;
493 @Override
494 protected void onCreate(Bundle savedInstanceState) {
495 super.onCreate(savedInstanceState);
496 setContentView(R.layout.activity_main);
497 btNotification = findViewById(R.id.bt_notification);
498 btNotification.setOnClickListener(new View.OnClickListener() {
499 @Override
500 public void onClick(View v) {
501 String message = "This is a notification.";
502 NotificationCompat.Builder builder = new
503NotificationCompat.Builder(
504 MainActivity.this
505 )
506 .setSmallIcon(R.drawable.ic_message)
507.setContentTitle("New Notification")
508.setContentText(message)
509.setAutoCancel(true);
510 Intent intent = new Intent(MainActivity.this,
511 NotificationActivity.class);
512 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
513 intent.putExtra("message",message);
514 PendingIntent pendingIntent =
515PendingIntent.getActivity(MainActivity.this,
516 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
517 builder.setContentIntent(pendingIntent);
518 NotificationManager notificationManager =
519(NotificationManager)getSystemService(
520 Context.NOTIFICATION_SERVICE
521 );
522 notificationManager.notify(0,builder.build());
523 }
524 });
525 }
526}
527
528
529activity_notification.xml
530<?xml version="1.0" encoding="utf-8"?>
531<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
532 xmlns:app="http://schemas.android.com/apk/res-auto"
533 xmlns:tools="http://schemas.android.com/tools"
534 android:layout_width="match_parent"
535 android:layout_height="match_parent"
536 tools:context=".NotificationActivity">
537 <TextView
538 android:layout_width="match_parent"
539 android:layout_height="match_parent"
540 android:id="@+id/text_view"
541 android:textSize="25sp"
542 android:gravity="center"/>
543</RelativeLayout>
544
545
546NotificationActivity.java
547package com.example.notification;
548import androidx.appcompat.app.AppCompatActivity;
549import android.os.Bundle;
550import android.widget.TextView;
551public class NotificationActivity extends AppCompatActivity {
552 @Override
553 protected void onCreate(Bundle savedInstanceState) {
554 super.onCreate(savedInstanceState);
555 setContentView(R.layout.activity_notification);
556 TextView textView = findViewById(R.id.text_view);
557String message = getIntent().getStringExtra("message");
558 textView.setText(message);
559 }
560}
561
562
563
564
565
566Create an Android App to save user data in a database and use of different Queries.
567MainActivity.java
568package com.example.kcm_15.myapplication;
569import android.database.Cursor; import
570android.support.v7.app.AlertDialog; import
571android.support.v7.app.AppCompatActivity; import
572android.os.Bundle; import android.view.View;
573import android.widget.Button; import
574android.widget.EditText; import
575android.widget.Toast;
576public class MainActivity extends AppCompatActivity {
577 DatabaseHelper myDb;
578 EditText editName,editSurname,editMarks ,editTextId;
579 Button btnAddData;
580 Button btnviewAll;
581Button btnDelete;
582 Button btnviewUpdate;
583 @Override
584 protected void onCreate(Bundle savedInstanceState) {
585super.onCreate(savedInstanceState);
586setContentView(R.layout.activity_main);
587myDb = new DatabaseHelper(this);
588 editName = (EditText)findViewById(R.id.editText_name);
589editSurname = (EditText)findViewById(R.id.editText_surname);
590editMarks = (EditText)findViewById(R.id.editText_Marks);
591editTextId = (EditText)findViewById(R.id.editText_id);
592btnAddData = (Button)findViewById(R.id.button_add);
593btnviewAll = (Button)findViewById(R.id.button_viewAll);
594btnviewUpdate= (Button)findViewById(R.id.button_update);
595btnDelete= (Button)findViewById(R.id.button_delete);
596AddData(); viewAll(); UpdateData();
597 DeleteData();
598 }
599 public void DeleteData() {
600btnDelete.setOnClickListener(
601new View.OnClickListener() {
602 @Override
603public void onClick(View v) {
604Integer deletedRows =
605myDb.deleteData(editTextId.getText().toString());
606if(deletedRows > 0)
607Toast.makeText(MainActivity.this,"Data
608 Deleted",Toast.LENGTH_LONG).show();
609else
610Toast.makeText(MainActivity.this,"Data not
611Deleted",Toast.LENGTH_LONG).show();
612}
613 }
614 );
615 }
616 public void UpdateData() {
617btnviewUpdate.setOnClickListener(
618new View.OnClickListener() {
619 @Override
620public void onClick(View v) {
621boolean isUpdate =
622myDb.updateData(editTextId.getText().toString(),
623 editName.getText().toString(),
624
625editSurname.getText().toString(),editMarks.getText().toString());
626if(isUpdate == true)
627 Toast.makeText(MainActivity.this,"Data
628Update",Toast.LENGTH_LONG).show();
629else
630 Toast.makeText(MainActivity.this,"Data not
631Updated",Toast.LENGTH_LONG).show();
632 }
633 }
634 );
635}
636 public void AddData() {
637btnAddData.setOnClickListener(
638new View.OnClickListener() {
639 @Override
640public void onClick(View v) {
641boolean isInserted =
642myDb.insertData(editName.getText().toString(),
643 editSurname.getText().toString(),
644editMarks.getText().toString() ); if(isInserted ==
645true)
646 Toast.makeText(MainActivity.this,"Data
647Inserted",Toast.LENGTH_LONG).show();
648else
649 Toast.makeText(MainActivity.this,"Data not
650Inserted",Toast.LENGTH_LONG).show();
651 }
652 }
653 );
654 }
655 public void viewAll() {
656btnviewAll.setOnClickListener(
657new View.OnClickListener() {
658 @Override
659public void onClick(View v) {
660Cursor res = myDb.getAllData();
661if(res.getCount() == 0) {
662 // show message
663showMessage("Error","Nothing found");
664return;
665 }
666 StringBuffer buffer = new StringBuffer();
667while (res.moveToNext()) {
668buffer.append("Id :"+ res.getString(0)+"\n");
669buffer.append("Name :"+ res.getString(1)+"\n");
670buffer.append("Surname :"+ res.getString(2)+"\n");
671buffer.append("Marks :"+ res.getString(3)+"\n\n");
672 }
673 // Show all data
674showMessage("Data",buffer.toString());
675 }
676 }
677 );
678}
679 public void showMessage(String title,String Message){
680AlertDialog.Builder builder = new AlertDialog.Builder(this);
681builder.setCancelable(true); builder.setTitle(title);
682builder.setMessage(Message); builder.show();
683 }
684}
685activity_main.xml
686<?xml version="1.0" encoding="utf-8"?>
687<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
688xmlns:app="http://schemas.android.com/apk/res-auto"
689xmlns:tools="http://schemas.android.com/tools"
690android:layout_width="match_parent" android:layout_height="match_parent"
691 tools:context="com.example.kcm_15.myapplication.MainActivity">
692 <TextView
693 android:layout_width="wrap_content"
694android:layout_height="wrap_content"
695 android:textAppearance="?android:attr/textAppearanceLarge"
696android:text="Name" android:id="@+id/textView"
697android:layout_alignParentTop="true"
698android:layout_alignParentLeft="true"
699android:layout_alignParentStart="true" />
700 <TextView
701 android:layout_width="wrap_content"
702android:layout_height="wrap_content"
703 android:textAppearance="?android:attr/textAppearanceLarge"
704android:text="Surname" android:id="@+id/textView2"
705android:layout_below="@+id/editText_name"
706android:layout_alignParentLeft="true"
707android:layout_alignParentStart="true" />
708 <TextView
709 android:layout_width="wrap_content"
710android:layout_height="wrap_content"
711 android:textAppearance="?android:attr/textAppearanceLarge"
712android:text="Marks" android:id="@+id/textView3"
713 android:layout_below="@+id/editText_surname"
714android:layout_alignParentLeft="true"
715android:layout_alignParentStart="true" />
716 <EditText
717android:id="@+id/editText_name"
718android:layout_width="match_parent"
719android:layout_height="wrap_content"
720android:layout_alignTop="@+id/textView"
721android:layout_toRightOf="@+id/textView"
722android:layout_toEndOf="@+id/textView" />
723 <EditText
724 android:layout_width="match_parent"
725android:layout_height="wrap_content"
726android:id="@+id/editText_surname"
727android:layout_alignTop="@+id/textView2"
728android:layout_toRightOf="@+id/textView2"
729android:layout_toEndOf="@+id/textView2" />
730 <EditText
731 android:layout_width="match_parent"
732android:layout_height="wrap_content"
733android:id="@+id/editText_Marks"
734android:layout_below="@+id/editText_surname"
735android:layout_toRightOf="@+id/textView3"
736android:layout_toEndOf="@+id/textView3" />
737
738<Button
739 android:layout_width="wrap_content"
740android:layout_height="wrap_content"
741android:text="Add Data" android:id="@+id/button_add"
742android:layout_below="@+id/editText_Marks"
743android:layout_alignParentLeft="true"
744android:layout_alignParentStart="true"
745android:layout_marginTop="76dp" />
746
747<Button
748 android:layout_width="wrap_content"
749android:layout_height="wrap_content" android:text="View
750All" android:id="@+id/button_viewAll"
751android:layout_above="@+id/button_update"
752android:layout_centerHorizontal="true" />
753
754<Button
755 android:layout_width="wrap_content"
756android:layout_height="wrap_content"
757android:text="Update"
758android:id="@+id/button_update"
759android:layout_below="@+id/button_add"
760android:layout_alignParentLeft="true"
761android:layout_alignParentStart="true" />
762
763<Button
764 android:layout_width="wrap_content"
765android:layout_height="wrap_content"
766android:text="Delete" android:id="@+id/button_delete"
767android:layout_centerVertical="true"
768android:layout_below="@+id/button_viewAll"
769android:layout_alignLeft="@+id/button_viewAll"
770android:layout_alignStart="@+id/button_viewAll" />
771 <TextView
772android:layout_width="wrap_content"
773android:layout_height="wrap_content"
774 android:textAppearance="?android:attr/textAppearanceLarge"
775android:text="id"
776 android:id="@+id/textView_id"
777android:layout_below="@+id/editText_Marks"
778android:layout_alignParentLeft="true"
779android:layout_alignParentStart="true" />
780 <EditText
781 android:layout_width="match_parent"
782android:layout_height="wrap_content"
783android:id="@+id/editText_id"
784android:layout_alignTop="@+id/textView_id"
785android:layout_toRightOf="@+id/textView3"
786android:layout_toEndOf="@+id/textView3" />
787</RelativeLayout>
788DatabaseHelper.java
789package com.example.kcm_15.myapplication;
790/**
791* Created by kcm-15 on 29-Jan-20.
792*/
793import android.content.ContentValues; import
794android.content.Context; import
795android.database.Cursor; import
796android.database.sqlite.SQLiteDatabase; import
797android.database.sqlite.SQLiteOpenHelper;
798/**
799* Created by admin on 31/10/2017.
800*/
801public class DatabaseHelper extends SQLiteOpenHelper{
802public static final String DATABASE_NAME = "Student.db";
803public static final String TABLE_NAME = "student_table";
804public static final String COL_1 = "ID"; public static
805final String COL_2 = "NAME"; public static final String
806COL_3 = "SURNAME"; public static final String COL_4 =
807"MARKS";
808 public DatabaseHelper(Context context) {
809super(context, DATABASE_NAME, null, 1);
810 }
811 @Override
812public void onCreate(SQLiteDatabase db) {
813 db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY
814AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
815 }
816 @Override
817 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
818db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); onCreate(db);
819 }
820 public boolean insertData(String name,String surname,String marks) {
821 SQLiteDatabase db = this.getWritableDatabase();
822ContentValues contentValues = new ContentValues();
823contentValues.put(COL_2,name);
824contentValues.put(COL_3,surname);
825contentValues.put(COL_4,marks);
826 long result = db.insert(TABLE_NAME,null ,contentValues);
827if(result == -1) return false;
828else
829return true;
830 }
831 public Cursor getAllData() {
832 SQLiteDatabase db = this.getWritableDatabase();
833 Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
834return res;
835 }
836 public boolean updateData(String id,String name,String surname,String marks) {
837 SQLiteDatabase db = this.getWritableDatabase();
838ContentValues contentValues = new ContentValues();
839contentValues.put(COL_1,id); contentValues.put(COL_2,name);
840contentValues.put(COL_3,surname);
841contentValues.put(COL_4,marks);
842 db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
843return true;
844 }
845 public Integer deleteData (String id) {
846 SQLiteDatabase db = this.getWritableDatabase();
847return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
848 }
849}