· 6 years ago · Aug 30, 2019, 06:06 AM
11. Notification
2
3import android.os.Bundle;
4import android.support.v4.app.NotificationCompat;
5import android.view.View;
6import android.widget.Button;
7import android.app.Activity;
8import android.app.NotificationManager;
9import android.app.PendingIntent;
10import android.content.Context;
11import android.content.Intent;
12
13public class MainActivity extends Activity {
14
15 Button b;
16 //Intent in;
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.activity_main);
22
23 b=(Button)findViewById(R.id.button1);
24 b.setOnClickListener(new View.OnClickListener() {
25
26 @Override
27 public void onClick(View arg0) {
28 // TODO Auto-generated method stub
29
30 NotificationCompat.Builder nBuilder=new NotificationCompat.Builder(getApplicationContext());
31 nBuilder.setSmallIcon(R.drawable.ic_launcher);
32 nBuilder.setContentTitle("Name...");
33 nBuilder.setContentText("Revathy .....i am notification for you!....");
34 //in=new Intent(MainActivity.this, MainActivity.class);
35
36 NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
37 nm.notify(0,nBuilder.build());
38
39 }
40 });
41
42
43 }
44
45
46}
47
48
49
50
51
52
532.SMS
54
55import android.os.Bundle;
56import android.app.Activity;
57import android.telephony.SmsManager;
58import android.view.View;
59import android.view.View.OnClickListener;
60import android.widget.Button;
61import android.widget.EditText;
62import android.widget.Toast;
63
64public class MainActivity extends Activity {
65
66 EditText mobileno,message;
67 Button sendsms;
68 @Override
69 protected void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 setContentView(R.layout.activity_main);
72
73 mobileno=(EditText)findViewById(R.id.editText1);
74 message=(EditText)findViewById(R.id.editText2);
75 sendsms=(Button)findViewById(R.id.button1);
76
77 sendsms.setOnClickListener(new OnClickListener() {
78 @Override
79 public void onClick(View arg0) {
80 String no=mobileno.getText().toString();
81 String msg=message.getText().toString();
82 SmsManager sms=SmsManager.getDefault();
83 if (no.length()==0)
84 {
85Toast.makeText(getApplicationContext(), "enter valid receipient", Toast.LENGTH_LONG).show();
86 }
87
88 else
89 {
90 sms.sendTextMessage(no, null,msg, null, null);
91Toast.makeText(getApplicationContext(), "Message Sent successfully!",Toast.LENGTH_LONG).show();
92 }
93 }
94 });
95 }
96
97
98}
99
100Android Manifest.xml
101<?xml version="1.0" encoding="utf-8"?>
102<manifest xmlns:android="http://schemas.android.com/apk/res/android"
103 package="com.example.sms"
104 android:versionCode="1"
105 android:versionName="1.0" >
106
107 <uses-sdk
108 android:minSdkVersion="8"
109 android:targetSdkVersion="17" />
110 <uses-permission android:name="android.permission.SEND_SMS"/>
111 <uses-permission android:name="android.permission.RECEIVE_SMS"/>
112
113
114 <application
115 android:allowBackup="true"
116 android:icon="@drawable/ic_launcher"
117 android:label="@string/app_name"
118 android:theme="@style/AppTheme" >
119 <activity
120 android:name="com.example.sms.MainActivity"
121 android:label="@string/app_name" >
122 <intent-filter>
123 <action android:name="android.intent.action.MAIN" />
124
125 <category android:name="android.intent.category.LAUNCHER" />
126 </intent-filter>
127 </activity>
128 </application>
129
130</manifest>
131
1323.WEB VIEW
133
134import android.os.Bundle;
135import android.view.View;
136import android.webkit.WebView;
137import android.webkit.WebViewClient;
138import android.widget.Button;
139import android.widget.EditText;
140import android.app.Activity;
141public class MainActivity extends Activity {
142 EditText txturl;
143 Button b;
144 WebView wv;
145
146@Override
147 protected void onCreate(Bundle savedInstanceState) {
148 super.onCreate(savedInstanceState);
149 setContentView(R.layout.activity_main);
150 txturl=(EditText)findViewById(R.id.editText1);
151 b=(Button)findViewById(R.id.button1);
152 wv=(WebView)findViewById(R.id.webView1);
153 //WebSettings webSettings = wv.getSettings();
154 //webSettings.setJavaScriptEnabled(true);
155
156
157 b.setOnClickListener(new View.OnClickListener() {
158 @Override
159 public void onClick(View arg0) {
160 // TODO Auto-generated method stub
161 wv.setWebViewClient(new WebViewClient());
162 wv.loadUrl("http://"+txturl.getText().toString());
163 }
164 });
165 }
166}
167
168Android Manifest.xml
169<?xml version="1.0" encoding="utf-8"?>
170<manifest xmlns:android="http://schemas.android.com/apk/res/android"
171 package="com.example.webview"
172 android:versionCode="1"
173 android:versionName="1.0" >
174
175 <uses-sdk
176 android:minSdkVersion="8"
177 android:targetSdkVersion="17" />
178<uses-permission android:name="android.permission.INTERNET"/>
179 <application
180 android:allowBackup="true"
181 android:icon="@drawable/ic_launcher"
182 android:label="@string/app_name"
183 android:theme="@style/AppTheme" >
184 <activity
185 android:name="com.example.webview.MainActivity"
186 android:label="@string/app_name" >
187 <intent-filter>
188 <action android:name="android.intent.action.MAIN" />
189
190 <category android:name="android.intent.category.LAUNCHER" />
191 </intent-filter>
192 </activity>
193 </application>
194
195</manifest>
196
197
1984. E-MAIL
199
200import android.os.Bundle;
201import android.view.View;
202import android.widget.Button;
203import android.widget.EditText;
204import android.app.Activity;
205import android.content.Intent;
206
207public class MainActivity extends Activity {
208
209 EditText txtTo, txtSub, txtMsg;
210 Button b;
211 String strTo, strSub, strMsg;
212 Intent in;
213
214@Override
215protected void onCreate(Bundle savedInstanceState) {
216super.onCreate(savedInstanceState);
217setContentView(R.layout.activity_main);
218
219txtTo=(EditText)findViewById(R.id.editText1);
220txtSub=(EditText)findViewById(R.id.editText2);
221txtMsg=(EditText)findViewById(R.id.editText3);
222
223b=(Button)findViewById(R.id.button1);
224
225b.setOnClickListener(new View.OnClickListener() {
226
227 @Override
228 public void onClick(View arg0) {
229 // TODO Auto-generated method stub
230
231 strTo=txtTo.getText().toString();
232 strSub=txtSub.getText().toString();
233 strMsg=txtMsg.getText().toString();
234
235 in=new Intent(Intent.ACTION_SEND);
236
237 //in.putExtra(Intent.EXTRA_EMAIL, new String[]{strTo});
238 in.putExtra(Intent.EXTRA_EMAIL, strTo);
239 in.putExtra(Intent.EXTRA_SUBJECT, strSub);
240 in.putExtra(Intent.EXTRA_TEXT, strMsg);
241
242 in.setType("message/rfc822");
243
244 startActivity(Intent.createChooser(in, "Choose an email client..."));
245
246
247
248 }
249 });
250
251 }
252
253
254}
255
256
257Android Manifest.xml
258<?xml version="1.0" encoding="utf-8"?>
259<manifest xmlns:android="http://schemas.android.com/apk/res/android"
260 package="com.example.webview"
261 android:versionCode="1"
262 android:versionName="1.0" >
263
264 <uses-sdk
265 android:minSdkVersion="8"
266 android:targetSdkVersion="17" />
267<uses-permission android:name="android.permission.INTERNET"/>
268 <application
269 android:allowBackup="true"
270 android:icon="@drawable/ic_launcher"
271 android:label="@string/app_name"
272 android:theme="@style/AppTheme" >
273 <activity
274 android:name="com.example.webview.MainActivity"
275 android:label="@string/app_name" >
276 <intent-filter>
277 <action android:name="android.intent.action.MAIN" />
278
279 <category android:name="android.intent.category.LAUNCHER" />
280 </intent-filter>
281 </activity>
282 </application>
283
284</manifest>
285
2865.STUDENT DATABASE
287
288import android.app.Activity;
289import android.app.AlertDialog.Builder;
290import android.content.Context;
291import android.database.Cursor;
292import android.database.sqlite.SQLiteDatabase;
293import android.os.Bundle;
294import android.view.View;
295import android.view.View.OnClickListener;
296import android.widget.Button;
297import android.widget.EditText;
298
299public class MainActivity extends Activity implements OnClickListener
300{
301 EditText Rollno,Name,Marks;
302 Button Insert,Delete,Update,View,ViewAll;
303 SQLiteDatabase db;
304 /** Called when the activity is first created. */
305 @Override
306 public void onCreate(Bundle savedInstanceState)
307 {
308 super.onCreate(savedInstanceState);
309 setContentView(R.layout.activity_main);
310
311 Rollno=(EditText)findViewById(R.id.editText1);
312 Name=(EditText)findViewById(R.id.editText2);
313 Marks=(EditText)findViewById(R.id.editText3);
314 Insert=(Button)findViewById(R.id.button1);
315 Delete=(Button)findViewById(R.id.button2);
316 Update=(Button)findViewById(R.id.button3);
317 View=(Button)findViewById(R.id.button4);
318 ViewAll=(Button)findViewById(R.id.button5);
319
320 Insert.setOnClickListener(this);
321 Delete.setOnClickListener(this);
322 Update.setOnClickListener(this);
323 View.setOnClickListener(this);
324 ViewAll.setOnClickListener(this);
325
326 // Creating database and table
327 db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
328 db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
329 }
330 public void onClick(View view)
331 {
332 // Inserting a record to the Student table
333 if(view==Insert)
334 {
335 // Checking for empty fields
336 if(Rollno.getText().toString().trim().length()==0||
337 Name.getText().toString().trim().length()==0||
338 Marks.getText().toString().trim().length()==0)
339 {
340 showMessage("Error", "Please enter all values");
341 return;
342 }
343 db.execSQL("INSERT INTO student VALUES('"+Rollno.getText()+"','"+Name.getText()+
344 "','"+Marks.getText()+"');");
345 showMessage("Success", "Record added");
346 clearText();
347 }
348 // Deleting a record from the Student table
349 if(view==Delete)
350 {
351 // Checking for empty roll number
352 if(Rollno.getText().toString().trim().length()==0)
353 {
354 showMessage("Error", "Please enter Rollno");
355 return;
356 }
357 Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+Rollno.getText()+"'", null);
358 if(c.moveToFirst())
359 {
360 db.execSQL("DELETE FROM student WHERE rollno='"+Rollno.getText()+"'");
361 showMessage("Success", "Record Deleted");
362 }
363 else
364 {
365 showMessage("Error", "Invalid Rollno");
366 }
367 clearText();
368 }
369 // Updating a record in the Student table
370 if(view==Update)
371 {
372 // Checking for empty roll number
373 if(Rollno.getText().toString().trim().length()==0)
374 {
375 showMessage("Error", "Please enter Rollno");
376 return;
377 }
378 Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+Rollno.getText()+"'", null);
379 if(c.moveToFirst()) {
380 db.execSQL("UPDATE student SET name='" + Name.getText() + "',marks='" + Marks.getText() +
381 "' WHERE rollno='"+Rollno.getText()+"'");
382 showMessage("Success", "Record Modified");
383 }
384 else {
385 showMessage("Error", "Invalid Rollno");
386 }
387 clearText();
388 }
389 // Display a record from the Student table
390 if(view==View)
391 {
392 // Checking for empty roll number
393 if(Rollno.getText().toString().trim().length()==0)
394 {
395 showMessage("Error", "Please enter Rollno");
396 return;
397 }
398 Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+Rollno.getText()+"'", null);
399 if(c.moveToFirst())
400 {
401 Name.setText(c.getString(1));
402 Marks.setText(c.getString(2));
403 }
404 else
405 {
406 showMessage("Error", "Invalid Rollno");
407 clearText();
408 }
409 }
410 // Displaying all the records
411 if(view==ViewAll)
412 {
413 Cursor c=db.rawQuery("SELECT * FROM student", null);
414 if(c.getCount()==0)
415 {
416 showMessage("Error", "No records found");
417 return;
418 }
419 StringBuffer buffer=new StringBuffer();
420 while(c.moveToNext())
421 {
422 buffer.append("Rollno: "+c.getString(0)+"\n");
423 buffer.append("Name: "+c.getString(1)+"\n");
424 buffer.append("Marks: "+c.getString(2)+"\n\n");
425 }
426 showMessage("Student Details", buffer.toString());
427 }
428 }
429 public void showMessage(String title,String message)
430 {
431 Builder builder=new Builder(this);
432 builder.setCancelable(true);
433 builder.setTitle(title);
434 builder.setMessage(message);
435 builder.show();
436 }
437 public void clearText()
438 {
439 Rollno.setText("");
440 Name.setText("");
441 Marks.setText("");
442 Rollno.requestFocus();
443 }
444}
445
446
4476.Android life cycle
448
449package com.example.androidlifecycle;
450
451import android.os.Bundle;
452import android.app.Activity;
453import android.view.Menu;
454import android.widget.Toast;
455
456public class MainActivity extends Activity {
457
458 @Override
459 protected void onCreate(Bundle savedInstanceState) {
460 super.onCreate(savedInstanceState);
461 setContentView(R.layout.activity_main);
462 Toast.makeText(getApplicationContext(), "I am create method", Toast.LENGTH_LONG).show();
463 }
464
465 @Override
466 protected void onStart() {
467 // TODO Auto-generated method stub
468 super.onStart();
469 Toast.makeText(getApplicationContext(), "I am start method", Toast.LENGTH_LONG).show();
470 }
471
472 @Override
473 protected void onResume() {
474 // TODO Auto-generated method stub
475 super.onResume();
476 Toast.makeText(getApplicationContext(), "I am resume method", Toast.LENGTH_LONG).show();
477 }
478
479 @Override
480 protected void onStop() {
481 // TODO Auto-generated method stub
482 super.onStop();
483 Toast.makeText(getApplicationContext(), "I am stop method", Toast.LENGTH_LONG).show();
484 }
485
486 @Override
487 protected void onPause() {
488 // TODO Auto-generated method stub
489 super.onPause();
490 Toast.makeText(getApplicationContext(), "I am pause method", Toast.LENGTH_LONG).show();
491 }
492
493 @Override
494 protected void onRestart() {
495 // TODO Auto-generated method stub
496 super.onRestart();
497 Toast.makeText(getApplicationContext(), "I am restart method", Toast.LENGTH_LONG).show();
498 }
499
500 @Override
501 protected void onDestroy() {
502 // TODO Auto-generated method stub
503 super.onDestroy();
504 Toast.makeText(getApplicationContext(), "I am destroy method", Toast.LENGTH_LONG).show();
505 }
506
507
508}
509
510
5117.AUTHRNTICATION VERFICATION
512
513package com.example.password;
514
515import android.os.Bundle;
516import android.app.Activity;
517import android.view.View;
518import android.widget.Button;
519import android.widget.EditText;
520import android.widget.Toast;
521
522public class MainActivity extends Activity {
523
524 EditText uname, pass;
525 Button blog, bclear;
526
527 @Override
528 protected void onCreate(Bundle savedInstanceState) {
529 super.onCreate(savedInstanceState);
530 setContentView(R.layout.activity_main);
531
532 uname=(EditText)findViewById(R.id.editText1);
533 pass=(EditText)findViewById(R.id.editText2);
534 blog=(Button)findViewById(R.id.button1);
535 bclear=(Button)findViewById(R.id.button2);
536
537 blog.setOnClickListener(new View.OnClickListener() {
538
539 @Override
540 public void onClick(View arg0) {
541 // TODO Auto-generated method stub
542
543 String na=uname.getText().toString();
544 String p=pass.getText().toString();
545 if(na.equals("admin") && p.equals("admin"))
546 Toast.makeText(getApplicationContext(), "Authenticated...", Toast.LENGTH_LONG).show();
547 else
548 {
549 Toast.makeText(getApplicationContext(), "Un Authenticated...", Toast.LENGTH_LONG).show();
550 uname.setText("");
551 pass.setText("");
552 }
553
554 }
555 });
556 bclear.setOnClickListener(new View.OnClickListener() {
557
558 @Override
559 public void onClick(View arg0) {
560 // TODO Auto-generated method stub
561
562 uname.setText("");
563 pass.setText("");
564 }
565 });
566
567
568 }
569
570}
571
572
5738.SIMPLE PARAMETER PASSING
574
575//Main Activity.java
576
577
578package com.example.multiplepages;
579
580
581import android.os.Bundle;
582import android.app.Activity;
583import android.content.Intent;
584import android.view.View;
585import android.widget.Button;
586import android.widget.EditText;
587import android.widget.Toast;
588
589public class MainActivity extends Activity {
590
591
592 EditText uname, pass;
593 Button blog, bclear;
594
595 @Override
596 protected void onCreate(Bundle savedInstanceState) {
597 super.onCreate(savedInstanceState);
598 setContentView(R.layout.activity_main);
599
600 uname=(EditText)findViewById(R.id.editText1);
601 pass=(EditText)findViewById(R.id.editText2);
602 blog=(Button)findViewById(R.id.button1);
603 bclear=(Button)findViewById(R.id.button2);
604
605 blog.setOnClickListener(new View.OnClickListener() {
606
607 @Override
608 public void onClick(View arg0) {
609 // TODO Auto-generated method stub
610
611 String na=uname.getText().toString();
612 String p=pass.getText().toString();
613 if(na.equals("admin") && p.equals("admin"))
614 {
615 Toast.makeText(getApplicationContext(), "Authenticated...", Toast.LENGTH_LONG).show();
616 Intent in=new Intent(getApplicationContext(),SecondActivity.class);
617 startActivity(in);
618 }
619 else
620 {
621 Toast.makeText(getApplicationContext(), "Un Authenticated...", Toast.LENGTH_LONG).show();
622 uname.setText("");
623 pass.setText("");
624 }
625
626 }
627 });
628 bclear.setOnClickListener(new View.OnClickListener() {
629
630 @Override
631 public void onClick(View arg0) {
632 // TODO Auto-generated method stub
633
634 uname.setText("");
635 pass.setText("");
636 }
637 });
638
639
640 }
641
642}
643
644
645Second Activity.java
646
647package com.example.multiplepages;
648import android.app.Activity;
649import android.content.Intent;
650import android.os.Bundle;
651import android.widget.Toast;
652
653
654public class SecondActivity extends Activity {
655
656
657 TextView v;
658
659 @Override
660 protected void onCreate(Bundle savedInstanceState) {
661 // TODO Auto-generated method stub
662 super.onCreate(savedInstanceState);
663 setContentView(R.layout.second_activity);
664 v.setText("Welcome");
665
666 }
667
668
669}
670
6719. CALCULATOR
672
673package com.example.addition;
674import android.os.Bundle;
675import android.app.Activity;
676 import android.view.Menu;
677import android.view.View;
678 import android.view.View.OnClickListener;
679 import android.widget.Button;
680import android.widget.EditText;
681 import android.widget.Toast;
682public class MainActivity extends Activity
683{
684private EditText edittext1,edittext2;
685private Button Btn_Add ;
private Button Btn_Sub ;
private Button Btn_Mul ;
686private Button Btn_Div ;
687@Override
688protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnButton();
689}
690public void addListenerOnButton()
691{
692edittext1=(EditText)findViewById(R.id.editText1); edittext2=(EditText)findViewById(R.id.editText2); Btn_Add=(Button)findViewById(R.id.button1); Btn_Sub=(Button)findViewById(R.id.button2); Btn_Mul=(Button)findViewById(R.id.button3); Btn_Div=(Button)findViewById(R.id.button4);
693Btn_Add.setOnClickListener(newOnClickListener(){
694@Override
695public void onClick(View view) {
String value1=edittext1.getText().toString();
696String value2=edittext2.getText().toString();
697int a=Integer.parseInt(value1);
int b=Integer.parseInt(value2);
int sum=a+b; Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG ).show();
698}
699});
700Btn_Sub.setOnClickListener(newOnClickListener(){
701@Override
702public void onClick(View view) {
String value1=edittext1.getText().toString();
703String value2=edittext2.getText().toString(); int a=Integer.parseInt(value1);
704int b=Integer.parseInt(value2);
int sub=a-b; Toast.makeText(getApplicationContext(),String.valueOf(sub),Toast.LENGTH_LONG ).show();
705} });
706Btn_Mul.setOnClickListener(newOnClickListener(){
707@Override
708public void onClick(View view) {
String value1=edittext1.getText().toString();
709String value2=edittext2.getText().toString(); int a=Integer.parseInt(value1);
710int b=Integer.parseInt(value2);
int m=a*b; Toast.makeText(getApplicationContext(),String.valueOf(m),Toast.LENGTH_LONG). show();
711} });
712Btn_Div.setOnClickListener(newOnClickListener(){
713@Override
714public void onClick(View view) {
String value1=edittext1.getText().toString();
715String value2=edittext2.getText().toString();
716 int a=Integer.parseInt(value1);
717int b=Integer.parseInt(value2);
718int n=a/b;
719 Toast.makeText(getApplicationContext(),String.valueOf(n),Toast.LENGTH_LONG).s how();
720}
721}); }
722@Override
723public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);
return true;
724} }
725
72610.Application with multiple activities
727
728MainActivity.java
729package com.example.multiplepagesapp;
730import android.os.Bundle;
731import android.app.Activity;
732import android.content.Intent;
733import android.view.Menu;
734import android.view.View;
735import android.widget.Button;
736public class MainActivity extends Activity {
737
738Intent in; Button b1;
739@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
740b1=(Button)findViewById(R.id.button1); b1.setOnClickListener(new View.OnClickListener() {
741}); }
742@Override
public void onClick(View arg0) {
743// TODO Auto-generated method stub
744in = new Intent(MainActivity.this, SeconActivity.class);
745startActivity(in);
746 }
747}
748SecondActivity.java
749packagecom.example.multiplepagesapp; importandroid.app.Activity;
750import android.os.Bundle;
public class SeconActivity extends Activity {
751@Override
protected void onCreate(Bundle savedInstanceState) {
752// TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second_layout);
753}
754 }