Sie sind auf Seite 1von 28

Experiment No: 2.

1 Date:

Objective: 2. Create "Hello World" Application

2.1 Create a new Android Project

Android Studio running with the Welcome to Android Studio dialog box.
1. From here, click Start a new Android Studio project.
Android Studio will respond with the Create New Project dialog box.
2. Enter HelloWorldApp as the application name
You should then see C:\Users\jeffrey\AndroidStudioProjects\HelloWorldApp as the
project location.
3. Click Next to select your target devices.
4. Android Studio lets you select form factors, or categories of target devices, for every app
you create. I would have preferred to keep the default API 15: Android 4.0.3
(IceCreamSandwich) minimum SDK setting (under Phone and Tablet).
5. Click Next, and you will be given the opportunity to choose a template for your app's
main activity.
6. For now we select with Empty Activity. Select this template and click Next.
7. Next you'll customize the activity:
Enter HelloWorldAppActivity as the activity name and activity_hello_world_app as the
layout name, and click Finish to complete this step.

 Android Studio will respond that it is creating the project, then take you to the project
workspace.
 The project workspace is organized around a menu bar, a tool bar, a work area, additional
components that lead to more windows (such as a Gradle Console window), and a status
bar. Also note the Tip of the Day dialog box, which you can disable if you like.
 Accessing AVD Manager or SDK Manager form Menu and Tool Bar:
To access the traditional AVD Manager or SDK Manager, select Android from the Tools
menu followed by AVD Manager or SDK Manager from the resulting pop-up menu (or
click their tool bar icons).

The Project and Editors Window:


When you enter the project workspace, HelloWorldApp is identified as the current
project.

 The project window is organized into a tree whose main branches are App and Gradle
Scripts.
 The App branch is further organized into manifests, java, and res subbranches:

 manifests stores AndroidManifest.xml, which is an XML file that describes the structure
of an Android app. This file also records permission settings (where applicable) and other
details about the app.
 java stores an app's Java source files according to a package hierarchy, which
is com.example.helloworldapp in this example.
 res stores an app's resource files, which are organized into drawable, layout, mipmap, and
values subbranches:
o drawable: an initially empty location in which to store an app's artwork
o layout: a location containing an app's layout files;
initially, activity_hello_world_app.xml(the main activity's layout file) is stored
here
o mipmap: a location containing various ic_launcher.png files that store launcher
screen icons of different resolutions
o values: a location containing colors.xml, dimens.xml, strings.xml, and styles.xml

Expand the values subfolder within the res folder. It includes these subfolders:

o colors.xml. Shows the default colors for your chosen theme, and you can add
your own colors or change them based on your app's requirements.
o dimens.xml. Store the sizes of views and objects for different resolutions.
o strings.xml. Create resources for all your strings. This makes it easy to
translate them to other languages.
o styles.xml. All the styles for your app and theme go here. Styles help give your
app a consistent look for all UI elements.

 The Gradle Scripts branch identifies various .gradle (such as build.gradle)


and .properties (such as local.properties) files that are used by the Gradle-based build
system.

You have created a project for your first Android Studio mobile app; now ready to build your
Android application. In Android Studio, this means populating your new project with Java
source code and resource files.

Listing 1.1. Default Code in the Layout File activity_hello_world_app.xml

<RelativeLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello World!"
tools:context=".HelloWorldAppActivity" />
</RelativeLayout>
Listing 1.2. Default Code in the Java Activity File HelloWorldAppActivity.java

package com.androidunleashed.helloworldapp;

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

public class HelloWorldAppActivity extends Activity {


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

@Override
public booleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hello_world_app, menu);
return true;
}
}
Experiment No: 2.2 Date:

Objective: 2. Create "Hello World" Application

2.2 Run "Hello World" on the Emulator

Create a virtual device (emulator)


In this task, you will use the Android Virtual Device (AVD) manager to create a virtual
device or emulator that simulates the configuration for a particular type of Android device.

Using the AVD Manager, you define the hardware characteristics of a device and its API
level, and save it as a virtual device configuration.

When you start the Android emulator, it reads a specified configuration and creates an
emulated device that behaves exactly like a physical version of that device , but it resides on
your computer.

In order to run an emulator on your computer, you have to create a configuration that
describes the virtual device.

1. In Android Studio, select Tools > Android > AVD Manager, or click the AVD Manager

icon in the toolbar.


2. Click the +Create Virtual Device…. (If you have created a virtual device before, the window
shows all of your existing devices and the button is at the bottom.)

The Select Hardware screen appears showing a list of preconfigured hardware devices. For
each device, the table shows its diagonal display size (Size), screen resolution in pixels
(Resolution), and pixel density (Density).

For the Nexus 5 device, the pixel density is xxhdpi, which means your app uses the launcher
icons in the xxhdpi folder of the mipmap folder. Likewise, your app will use layouts and
drawables from folders defined for that density as well.

3. Choose the Nexus 5 hardware device and click Next.


4. On the System Image screen, from the Recommended tab, choose which version of the
Android system to run on the virtual device. You can select the latest system image.

There are many more versions available than shown in the Recommended tab. Look at
the x86 Images and Other Images tabs to see them.

5. If a Download link is visible next to a system image version, it is not installed yet, and you
need to download it. If necessary, click the link to start the download, and click Finish when
it's done.
6. On System Image screen, choose a system image and click Next.
7. Verify your configuration, and click Finish. (If the Your Android Devices AVD Manager
window stays open, you can go ahead and close it.).
Run your app on an emulator

1. In Android Studio, select Run > Run app or click the Run icon in the toolbar.
2. In the Select Deployment Target window, under Available Emulators, select Nexus 5 API
23 and click OK.

The emulator starts and boots just like a physical device. Depending on the speed of your
computer, this may take a while. Your app builds, and once the emulator is ready, Android
Studio will upload the app to the emulator and run it.

You should see the Hello World app as shown in the following screenshot.

Listing 1.1. Default Code in the Layout File activity_hello_world_app.xml


<RelativeLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello World!"
tools:context=".HelloWorldAppActivity" />
</RelativeLayout>
Listing 1.2. Default Code in the Java Activity File HelloWorldAppActivity.java
package com.androidunleashed.helloworldapp;

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

public class HelloWorldAppActivity extends Activity {


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

@Override
public booleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hello_world_app, menu);
return true;
}
}
Experiment No: 2.3 Date:

Objective: 2. Create "Hello World" Application

2.3 On a Physical Device

Install OEM USB drivers

If you are developing on Windows and want to connect a device for testing, then you need to
install the appropriate USB driver.

OEM stands for original equipment manufacturers.

To connect and debug with any of the Google Nexus devices using Windows, you need to install
the Google USB driver (https://developer.android.com/studio/run/win-usb.html)

Install a USB driver

First, find the appropriate driver for your device from the OEM drivers.

Once you've downloaded your USB driver, follow the instructions below to install or upgrade the
driver, based on your version of Windows and whether you're installing for the first time or
upgrading an existing driver. Then see Using Hardware Devices for other important information
about using an Android device for development.

In this task, you will run your app on a physical mobile device such as a phone or tablet.

Why: Your users will run your app on physical devices. You should always test your apps on
both virtual and physical devices.
Requirement Specifications:

 An Android device such as a phone or tablet.


 A data cable to connect your Android device to your computer via the USB port.
 If you are using a Linux or Windows OS, you may need to perform additional steps to run on
a hardware device. Check the Using Hardware Devices documentation. On Windows, you
may need to install the appropriate USB driver for your device. See OEM USB Drivers.

Run your app on a device

To let Android Studio communicate with your device, you must turn on USB Debugging on
your Android device. This is enabled in the Developer options settings of your device. Note
this is not the same as rooting your device.
On Android 4.2 and higher, the Developer options screen is hidden by default. To show
Developer options and enable USB Debugging:

1. On your device, open Settings >About phone and tap Build number seven times.
2. Return to the previous screen (Settings). Developer options appears at the bottom of the list.
Click Developer options.
3. Choose Debugging ->USB Debuggingon->Allow USB debugging?

USB debugging is intended for development purposes only. Use it to copy data between
your computer and your device, install apps on your device without notification and read
log data

Click OK to continue

Now you can connect your device and run the app from Android Studio.

1. Connect your device to your development machine with a USB cable.


2. In Android Studio, at the bottom of the window, click the Android Monitor tab. You should
see your device listed in the top-left drop-down menu.
3. Click the Run button in the toolbar. The Select Deployment Target window opens with
the list of available emulators and connected devices.
4. Select your device, and click OK.

Android Studio should install and runs the app on your device.

Listing 1.1. Default Code in the Layout File activity_hello_world_app.xml

<RelativeLayout
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">
<TextView a
ndroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello"
tools:context=".HelloWorldAppActivity" />
</RelativeLayout>
Action code in Activity File: HelloWorldAppActivity.java
packagecom.androidunleashed.helloworldapp;

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

public class HelloWorldAppActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world_app);
}
}
Experiment No: 2.4 Date:

Objective: 2. Create "Hello World" Application

2.4 Greeting the User

Listing :Code Written in the Layout File activity_hello_world_app.xml

<RelativeLayout
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" >
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".HelloWorldAppActivity" />
</RelativeLayout>

Listing: Code Written in the Java Activity


File HelloWorldAppActivity.java

package com.androidunleashed.helloworldapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldAppActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextViewmesg = (TextView)findViewById(R.id.message);
mesg.setText("Hello World!");
}
}
Output:
Experiment No: 3.1 Date:

Objective: 3. Create Application by Using Widgets


3.1 Creating the Application by using the Activity class
(i) onCreate(), (ii) onStart(), (iii) onResume(), (iv) onPause(), (v) onStop(), (vi) onDestroy(),
(vii) onRestart()

Android Activity Life Cycle


Create the LifeCycleActivityproject
next create Layout File: activity_life_cycle.xml

Listing 1.1. Default Code in the Layout File activity_hello_world_app.xml

<RelativeLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello World!"
tools:context=".HelloWorldAppActivity" />
</RelativeLayout>

Activity File: LifeCycleActivity.java


package com.example.svrec.lifecycleactivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.util.Log;

public class LifeCycleActivityextends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_cycle);
Log.d("life cycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("life cycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("life cycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("life cycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("life cycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("life cycle","onReStart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("life cycle","onDestroy invoked");
}
}

Output:

Open logcat to see output


09-19 14:08:31.413 5854-5854/com.example.svr.lifecycleactivity I/zygote: Not late-enabling -
Xcheck:jni (already on)
09-19 14:08:31.423 5854-5854/com.example.svr.lifecycleactivity W/zygote: Unexpected CPU
variant for X86 using defaults: x86
09-19 14:08:32.162 5854-5854/com.example.svr.lifecycleactivity I/InstantRun: starting instant
run server: is main process
09-19 14:08:32.285 5854-5854/com.example.svr.lifecycleactivity D/life cycle: onCreate invoked
09-19 14:08:32.288 5854-5854/com.example.svr.lifecycleactivity D/life cycle: onStart invoked
09-19 14:08:32.292 5854-5854/com.example.svr.lifecycleactivity D/life cycle: onResume
invoked
09-19 14:08:32.317 5854-5876/com.example.svr.lifecycleactivity D/OpenGLRenderer: HWUI GL
Pipeline
09-19 14:08:32.919 5854-5876/com.example.svr.lifecycleactivity I/OpenGLRenderer: Initialized
EGL, version 1.4
09-19 14:08:32.919 5854-5876/com.example.svr.lifecycleactivity D/OpenGLRenderer: Swap
behavior 1
09-19 14:08:33.832 5854-5876/com.example.svr.lifecycleactivity D/EGL_emulation:
eglCreateContext: 0xaffb1b60: maj 2 min 0 rcv 2
09-19 14:08:33.846 5854-5876/com.example.svr.lifecycleactivity D/EGL_emulation:
eglMakeCurrent: 0xaffb1b60: ver 2 0 (tinfo 0xaff8b030)
09-19 14:08:34.363 5854-5876/com.example.svr.lifecycleactivity D/EGL_emulation:
eglMakeCurrent: 0xaffb1b60: ver 2 0 (tinfo 0xaff8b030)
On pressing Back button
09-19 14:11:28.757 5854-5854/com.example.svr.lifecycleactivity D/life cycle: onPause invoked
09-19 14:11:30.122 5854-5876/com.example.svr.lifecycleactivity D/EGL_emulation:
eglMakeCurrent: 0xaffb1b60: ver 2 0 (tinfo 0xaff8b030)
09-19 14:11:30.570 5854-5854/com.example.svr.lifecycleactivity D/life cycle: onStop invoked
09-19 14:11:30.571 5854-5854/com.example.svr.lifecycleactivity D/life cycle: onDestroy
invoked
On click ActivityLifeCycle icon it will start again
09-19 14:14:48.869 7511-7511/? I/zygote: Not late-enabling -Xcheck:jni (already on)
09-19 14:14:48.880 7511-7511/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
09-19 14:14:49.474 7511-7511/? I/InstantRun: starting instant run server: is main process
09-19 14:14:49.653 7511-7511/com.example.svr.lifecycleactivity D/life cycle: onCreate invoked
09-19 14:14:49.654 7511-7511/com.example.svr.lifecycleactivity D/life cycle: onStart invoked
09-19 14:14:49.664 7511-7511/com.example.svr.lifecycleactivity D/life cycle: onResume
invoked
09-19 14:14:49.689 7511-7528/com.example.svr.lifecycleactivity D/OpenGLRenderer: HWUI GL
Pipeline
09-19 14:14:49.770 7511-7528/com.example.svr.lifecycleactivity I/OpenGLRenderer: Initialized
EGL, version 1.4
09-19 14:14:49.770 7511-7528/com.example.svr.lifecycleactivity D/OpenGLRenderer: Swap
behavior 1
09-19 14:14:49.785 7511-7528/com.example.svr.lifecycleactivity D/EGL_emulation:
eglCreateContext: 0xa3ffdf80: maj 2 min 0 rcv 2
09-19 14:14:49.830 7511-7528/com.example.svr.lifecycleactivity D/EGL_emulation:
eglMakeCurrent: 0xa3ffdf80: ver 2 0 (tinfo 0xa6968330)
09-19 14:14:50.074 7511-7528/com.example.svr.lifecycleactivity D/EGL_emulation:
eglMakeCurrent: 0xa3ffdf80: ver 2 0 (tinfo 0xa6968330)
09-19 14:18:53.692 7511-7511/com.example.svr.lifecycleactivity D/life cycle: onPause invoked
09-19 14:18:54.126 7511-7528/com.example.svr.lifecycleactivity D/EGL_emulation:
eglMakeCurrent: 0xa3ffdf80: ver 2 0 (tinfo 0xa6968330)
09-19 14:18:54.534 7511-7511/com.example.svr.lifecycleactivity D/life cycle: onStop invoked
09-19 14:18:54.534 7511-7511/com.example.svr.lifecycleactivity D/life cycle: onDestroy
invoked
Experiment No: 3.2 Date:

Objective: 3.2 Create the application by using EditText Control


Create the EditTextApp project next create
Layout File: activity_edit_text_app.xml

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


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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:id="@+id/user_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/response"/>

</LinearLayout>

nextActivity File: EditTextAppActivity.java

package com.example.svr.edittextapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;

public class EditTextAppActivityextends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text_app);
final TextViewresp = (TextView)this.findViewById(R.id.response);
final EditText username = (EditText) findViewById(R.id.user_name);
username.setOnKeyListener(new OnKeyListener() {
public booleanonKey(View v, intkeyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_UP) && (keyCode ==
KeyEvent.KEYCODE_ENTER)) {
resp.setText("Welcome "+username.getText()+"!");
return true;
}
return false;
}
});
}

}
Output:
Experiment No: 3.3 Date:

Objective: 3.3 Creating the Application Choosing Options

(i) CheckBox

Create the CheckBOxApp project,

next create Layout File: activity_check_box_app.xml

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

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Players you want"/>
<CheckBox
android:id="@+id/checkbox_sachin"
android:layout_height="wrap_content"
android:text="Sachin"
android:layout_width="match_parent" />
<CheckBox
android:id="@+id/checkbox_kohli"
android:layout_height="wrap_content"
android:text="Kohli"
android:layout_width="match_parent" />
<CheckBox
android:id="@+id/checkbox_dhoni"
android:layout_height="wrap_content"
android:text="Dhoni"
android:layout_width="match_parent" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/players_btn"
android:text="List of Players"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/players"/>
</LinearLayout>

Next create Activity File: CheckBoxAppActivity.java

package com.example.svr.checkboxappactivity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.CheckBox;
import android.view.View;
import android.view.View.OnClickListener;

public class CheckBoxAppActivityextends Activity implements OnClickListener {


CheckBoxc1,c2,c3;
TextViewresp;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_app);
Button b = (Button)this.findViewById(R.id.players_btn);
resp= (TextView)this.findViewById(R.id.players);
c1 = (CheckBox)findViewById(R.id.checkbox_sachin);
c2 = (CheckBox)findViewById(R.id.checkbox_kohli);
c3 = (CheckBox)findViewById(R.id.checkbox_dhoni);
b.setOnClickListener(this);
}

public void onClick(View v) {


String s="",s1="",s2="",s3="";
if (c1.isChecked()) {
s1="sachin\n ";

}
if (c2.isChecked()) {
s2="Kohli\n ";
}
if (c3.isChecked()) {
s3="Dhoni ";
}
s=s1+s2+s3;
resp.setText("Players are:\n"+s);
}
}

Output:
Experiment No: 3.3 Date:

Objective3.3 Creating the Application Choosing Options

(i) RadioButton (ii) RadioGroup

Create the RadioButtonApp project,

next create Layout File: activity_radio_button_app.xml

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


<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Fill the answers for Multiple choice Questions"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Who is the president of USA?"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButtonandroid:id="@+id/radio_trump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#FF0000FF"
android:text="Donald John Trump" />
<RadioButtonandroid:id="@+id/radio_william"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#FF0000FF"
android:text="William Bar" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Who is the Prime Minister of INDIA?"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButtonandroid:id="@+id/radio_modi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#FF0000FF"
android:text="NarendraDamodardasModi" />
<RadioButtonandroid:id="@+id/radio_kovind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#FF0000FF"
android:text="Ram NathKovind" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/answer1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/answer2"/>
</LinearLayout>
Next create Activity File: RadioButtonAppActivity.java

package com.example.svr.radiobuttonapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.RadioButton;
import android.view.View;
import android.view.View.OnClickListener;

public class RadioButtonAppActivityextends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button_app);
RadioButtonradioTrump = (RadioButton) findViewById(R.id.radio_trump);
RadioButtonradioWilliam = (RadioButton) findViewById(R.id.radio_william);
RadioButtonradioModi = (RadioButton) findViewById(R.id.radio_modi);
RadioButtonradioKovind = (RadioButton) findViewById(R.id.radio_kovind);
radioTrump.setOnClickListener(radioListener1);
radioWilliam.setOnClickListener(radioListener1);
radioModi.setOnClickListener(radioListener2);
radioKovind.setOnClickListener(radioListener2);
}

private OnClickListenerradioListener1 = new OnClickListener() {


public void onClick(View v) {
TextView selectedAnswer1 = (TextView) findViewById(R.id.answer1);
RadioButtonrb = (RadioButton) v;
String str=rb.getText().toString();
if(str.equalsIgnoreCase("Donald John Trump"))
selectedAnswer1.setText("Chosen right answer for Who is the president of USA: "
+rb.getText());
else
selectedAnswer1.setText("Chosen wrong answer for Who is the president of USA: "
+rb.getText());
}
};
private OnClickListenerradioListener2 = new OnClickListener() {
public void onClick(View v) {
TextView selectedAnswer2 = (TextView) findViewById(R.id.answer2);
RadioButtonrb = (RadioButton) v;
String str=rb.getText().toString();
if(str.equalsIgnoreCase("NarendraDamodardasModi"))
selectedAnswer2.setText("Chosen right answer for Who is the president of INDIA: "
+rb.getText());
else
selectedAnswer2.setText("Chosen wrong answer for Who is the president of INDIA: "
+rb.getText());
}
};
}

Output:
Experiment No: 4.1 Date:

Objective:4. Create Application by Using Building Blocks for Android Application Design 4.1 Design the
Application by using (i) Linear Layout

Create the LinearLayoutApp1 project,

next create Layout File: activity_linear_layout_app1.xml

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


<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/a
ndroid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/natekar"
android:text="NanduNatekar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/gopichand"
android:text="PullelaGopi Chand "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"/>
<Button
android:id="@+id/prakash"
android:text="PrakashPadukone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="@+id/srikanth"
android:text="SrikanthKidambi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"/>
<Button
android:id="@+id/sameer"
android:text="Sameer Verma"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:layout_weight="1.0"/>
<Button
android:id="@+id/kashyap"
android:text="ParupalliKashyap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="1.0"/>
</LinearLayout>

Next create Activity File: LinearLayoutApp1Activity.java

packagecom.example.svr.linearlayoutapp1;

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

public class LinearLayoutApp1Activity extends Activity {

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

Output:
Create the LinearLayoutApp2 project,

next create Layout File: activity_linear_layout_app2.xml

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


<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/a
ndroid"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/aparna"
android:text="AparnaPopat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<Button
android:id="@+id/sindhu"
android:text="P.V.Sindhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"/>
<Button
android:id="@+id/saina"
android:text="SainaNehwal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<Button
android:id="@+id/ashwini"
android:text="AshwiniPonnapa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<Button
android:id="@+id/jwala"
android:text="JwalaGutta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
</LinearLayout>
Next create Activity File: LinearLayoutApp2Activity.java

packagecom.example.svr.linearlayoutapp2;

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

public class LinearLayoutApp2Activity extends Activity {

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

Output: