MCA Lab Manual New

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Aim:

To develop android application and display “Hello World” message.

Procedure:

Creating a new project:

 Open Android Studio and Click on File -> New-> New Project

 Choose Empty Activity and Click on next button.


 Type Name as Program2_HelloWorld in name
 Select the project save location
 Select language as Java
 Select Minimum API Level as 15 ( Android 4.0.3 )
 Click on Finish.
 After project is build, it will look like below screen.

Designing the Layout for Android Application:


 Choose app-> res-> layout -> activity_main.xml
 Now delete the code present in activity_main.xml
 Type the below new code.

Code for activity_main.xml:

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


<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

</LinearLayout>

 Now click on design mode and your android app design will look like below screen.

The design part for android application with text Hello World is done.

Now run the application and see the output.


Output:

Result:

A simple android application to display Hello World message is developed and executed successfully.
Aim:

To create an android application using layouts.

Procedure:

Creating a new project:

 Open Android Studio and Click on File -> New-> New Project

 Choose Empty Activity and Click on next button.


 Type Name as Program3_Layouts in name
 Select the project Save Location
 Select language as Java
 Select Minimum API Level as 15 ( Android 4.0.3 )
 Click on Finish.

Designing the Layout for Android Application:

 Choose app-> res-> layout -> activity_main.xml


 Remove all the existing code and type the below code.

Code for activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:background="#E2E8D9"
tools:context=".MainActivity">

<TextView
android:layout_marginTop="25dp"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:textColor="#3709F3"
android:textSize="30dp"
android:textStyle="bold"
android:text="Select Layout Type"/>

<Button
android:id="@+id/btnLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Linear Layout"
android:layout_gravity="center"
android:layout_margin="15dp"/>

<Button
android:id="@+id/btnRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative Layout"
android:layout_gravity="center"
android:layout_margin="15dp"/>
<Button
android:id="@+id/btnFrameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Frame Layout"
android:layout_gravity="center"
android:layout_margin="15dp"/>
</LinearLayout>

 Switch to design view & verify the design for application interface.

 Let’s add activity & design layout files for Linear Layout
 Click on File -> New -> Activity -> Empty Activity
 Enter Activity Name as LinearLayoutActivity
 Enter Layout name as act_linear_layout
 Click on Finish

Open Linear Layout design file at ProjectExplorer-> Android->app->res->layout->act_linear_layout.xml

Code for act_linear_layout.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"
tools:context=".LinearLayoutActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic|bold"
android:textSize="28dp"
android:textColor="#E00C0C"
android:text="Linear Layout"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second "
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third"
/>
</LinearLayout>

 Switch to design view & verify the design for application interface. Design will look like as below
screen.
Output:

 When you change your code to android:orientation=" horizontal” screen will look below:
 Let’s add activity & design layout files for Relative Layout.
 Click on File -> New -> Activity -> Empty Activity

 Enter Activity Name as RelativeLayoutActivity


 Enter Layout name as act_relative_layout
 Click on Finish
 Open Linear Layout design file at ProjectExplorer-> Android->app->res->layout->
act_relative_layout.xml

Code for act_relative_layout.xml


<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".RelativeLayoutActivity">
<TextView
android:id="@+id/textviewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic|bold"
android:textSize="28dp"
android:textColor="#E00C0C"
android:text="Relative Layout"/>
<Button
android:id="@+id/btnFirst"
android:layout_below="@+id/textviewMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First"/>
<Button
android:id="@+id/btnSecond"
android:layout_below="@+id/btnFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"/>
<Button
android:id="@+id/btnThird"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="THIRD"
android:layout_below="@+id/btnFirst"
android:layout_toRightOf="@+id/btnSecond"/>
<Button
android:id="@+id/btnFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnThird"
android:layout_alignLeft="@+id/btnThird"
android:layout_centerHorizontal="true"
android:text="FOUR" />
<Button
android:id="@+id/btnFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnFour"
android:layout_alignParentRight="true"
android:text="Five" />
</RelativeLayout>

 Switch to design view & verify the design for application interface. Design will look like as below screen.

Output:

 Let’s add activity & design layout files for Frame Layout
 Click on File -> New -> Activity -> Empty Activity
 Enter Activity Name as FrameLayoutActivity
 Enter Layout name as act_frame_layout
 Click on Finish

 Open Linear Layout design file at ProjectExplorer-> Android->app->res->layout-


>act_frame_layout.xml
 Add one image to drawable folder. Copy the image from computer and right click on drawable
folder available at res-> drawable -> Right click & Paste.
Code for act_frame_layout.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FrameActivity" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Frame Layout"
android:textSize="28dp"
android:textStyle="bold"
android:textColor="#FF0000"
android:gravity="center_horizontal"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="275dp"
android:layout_height="275dp"
android:src="@drawable/logo"
android:scaleType="fitCenter"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center"
android:text="Siddharth Institutions"/>
</FrameLayout>
</RelativeLayout>

 Switch to design view & verify the design for application interface. Design will look like as below screen.

Code for MainActivity:


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{

Button btnLinearLayout;
Button btnRelativeLayout;
Button btnFrameLayout;

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

btnLinearLayout=(Button)findViewById(R.id.btnLinearLayout);
btnLinearLayout.setOnClickListener(this);

btnRelativeLayout=(Button)findViewById(R.id.btnRelativeLayout);
btnRelativeLayout.setOnClickListener(this);

btnFrameLayout=(Button)findViewById(R.id.btnFrameLayout);
btnFrameLayout.setOnClickListener(this);

@Override
public void onClick(View view) {
if(view==btnLinearLayout)
{
Toast.makeText(getApplicationContext(),"Linear Layout Button is
Clicked",Toast.LENGTH_LONG).show();
Intent intentLinLayout=new
Intent(getApplicationContext(),LinearLayoutActivity.class);
startActivity(intentLinLayout);
}
else if(view==btnRelativeLayout)
{
Toast.makeText(getApplicationContext(),"Relative Layout Button is
Clicked",Toast.LENGTH_LONG).show();
Intent intentRelLayout=new
Intent(getApplicationContext(),RelativeLayoutActivity.class);
startActivity(intentRelLayout);
}
else if(view==btnFrameLayout)
{
Toast.makeText(getApplicationContext(),"Frame Layout Button is
Clicked",Toast.LENGTH_LONG).show();
Intent intentFrameLayout=new Intent(getApplicationContext(),
FrameLayoutActivity.class);
startActivity(intentFrameLayout);
}
}
}

Output:

You might also like