· 8 years ago · Apr 18, 2018, 06:32 PM
1Practical No: 1
2Design a simple calculator.
3
4XML code
5<?xml version="1.0" encoding="utf-8"?>
6<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
7 xmlns:app="http://schemas.android.com/apk/res-auto"
8 xmlns:tools="http://schemas.android.com/tools"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 tools:context="com.example.student.my_practicalno1.MainActivity">
12
13 <EditText
14 android:id="@+id/editText2"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_alignParentTop="true"
18 android:layout_centerHorizontal="true"
19 android:layout_marginTop="18dp"
20 android:ems="10"
21 android:inputType="number" />
22
23 <EditText
24 android:id="@+id/editText3"
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:layout_alignEnd="@+id/editText2"
28 android:layout_alignRight="@+id/editText2"
29 android:layout_below="@+id/editText2"
30 android:layout_marginTop="81dp"
31 android:ems="10"
32 android:inputType="number" />
33
34 <Button
35 android:id="@+id/button"
36 android:layout_width="wrap_content"
37 android:layout_height="wrap_content"
38 android:layout_alignLeft="@+id/editText3"
39 android:layout_alignParentBottom="true"
40 android:layout_alignStart="@+id/editText3"
41 android:layout_marginBottom="107dp"
42 android:onClick="ADD"
43 android:text="ADD" />
44
45 <Button
46 android:id="@+id/button2"
47 android:layout_width="wrap_content"
48 android:layout_height="wrap_content"
49 android:layout_alignBaseline="@+id/button"
50 android:layout_alignBottom="@+id/button"
51 android:layout_alignEnd="@+id/editText3"
52 android:layout_alignRight="@+id/editText3"
53 android:onClick="SUB"
54 android:text="SUB" />
55
56 <TextView
57 android:id="@+id/textView"
58 android:layout_width="wrap_content"
59 android:layout_height="wrap_content"
60 android:layout_above="@+id/button"
61 android:layout_centerHorizontal="true"
62 android:layout_marginBottom="18dp"
63 android:text="Result" />
64</RelativeLayout>
65
66MainActivity.java
67package com.example.student.my_practicalno1;
68
69import android.support.v7.app.AppCompatActivity;
70import android.os.Bundle;
71import android.view.View;
72import android.widget.EditText;
73import android.widget.TextView;
74
75public class MainActivity extends AppCompatActivity {
76
77 @Override
78 protected void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
80 setContentView(R.layout.activity_main);
81 }
82 public void ADD(View v)
83 {
84 EditText e1=(EditText)findViewById(R.id.editText2);
85 EditText e2=(EditText)findViewById(R.id.editText3);
86 TextView t1=(TextView)findViewById(R.id.textView);
87 int num1=Integer.parseInt(e1.getText().toString());
88 int num2=Integer.parseInt(e2.getText().toString());
89 int sum=num1+num2;
90 t1.setText(""+sum);
91 }
92 public void SUB(View v)
93 {
94 EditText e1=(EditText)findViewById(R.id.editText2);
95 EditText e2=(EditText)findViewById(R.id.editText3);
96 TextView t1=(TextView)findViewById(R.id.textView);
97 int num1=Integer.parseInt(e1.getText().toString());
98 int num2=Integer.parseInt(e2.getText().toString());
99 int sub=num1-num2;
100 t1.setText(""+sub);
101 }
102}
103
104Output:-
105Addition
106
107
108Subtraction:-
109
110Practical No:-2
111Using buttons, checkboxes on Mobile.
112
113XML Code
114<?xml version="1.0" encoding="utf-8"?>
115<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
116 xmlns:tools="http://schemas.android.com/tools"
117 xmlns:app="http://schemas.android.com/apk/res-auto"
118 android:layout_width="match_parent"
119 android:layout_height="match_parent"
120 tools:context="com.example.student.practical_no_2.MainActivity">
121
122 <CheckBox
123 android:id="@+id/checkBox4"
124 android:layout_width="wrap_content"
125 android:layout_height="wrap_content"
126 android:layout_alignLeft="@+id/checkBox6"
127 android:layout_alignParentTop="true"
128 android:layout_alignStart="@+id/checkBox6"
129 android:layout_marginTop="42dp"
130 android:text="Dog" />
131
132 <CheckBox
133 android:id="@+id/checkBox5"
134 android:layout_width="wrap_content"
135 android:layout_height="wrap_content"
136 android:layout_alignEnd="@+id/checkBox4"
137 android:layout_alignLeft="@+id/checkBox4"
138 android:layout_alignRight="@+id/checkBox4"
139 android:layout_alignStart="@+id/checkBox4"
140 android:layout_below="@+id/checkBox4"
141 android:layout_marginTop="38dp"
142 android:text="Cat" />
143
144 <CheckBox
145 android:id="@+id/checkBox6"
146 android:layout_width="wrap_content"
147 android:layout_height="wrap_content"
148 android:layout_below="@+id/checkBox5"
149 android:layout_centerHorizontal="true"
150 android:layout_marginTop="38dp"
151 android:text="Cow" />
152
153 <Button
154 android:id="@+id/button2"
155 android:layout_width="wrap_content"
156 android:layout_height="wrap_content"
157 android:layout_below="@+id/checkBox6"
158 android:layout_centerHorizontal="true"
159 android:layout_marginTop="63dp"
160 android:text="Select" />
161
162</RelativeLayout>
163
164
165
166MainActivity.java
167package com.example.student.practical_no_2;
168
169import android.support.v7.app.AppCompatActivity;
170import android.os.Bundle;
171import android.view.View;
172import android.widget.Button;
173import android.widget.CheckBox;
174import android.widget.Toast;
175
176public class MainActivity extends AppCompatActivity {
177
178 private CheckBox check1, check2, check3;
179 private Button button1;
180
181 @Override
182 protected void onCreate(Bundle savedInstanceState) {
183 super.onCreate(savedInstanceState);
184 setContentView(R.layout.activity_main);
185 addListenerOnButton();
186 }
187
188 private void addListenerOnButton() {
189 check1 = (CheckBox) findViewById(R.id.checkBox4);
190 check2 = (CheckBox) findViewById(R.id.checkBox5);
191 check3 = (CheckBox) findViewById(R.id.checkBox6);
192 button1 = (Button) findViewById(R.id.button2);
193 button1.setOnClickListener(new View.OnClickListener()
194 {
195 @Override
196 public void onClick(View V)
197
198 {
199 StringBuffer result = new StringBuffer();
200 result.append("Dog:" + check1.isChecked());
201 result.append("\nCat:" + check2.isChecked());
202 result.append("\nCow:" + check3.isChecked());
203 Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).
204 show();
205 }
206 });
207 }
208}
209
210
211Output:
212
213
214
215Practical No:-3
216
217Create a simple temperature converter application.
218
219
220
221
222
223XML Code
224
225<?xml version="1.0" encoding="utf-8"?>
226<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
227 xmlns:app="http://schemas.android.com/apk/res-auto"
228 xmlns:tools="http://schemas.android.com/tools"
229 android:id="@+id/layout1"
230 android:layout_width="match_parent"
231 android:layout_height="match_parent"
232 android:orientation="vertical"
233 tools:context="com.example.student.practicalno_3.MainActivity">
234
235 <EditText
236 android:id="@+id/editText"
237 android:layout_width="match_parent"
238 android:layout_height="wrap_content"
239 android:ems="10"
240 android:inputType="number" />
241
242 <TextView
243 android:id="@+id/textView"
244 android:layout_width="match_parent"
245 android:layout_height="wrap_content"
246 android:text="Result" />
247
248 <RadioGroup
249 android:layout_width="wrap_content"
250 android:layout_height="wrap_content">
251 <RadioButton
252 android:id="@+id/radioButton"
253 android:layout_width="match_parent"
254 android:layout_height="match_parent"
255 android:layout_weight="1"
256 android:text="celsius" />
257 <RadioButton
258 android:id="@+id/radioButton1"
259 android:layout_width="wrap_content"
260 android:layout_height="wrap_content"
261 android:layout_weight="1"
262 android:text="fahrenheit" />
263 </RadioGroup>
264
265<Button
266 android:layout_width="109dp"
267 android:layout_height="177dp"
268 android:layout_weight="0"
269 android:onClick="Convert"
270 android:text="Convert" />
271
272</LinearLayout>
273
274
275Java Code
276
277package com.example.student.practicalno_3;
278
279import android.graphics.Color;
280import android.support.v7.app.AppCompatActivity;
281import android.os.Bundle;
282import android.view.View;
283import android.widget.EditText;
284import android.widget.LinearLayout;
285import android.widget.RadioButton;
286import android.widget.TextView;
287
288public class MainActivity extends AppCompatActivity {
289
290 @Override
291 protected void onCreate(Bundle savedInstanceState) {
292 super.onCreate(savedInstanceState);
293 setContentView(R.layout.activity_main);
294 }
295 public void Convert(View v)
296 {
297 LinearLayout l1=(LinearLayout)findViewById(R.id.layout1);
298 TextView t1=(TextView)findViewById(R.id.textView);
299 EditText e1=(EditText)findViewById(R.id.editText);
300
301 double a=Double.parseDouble(String.valueOf(e1.getText()));
302 RadioButton cb=(RadioButton)findViewById(R.id.radioButton);
303 RadioButton fb=(RadioButton)findViewById(R.id.radioButton1);
304
305 if (cb.isChecked())
306 {
307 l1.setBackgroundColor(Color.GRAY);
308 t1.setText(f2c(a)+" degree C");
309 fb.setChecked(true);
310 }
311 else
312 {
313 l1.setBackgroundColor(Color.LTGRAY);
314 t1.setText(c2f(a)+" degree F");
315 fb.setChecked(true);
316
317 }
318 }
319 private double c2f(double c)
320 {
321 return(c*9)/5+32;
322 }
323 private double f2c(double f)
324 {
325 return (f-32)*5/9;
326 }
327}
328
329
330Output
331
332
333Practical No:-4
334Program to generate Calendar.
335
336
337
338
339XML code
340<?xml version="1.0" encoding="utf-8"?>
341<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
342 xmlns:app="http://schemas.android.com/apk/res-auto"
343 xmlns:tools="http://schemas.android.com/tools"
344 android:layout_width="match_parent"
345 android:layout_height="match_parent"
346 tools:context="com.example.student.practicalno_4_mc.MainActivity">
347
348 <CalendarView
349 android:id="@+id/calendarView"
350 android:layout_width="match_parent"
351 android:layout_height="match_parent"
352 android:layout_alignParentLeft="true"
353 android:layout_alignParentStart="true"
354 android:layout_alignParentTop="true"
355 android:firstDayOfWeek="2"
356 android:maxDate="01/01/2022"
357 android:minDate="01/01/2016"
358 android:layout_marginLeft="23dp"
359 android:layout_marginStart="23dp"
360 android:layout_marginTop="47dp" />
361</RelativeLayout>
362
363
364Java Code
365
366package com.example.student.practicalno_4_mc;
367
368 import android.graphics.Color;
369 import android.support.annotation.NonNull;
370 import android.support.v7.app.AppCompatActivity;
371 import android.os.Bundle;
372 import android.widget.CalendarView;
373 import android.widget.Toast;
374
375public class MainActivity extends AppCompatActivity
376{
377 CalendarView simplecalendarview;
378
379 @Override
380 protected void onCreate(Bundle savedInstanceState) {
381 super.onCreate(savedInstanceState);
382 setContentView(R.layout.activity_main);
383
384 simplecalendarview = (CalendarView) findViewById(R.id.calendarView);
385 simplecalendarview.setFocusedMonthDateColor(Color.BLUE);
386 simplecalendarview.setWeekSeparatorLineColor(Color.GREEN);
387
388 simplecalendarview.setOnDateChangeListener(new CalendarView.OnDateChangeListener()
389 {
390 @Override
391 public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth)
392 {
393 Toast.makeText(getApplicationContext(),dayOfMonth+"/"+month+"/"+year,Toast.LENGTH_LONG).show();
394 }
395 });
396 }
397}
398
399
400Output:-
401
402
403
404Practical No:-5
405Design a simple to-do list.
406
407
408
409
410
411XML Code
412<?xml version="1.0" encoding="utf-8"?>
413<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
414 xmlns:app="http://schemas.android.com/apk/res-auto"
415 xmlns:tools="http://schemas.android.com/tools"
416 android:layout_width="match_parent"
417 android:layout_height="match_parent"
418 tools:context="com.example.student.practical_no_5_mc.MainActivity">
419
420 <ListView
421 android:id="@+id/lv"
422 android:layout_width="match_parent"
423 android:layout_height="match_parent">
424 </ListView>
425
426</LinearLayout>
427
428Java Code
429package com.example.student.practical_no_5_mc;
430
431import android.support.v7.app.AppCompatActivity;
432import android.os.Bundle;
433import android.view.View;
434import android.widget.AdapterView;
435import android.widget.ArrayAdapter;
436import android.widget.ListView;
437import android.widget.Toast;
438
439public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
440 String values[]= {"January", "July", "June", "Ravi", "Ravinder", "Abhishek", "Abhinav", "Harkishore", "Hardev"};
441
442
443 @Override
444 protected void onCreate(Bundle savedInstanceState) {
445 super.onCreate(savedInstanceState);
446 setContentView(R.layout.activity_main);
447 ListView lv = (ListView) findViewById(R.id.lv);
448 ArrayAdapter<?> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, values);
449 lv.setAdapter(adapter);
450 lv.setOnItemClickListener(this);
451 }
452
453
454 @Override
455 public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
456 String name =(String)av.getItemAtPosition(pos);
457 Toast.makeText(getApplicationContext(), "Welcome" +name, Toast.LENGTH_SHORT).show();
458 }
459}
460
461
462Output:-
463
464
465
466Practical No-6_MC
467Program to insert and display data from.
468
469
470
471
472XML Code
473<?xml version="1.0" encoding="utf-8"?>
474<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
475 xmlns:app="http://schemas.android.com/apk/res-auto"
476 xmlns:tools="http://schemas.android.com/tools"
477 android:layout_width="match_parent"
478 android:layout_height="match_parent"
479 tools:context="com.example.student.practicalno_6_mc.MainActivity">
480
481 <EditText
482 android:id="@+id/editText"
483 android:layout_width="wrap_content"
484 android:layout_height="wrap_content"
485 android:layout_alignParentTop="true"
486 android:layout_centerHorizontal="true"
487 android:maxLines="1"
488 android:hint="First Name"
489 android:layout_marginTop="28dp"
490 android:ems="10"
491 android:inputType="textPersonName" />
492
493 <EditText
494 android:id="@+id/editText2"
495 android:layout_width="wrap_content"
496 android:layout_height="wrap_content"
497 android:layout_alignEnd="@+id/editText"
498 android:layout_below="@+id/editText"
499 android:maxLines="1"
500 android:hint="Last Name"
501 android:ems="10"
502 android:inputType="textPersonName" />
503
504 <Button
505 android:id="@+id/button"
506 android:layout_width="wrap_content"
507 android:layout_height="wrap_content"
508 android:layout_alignStart="@+id/editText2"
509 android:layout_below="@+id/editText2"
510 android:layout_marginStart="51dp"
511 android:text="Insert Values"
512 android:onClick="insert"/>
513
514 <Button
515 android:id="@+id/button2"
516 android:layout_width="wrap_content"
517 android:layout_height="wrap_content"
518 android:layout_alignStart="@+id/button"
519 android:layout_below="@+id/button"
520 android:layout_marginTop="27dp"
521 android:onClick="display"
522 android:text="Display Values" />
523 <ScrollView
524 android:id="@+id/scrollView1"
525 android:layout_width="match_parent"
526 android:layout_height="wrap_content"
527 android:layout_alignParentBottom="true"
528 android:layout_alignParentLeft="true"
529 android:layout_alignParentRight="true"
530 android:layout_below="@+id/button2">
531 <LinearLayout
532 android:layout_width="match_parent"
533 android:layout_height="match_parent">
534 android:orientation="vertical">
535 <TextView
536 android:id="@+id/textview"
537 android:layout_width="fill_parent"
538 android:layout_height="fill_parent"
539 android:textSize="20sp"
540 tools:ignore="InvalidId" />
541
542 </LinearLayout>
543
544 </ScrollView>
545
546</RelativeLayout>
547
548
549
550Java Code
551package com.example.student.practicalno_6_mc;
552
553import android.database.Cursor;
554import android.database.sqlite.SQLiteDatabase;
555import android.support.v7.app.AppCompatActivity;
556import android.os.Bundle;
557import android.view.View;
558import android.widget.EditText;
559import android.widget.TextView;
560import android.widget.Toast;
561
562public class MainActivity extends AppCompatActivity {
563 SQLiteDatabase db;
564 TextView tv;
565 EditText et1, et2;
566
567 @Override
568 protected void onCreate(Bundle savedInstanceState) {
569 super.onCreate(savedInstanceState);
570 setContentView(R.layout.activity_main);
571 tv = (TextView)findViewById(R.id.textview);
572 et1 = (EditText)findViewById(R.id.editText);
573 et2 = (EditText)findViewById(R.id.editText2);
574 db = openOrCreateDatabase("Mydb", MODE_PRIVATE,null);
575 db.execSQL("create table if not exists mytable(name varchar, sur_name)");
576 }
577 public void insert(View v)
578 {
579 String name = et1.getText().toString();
580 String last_name = et2.getText().toString();
581 et1.setText("");
582 et2.setText("");
583 db.execSQL("insert into mytable values('"+name+" ','"+last_name+"')");
584 Toast.makeText(this, "values inserted successfully.", Toast.LENGTH_LONG).show();
585 }
586 public void display(View v)
587 {
588 Cursor c = db.rawQuery("select * from mytable", null);
589 tv.setText("");
590 c.moveToFirst();
591 do {
592 {
593 String fname = c.getString(c.getColumnIndex("name"));
594 String lname = c.getString(1);
595 tv.append("First Name: " +fname+ "Last Name:" +lname+ "\n");
596
597 }
598
599 }while(c.moveToNext());
600 }
601}
602Output:-
603
604Practical No_MC:7
605Create a program to open camera.
606
607XML Document
608<?xml version="1.0" encoding="utf-8"?>
609<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
610 xmlns:app="http://schemas.android.com/apk/res-auto"
611 xmlns:tools="http://schemas.android.com/tools"
612 android:layout_width="match_parent"
613 android:layout_height="match_parent"
614 tools:context="com.example.student.practicalno_7_camera.MainActivity">
615
616 <Button
617 android:id="@+id/button"
618 android:layout_width="wrap_content"
619 android:layout_height="wrap_content"
620 android:layout_alignParentBottom="true"
621 android:layout_centerHorizontal="true"
622 android:layout_marginBottom="91dp"
623 android:onClick="takephoto"
624 android:text="Take A Photo" />
625
626 <ImageView
627 android:id="@+id/imageView2"
628 android:layout_width="wrap_content"
629 android:layout_height="wrap_content"
630 android:layout_above="@+id/button"
631 android:layout_centerHorizontal="true"
632 android:layout_marginBottom="144dp"
633 app:srcCompat="@android:drawable/alert_light_frame" />
634
635</RelativeLayout>
636
637
638Java Code
639package com.example.student.practicalno_7_camera;
640
641import android.content.Intent;
642import android.graphics.Bitmap;
643import android.provider.MediaStore;
644import android.support.v7.app.AppCompatActivity;
645import android.os.Bundle;
646import android.view.View;
647import android.widget.ImageView;
648
649public class MainActivity extends AppCompatActivity {
650
651 private static final int REQUEST_CODE=1;
652 ImageView imageView;
653
654 @Override
655 protected void onCreate(Bundle savedInstanceState) {
656 super.onCreate(savedInstanceState);
657 setContentView(R.layout.activity_main);
658
659 imageView=(ImageView)findViewById(R.id.imageView2);
660 }
661
662 public void takephoto(View v)
663 {
664 Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
665 startActivityForResult(cameraIntent,REQUEST_CODE);
666 }
667 @Override
668 protected void onActivityResult(int requestCode,int resultCode,Intent data)
669 {
670 super.onActivityResult(requestCode,resultCode,data);
671 if (requestCode==REQUEST_CODE && resultCode==RESULT_OK && data!=null)
672 {
673 Bitmap photo=(Bitmap)data.getExtras().get("data");
674 imageView.setImageBitmap(photo);
675 }
676 }
677}
678
679
680Output:-
681
682
683
684Practical No8_MC
685Create a program to pick date and time.
686
687
688
689
690Fragment Creation Process (Create two more fragments)
691
692
693
694XML Code:
695<?xml version="1.0" encoding="utf-8"?>
696<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
697 xmlns:app="http://schemas.android.com/apk/res-auto"
698 xmlns:tools="http://schemas.android.com/tools"
699 android:layout_width="match_parent"
700 android:layout_height="match_parent"
701 tools:context="com.example.student.practical_8_dm.MainActivity">
702
703 <RelativeLayout
704 android:layout_width="match_parent"
705 android:layout_height="match_parent">
706
707 <Button
708 android:id="@+id/button_date"
709 android:layout_width="wrap_content"
710 android:layout_height="wrap_content"
711 android:layout_alignBaseline="@+id/button_time"
712 android:layout_alignBottom="@+id/button_time"
713 android:layout_alignParentEnd="true"
714 android:layout_alignParentRight="true"
715 android:layout_marginEnd="43dp"
716 android:layout_marginRight="43dp"
717 android:onClick="showDatePickerDialog"
718 android:text="date_button" />
719
720 <Button
721 android:id="@+id/button_time"
722 android:layout_width="wrap_content"
723 android:layout_height="wrap_content"
724
725 android:layout_alignParentLeft="true"
726 android:layout_alignParentStart="true"
727 android:layout_alignParentTop="true"
728 android:layout_marginLeft="36dp"
729 android:layout_marginStart="36dp"
730 android:layout_marginTop="127dp"
731 android:onClick="showTimePickerDialog"
732 android:text="time_button" />
733
734 <TextView
735 android:id="@+id/textView"
736 android:layout_width="wrap_content"
737 android:layout_height="81dp"
738 android:layout_below="@+id/button_date"
739 android:layout_centerHorizontal="true"
740 android:layout_marginTop="125dp"
741 android:text="choose_datetime" />
742
743 </RelativeLayout>
744
745</LinearLayout>
746
747
748
749MainActivity.java
750package com.example.student.practical_8_dm;
751
752import android.support.v4.app.DialogFragment;
753import android.support.v7.app.AppCompatActivity;
754import android.os.Bundle;
755import android.view.View;
756import android.widget.Toast;
757
758public class MainActivity extends AppCompatActivity {
759
760 @Override
761 protected void onCreate(Bundle savedInstanceState) {
762 super.onCreate(savedInstanceState);
763 setContentView(R.layout.activity_main);
764 }
765 public void showDatePickerDialog(View v) {
766 DialogFragment newFragment = new DatePickerFragment();
767 newFragment.show(getSupportFragmentManager(),
768 getString(R.string.date_button));
769 }
770 public void showTimePickerDialog(View v) {
771 DialogFragment newFragment = new TimePickerFragment();
772 newFragment.show(getSupportFragmentManager(),
773 getString(R.string.time_button));
774 }
775
776
777 public void processDatePickerResult(int year, int month, int day){
778 String month_string = Integer.toString(month+1);
779 String day_string = Integer.toString(day);
780 String year_string = Integer.toString(year);
781 String dateMessage = (month_string + "/" +
782 day_string + "/" + year_string);
783 Toast.makeText(this, "Date: " + dateMessage,
784 Toast.LENGTH_SHORT).show();
785 }
786 public void processTimePickerResult(int hourOfDay, int minute) {
787 String hour_string = Integer.toString(hourOfDay);
788 String minute_string = Integer.toString(minute);
789 String timeMessage = (hour_string + ":" + minute_string);
790 Toast.makeText(this, "Time: " + timeMessage,
791 Toast.LENGTH_SHORT).show();
792 }
793}
794
795TimePickerFragment.java
796package com.example.student.practical_8_dm;
797
798
799import android.app.Dialog;
800import android.app.TimePickerDialog;
801import android.os.Bundle;
802import android.support.annotation.NonNull;
803import android.support.v4.app.DialogFragment;
804import android.support.v4.app.Fragment;
805import android.text.format.DateFormat;
806import android.view.LayoutInflater;
807import android.view.View;
808import android.view.ViewGroup;
809import android.widget.TextView;
810import android.widget.TimePicker;
811import android.widget.Toast;
812
813import java.util.Calendar;
814
815/**
816 * A simple {@link Fragment} subclass.
817 */
818public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
819
820
821 public TimePickerFragment() {
822 // Required empty public constructor
823 }
824
825 @NonNull
826 @Override
827 public Dialog onCreateDialog(Bundle savedInstanceState) {
828 final Calendar c = Calendar.getInstance();
829 int hour = c.get(Calendar.HOUR_OF_DAY);
830 int minute = c.get(Calendar.MINUTE);
831
832 // Create a new instance of TimePickerDialog and return it.
833 return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
834 }
835
836 @Override
837 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
838 MainActivity activity = (MainActivity) getActivity();
839 // Invoke Main Activity's processTimePickerResult() method.
840 activity.processTimePickerResult(hourOfDay, minute);
841 }
842}
843
844DatePickerFragment.java
845
846package com.example.student.practical_8_dm;
847
848
849import android.app.DatePickerDialog;
850import android.app.Dialog;
851import android.os.Bundle;
852import android.support.annotation.NonNull;
853import android.support.v4.app.DialogFragment;
854import android.support.v4.app.Fragment;
855import android.view.LayoutInflater;
856import android.view.View;
857import android.view.ViewGroup;
858import android.widget.DatePicker;
859import android.widget.TextView;
860
861import java.util.Calendar;
862
863/**
864 * A simple {@link Fragment} subclass.
865 */
866public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
867
868 @NonNull
869 @Override
870 public Dialog onCreateDialog(Bundle savedInstanceState) {
871 final Calendar c = Calendar.getInstance();
872 int year = c.get(Calendar.YEAR);
873 int month = c.get(Calendar.MONTH);
874 int day = c.get(Calendar.DAY_OF_MONTH);
875
876 // Create a new instance of DatePickerDialog and return it.
877 return new DatePickerDialog(getActivity(), this, year, month,day);
878 }
879
880 @Override
881 public void onDateSet(DatePicker view, int year, int month, int day) {
882 MainActivity activity = (MainActivity) getActivity();
883 // Invoke Main Activity's processDatePickerResult() method.
884 activity.processDatePickerResult(year, month, day);
885 }
886}
887Output:-
888
889
890
891Practical No:-9
892Program to demonstrate simple Animation.
893
894
895
896
897New layout
898
899
900Create new class
901
902
903
904Activity_main.xml
905<?xml version="1.0" encoding="utf-8"?>
906<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
907 xmlns:app="http://schemas.android.com/apk/res-auto"
908 xmlns:tools="http://schemas.android.com/tools"
909 android:layout_width="match_parent"
910 android:layout_height="match_parent"
911 tools:context="com.example.student.practical_no9_mc.MainActivity">
912
913 <android.support.v4.view.ViewPager
914 android:layout_width="wrap_content"
915 android:layout_height="wrap_content"
916 android:text="Hello World"
917 android:id="@+id/vp"
918 android:layout_alignParentTop="true"
919 android:layout_alignParentStart="true">
920 </android.support.v4.view.ViewPager>
921
922</RelativeLayout>
923
924swipelayout.xml
925<?xml version="1.0" encoding="utf-8"?>
926<RelativeLayout
927 xmlns:android="http://schemas.android.com/apk/res/android"
928 xmlns:app="http://schemas.android.com/apk/res-auto"
929 android:layout_width="match_parent"
930 android:layout_height="match_parent">
931 <TextView
932 android:id="@+id/textView"
933 android:layout_width="wrap_content"
934 android:layout_height="wrap_content"
935 android:text="Hello world"
936 android:textSize="25sp"
937 android:layout_alignParentTop="true"
938 android:layout_centerHorizontal="true"
939 android:layout_marginTop="35dp" />
940 <ImageView
941 android:id="@+id/iv"
942 android:layout_width="300sp"
943 android:layout_height="300sp"
944 android:layout_centerHorizontal="true"
945 android:layout_centerVertical="true" />
946</RelativeLayout>
947
948MainActivity.java
949package com.example.student.practical_no9_mc;
950
951import android.support.v4.view.ViewPager;
952import android.support.v7.app.AppCompatActivity;
953import android.os.Bundle;
954
955public class MainActivity extends AppCompatActivity {
956 ViewPager viewPager;
957 CustomPager adapter;
958 @Override
959 protected void onCreate(Bundle savedInstanceState) {
960 super.onCreate(savedInstanceState);
961 setContentView(R.layout.activity_main);
962 viewPager=(ViewPager)findViewById(R.id.vp);
963 adapter=new CustomPager(this);
964 viewPager.setAdapter(adapter);
965 }
966}
967
968CustomPager.java
969package com.example.student.practical_no9_mc;
970
971/**
972 * Created by student on 3/31/2018.
973 */
974
975import android.animation.ObjectAnimator;
976import android.content.Context;
977import android.support.v4.view.PagerAdapter;
978import android.view.LayoutInflater;
979import android.view.View;
980import android.view.ViewGroup;
981import android.widget.ImageView;
982import android.widget.TextView;
983
984
985public class CustomPager extends PagerAdapter {
986 private int[] image_resources={R.drawable.a,R.drawable.b,R.drawable.c};
987 private Context ctx;
988 private LayoutInflater layoutInflater;
989 public CustomPager(Context ctx)
990 {
991 this.ctx=ctx;
992 }
993
994 @Override
995 public int getCount() {
996 return image_resources.length;
997 }
998
999 @Override
1000 public boolean isViewFromObject(View view, Object object) {
1001 return view==object;
1002 }
1003
1004 @Override
1005 public Object instantiateItem(ViewGroup conatiner,int position){
1006 layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1007 View item_view=layoutInflater.inflate(R.layout.swipelayout,conatiner,false);
1008 ImageView iv=(ImageView)item_view.findViewById(R.id.iv);
1009 TextView tv=(TextView)item_view.findViewById(R.id.textView);
1010 iv.setImageResource(image_resources[position]);
1011 tv.setText("Image: "+position);
1012 conatiner.addView(item_view);
1013 return item_view;
1014 }
1015
1016 @Override
1017 public void destroyItem(ViewGroup container, int position,Object object){
1018 container.removeView((View) object);
1019 }
1020}
1021
1022Output:
1023
1024
1025Practical No:-10_MC
1026
1027
1028
1029
1030Create new layout
1031
1032
1033
1034first_layout.xml
1035
1036<?xml version="1.0" encoding="utf-8"?>
1037<RelativeLayout
1038 xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
1039 android:layout_height="match_parent">
1040
1041 <TextView
1042 android:id="@+id/TextView"
1043 android:layout_width="wrap_content"
1044 android:layout_height="wrap_content"
1045 android:layout_alignParentStart="true"
1046 android:layout_alignParentTop="true"
1047 android:layout_marginStart="130dp"
1048 android:layout_marginTop="180dp"
1049 android:text="First Layout" />
1050</RelativeLayout>
1051
1052
1053second_layout.xml
1054
1055<?xml version="1.0" encoding="utf-8"?>
1056<RelativeLayout
1057 xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
1058 android:layout_height="match_parent">
1059
1060 <TextView
1061 android:id="@+id/TextView"
1062 android:layout_width="wrap_content"
1063 android:layout_height="wrap_content"
1064 android:layout_alignParentStart="true"
1065 android:layout_alignParentTop="true"
1066 android:layout_marginStart="130dp"
1067 android:layout_marginTop="180dp"
1068 android:text="Second Layout" />
1069</RelativeLayout>
1070
1071third_layout.xml
1072
1073<?xml version="1.0" encoding="utf-8"?>
1074<RelativeLayout
1075 xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
1076 android:layout_height="match_parent">
1077
1078 <TextView
1079 android:id="@+id/TextView"
1080 android:layout_width="wrap_content"
1081 android:layout_height="wrap_content"
1082 android:layout_alignParentStart="true"
1083 android:layout_alignParentTop="true"
1084 android:layout_marginStart="130dp"
1085 android:layout_marginTop="180dp"
1086 android:text="Third Layout" />
1087
1088</RelativeLayout>
1089
1090activity_main.xml
1091<?xml version="1.0" encoding="utf-8"?>
1092<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
1093 xmlns:app="http://schemas.android.com/apk/res-auto"
1094 xmlns:tools="http://schemas.android.com/tools"
1095 android:id="@+id/drawer_layout"
1096 android:layout_width="match_parent"
1097 android:layout_height="match_parent"
1098 android:fitsSystemWindows="true"
1099 tools:openDrawer="start">
1100 <include
1101 layout="@layout/app_bar_main"
1102 android:layout_width="match_parent"
1103 android:layout_height="match_parent" />
1104 <android.support.design.widget.NavigationView
1105 android:id="@+id/nav_view"
1106 android:layout_width="wrap_content"
1107 android:layout_height="match_parent"
1108 android:layout_gravity="start"
1109 android:fitsSystemWindows="true"
1110 app:headerLayout="@layout/nav_header_main"
1111 app:menu="@menu/activity_main_drawer" />
1112</android.support.v4.widget.DrawerLayout>
1113
1114MainActivity.java
1115package com.example.student.practicalno_10_mc;
1116
1117import android.os.Bundle;
1118import android.support.design.widget.FloatingActionButton;
1119import android.support.design.widget.Snackbar;
1120import android.support.v4.app.FragmentManager;
1121import android.view.View;
1122import android.support.design.widget.NavigationView;
1123import android.support.v4.view.GravityCompat;
1124import android.support.v4.widget.DrawerLayout;
1125import android.support.v7.app.ActionBarDrawerToggle;
1126import android.support.v7.app.AppCompatActivity;
1127import android.support.v7.widget.Toolbar;
1128import android.view.Menu;
1129import android.view.MenuItem;
1130
1131public class MainActivity extends AppCompatActivity
1132 implements NavigationView.OnNavigationItemSelectedListener {
1133
1134 @Override
1135 protected void onCreate(Bundle savedInstanceState) {
1136 super.onCreate(savedInstanceState);
1137 setContentView(R.layout.activity_main);
1138 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
1139 setSupportActionBar(toolbar);
1140
1141 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
1142 fab.setOnClickListener(new View.OnClickListener() {
1143 @Override
1144 public void onClick(View view) {
1145 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
1146 .setAction("Action", null).show();
1147 }
1148 });
1149
1150 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
1151 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
1152 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
1153 drawer.addDrawerListener(toggle);
1154 toggle.syncState();
1155
1156 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
1157 navigationView.setNavigationItemSelectedListener(this);
1158 }
1159
1160 @Override
1161 public void onBackPressed() {
1162 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
1163 if (drawer.isDrawerOpen(GravityCompat.START)) {
1164 drawer.closeDrawer(GravityCompat.START);
1165 } else {
1166 super.onBackPressed();
1167 }
1168 }
1169
1170 @Override
1171 public boolean onCreateOptionsMenu(Menu menu) {
1172 // Inflate the menu; this adds items to the action bar if it is present.
1173 getMenuInflater().inflate(R.menu.main, menu);
1174 return true;
1175 }
1176
1177 @Override
1178 public boolean onOptionsItemSelected(MenuItem item) {
1179 // Handle action bar item clicks here. The action bar will
1180 // automatically handle clicks on the Home/Up button, so long
1181 // as you specify a parent activity in AndroidManifest.xml.
1182 int id = item.getItemId();
1183
1184 //noinspection SimplifiableIfStatement
1185 if (id == R.id.action_settings) {
1186 return true;
1187 }
1188
1189 return super.onOptionsItemSelected(item);
1190 }
1191
1192 @SuppressWarnings("StatementWithEmptyBody")
1193 @Override
1194 public boolean onNavigationItemSelected(MenuItem item) {
1195 // Handle navigation view item clicks here.
1196 int id = item.getItemId();
1197
1198 FragmentManager fragmentManager =getSupportFragmentManager();
1199
1200 if (id == R.id.nav_first_layout) {
1201 fragmentManager.beginTransaction()
1202 .replace(R.id.content_frame, new FirstFragment())
1203 .commit();
1204
1205 // Handle the camera action
1206 } else if (id == R.id.nav_second_layout) {
1207 fragmentManager.beginTransaction()
1208 .replace(R.id.content_frame, new SecondFragment())
1209 .commit();
1210 } else if (id == R.id.nav_third_layout) {
1211 fragmentManager.beginTransaction()
1212 .replace(R.id.content_frame, new ThirdFragment())
1213 .commit();
1214
1215 } else if (id == R.id.nav_share) {
1216
1217 } else if (id == R.id.nav_send) {
1218
1219 }
1220 /* switch (item.getItemId()){
1221 case R.id.nav_first_layout:
1222 fragmentManager.beginTransaction()
1223 .replace(R.id.content_frame, new FirstFragment())
1224 .commit();
1225 break;
1226 case R.id.nav_second_layout:
1227
1228 }*/
1229
1230 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
1231 drawer.closeDrawer(GravityCompat.START);
1232 return true;
1233 }
1234}
1235FirstFragment.java
1236package com.example.student.practicalno_10_mc;
1237
1238import android.os.Bundle;
1239import android.support.annotation.Nullable;
1240import android.support.v4.app.Fragment;
1241import android.view.LayoutInflater;
1242import android.view.View;
1243import android.view.ViewGroup;
1244
1245
1246public class FirstFragment extends Fragment {
1247 View myView;
1248 @Nullable
1249 @Override
1250 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
1251 myView = inflater.inflate(R.layout.first_layout,container,false);
1252 return myView;
1253 }
1254}
1255
1256SecondFragment.java
1257package com.example.student.practicalno_10_mc;
1258
1259import android.os.Bundle;
1260import android.support.annotation.Nullable;
1261import android.support.v4.app.Fragment;
1262import android.view.LayoutInflater;
1263import android.view.View;
1264import android.view.ViewGroup;
1265
1266
1267public class SecondFragment extends Fragment {
1268 View myView;
1269 @Nullable
1270 @Override
1271 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
1272 myView = inflater.inflate(R.layout.second_layout,container,false);
1273 return myView;
1274 }
1275}
1276
1277ThirdFragment.java
1278package com.example.student.practicalno_10_mc;
1279
1280import android.support.v4.app.Fragment;
1281import android.os.Bundle;
1282import android.support.annotation.Nullable;
1283import android.support.v4.app.Fragment;
1284import android.view.LayoutInflater;
1285import android.view.View;
1286import android.view.ViewGroup;
1287
1288
1289public class ThirdFragment extends Fragment {
1290 View myView;
1291 @Nullable
1292 @Override
1293 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
1294 myView = inflater.inflate(R.layout.third_layout,container,false);
1295 return myView;
1296 }
1297}
1298
1299
1300
1301Output:-