0% found this document useful (0 votes)
9 views

Assignment 1

wgwrfwf wfwfrwg fw4f

Uploaded by

ultron jarvis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment 1

wgwrfwf wfwfrwg fw4f

Uploaded by

ultron jarvis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Assignment 1 12102040501074

Q6: How to create menus in android? Explain with suitable example.


Ans: Creating menus in Android is a common task for adding options to your app, typically
placed in the app's toolbar or bottom navigation. Android provides several types of menus,
such as options menus, context menus, and popup menus. Below, I'll explain how to create
an options menu in Android, along with code examples.
Steps to Create an Options Menu in Android
1. Create a Menu Resource File:
o Define your menu items in an XML file inside the res/menu directory.
2. Inflate the Menu in Your Activity:
o Override the onCreateOptionsMenu method in your activity to inflate the
menu.
3. Handle Menu Item Clicks:
o Override the onOptionsItemSelected method to handle clicks on menu items.
<!-- res/menu/menu_main.xml -->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_settings"
android:title="Settings"
android:icon="@android:drawable/ic_menu_preferences"
android:showAsAction="always" />
<item
android:id="@+id/action_about"
android:title="About us"
android:showAsAction="never" />
</menu>
// MainActivity.java
package com.example.tirth3;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_about:
Toast.makeText(this, "About selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
<!-- res/layout/activity_main.xml -->

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

<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=".MainActivity">
</RelativeLayout>
Output:

Q7: Create an android application which pops up AlertDialog with three buttons.
Code:
<!-- res/layout/activity_main.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"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_show_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:layout_centerInParent="true" />
</RelativeLayout>
// MainActivity.java
package com.example.tirth3;

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowDialog = findViewById(R.id.btn_show_dialog);
btnShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog();
}
});
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an Option")
.setMessage("Select one of the options below:")
.setPositiveButton("Option 1", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Option 1 selected",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Option 2", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

Toast.makeText(MainActivity.this, "Option 2 selected",


Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
Output:

Q8: Create a Simple Grid View android application.


Code:
<!-- res/layout/activity_main.xml -->

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

<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=".MainActivity">
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" />
</RelativeLayout>
// NumberAdapter.java
package com.example.tirth3;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class NumberAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mNumbers;
public NumberAdapter(Context context, Integer[] numbers) {
mContext = context;
mNumbers = numbers;
}
@Override
public int getCount() {

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

return mNumbers.length;
}
@Override
public Object getItem(int position) {
return mNumbers[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) {
textView = new TextView(mContext);
textView.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
textView.setGravity(android.view.Gravity.CENTER);
textView.setPadding(8, 8, 8, 8);
textView.setTextSize(20);
} else {
textView = (TextView) convertView;
}
textView.setText(String.valueOf(mNumbers[position]));
return textView;
}
}
// MainActivity.java
package com.example.tirth3;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = findViewById(R.id.grid_view);
NumberAdapter adapter = new NumberAdapter(this, numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Item " + numbers[position] + " clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}
Output:

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

Q10: What are View and ViewGroup? Create simple application for registration form that
contains Name, Email ID, phone number, checkbox, radiobutton and address.
Ans:
View:
• View is the base class for all UI components in Android. It represents the building
blocks for user interface elements, like buttons, text fields, images, etc. Every
element that you see in an Android app is a subclass of View.
ViewGroup:
• ViewGroup is a subclass of View that can contain other View objects (or even other
ViewGroup objects). It acts as a container and layout manager for other views.
Examples of ViewGroup classes include LinearLayout, RelativeLayout,
ConstraintLayout, etc.
Code:
<!-- res/layout/activity_main.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
<TextView
android:layout_width="wrap_content"

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

android:layout_height="wrap_content"
android:text="Email ID" />
<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your email" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your phone number"
android:inputType="phone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender" />
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<CheckBox
android:id="@+id/cb_accept_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I accept the terms and conditions" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address" />
<EditText
android:id="@+id/et_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your address"
android:inputType="textMultiLine"
android:lines="3" />
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</ScrollView>
// MainActivity.java
package com.example.tirth3;

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText etName, etEmail, etPhone, etAddress;
private RadioGroup rgGender;
private CheckBox cbAcceptTerms;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.et_name);
etEmail = findViewById(R.id.et_email);
rgGender = findViewById(R.id.rg_gender);
cbAcceptTerms = findViewById(R.id.cb_accept_terms);
etAddress = findViewById(R.id.et_address);
btnSubmit = findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleSubmit();
}
});
}
private void handleSubmit() {

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

String name = etName.getText().toString();


String email = etEmail.getText().toString();
String phone = etPhone.getText().toString();
String address = etAddress.getText().toString();
int selectedGenderId = rgGender.getCheckedRadioButtonId();
RadioButton selectedGender = findViewById(selectedGenderId);
String gender = selectedGender != null ? selectedGender.getText().toString() : "";
boolean isTermsAccepted = cbAcceptTerms.isChecked();
if (name.isEmpty() || email.isEmpty() || phone.isEmpty() || address.isEmpty() ||
gender.isEmpty() || !isTermsAccepted) {
Toast.makeText(MainActivity.this, "Please fill in all fields and accept the terms",
Toast.LENGTH_SHORT).show();
} else {
String message = "Name: " + name + "\nEmail: " + email + "\nPhone: " + phone;
Toast.makeText(MainActivity.this, "Registration Successful!\n" + message,
Toast.LENGTH_LONG).show();
}
}
}
Output:

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

Q14: Write a simple code to create custom toolbar.


Code:
<!-- res/layout/custom_toolbar.xml -->
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Toolbar"
android:textColor="@android:color/white"
android:textSize="20sp"
android:layout_gravity="center" />
</androidx.appcompat.widget.Toolbar>
<!-- res/layout/activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/custom_toolbar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

android:layout_centerInParent="true"
android:text="Hello, World!"
android:textSize="18sp" />
</RelativeLayout>
// MainActivity.java
package com.example.tirth3;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.custom_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null); // Hide default title
}
}
Output:

Q15: What is Tab Layout? Which are the important components of Tab Layout? Create
simple app that has three tabs.
Ans: TabLayout in Android is a user interface component that allows you to organize content
into tabs, where each tab represents a different view or section. It provides a way to switch
between different pages or fragments by selecting tabs.
Key Components of Tab Layout:

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

1. TabLayout:
o The main container that holds the tabs.
o It can be used to display multiple tabs and allows switching between them.
2. ViewPager2 (or ViewPager):
o A layout manager that allows users to swipe left or right to navigate through
pages associated with tabs.
o Each page can host a fragment or a simple view.
3. Fragment:
o Represents each individual tab content. Each fragment can display different
data or UI elements.
Code:
<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
// MainActivity.java
package com.example.tirth3;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = findViewById(R.id.tabLayout);
ViewPager2 viewPager = findViewById(R.id.viewPager);
TabPagerAdapter tabPagerAdapter = new TabPagerAdapter(this);
viewPager.setAdapter(tabPagerAdapter);
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
switch (position) {
case 0: tab.setText("Tab 1");
break;
case 1: tab.setText("Tab 2");
break;
case 2: tab.setText("Tab 3");
break;
}
}).attach();
}
}

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

//TabPagerAdapter.java
package com.example.tirth3;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class TabPagerAdapter extends FragmentStateAdapter {
public TabPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0: return new FragmentOne();
case 1: return new FragmentTwo();
case 2: return new FragmentThree();
default: return new FragmentOne();
}
}
@Override
public int getItemCount() { return 3; }
}
// FragmentOne.java
package com.example.tirth3;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class FragmentOne extends Fragment {
@Nullable
@Override

Mobile Application Development Tirth M. Jasoliya


Assignment 1 12102040501074

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup


container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
<!-- res/layout/fragment_one -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Tab 1"
android:textSize="20sp" />
</LinearLayout>
//Same for other two fragment
Output:

Mobile Application Development Tirth M. Jasoliya

You might also like