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

Practical 1: Aim: Create Simple Hello World Application in Android. Activity - Pract1.xml

This document describes three Android practical assignments: 1. Create a "Hello World" application with two text views to display name and enrollment number. 2. Create an app with three buttons, each displaying a toast message on click to indicate which button was pressed. 3. Create an app with a button that opens an alert dialog box when clicked.

Uploaded by

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

Practical 1: Aim: Create Simple Hello World Application in Android. Activity - Pract1.xml

This document describes three Android practical assignments: 1. Create a "Hello World" application with two text views to display name and enrollment number. 2. Create an app with three buttons, each displaying a toast message on click to indicate which button was pressed. 3. Create an app with a button that opens an alert dialog box when clicked.

Uploaded by

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

Batch:A3 Enroll:150160107065

Practical 1
Aim: Create Simple Hello World Application in android.
activity_pract1.xml:-

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="@android:color/holo_red_dark" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enroll"
android:textColor="@android:color/holo_red_dark" />

</LinearLayout>

Pract1.java

package com.example.practical.android;

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

public class pract1_1 extends AppCompatActivity {

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

Page | 1
Batch:A3 Enroll:150160107065

AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical.android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".pract1"
android:label="Practical 1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

</application>

</manifest>

Page | 2
Batch:A3 Enroll:150160107065

Output:-

Page | 3
Batch:A3 Enroll:150160107065

Practical 2
Aim : Create an Android application to check which button you have
clicked.

activity_pract2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="@android:color/holo_red_dark" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enroll"
android:textColor="@android:color/holo_red_dark" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1 Clicked" />

<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2 Clicked" />

<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3 Clicked" />
Page | 4
Batch:A3 Enroll:150160107065

</LinearLayout>
</LinearLayout>

pract2.java
package com.example.practical.android;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class pract2 extends AppCompatActivity {

Button bt1, bt2, bt3;

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

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


bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);

bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(pract2.this, bt1.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(pract2.this, bt2.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(pract2.this, bt3.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});

Page | 5
Batch:A3 Enroll:150160107065

}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical.android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".pract2"
android:label="Practical 2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

</application>

</manifest>

Page | 6
Batch:A3 Enroll:150160107065

OUTPUT:

Page | 7
Batch:A3 Enroll:150160107065

Practical 3
Aim : Create applications that will Demonstrate Dialog Box Control In
Android.

activity_pract3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="@android:color/holo_red_dark" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enroll"
android:textColor="@android:color/holo_red_dark" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<Button
android:id="@+id/btDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Dialog Box" />

</LinearLayout>
</LinearLayout>

Page | 8
Batch:A3 Enroll:150160107065

pract3.java
package com.example.practical.android;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class pract3 extends AppCompatActivity {

Button btDialogButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pract3);
btDialogButton = (Button) findViewById(R.id.btDialogButton);

btDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

AlertDialog.Builder builder1 = new AlertDialog.Builder(pract3.this);


builder1.setMessage("Dialog Box Opened");
builder1.setCancelable(true);

builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

AlertDialog alert11 = builder1.create();


alert11.show();

}
});
}

Page | 9
Batch:A3 Enroll:150160107065

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical.android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".pract3"
android:label="Practical 3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

</application>

</manifest>

Page | 10
Batch:A3 Enroll:150160107065

OUTPUT:

Page | 11
Batch:A3 Enroll:150160107065

Page | 12
Batch:A3 Enroll:150160107065

Practical 4
Aim : Develop an application that uses GUI components, Fonts and Colors

activity_pract4.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="@android:color/holo_red_dark" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enroll"
android:textColor="@android:color/holo_red_dark" />

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="30dp"
android:text="background color change practical!" />

<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Color" />

<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Font"
android:textColor="@android:color/black" />

Page | 13
Batch:A3 Enroll:150160107065

</LinearLayout>
</ScrollView>

pract4.java
package com.example.practical.android;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Random;

public class prac4 extends AppCompatActivity {


Button b1, b2;
TextView text;
int txtSize = 10;
boolean txtSizeOk = true;

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

b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn2);
text = (TextView) findViewById(R.id.text);

b1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
text.setTextColor(color);
}
});
b2.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


Random rnd = new Random();

if (txtSize < 60 && txtSizeOk) {


txtSize = txtSize + 10;
if (txtSize == 60)
txtSizeOk = false;
} else if (txtSize > 00 && !txtSizeOk) {
txtSize = txtSize - 10;
Page | 14
Batch:A3 Enroll:150160107065

if (txtSize == 0)
txtSizeOk = true;
}
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, txtSize);

int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));


text.setTextColor(color);
}
});
}
}

AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical.android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".pract4"
android:label="Practical 4">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

</application>

</manifest>

Page | 15
Batch:A3 Enroll:150160107065

Output:-

Page | 16
Batch:A3 Enroll:150160107065

Page | 17

You might also like