0% found this document useful (0 votes)
5 views

android

The document outlines practical exercises for Android app development using Android Studio, including installation instructions and programming examples. It covers various components like Activity Lifecycle, ListView, DatePicker, and Option Menus, providing XML layouts and Java code for each. The document serves as a guide for students to learn and implement basic Android functionalities.

Uploaded by

ashuvish70
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

android

The document outlines practical exercises for Android app development using Android Studio, including installation instructions and programming examples. It covers various components like Activity Lifecycle, ListView, DatePicker, and Option Menus, providing XML layouts and Java code for each. The document serves as a guide for students to learn and implement basic Android functionalities.

Uploaded by

ashuvish70
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

C.S.J.M. Govt.

Polytechnic Ambedkar
Nagar Pin Code - 224122
-----------------------------------------------------

BRANCH – Computer Science & Engg.


Enroll No. – E18220735500011

Vashu Sir Ashutosh Vishwakarma


Practical NO. – 1

Object - To Install the Android Studio and Setup the Development


Environment.

Introduction :-

Android studio provides the fastest tools for building apps on every
type of android device.

Android studio is an integrated development environment(IDE) for


android app development , based on IntelliJIDEA(a unified environment
where one can develop apps for all android devices).

PhoneGap is one of the most used android app frameworks.

Android Studio was developed by Google ,JetBrains.


Features of Android Studio:-

 Gradle based build support.


 Proguard integration and app signing capabilities.
 Support for building Android wear apps.
 Built-in support for Google Cloud Platforms
 AVD(android virtual device) to debug and run the apps

Version History of Android Studio:-

Version Release Date

1.0 Dec 2014


2.0 Apr 2016
3.0 Oct 2017
4.0 May 2020

Installation of Android Studio(In Windows):-

To install android studio –

1) First download the .exe file.


2) After downloading the .exe file ,double click on it to launch it.
3) Follow the set-up wizard in android studio and install any SDK
package that is recommended .
Practical No. – 2

Object:- Write a program to demonstrate activity (Application Life


Cycle).

Activity :- An activity is a touchable component, user interacts with it by


using touch screen.

Steps to create the first Android program:-

1. Create the new android project


2. Write the message (optional)
3. Run the android application

Hello Android Program-

You need to follow the 3 steps mentioned above for creating the
Hello android application.

1) Create the New Android project

For creating the new android studio project:

1) Select Start a new Android Studio project

Features of Java - Javatpoint


2) Provide the following information: Application name, Company
domain, Project location and Package name of application and
click next.
3) Select the API level of application and click next.
4) Select the Activity type (Empty Activity).
5) Provide the Activity Name and click finish.
activity_main.xml

1. <?xml version="1.0" encoding="utf-8"?>


2. <android.support.constraint.ConstraintLayout xmlns:android="htt
p://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="first.javatpoint.com.welcome.MainActivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:text="Hello Android!"
13. app:layout_constraintBottom_toBottomOf="parent"
14. app:layout_constraintLeft_toLeftOf="parent"
15. app:layout_constraintRight_toRightOf="parent"
16. app:layout_constraintTop_toTopOf="parent" />
17.
18. </android.support.constraint.ConstraintLayout>
19. }

MainActivity.java

1. package first.javatpoint.com.welcome;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5.
6. public class MainActivity extends AppCompatActivity {
7. @Override
8. protected void onCreate(Bundle savedInstanceState) {
9. super.onCreate(savedInstanceState);
10. setContentView(R.layout.activity_main);
11. }
12. }

Run the android application

.
Practical No. - 3

Object:- Write a program to demonstrate the list view.

Introductin:-

Android ListView is a view which contains the group of items and


displays in a scrollable list. ListView is implemented by
importing android.widget.ListView class. ListView is a default
scrollable which does not use other scroll view.

Program for List View:-

activity_main.xml

activity_main.xml

1. <?xml version="1.0" encoding="utf-8"?>


2. <android.support.constraint.ConstraintLayout xmlns:android="
http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="listview.example.com.listview.MainActivity">
8.
9. <ListView
10. android:id="@+id/listView"
11. android:layout_width="match_parent"
12. android:layout_height="fill_parent"
13. />
14. </android.support.constraint.ConstraintLayout>
mylist.xml

1. <?xml version="1.0" encoding="utf-8"?>


2.
3. <TextView xmlns:android="http://schemas.android.com/apk/res/
android"
4. android:id="@+id/textView"
5. android:layout_width="wrap_content"
6. android:layout_height="wrap_content"
7. android:text="Medium Text"
8. android:textStyle="bold"
9. android:textAppearance="?android:attr/textAppearanceMediu
m"
10. android:layout_marginLeft="10dp"
11. android:layout_marginTop="5dp"
12. android:padding="2dp"
13. android:textColor="#4d4d4d"
14. />

strings.xml

1. <resources>
2. <string name="app_name">ListView</string>
3. <string-array name="array_technology">
4. <item>Android</item>
5. <item>Java</item>
6. <item>Php</item>
7. <item>Hadoop</item>
8. <item>Sap</item>
9. <item>Python</item>
10. <item>Ajax</item>
11. <item>C++</item>
12. <item>Ruby</item>
13. <item>Rails</item>
14. <item>.Net</item>
15. <item>Perl</item>
16. </string-array>
17. </resources>

MainActivity.java

1. package listview.example.com.listview;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5. import android.view.View;
6. import android.widget.AdapterView;
7. import android.widget.ArrayAdapter;
8. import android.widget.ListView;
9. import android.widget.TextView;
10. import android.widget.Toast;
11.
12. public class MainActivity extends AppCompatActivity {
13. ListView listView;
14. TextView textView;
15. String[] listItem;
16. @Override
17. protected void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.activity_main);
20.
21. listView=(ListView)findViewById(R.id.listView);
22. textView=(TextView)findViewById(R.id.textView);
23. listItem = getResources().getStringArray(R.array.array_te
chnology);
24. final ArrayAdapter<String> adapter = new ArrayAdapt
er<String>(this,
25. android.R.layout.simple_list_item_1, android.R.id.tex
t1, listItem);
26. listView.setAdapter(adapter);
27.
28. listView.setOnItemClickListener(new AdapterView.OnIte
mClickListener() {
29. @Override
30. public void onItemClick(AdapterView<?> adapterVie
w, View view, int position, long l) {
31. // TODO Auto-generated method stub
32. String value=adapter.getItem(position);
33. Toast.makeText(getApplicationContext(),value,Toas
t.LENGTH_SHORT).show();
34.
35. }
36. });
37. }
38. }
Output:-
Practical No. – 4
Object:- Write a program to demonstrate the DatePicker.

Introduction:-

Android DatePicker is a widget to select date. It allows you to


select date by day, month and year. Like DatePicker, android also
provides TimePicker to select time.

The android.widget.DatePicker is the subclass of FrameLayout


class.

Android DatePicker Program

activity_main.xml

1. <?xml version="1.0" encoding="utf-8"?>


2. <RelativeLayout xmlns:android="http://schemas.android.com/ap
k/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.datepicker.MainActivity"
>
8.
9. <TextView
10. android:id="@+id/textView1"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:layout_above="@+id/button1"
14. android:layout_alignParentLeft="true"
15. android:layout_alignParentStart="true"
16. android:layout_marginBottom="102dp"
17. android:layout_marginLeft="30dp"
18. android:layout_marginStart="30dp"
19. android:text="" />
20.
21. <Button
22. android:id="@+id/button1"
23. android:layout_width="wrap_content"
24. android:layout_height="wrap_content"
25. android:layout_alignParentBottom="true"
26. android:layout_centerHorizontal="true"
27. android:layout_marginBottom="20dp"
28. android:text="Change Date" />
29.
30. <DatePicker
31. android:id="@+id/datePicker"
32. android:layout_width="wrap_content"
33. android:layout_height="wrap_content"
34. android:layout_above="@+id/textView1"
35. android:layout_centerHorizontal="true"
36. android:layout_marginBottom="36dp" />
37.
38. </RelativeLayout>

MainActivity.java

1. package example.javatpoint.com.datepicker;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5. import android.view.View;
6. import android.widget.Button;
7. import android.widget.DatePicker;
8. import android.widget.TextView;
9.
10. public class MainActivity extends AppCompatActivity {
11. DatePicker picker;
12. Button displayDate;
13. TextView textview1;
14. @Override
15. protected void onCreate(Bundle savedInstanceState) {
16. super.onCreate(savedInstanceState);
17. setContentView(R.layout.activity_main);
18.
19. textview1=(TextView)findViewById(R.id.textView1);
20. picker=(DatePicker)findViewById(R.id.datePicker);
21. displayDate=(Button)findViewById(R.id.button1);
22.
23. textview1.setText("Current Date: "+getCurrentDate());
24.
25. displayDate.setOnClickListener(new View.OnClickListene
r(){
26. @Override
27. public void onClick(View view) {
28.
29. textview1.setText("Change Date: "+getCurrentDate
());
30. }
31.
32. });
33.
34. }
35. public String getCurrentDate(){
36. StringBuilder builder=new StringBuilder();;
37. builder.append((picker.getMonth() + 1)+"/");//month is
0 based
38. builder.append(picker.getDayOfMonth()+"/");
39. builder.append(picker.getYear());
40. return builder.toString();
41. }
42. }

Output:
Practical No. – 5

Object:- Write a program to demonstrate Option menu.

Android Option Menus are the primary menus of android. They


can be used for settings, search, delete item etc.

Android Option Menu Program-

activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.design.widget.CoordinatorLayout xmlns:and
roid="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.optionmenu.MainActivit
y">
8.
9. <android.support.design.widget.AppBarLayout
10. android:layout_width="match_parent"
11. android:layout_height="wrap_content"
12. android:theme="@style/AppTheme.AppBarOverlay">
13.
14. <android.support.v7.widget.Toolbar
15. android:id="@+id/toolbar"
16. android:layout_width="match_parent"
17. android:layout_height="?attr/actionBarSize"
18. android:background="?attr/colorPrimary"
19. app:popupTheme="@style/AppTheme.PopupOverlay
" />
20.
21. </android.support.design.widget.AppBarLayout>
22.
23. <include layout="@layout/content_main" />
24.
25. </android.support.design.widget.CoordinatorLayout>

context_main.xml

1. <?xml version="1.0" encoding="utf-8"?>


2. <android.support.constraint.ConstraintLayout xmlns:android="
http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. app:layout_behavior="@string/appbar_scrolling_view_behavior"

8. tools:context="example.javatpoint.com.optionmenu.MainActivit
y"
9. tools:showIn="@layout/activity_main">
10.
11. <TextView
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:text="Hello World!"
15. app:layout_constraintBottom_toBottomOf="parent"
16. app:layout_constraintLeft_toLeftOf="parent"
17. app:layout_constraintRight_toRightOf="parent"
18. app:layout_constraintTop_toTopOf="parent" />
19.
20. </android.support.constraint.ConstraintLayout>

menu_main.xml

1. <menu xmlns:android="http://schemas.android.com/apk/res/and
roid"
2. xmlns:app="http://schemas.android.com/apk/res-auto"
3. xmlns:tools="http://schemas.android.com/tools"
4. tools:context="example.javatpoint.com.optionmenu.MainActivit
y">
5.
6. <item android:id="@+id/item1"
7. android:title="Item 1"/>
8. <item android:id="@+id/item2"
9. android:title="Item 2"/>
10. <item android:id="@+id/item3"
11. android:title="Item 3"
12. app:showAsAction="withText"/>
13. </menu>

MainActivity.java

1. package example.javatpoint.com.optionmenu;
2.
3. import android.os.Bundle;
4. import android.support.v7.app.AppCompatActivity;
5. import android.support.v7.widget.Toolbar;
6. import android.view.Menu;
7. import android.view.MenuItem;
8. import android.widget.Toast;
9.
10. public class MainActivity extends AppCompatActivity {
11.
12. @Override
13. protected void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.activity_main);
16. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
17. setSupportActionBar(toolbar);
18. }
19.
20. @Override
21. public boolean onCreateOptionsMenu(Menu menu) {
22. // Inflate the menu; this adds items to the action bar if it
is present.
23. getMenuInflater().inflate(R.menu.menu_main, menu);
24. return true;
25. }
26.
27. @Override
28. public boolean onOptionsItemSelected(MenuItem item) {

29. int id = item.getItemId();


30. switch (id){
31. case R.id.item1:
32. Toast.makeText(getApplicationContext(),"Item 1 Sel
ected",Toast.LENGTH_LONG).show();
33. return true;
34. case R.id.item2:
35. Toast.makeText(getApplicationContext(),"Item 2 Sel
ected",Toast.LENGTH_LONG).show();
36. return true;
37. case R.id.item3:
38. Toast.makeText(getApplicationContext(),"Item 3 Sel
ected",Toast.LENGTH_LONG).show();
39. return true;
40. default:
41. return super.onOptionsItemSelected(item);
42. }
43. }
44. }

Output:

Output without clicking on the menu button.


Output after clicking on the menu button.
Practical no.-6

Object:- Write a program to create a simple calculator in Android


studio.
Steps Calculator program:-

 Open your Android Studio


 Click on Start a New Android Studio Project.
 Give your Application Name CrunchifyCalculator and leave
other fields blank as it is, then click NEXT.

 Select the Minimum SDK API 15: Android


4.0.3(IceCreamSandwich). I selected API 15
(IceCreamSandwich) because it covers almost 94% device and it
has almost all the features. If you want to cover 100% device then
you can select API 8: Android 2.2(Froyo).
 Select the Empty Activity and click NEXT.

 Leave the activity name MainActivity as it is and leave


everything as it is. Click Finish.

Code-

MainActivity.java

package com.crunchify.tutorials.crunchifycalculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

Button button0, button1, button2, button3, button4, button5, button6,

button7, button8, button9, buttonAdd, buttonSub,

buttonDivision,

buttonMul, button10, buttonC, buttonEqual;

EditText crunchifyEditText;

float mValueOne, mValueTwo;

boolean crunchifyAddition, mSubtract, crunchifyMultiplication,

crunchifyDivision;
@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button0 = (Button) findViewById(R.id.button0);

button1 = (Button) findViewById(R.id.button1);

button2 = (Button) findViewById(R.id.button2);

button3 = (Button) findViewById(R.id.button3);

button4 = (Button) findViewById(R.id.button4);

button5 = (Button) findViewById(R.id.button5);

button6 = (Button) findViewById(R.id.button6);

button7 = (Button) findViewById(R.id.button7);

button8 = (Button) findViewById(R.id.button8);

button9 = (Button) findViewById(R.id.button9);

button10 = (Button) findViewById(R.id.button10);


buttonAdd = (Button) findViewById(R.id.buttonadd);

buttonSub = (Button) findViewById(R.id.buttonsub);

buttonMul = (Button) findViewById(R.id.buttonmul);

buttonDivision = (Button) findViewById(R.id.buttondiv);

buttonC = (Button) findViewById(R.id.buttonC);

buttonEqual = (Button) findViewById(R.id.buttoneql);

crunchifyEditText = (EditText) findViewById(R.id.edt1);

button1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "1");

});

button2.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "2");

});

button3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "3");

});

button4.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "4");
}

});

button5.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "5");

});

button6.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "6");

});
button7.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "7");

});

button8.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "8");

});

button9.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {


crunchifyEditText.setText(crunchifyEditText.getText() + "9");

});

button0.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + "0");

});

buttonAdd.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (crunchifyEditText == null) {

crunchifyEditText.setText("");
} else {

mValueOne = Float.parseFloat(crunchifyEditText.getText()

+ "");

crunchifyAddition = true;

crunchifyEditText.setText(null);

});

buttonSub.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mValueOne = Float.parseFloat(crunchifyEditText.getText() +

"");

mSubtract = true;

crunchifyEditText.setText(null);

}
});

buttonMul.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mValueOne = Float.parseFloat(crunchifyEditText.getText() +

"");

crunchifyMultiplication = true;

crunchifyEditText.setText(null);

});

buttonDivision.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mValueOne = Float.parseFloat(crunchifyEditText.getText() +

"");
crunchifyDivision = true;

crunchifyEditText.setText(null);

});

buttonEqual.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mValueTwo = Float.parseFloat(crunchifyEditText.getText() +

"");

if (crunchifyAddition == true) {

crunchifyEditText.setText(mValueOne + mValueTwo + "");

crunchifyAddition = false;

if (mSubtract == true) {
crunchifyEditText.setText(mValueOne - mValueTwo + "");

mSubtract = false;

if (crunchifyMultiplication == true) {

crunchifyEditText.setText(mValueOne * mValueTwo + "");

crunchifyMultiplication = false;

if (crunchifyDivision == true) {

crunchifyEditText.setText(mValueOne / mValueTwo + "");

crunchifyDivision = false;

});

buttonC.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {

crunchifyEditText.setText("");

});

button10.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

crunchifyEditText.setText(crunchifyEditText.getText() + ".");

});

layout file -

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/relative1"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<EditText

android:id="@+id/edt1"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

<Button

android:id="@+id/button1"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_alignEnd="@+id/button4"

android:layout_alignRight="@+id/button4"

android:layout_below="@+id/edt1"

android:layout_marginTop="94dp"

android:text="1" />

<Button

android:id="@+id/button2"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignTop="@+id/button1"

android:layout_toLeftOf="@+id/button3"

android:layout_toStartOf="@+id/button3"

android:text="2" />
<Button

android:id="@+id/button3"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignTop="@+id/button2"

android:layout_centerHorizontal="true"

android:text="3" />

<Button

android:id="@+id/button4"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/button1"

android:layout_toLeftOf="@+id/button2"

android:text="4" />
<Button

android:id="@+id/button5"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/button4"

android:layout_alignLeft="@+id/button2"

android:layout_alignStart="@+id/button2"

android:text="5" />

<Button

android:id="@+id/button6"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3"

android:layout_below="@+id/button3"

android:text="6" />

<Button

android:id="@+id/button7"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/button4"

android:layout_toLeftOf="@+id/button2"

android:text="7" />

<Button

android:id="@+id/button8"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button5"

android:layout_alignStart="@+id/button5"

android:layout_below="@+id/button5"

android:text="8" />

<Button

android:id="@+id/button9"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button6"

android:layout_alignStart="@+id/button6"

android:layout_below="@+id/button6"

android:text="9" />

<Button
android:id="@+id/buttonadd"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignEnd="@+id/edt1"

android:layout_alignRight="@+id/edt1"

android:layout_alignTop="@+id/button3"

android:layout_marginLeft="46dp"

android:layout_marginStart="46dp"

android:layout_toRightOf="@+id/button3"

android:text="+" />

<Button

android:id="@+id/buttonsub"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonadd"

android:layout_alignLeft="@+id/buttonadd"

android:layout_alignRight="@+id/buttonadd"

android:layout_alignStart="@+id/buttonadd"

android:layout_below="@+id/buttonadd"

android:text="-" />

<Button

android:id="@+id/buttonmul"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/buttonsub"

android:layout_alignParentEnd="true"

android:layout_alignParentRight="true"

android:layout_alignStart="@+id/buttonsub"

android:layout_below="@+id/buttonsub"
android:text="*" />

<Button

android:id="@+id/button10"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/button7"

android:layout_toLeftOf="@+id/button2"

android:text="." />

<Button

android:id="@+id/button0"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button8"
android:layout_alignStart="@+id/button8"

android:layout_below="@+id/button8"

android:text="0" />

<Button

android:id="@+id/buttonC"

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button9"

android:layout_alignStart="@+id/button9"

android:layout_below="@+id/button9"

android:text="C" />

<Button

android:id="@+id/buttondiv"

style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignEnd="@+id/buttonmul"

android:layout_alignLeft="@+id/buttonmul"

android:layout_alignRight="@+id/buttonmul"

android:layout_alignStart="@+id/buttonmul"

android:layout_below="@+id/buttonmul"

android:text="/" />

<Button

android:id="@+id/buttoneql"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignEnd="@+id/buttondiv"

android:layout_alignLeft="@+id/button10"

android:layout_alignRight="@+id/buttondiv"

android:layout_alignStart="@+id/button10"
android:layout_below="@+id/button0"

android:layout_marginTop="37dp"

android:text="=" />

</RelativeLayout>
Practical no. – 7

Object:- write a program to demonstrate the absolute layout .

Program for absolute layout-

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Activity_Main.xml
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/re
s/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_x="50px"
android:layout_y="361px" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_x="225px"
android:layout_y="361px" />

</AbsoluteLayout>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">demo</string>
<string
name="action_settings">Settings</string>
</resources>

Output-
Practical no. – 8

Object:- Write a program to demonstrate the implicit intent.


Program for implicit intent:-
activity_main.xml-

1. <?xml version="1.0" encoding="utf-8"?>


2. <android.support.constraint.ConstraintLayout xmlns:android="
http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.implicitintent.MainActivi
ty">
8.
9. <EditText
10. android:id="@+id/editText"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:layout_marginEnd="8dp"
14. android:layout_marginStart="8dp"
15. android:layout_marginTop="60dp"
16. android:ems="10"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.575"
19. app:layout_constraintStart_toStartOf="parent"
20. app:layout_constraintTop_toTopOf="parent" />
21.
22. <Button
23. android:id="@+id/button"
24. android:layout_width="wrap_content"
25. android:layout_height="wrap_content"
26. android:layout_marginRight="8dp"
27. android:layout_marginLeft="156dp"
28. android:layout_marginTop="172dp"
29. android:text="Visit"
30. app:layout_constraintEnd_toEndOf="parent"
31. app:layout_constraintHorizontal_bias="0.0"
32. app:layout_constraintStart_toStartOf="parent"
33. app:layout_constraintTop_toBottomOf="@+id/editText"
/>
34. </android.support.constraint.ConstraintLayout>

MainActivity.java-

1. package example.javatpoint.com.implicitintent;
2.
3. import android.content.Intent;
4. import android.net.Uri;
5. import android.support.v7.app.AppCompatActivity;
6. import android.os.Bundle;
7. import android.view.View;
8. import android.widget.Button;
9. import android.widget.EditText;
10.
11. public class MainActivity extends AppCompatActivity {
12.
13. Button button;
14. EditText editText;
15.
16. @Override
17. protected void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.activity_main);
20.
21. button = findViewById(R.id.button);
22. editText = findViewById(R.id.editText);
23.
24. button.setOnClickListener(new View.OnClickListener() {
25. @Override
26. public void onClick(View view) {
27. String url=editText.getText().toString();
28. Intent intent=new Intent(Intent.ACTION_VIEW, Uri.
parse(url));
29. startActivity(intent);
30. }
31. });
32. }
33. }

Output:

You might also like