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

DA2 Android

The document outlines a digital assessment for an Android Programming course, focusing on creating an options menu, contextual menu, pop-up menu, and data sharing between activities using intents. It includes XML layout files and Java classes for various activities, demonstrating the implementation of menus and data transfer in an Android application. The due date for the assessment is September 29, 2023.

Uploaded by

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

DA2 Android

The document outlines a digital assessment for an Android Programming course, focusing on creating an options menu, contextual menu, pop-up menu, and data sharing between activities using intents. It includes XML layout files and Java classes for various activities, demonstrating the implementation of menus and data transfer in an Android application. The due date for the assessment is September 29, 2023.

Uploaded by

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

Course-:Android Programming Course Code-: SWE2008

Faculty -: Srinivasan P SCORE Due Date-: 29-09-2023

DIGITAL ASSESSMENT 2

Q.1 Option Menu

XML Files

Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="An App to Demonstrate the Options Menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

RAJVARDHAN SINGH
(20MIS0174)
Main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="An App to Demonstrate the Options Menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity_Settings.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Settings">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Settings Page Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Activity_my_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MyProfile">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Profile Page Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Java Classes

Main Activity.java
package com.example.menu_option;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

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) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
// Handle item selection
if(item.getItemId()==R.id.myprofile)
{
// For My Profile Activity
Intent i=new Intent(this,MyProfile.class);
startActivity(i);
}
if(item.getItemId()==R.id.settings)
{
// For Settings Activity
Intent i=new Intent(this,Settings.class);
startActivity(i);
}
else {

}
return super.onOptionsItemSelected(item);
}

Screenshots
Q.2 Contextual Menu
MainActivity.java
package com.example.menu_option;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


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

// Link those objects with their respective id's that we have given
in .XML file
textView = (TextView) findViewById(R.id.textView);
relativeLayout = (RelativeLayout) findViewById(R.id.relLayout);

// here you have to register a view for context menu you can
register any view
// like listview, image view, textview, button etc
registerForContextMenu(textView);

}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// you can set menu header with title icon etc
menu.setHeaderTitle("Choose a color");
// add menu items
menu.add(0, v.getId(), 0, "Yellow");
menu.add(0, v.getId(), 0, "Gray");
menu.add(0, v.getId(), 0, "Cyan");
}

// menu item select listener


@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Yellow") {
relativeLayout.setBackgroundColor(Color.YELLOW);
} else if (item.getTitle() == "Gray") {
relativeLayout.setBackgroundColor(Color.GRAY);
} else if (item.getTitle() == "Cyan") {
relativeLayout.setBackgroundColor(Color.CYAN);
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
// Handle item selection
if(item.getItemId()==R.id.myprofile)
{
// For My Profile Activity
Intent i=new Intent(this,MyProfile.class);
startActivity(i);
}
if(item.getItemId()==R.id.settings)
{
// For Settings Activity
Intent i=new Intent(this,Settings.class);
startActivity(i);
}
else {

}
return super.onOptionsItemSelected(item);
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Relative Layout to display all the details -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Press Here to See the Menu"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>

ScreenShots

Q.3 Pop Up Menu


Pop_up_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/java"
android:title="Java" />

<item
android:id="@+id/kotlin"
android:title="Kotlin" />

<item
android:id="@+id/android"
android:title="Android" />

<item
android:id="@+id/react_native"
android:title="React Native" />
</menu>

Activity_pop_up_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Pop_up_menu">

<Button
android:id="@+id/popUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here to See PopUp Menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Pop_up_menu.java
package com.example.menu_option;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class Pop_up_menu extends AppCompatActivity {


Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_up_menu);
// Referencing and Initializing the button
button = (Button) findViewById(R.id.popUpButton);

// Setting onClick behavior to the button


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Initializing the popup menu and giving the reference as
current context
PopupMenu popupMenu = new PopupMenu(Pop_up_menu.this,
button);
// Inflating popup menu from popup_menu.xml file
popupMenu.getMenuInflater().inflate(R.menu.pop_up_menu,
popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new
PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Toast message on menu item clicked
Toast.makeText(Pop_up_menu.this, "You Clicked " +
menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
// Showing the popup menu
popupMenu.show();
}
});
}
}
Q.4 Passing Data From one Activity to other activity using intent
Screen Shots
Activity_data_sharing.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=".Data_sharing_using_Intent">

<EditText
android:id="@+id/send_text_id"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:hint="Input"
android:textSize="25dp"
android:textStyle="bold" />

<Button
android:id="@+id/send_button_id"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="150dp"
android:text="send"
android:textStyle="bold" />
</RelativeLayout>

Activity_data_receiving.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=".Data_receiving_actvity">

<TextView
android:id="@+id/received_value_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:textSize="40sp"
android:textStyle="bold"
android:layout_marginStart="40dp" />
</RelativeLayout>

Data_sharing.java
package com.example.menu_option;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class Data_sharing_using_Intent extends AppCompatActivity {

// define the variable


Button send_button;
EditText send_text;

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

send_button = findViewById(R.id.send_button_id);
send_text = findViewById(R.id.send_text_id);

// add the OnClickListener in sender button after clicked this


button following Instruction will run
send_button.setOnClickListener(v -> {
// get the value which input by user in EditText and convert it
to string
String str = send_text.getText().toString();
// Create the Intent object of this class Context() to
Second_activity class
Intent intent = new Intent(getApplicationContext(),
Data_receiving_actvity.class);
// now by putExtra method put the value in key, value pair key
is
// message_key by this key we will receive the value, and put
the string
intent.putExtra("message_key", str);
// start the Intent
startActivity(intent);
});
}
}

Data_recieving.java

package com.example.menu_option;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Data_receiving_actvity extends AppCompatActivity {
TextView receiver_msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_receiving_actvity);

receiver_msg = findViewById(R.id.received_value_id);
// create the get Intent object
Intent intent = getIntent();
// receive the value by getStringExtra() method and
// key must be same which is send by first activity
String str = intent.getStringExtra("message_key");
// display the string into textView
receiver_msg.setText(str);
}
}

You might also like