Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Practical 1) Install Android Studio and Run Hello World Program.

Step 1 - System Requirements

You will be delighted, to know that you can start your Android application development on either of the following
operating systems −

Microsoft Windows 10/8/7/Vista/2003 32or64−bit

Mac OS X 10.8.5 or higher, up to 10.9 Mavericks

GNOME or KDE desktop

Second point is that all the required tools to develop Android applications are open source and can be downloaded
from the Web. Following is the list of software's you will need before you start your Android application
programming.

Java JDK5 or later version


Java Runtime Environment JRE

6
Android Studio

Step 2 - Setup Android Studio

Overview
Android Studio is the official IDE for android application development. It works based on IntelliJ IDEA, You can
download the latest version of android studio from Android Studio 2.2 Download, If you are new to installing
Android Studio on windows, you will find a file, which is named as android-studio-bundle-143.3101438-
windows.exe.So just download and run on windows machine according to android studio wizard guideline.
If you are installing Android Studio on Mac or Linux, You can download the latest version from Android Studio
Mac Download, or Android Studio Linux Download, check the instructions provided along with the downloaded
file for Mac OS and Linux. This tutorial will consider that you are going to setup your environment on Windows
machine having Windows 8.1 operating system.

Installation
So let's launch Android Studio.exe, Make sure before launch Android Studio, Our Machine should required
installed Java JDK. To install Java JDK, take a reference of Android environment setup

Android Developer Fundamentals Sem IV


Once you launched Android Studio, it’s time to mention JDK7 path or later version in android studio installer.

Android Developer Fundamentals Sem IV


Below the image initiating JDK to android SDK

Need to check the components, which are required to create applications, below the image have selected Android
Studio, Android SDK, Android Virtual Machine and performance Intel chip.

Android Developer Fundamentals Sem IV


Need to specify the location of local machine path for Android studio and Android SDK, below the image has taken
default location of windows 8.1 x64 bit architecture.

At final stage, it would extract SDK packages into our local machine, it would take a while time to finish the task
and would take 2626MB of Hard disk space.

Android Developer Fundamentals Sem IV


After done all above steps perfectly, you must get finish button and it gonna be open android studio project with
Welcome to android studio message as shown below.

You can start your application development by calling start a new android studio project. in a new installation
frame should ask Application name, package information and location of the project.

Android Developer Fundamentals Sem IV


After entered application name, it going to be called select the form factors your application runs on, here need to
specify Minimum SDK, in our tutorial, I have declared as API23: Android 6.0Mashmallow

The next level of installation should contain selecting the activity to mobile, it specifies the default layout for
Applications

Android Developer Fundamentals Sem IV


At the final stage it going to be open development tool to write the application code.

Android Developer Fundamentals Sem IV


Practical 2) Addition of two numbers.

Input:
activity_main.xml

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


<android.support.constraint.ConstraintLayout xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number"
android:inputType="textPersonName" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Android Developer Fundamentals Sem IV


MainAcitivity.java

package e.saeed.sum;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private EditText editText1,editText2;
private Button buttonsum;

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

public void addListenerOnButton(){

editText1=(EditText)findViewById(R.id.editText);
editText2=(EditText)findViewById(R.id.editText2);
buttonsum=(Button)findViewById(R.id.button);

buttonsum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value1 = editText1.getText().toString();
String value2 = editText2.getText().toString();
int a = Integer.parseInt(value1);
int b = Integer.parseInt(value2);
int sum = a+b;
Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG).show();

}
});

}
}

Android Developer Fundamentals Sem IV


Output:

Android Developer Fundamentals Sem IV


Practical 3) Square & Cube of a number.

Input:
activity_main.xml

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


<android.support.constraint.ConstraintLayout xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number"
android:inputType="textPersonName" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="square"
android:text="SQUARE" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="cube"
android:text="CUBE" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>

Android Developer Fundamentals Sem IV


MainActivity.java

package e.saeed.square;

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

public class MainActivity extends AppCompatActivity {


EditText editText;

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

}
protected void square(View view){
editText = (EditText) findViewById(R.id.editText);
int num = Integer.parseInt(editText.getText().toString());
int result = num*num;
Toast.makeText(getApplicationContext(), "The square of number " + String.valueOf(num) + " is : "+
String.valueOf(result),Toast.LENGTH_SHORT).show();
}
protected void cube(View view){
editText = (EditText) findViewById(R.id.editText);
int num = Integer.parseInt(editText.getText().toString());
int result = num*num*num;
Toast.makeText(getApplicationContext(), "The cube of number " + String.valueOf(num) + " is : "+
String.valueOf(result),Toast.LENGTH_SHORT).show();
}
}

Android Developer Fundamentals Sem IV


Output:

Android Developer Fundamentals Sem IV


Practical 4) Basic Calculator.

Input:
activity_main.xml

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


<android.support.constraint.ConstraintLayout xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="395dp"
android:layout_height="715dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number 1"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number 2"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="add"
android:text="+" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sub"
android:text="-" />
<Button
android:id="@+id/button3"

Android Developer Fundamentals Sem IV


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="mul"
android:text="*" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="div"
android:text="/" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java

package e.saeed.basiccalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int num1,num2,result;
EditText editText,editText1;
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText1 = (EditText) findViewById(R.id.editText2);
textView = (TextView) findViewById(R.id.textView);
}
public void add(View view){
num1 = Integer.parseInt(editText.getText().toString());
num2 = Integer.parseInt(editText1.getText().toString());
textView.setText("Addition is : " + (num1+num2));
}
public void sub(View view){
num1 = Integer.parseInt(editText.getText().toString());
num2 = Integer.parseInt(editText1.getText().toString());

Android Developer Fundamentals Sem IV


textView.setText("Subtraction is : " + (num1-num2));
}
public void mul(View view){
num1 = Integer.parseInt(editText.getText().toString());
num2 = Integer.parseInt(editText1.getText().toString());
textView.setText("Multiplication is : " + (num1*num2));
}
public void div(View view){
num1 = Integer.parseInt(editText.getText().toString());
num2 = Integer.parseInt(editText1.getText().toString());
textView.setText("Division is : " + (num1/num2));
}
}

Output:

Android Developer Fundamentals Sem IV


Practical 5) Login Form.

Input:
activity_main.xml

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


<android.support.constraint.ConstraintLayout xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter username here"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter password here"
android:inputType="textPassword" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="check"
android:text="Login" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Android Developer Fundamentals Sem IV


MainActivity.java

package e.saeed.login2;

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

public class MainActivity extends AppCompatActivity {


EditText editText;
EditText editText2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void check(View v){
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
String s1,s2;
s1 = editText.getText().toString();
s2 = editText2.getText().toString();
if(s1.equals("admin") && s2.equals("123")){
Toast.makeText(this,"You are a valid user...",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this,"Invalid user..",Toast.LENGTH_SHORT).show();
}
}
}

Android Developer Fundamentals Sem IV


Output:

Android Developer Fundamentals Sem IV


Practical 6) Pizza Hut.

Input:
activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns: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:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="395dp"
android:layout_height="715dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Place Your Order here"
android:textSize="30sp"
app:fontFamily="cursive" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tea" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pizza" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sandwich" />

Android Developer Fundamentals Sem IV


<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Burger" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/submit" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java
package e.saeed.pizzacorner;

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

public class MainActivity extends AppCompatActivity {


public CheckBox c1;
public CheckBox c2;
public CheckBox c3;
public CheckBox c4;
public Button b1;
public TextView t1;
public int t = 0;
public StringBuilder rs;
public TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Android Developer Fundamentals Sem IV


c1 = (CheckBox)findViewById(R.id.checkBox);
c2 = (CheckBox)findViewById(R.id.checkBox2);
c3 = (CheckBox)findViewById(R.id.checkBox3);
c4 = (CheckBox)findViewById(R.id.checkBox4);

txt = (TextView)findViewById(R.id.textView3);
rs = new StringBuilder();

b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(c1.isChecked()){

rs.append("1 pizza = 100 $");


t = t + 100;
}
if(c2.isChecked()){

rs.append("\n1 Burger = 500 $");


t = t + 500;
}
if(c3.isChecked()){

rs.append("\n1 Sandwich = 1000 $");


t = t + 1000;
}
if(c4.isChecked()){

rs.append("\n1 Tea = 07 $");


t = t + 7;
}
rs.append("\nTotal price :" + t);
txt.setText(rs);

}
});

}
}

Android Developer Fundamentals Sem IV


Output:

Android Developer Fundamentals Sem IV


Practical 7) My Profile.

Input:
activity_main.xml

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

<android.support.constraint.ConstraintLayout xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="8dp">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="First Name :"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Last Name : "
android:inputType="textPersonName" />

<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Phone Number :"
android:inputType="textPersonName" />

<EditText

Android Developer Fundamentals Sem IV


android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Area : "
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.4">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:padding="2dp"
android:text="Male"
/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:paddingRight="5dp"
android:paddingLeft="2dp"
android:layout_gravity="center"
android:text="Female"
/>
</RadioGroup>

</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="521dp" />

<TextView
android:id="@+id/textView2"

Android Developer Fundamentals Sem IV


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="553dp" />

</LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java

package e.saeed.myprofile;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
int selected;
Button button;
EditText editText;
EditText editText1;
EditText editText3;
EditText editText4;
TextView textView;
String gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
radioGroup = (RadioGroup) findViewById(R.id.radio);
selected = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selected);
gender = String.valueOf(selected);
editText = (EditText) findViewById(R.id.editText);

Android Developer Fundamentals Sem IV


editText1 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText5);
editText4 = (EditText) findViewById(R.id.editText4);
String s1 = String.valueOf(editText.getText());
String s2 = String.valueOf(editText1.getText());
String s3 = String.valueOf(editText3.getText());
String s4 = String.valueOf(editText4.getText());
textView = (TextView) findViewById(R.id.textView2);
textView.setText("\nFirst-Name : "+s1 +"\nLast-Name : " + s2 +"\nPhone-Number : " + s4 +"\nArea : " +
s3 +"\nGender : " + radioButton.getText());
}
});
}
}
Output:

Android Developer Fundamentals Sem IV


Practical 8) Activity Life Cycle.

Input:

MainActivity.java

package e.saeed.activitylifecycle;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Now","in onCreate");
}

@Override
protected void onPause() {
super.onPause();
Log.i("Now","in onPause");
}

@Override
protected void onResume() {
super.onResume();
Log.i("Now","in onResume");
}

@Override
protected void onStop() {
super.onStop();
Log.i("Now","in onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i("Now","in onDestroy");
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

Android Developer Fundamentals Sem IV


super.onRestoreInstanceState(savedInstanceState);
Log.i("Now","in onRestoreInstanceState");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("Now","in onSaveInstanceState");
}

Output:

Android Developer Fundamentals Sem IV


Practical 9) Date Picker.

Input:
activity_main.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/displayDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Date will appear here after being selected"
android:textSize="30sp"/>
<Button android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick a date"/>
</LinearLayout>

MainActivity.java
package e.saeed.time_date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


private TextView pDisplayDate;
private Button pPickDate;
private int pYear;
private int pMonth;
private int pDay;

Android Developer Fundamentals Sem IV


static final int DATE_DIALOG_ID = 0;
private DatePickerDialog.OnDateSetListener pDateSetListener =
new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year,


int monthOfYear, int dayOfMonth) {
pYear = year;
pMonth = monthOfYear;
pDay = dayOfMonth;
updateDisplay();
displayToast();
}
};

private void updateDisplay() {


pDisplayDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(pMonth + 1).append("/")
.append(pDay).append("/")
.append(pYear).append(" "));
}

private void displayToast() {


Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(pDisplayDate.getText()),
Toast.LENGTH_SHORT).show();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pDisplayDate = (TextView) findViewById(R.id.displayDate);
pPickDate = (Button) findViewById(R.id.pickDate);

pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});

final Calendar cal = Calendar.getInstance();


pYear = cal.get(Calendar.YEAR);
pMonth = cal.get(Calendar.MONTH);
pDay = cal.get(Calendar.DAY_OF_MONTH);

Android Developer Fundamentals Sem IV


updateDisplay();
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
pDateSetListener,
pYear, pMonth, pDay);
}
return null;
}
}

Output:

Android Developer Fundamentals Sem IV


Practical 10) Background color.

Input:

activity_main.xml

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


<android.support.constraint.ConstraintLayout xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="changeRed"
android:text="Red" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="changeGreen"
android:text="Green" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="changeBlue"
android:text="Blue" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Android Developer Fundamentals Sem IV


MainActivity.java

package e.saeed.backgroundex;

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

public class MainActivity extends AppCompatActivity {


LinearLayout la;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
la = (LinearLayout) findViewById(R.id.l1);
}

public void changeRed(View view){


la.setBackgroundColor(Color.RED);
}

public void changeGreen(View view){


la.setBackgroundColor(Color.GREEN);
}

public void changeBlue(View view){


la.setBackgroundColor(Color.BLUE);

}
}

Android Developer Fundamentals Sem IV


Output:

Android Developer Fundamentals Sem IV


Practical 11) Image View.

Input:
activity_main.xml

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


<android.support.constraint.ConstraintLayout xmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_weight="0.1"
android:text="Apple" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="Mango" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="Strawberry" />

Android Developer Fundamentals Sem IV


</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:onClick="show"/>

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java

package e.saeed.imageview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {


RadioButton r1,r2,r3;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r1 = (RadioButton) findViewById(R.id.radioButton);
r2 = (RadioButton) findViewById(R.id.radioButton2);
r3 = (RadioButton) findViewById(R.id.radioButton3);
imageView = (ImageView) findViewById(R.id.imageView);
}
public void show(View v){
if(r1.isChecked()){
imageView.setImageResource(R.drawable.apple);
}
if(r2.isChecked()){
imageView.setImageResource(R.drawable.mango);

Android Developer Fundamentals Sem IV


}
if(r3.isChecked()){
imageView.setImageResource(R.drawable.strawberry);
}
}
}

Output:

Android Developer Fundamentals Sem IV

You might also like