0% found this document useful (0 votes)
78 views11 pages

Android Development: Hello World & Broadcasts

The document discusses creating simple programs in Android Studio like a Hello World program and creating applications to demonstrate broadcast receivers and using events and listeners. It also covers the activity lifecycle, methods, multiple activities and fragment lifecycle and multiple fragments.

Uploaded by

vinay009pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views11 pages

Android Development: Hello World & Broadcasts

The document discusses creating simple programs in Android Studio like a Hello World program and creating applications to demonstrate broadcast receivers and using events and listeners. It also covers the activity lifecycle, methods, multiple activities and fragment lifecycle and multiple fragments.

Uploaded by

vinay009pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Name : Archana Pal Roll No :29

Practical 1

Aim : Creating a simple “Hello World” Program in Android


Studio
Steps:

1. Create New Project by

File → New → New Project

2. Select Template

Templates → Phone and Tablet → Empty Activity

Click Next

3. Give Name to the Project

Select Language Kotlin or Java

4. Select SDK “API 21 : Android 5.1(Lollipop)” for 98 % of device Support

Click Finish.

5. Now the Code window will appear we can see two files activity_main.xml and MainActivity.kt

6. activity_main.xml file is used for desigining the Layout and MainActivity.kt is used for giving
Functionality to the Layout.

7. Now Go to Device Manager option → Create device

8. Select Phone-→ Click Next → Select a System Image → It will download if not downloaded

9. Click Next → Verify Configuration window will appear → give name to AVD → Select Orientation
→ Click Finish

10. Then Go to Project Window → Run App

11. A Mobile like Window will appear in top It will display name of Project and In center
“Hello World ” will appear.

1
Name : Archana Pal Roll No :29

Practical 2

Aim : Create an Application to demonstrate broadcastActivity


Steps:

1. Create a new Android Application


2. Creating Broadcast Receivers
Creati and extends Subclass and BroadcastReceiver implement. onReceiver(Context, Intent)
where onReceiver method each message is received as an Intent object parameter.

MyReceiver.kt :

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class MyReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {


// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "Broadcast : Flight mode changed.",
Toast.LENGTH_LONG).show()
}
}

3. Declare a broadcast receiver in the manifest file

add the element <reciver> in app’s manifest.

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.eyehunt.androidbroadcasts">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>

2
Name : Archana Pal Roll No :29

</activity>

<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
</manifest>

4. MainActivity.kt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

5. main_acitvity.xml

<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/baseline_airplanemode_active_white_24" />

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

android:layout_width="300dp"
android:layout_height="36dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center_vertical"
android:text="Flight Mode"
android:textColor="@color/colorWhite"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"

3
Name : Archana Pal Roll No :29

app:layout_constraintTop_toTopOf="@+id/imageView" />
</android.support.constraint.ConstraintLayout>
6. Run the created application on Android virutal device.

Practical 3
Aim: Create Programs on Events and Listeners.
Steps:

1. Create a new project and go to, activity_main.xml and make the following additions to the code.

Activity_main.xml

<EditText android:id="@+id/txt1" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_marginTop="132dp" android:ems="10"
android:hint="Enter Data" android:inputType="textPersonName" />

<Button android:id="@+id/btn1" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Send" />
</RelativeLayout>

2. Go to Main_Activity.java and insert the following code.

import android.content.Intent; import android.support.v7.app.AppCompatActivity; import


android.os.Bundle; import android.view.View; import android.widget.Button; import
android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText t1; Button b1; @Override protected void onCreate(Bundle savedInstanceState)


{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.txt1); b1=(Button)findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v)
{ String data=t1.getText().toString(); Intent intent=new
Intent(getApplicationContext(),DataRecieve.class); intent.putExtra("data",data); startActivity(intent);
} }); } }

3. Now create another activity Activity_data_receive.xml and write the following Code.

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


<RelativeLayoutxmlns: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=".DataRecieve">

4
Name : Archana Pal Roll No :29

<TextView android:id="@+id/tv1" android:layout_width="100dp" android:layout_height="50dp"


android:layout_alignParentStart="true" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" android:layout_marginStart="152dp"
android:layout_marginLeft="152dp" android:layout_marginTop="161dp"
android:layout_marginEnd="132dp" android:layout_marginRight="132dp" />
</RelativeLayout>

4. Create Data_Receive.java

package com.org.datapass.datapass;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class DataRecieve extends AppCompatActivity {


TextViewt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_data_recieve);
t1=(TextView)findViewById(R.id.tv1);
Intent intent=getIntent();
String data=intent.getStringExtra("data"); t1.setText(data);
}
}

5
Name : Archana Pal Roll No :29

Practical 4

Aim: Programming Activities and fragments.


Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle of fragments and multiple
fragments.

Activity Lifecycle:

1. Create a new project and go to , MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {

String tag= "Android Lifecycle Demonstration";


@Override

6
Name : Archana Pal Roll No :29

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
}
public void onStart()
{ super.onStart();
Log.d(tag,"In the onStart() event");
}

public void onRestart() {


super.onRestart();
Log.d(tag,"In the onRestart() event");
}

public void onPause() {


super.onPause();
Log.d(tag,"In the onPause() event");
}

public void onStop() {


super.onStop();
Log.d(tag,"In the onStop() event");
}

public void onDestroy() {


super.onDestroy();
Log.d(tag,"In the onDestroy() event"); }
}

b) Life Cycle of fragments

Create a new project Add a new Fragment ProjectName>App>src>main>Java 3. Right click


on Java and select ‘Add Fragment’>Add Fragment(Blank) Give a name to the fragment and
click Finish

import android.content.Context; import android.net.Uri; import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater; import android.view.View; import


android.view.ViewGroup; import android.util.Log;

public class LifeCycleofFragment extends Fragment { String tag = "Life Cycle of Fragment";
// TODO: Rename parameter arguments, choose names that match // the fragment
initialization parameters, e.g. ARG_ITEM_NUMBER private static final String
ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters private String mParam1; private
String mParam2;

7
Name : Archana Pal Roll No :29

private OnFragmentInteractionListener mListener;

public LifeCycleofFragment() {

// TODO: Rename and change types and number of parameters


public static LifeCycleofFragment newInstance(String param1, String param2) {
LifeCycleofFragment fragment = new LifeCycleofFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
} }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_life_cycleof, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
}
else
{
throw new RuntimeException(context.toString() + " must implement
OnFragmentInteractionListener");
} }

@Override

8
Name : Archana Pal Roll No :29

public void onDetach() {


super.onDetach();
mListener = null; }

public interface OnFragmentInteractionListener {


// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);

public void onStart() {


super.onStart();
Log.d(tag,"In the onStart() event");
}
public void onRestart() {

Log.d(tag,"In the onRestart() event");


}
public void onPause()
{ super.onPause();
Log.d(tag,"In the onPause() event");
}
public void onStop() {
super.onStop();
Log.d(tag,"In the onStop() event");
}
public void onDestroy() {
super.onDestroy();
Log.d(tag,"In the onDestroy() event");
}
}

9
Name : Archana Pal Roll No :29

Practical 5

Aim: Programs Related to different Layouts in Linear


Linear Layout in Android

LinearLayout is a ViewGroup that is responsible for holding views in it. It is a layout that
arranges its children i.e the various views and layouts linearly (one after another) in a single
column(vertically) or a single row(horizontally).

Horizontal LinearLayout
In a horizontal LinearLayout, as the name suggests, the Views defined inside the Linear Layout
will be arranged horizontally one after another, like in a row. By default, the orientation is set to
horizontal. But its a good practice to explicitly specify the orientation of the linear layout by
setting the attribute android:orientation with value horizontal in the LinearLayout tag.

Vertical Linear Layout


In a vertical LinearLayout, as the name suggests, the Views defined inside the Linear Layout are
arranged verically one after another, like in a column. And for this we need to mention the
android:orientation attribute with value vertical within the LinearLayout tag.

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">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="86dp"
android:layout_weight="1"
android:ems="10"
android:hint="Enter The Text"
android:inputType="textPersonName"
android:text="Type Here"
android:textColor="#FFEB3B" />
10
Name : Archana Pal Roll No :29

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

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" /> </LinearLayout>

<item name="android:background">#000000</item> <item


name="android:textColor">#FFFFAA</item>

11

You might also like