ANDROID PROGRAMMING
(Lab Program File)
SUBMITTED BY:
Student Name: Samar Bezbaruah
Roll Number: 07
GU Roll Number: UA-211-033-1150
Registration Number: 21009903
BVoc IT 6th Semester
COURSE INSTRUCTOR:
Jyotishman Das
Guest lecturer
PUB KAMRUP COLLEGE, BAIHATA CHARIALI
---INDEX---
SL. NO NAME OF THE PROGRAM PAGE NO
01 Hello world! 01-02
02 Text & Scroll View 03-04
03 Camera Permission 05-10
04 Fragment Implement 11-18
05 Create an application with two field (numbers 1 , numbers 2 ) .on clicking 19-23
button the sum of the two number are displayed on another text field.
06 Create a login application with two fields’ username and password. On 24-31
successful login go to next screen, also pass the username to the next
screen. Also on failed login alert user using toast
07 Create an application a spinner with name of some country. On selecting a 32-34
particular country. Display a toast message with currency name of that
country
08 Create an application with a button on the clicking button web browser with 35-38
an URL(open google page)
09 Create an android application to display internal storage data using Array. 39-41
Program No 1: HELLO WORLD!
[Link]
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#091971"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#1BCC22"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
OUTPUT:
2nd Program: TextAndScrolling Views
[Link]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2196F3"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/article_gallery"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="203dp"
android:layout_height="222dp"
android:layout_gravity="center"
android:layout_weight="1"
app:srcCompat="@drawable/pub" />
</LinearLayout>
OUTPUT:
Program 3: Camera Permissions
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{ImageView imageView;Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
imageView = findViewById([Link]);
button=findViewById([Link]);
//Repuest for Camera Runtime permission
if ([Link]( [Link],
[Link])
!=PackageManager.PERMISSION_GRANTED) {
[Link]([Link], new String[]{
[Link]
},100);
[Link] (new [Link]() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 100);
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent
data) {
[Link](requestCode, resultCode, data);
if (requestCode == 100){
Bitmap bitmap = (Bitmap) [Link]().get("data");
[Link](bitmap);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_weight="1"
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/design_default_color_primary"
android:text="CAPTURE AN IMAGE"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/image_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginBottom="300dp"
android:text="To capture an image "
android:textColor="#131212"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<uses-feature
android:name="[Link]"
android:required="false" />
<uses-permission android:name="[Link]"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT:
Program 4: Fragment
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById([Link]);
[Link](mSectionsPagerAdapter);
TabLayout tabLayout = findViewById([Link]);
[Link](mViewPager);
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
default:
return null;
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Fragment 1";
case 1:
return "Fragment 2";
case 2:
return "Fragment 3";
return null;
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
xmlns:app="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<[Link]
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabSelectedTextColor="@android:color/holo_red_dark">
<[Link]
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<[Link]
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<[Link]
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</[Link]>
<[Link]
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Fragment_1.java
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return [Link]([Link].fragment_1, container, false);
Fragment_2.java
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return [Link]([Link].fragment_2, container,false);
}
Fragment_3.java
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Fragment3 extends Fragment {
public Fragment3() {
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return [Link]([Link].fragment_3, container,false);
fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text=" Welcome to Fragment 1"
android:textColor="@android:color/holo_red_dark"
android:textSize="30dp"
android:textStyle="bold" />
</RelativeLayout>
fragment_2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Hello! Fragment 2"
android:textColor="@android:color/holo_orange_dark"
android:textSize="30dp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
fragment_3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text=" Hey! Fragment 3"
android:textColor="@android:color/holo_blue_bright"
android:textSize="30dp"
android:textStyle="bold" />
</RelativeLayout>
OUTPUT:
Program 5 : Create an application with two field (numbers 1 , numbers 2
) .on clicking button the sum of the two number are displayed on
another text field.
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText number1;
EditText number2;
Button Add_button;
TextView result;
int ans=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
// by ID we can use each component which id is assign in xml file
number1=(EditText) findViewById([Link].editText_first_no);
number2=(EditText) findViewById([Link].editText_second_no);
Add_button=(Button) findViewById([Link].add_button);
result = (TextView) findViewById([Link].textView_answer);
// Add_button add clicklistener
Add_button.setOnClickListener(new [Link]() {
public void onClick(View v) {
// num1 or num2 double type
// get data which is in edittext, convert it to string
// using parse Double convert it to Double type
double num1 = [Link]([Link]().toString());
double num2 = [Link]([Link]().toString());
// add both number and store it to sum
double sum = num1 + num2;
// set it ot result textview
[Link]([Link](sum));
});
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView_answer"
android:layout_width="100dp"
android:layout_height="25dp"
android:text="0"
android:layout_marginLeft="130dp"
android:layout_marginTop="300dp"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView_first_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:text="First number"
android:textSize="20dp"/>
<EditText
android:id="@+id/editText_first_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="40dp"
android:inputType="number"
/>
<TextView
android:id="@+id/textView_second_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:text="Second number"
android:textSize="20dp" />
<EditText
android:id="@+id/editText_second_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="90dp"
android:inputType="number"
/>
<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="200dp"
android:text="Add"/>
</RelativeLayout>
OUTPUT:
Program 6: create a login application with two fields username and
password . on successful login go to next screen ,also pass the username
to the next screen . also on failed login alert user using toast.
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText usernameEditText, passwordEditText;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize EditText and Button
usernameEditText = findViewById([Link]);
passwordEditText = findViewById([Link]);
loginButton = findViewById([Link]);
// Set click listener for the login button
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Get the entered username and password
String username = [Link]().toString().trim();
String password = [Link]().toString().trim();
// Check if username and password are not empty
if (![Link]() && ![Link]()) {
// You can implement your login logic here
// For now, let's just show a toast message
[Link]([Link], "Username: " + username + "\nPassword: " +
password, Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Please enter username and password",
Toast.LENGTH_SHORT).show();
});
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--text view for heading-->
<TextView
android:id="@+id/idTVHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="Welcome to my \n Login Form"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_bright"
android:textSize="30dp" />
<!--edit text for user name-->
<EditText
android:id="@+id/idEdtUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeader"
android:layout_marginStart="10dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:hint="Enter UserName"
android:inputType="textEmailAddress" />
<!--edit text for user password-->
<EditText
android:id="@+id/idEdtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtUserName"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Enter Password"
android:inputType="textPassword" />
<!--button to register our new user-->
<Button
android:id="@+id/idBtnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtPassword"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:text="Login"
android:textAllCaps="false" />
</RelativeLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SecondActivity extends AppCompatActivity {
private TextView usernameTextView;
private Button logoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
// Initialize TextView and Button
usernameTextView = findViewById([Link]);
logoutButton = findViewById([Link]);
// Get the username passed from the previous activity
String username = getIntent().getStringExtra("username");
// Display the username
[Link](username);
// Set click listener for the logout button
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Perform logout action here
// For now, let's just finish the activity
finish();
});
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<!--text view for displaying heading-->
<TextView
android:id="@+id/idTVHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="Welcome back again to my"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_bright"
android:textSize="18sp" />
<!--text view for displaying user name-->
<TextView
android:id="@+id/idTVUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeader"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="UserName"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_bright"
android:textSize="25sp" />
<!--button for making user log out-->
<Button
android:id="@+id/idBtnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/idTVUserName"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="LogOut"
android:textAllCaps="false" />
</RelativeLayout>
OUTPUT:
Program 7: Create an application a spinner with name of some country,
on selecting a particular country. Display a toast message with currency
name of that country.
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity implements
[Link] {
String[] country = { "India", "USA", "China", "Japan", "Other"};
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById([Link]);
[Link](this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new
ArrayAdapter(this,[Link].simple_spinner_item,country);
[Link]([Link].simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
[Link](aa);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
[Link](getApplicationContext(),country[position] ,
Toast.LENGTH_LONG).show();
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner android:id="@+id/spinner"
android:layout_width="149dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
</[Link]>
OUTPUT:
Program 8: Create an application with a button on the clicking button
web browser with an URL (open Google page).
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/layout">
<TextView
android:textSize="20dp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="[Link]
android:id="@+id/textview" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/urltxt"
android:textColor="@color/black"
android:layout_marginEnd="20dp"
android:layout_centerInParent="true"
android:alpha="0.7"
android:textSize="20dp"
android:padding="10dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:layout_below="@+id/layout"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:id="@+id/gotoURL" />
</RelativeLayout>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
editText = findViewById([Link]);
button = findViewById([Link]);
textView = findViewById([Link]);
[Link](findViewById([Link]), (v, insets) -> {
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link], [Link]);
return insets;
});
}
OUTPUT:
Program 9: Create an android application to display internal storage data
using Array.
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Get the ListView by its ID
ListView listView = findViewById([Link]);
// Get the array from [Link]
String path = "/storage/emulated/0/";
File f = new File(path);
String[] items = {
"SHAREit",
"Android",
"mbstph",
"Playlists",
"IMO",
"[Link]",
"Pictures",
"[Link]",
"Sounds",
"Download",
"DCIM"
};
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>([Link],
[Link].support_simple_spinner_dropdown_item,items);
[Link](adapter);
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#009688"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2B2D30"
android:text="LISTVIEW!"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="italic" />
<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
OUTPUT: