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.

Simples Menus in Android

How to add simple menu in android?


Now lets Create a simple menu item in android.
There are 3 types of menu in android.
1.Options menu
2.Context menu
3.Popup menu

First we'll discuss about options menu

1.MainActivity.java

package com.sri.menus;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

@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.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.m1:
Toast.makeText(getApplicationContext(), "menu 1",
Toast.LENGTH_SHORT).show();
break;
case R.id.m2:
Toast.makeText(getApplicationContext(), "menu 2",
Toast.LENGTH_SHORT).show();
default:
break;
}
return super.onOptionsItemSelected(item);
}

}


2.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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Displaying Menu" />

</RelativeLayout>

3. Check in res/menu folder the main.xml
in that

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/m1"
        android:showAsAction="ifRoom"
        android:title="menu 1"/>
    <item
        android:id="@+id/m2"
        android:showAsAction="ifRoom"
        android:title="menu 2"/>

</menu>

4. In main.xml, the android:showAsAction="ifRoom" is written to show it on the action bar and have the more priority.
android:showAsAction="never" is used to hide in the menu and show's on more button.

5.That's it now run it.

Friday, 27 September 2013

POPUP window in android

How to Display Popup Window in Android?


Friends, Lets learn how to display a popup window in android.
Popup is different from Dialog Box. Dialog has the priority to display in only center of the screen but popup can be displayed anywhere in the screen.

Now let's start with the
1.MainActivity.java

package com.sri.popup;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
private Button btnShowPopUp;
private PopupWindow mpopup;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnShowPopUp = (Button) findViewById(R.id.btnShowPopUp);

btnShowPopUp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
View popUpView = getLayoutInflater().inflate(R.layout.popup,
null); // inflating popup layout
mpopup = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, true); // Creation of popup
mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 20, 20); // Displaying
// popup

Button btnOk = (Button) popUpView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mpopup.dismiss(); // dismissing the popup
}
});

Button btnCancel = (Button) popUpView
.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mpopup.dismiss(); // dismissing the popup
}
});
}
});
}
}

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

    <Button
        android:id="@+id/btnShowPopUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show PopUp" >
    </Button>

</LinearLayout>



3.Add the new xml file in the res/layout folder as popup.xml
popup.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="wrap_content"
    android:background="#90FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="PopUp Window"
        android:textColor="#000000"
        android:textStyle="bold" >
    </TextView>

    <Button
        android:id="@+id/btnOk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Ok" >
    </Button>

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Cancel" >
    </Button>

</LinearLayout>

4.That's it now you can easily display popup window in your application.

The full source code is here