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

Ali Assignment

This document contains code snippets and descriptions for implementing context menus, option menus, and popup menus in Android mobile applications. It includes XML layout code and Java code for displaying a context menu when long pressing a text view and changing the background color based on the selected menu option. It also includes code for inflating an options menu in the app bar and handling item selections. Finally, it shows how to create and display a popup menu when a button is clicked.

Uploaded by

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

Ali Assignment

This document contains code snippets and descriptions for implementing context menus, option menus, and popup menus in Android mobile applications. It includes XML layout code and Java code for displaying a context menu when long pressing a text view and changing the background color based on the selected menu option. It also includes code for inflating an options menu in the app bar and handling item selections. Finally, it shows how to create and display a popup menu when a button is clicked.

Uploaded by

Ali Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment no 4

Android Mobile Development

Submitted by: -
Ali Haider
Fa-19/BSIT/102
Section C

Submitted to: -
Ma’am Rabia Aslam

Faculty of Computer Sciences Department of Information Technology


DHA Phase-VI Lahore
Context Menu

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="Long press me!"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>

Java
import android.graphics.Color; import
android.os.Bundle; import
android.view.ContextMenu; import
android.view.MenuItem; import android.view.View;
import android.widget.RelativeLayout; import
android.widget.TextView; import
androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


TextView textView;
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView = (TextView)
findViewById(R.id.textView); relativeLayout = (RelativeLayout)
findViewById(R.id.relLayout);
registerForContextMenu(textView);
}
@Override
publicvoidonCreateContextMenu(ContextMenumenu,Viewv,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Choose a color"); menu.add(0,
v.getId(), 0, "Yellow"); menu.add(0, v.getId(), 0, "Gray");
menu.add(0, v.getId(), 0, "Cyan");
}

@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;
}
}

Option Menu

Option_xml
<menu 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"
tools:context="example.javatpoint.com.optionmenu.MainActivity">

<item android:id="@+id/item1"
android:title="Item 1"/> <item
android:id="@+id/item2"
android:title="Item 2"/> <item
android:id="@+id/item3"
android:title="Item 3"
app:showAsAction="withText"/>
</menu>

Java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import
android.view.Menu; import
android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){ case
R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3
Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
} }

Popup Menu

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

Java
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 MainActivity extends AppCompatActivity {

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button =
(Button) findViewById(R.id.clickBtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(newPopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem)
Toast.makeText(MainActivity.this, "You Clicked " + menuItem.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}
});
}
}

You might also like