Technical Knowledge

technical knowledge is that type of blogger in which you can find all the soluctions of your problems which you cannot solve you can see the video given on this blog and solve your problem by yourself.

Like us on Facebook

Technical Knowledge
Showing posts with label context. Show all posts
Showing posts with label context. Show all posts

Sunday, March 29, 2015

options menu, context menu, and sub menu: android tutorial

Program description:

This program will show how to design, create, and handle menus in android.

There are 3 types menus:

1. Options menu - displayed when user clicks hard menu key on the keyboard
2. Sub menu - menu with in a menu
3. Context menu - press and hold on a view, to show context menu.
                                just like when you right click on some item in your desktop.

This will show how to create options menu, how to handle option menu items &

How to register context menu for a button, and how to handle context menu items.

Note: From android version 3.0 on wards there is no support for hard key menu, so using options menu is discouraged. In place of options menu we will be using action bars.


First Activity
package com.techpalle.b15_menus;

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
registerForContextMenu(b1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if(v.getId() == R.id.button1){

}
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.contextmenu, menu);
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("Contact Us!");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item1:
Toast.makeText(getApplicationContext(),
"Contact by SMS", 0).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(),
"Contact by Call", 0).show();
break;
case R.id.item3:
Toast.makeText(getApplicationContext(),
"Contact by Map location", 0).show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.optionsmenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.getItem(0).setTitle("My home");
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item1:
Toast.makeText(getApplicationContext(), "Home", 0).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(), "AboutUs", 0).show();
break;
case R.id.item3:
item.getSubMenu().setHeaderIcon(R.drawable.ic_launcher);
item.getSubMenu().setHeaderTitle("Techpalle Trainings");
Toast.makeText(getApplicationContext(), "Trainings", 0).show();
break;
case R.id.item4:
Toast.makeText(getApplicationContext(), item.getTitle(),
0).show();
break;
case R.id.item5:
item.setChecked(!item.isChecked());
Toast.makeText(getApplicationContext(), "Android", 0).show();
break;
case R.id.item6:
Toast.makeText(getApplicationContext(), "DotNet", 0).show();
break;
}
return super.onOptionsItemSelected(item);
}

}




xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


options menu file in res -> menu folder
optionsmenu.xml (in res-menu folder)
  




android:checkable="true" android:checked="true"/>
android:checkable="true"/>










context menu file in res -> menu folder
contextmenu.xml (in res-menu folder)
  





Download complete code : Click to download

Similar programs:
android Dialogs tutorial

Tags: android, tutorial, examples, menus, options menu, sub menu, context menu

List view with options menu and context menu: Android tutorial


Program description:

This is a ListView program with options menu, and context menu.

This activity has a button, and edit text. User will enter the item in edit text and clicks button to add element to list view dynamically.

This program contains an options menu with send, add, and remove buttons.

On clicking send button all list view items will be sent to next activity. Next activity will display all the elements in toast messages. This is done by using putExtras.
On clicking add button in menu items, it will check if edit text is having any element to add to list view. If there is any it will be added to list view, else a toast message.
On clicking remove button in menu items, it will remove 0th element from list view.
Note: I have not added any check if list view is empty or not, so if you click remove from menu and if list view is empty, it will crash your program.

This program also contains a context menu to be displayed when ever user press and hold on any list view items, and asks user if he wants to delete that item from list view. On clicking remove it will remove that item from list view.


First activity 


package com.techpalle.b15_listviewassignment;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class MainActivity extends Activity {
EditText et;
Button b;
ListView lv;
ArrayList al;
ArrayAdapter aa;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
lv = (ListView) findViewById(R.id.listView1);
al = new ArrayList();
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, al);
lv.setAdapter(aa);
registerForContextMenu(lv);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
al.add(0, et.getText().toString());
aa.notifyDataSetChanged();
et.setText("");
}
});
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.myconmenu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo ac = (AdapterContextMenuInfo)
item.getMenuInfo();
int id = ac.position;
al.remove(id);
aa.notifyDataSetChanged();
return super.onContextItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item1: //send
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
in.putStringArrayListExtra("items", al);
startActivity(in);
break;
case R.id.item2: //add
if(et.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),
"plz try again", 0).show();
}else{
al.add(et.getText().toString());
aa.notifyDataSetChanged();
et.setText("");
}
break;
case R.id.item3: //remove
al.remove(0);
aa.notifyDataSetChanged();
break;
}
return super.onOptionsItemSelected(item);
}
}
 xml file for first activity 
 File name : activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:hint="enter item to add"
android:ems="10" >



Second activity

package com.techpalle.b15_listviewassignment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class SecondScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
ArrayList al = in.getStringArrayListExtra("items");
for(String item:al){
Toast.makeText(getApplicationContext(), item, 0).show();
}
}
}

xml file for second activity 
File name : second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to second screen"
android:textAppearance="?android:attr/textAppearanceLarge" />



menu file for options menu 
File name : main.xml
  





menu file for context menu 
File name : main.xml

  



Download complete code : Click to download

Tags : android, listview, context menu for list items, listview context menu, options menu, pass list view elements to second activity.

Monday, March 23, 2015

List view with options menu and context menu: Android tutorial


Program description:

This is a ListView program with options menu, and context menu.

This activity has a button, and edit text. User will enter the item in edit text and clicks button to add element to list view dynamically.

This program contains an options menu with send, add, and remove buttons.

On clicking send button all list view items will be sent to next activity. Next activity will display all the elements in toast messages. This is done by using putExtras.
On clicking add button in menu items, it will check if edit text is having any element to add to list view. If there is any it will be added to list view, else a toast message.
On clicking remove button in menu items, it will remove 0th element from list view.
Note: I have not added any check if list view is empty or not, so if you click remove from menu and if list view is empty, it will crash your program.

This program also contains a context menu to be displayed when ever user press and hold on any list view items, and asks user if he wants to delete that item from list view. On clicking remove it will remove that item from list view.


First activity 


package com.techpalle.b15_listviewassignment;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class MainActivity extends Activity {
EditText et;
Button b;
ListView lv;
ArrayList al;
ArrayAdapter aa;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
lv = (ListView) findViewById(R.id.listView1);
al = new ArrayList();
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, al);
lv.setAdapter(aa);
registerForContextMenu(lv);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
al.add(0, et.getText().toString());
aa.notifyDataSetChanged();
et.setText("");
}
});
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.myconmenu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo ac = (AdapterContextMenuInfo)
item.getMenuInfo();
int id = ac.position;
al.remove(id);
aa.notifyDataSetChanged();
return super.onContextItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item1: //send
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
in.putStringArrayListExtra("items", al);
startActivity(in);
break;
case R.id.item2: //add
if(et.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),
"plz try again", 0).show();
}else{
al.add(et.getText().toString());
aa.notifyDataSetChanged();
et.setText("");
}
break;
case R.id.item3: //remove
al.remove(0);
aa.notifyDataSetChanged();
break;
}
return super.onOptionsItemSelected(item);
}
}
 xml file for first activity 
 File name : activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:hint="enter item to add"
android:ems="10" >



Second activity

package com.techpalle.b15_listviewassignment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class SecondScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
ArrayList al = in.getStringArrayListExtra("items");
for(String item:al){
Toast.makeText(getApplicationContext(), item, 0).show();
}
}
}

xml file for second activity 
File name : second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to second screen"
android:textAppearance="?android:attr/textAppearanceLarge" />



menu file for options menu 
File name : main.xml
  





menu file for context menu 
File name : main.xml

  



Download complete code : Click to download

Tags : android, listview, context menu for list items, listview context menu, options menu, pass list view elements to second activity.