Saturday 28 September 2013

Context Menu in Android

In previous topic we learnt the simple menu, So let's learn how to create context menu in android.

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

Now let's create it.
1. MainActivity.java

package com.sri.menus;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

final int MENU_RED = 1;
final int MENU_GREEN = 2;
final int MENU_BLUE = 3;

final int MENU_SIZE_22 = 4;
final int MENU_SIZE_26 = 5;
final int MENU_SIZE_30 = 6;

TextView tvColor, tvSize;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvColor = (TextView) findViewById(R.id.tvColor);
tvSize = (TextView) findViewById(R.id.tvSize);

// context menu should be created for tvColor and tvSize
registerForContextMenu(tvColor);
registerForContextMenu(tvSize);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
switch (v.getId()) {
case R.id.tvColor:
menu.add(0, MENU_RED, 0, "Red");
menu.add(0, MENU_GREEN, 0, "Green");
menu.add(0, MENU_BLUE, 0, "Blue");
break;
case R.id.tvSize:
menu.add(0, MENU_SIZE_22, 0, "22");
menu.add(0, MENU_SIZE_26, 0, "26");
menu.add(0, MENU_SIZE_30, 0, "30");
break;
}
}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
// menu items for tvColor
case MENU_RED:
tvColor.setTextColor(Color.RED);
tvColor.setText("Text color = red");
break;
case MENU_GREEN:
tvColor.setTextColor(Color.GREEN);
tvColor.setText("Text color = green");
break;
case MENU_BLUE:
tvColor.setTextColor(Color.BLUE);
tvColor.setText("Text color = blue");
break;
// menu items for tvSize
case MENU_SIZE_22:
tvSize.setTextSize(22);
tvSize.setText("Text size = 22");
break;
case MENU_SIZE_26:
tvSize.setTextSize(26);
tvSize.setText("Text size = 26");
break;
case MENU_SIZE_30:
tvSize.setTextSize(30);
tvSize.setText("Text size = 30");
break;
}
return super.onContextItemSelected(item);
}

}

2.activity_main.xml

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/tvColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="50dp"
        android:text="Text color"
        android:textSize="26sp" >
    </TextView>

    <TextView
        android:id="@+id/tvSize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text size"
        android:textSize="22sp" >
    </TextView>


</LinearLayout>

3.That's it.

No comments:

Post a Comment