We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6
Android Fragment
A fragment is part of the activity.
activity_main.xml File: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/fragment2" android:name="com.example.fragmentexample.Fragment2" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment1" android:name="com.example.fragmentexample.Fragment1" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> File: fragment1.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" android:background="#00ff00"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment frist" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> File: fragment2.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" android:background="#0000ff" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second Fragment" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> ________________________________________ MainActivity class File: MainActivity.java package com.example.fragmentexample; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } ________________________________________ File: Fragment1.java package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment1,container, false); } } ________________________________________ File: Fragment2.java package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment2,container, false); } } Android Option Menu Android Option Menus are the primary menus of android. They can be used for sett ings, search, delete item etc. Here, we are going to see two examples of option menus. First, the simple option menus and second, options menus with images. Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsIt emSelected() method of Activity class. Android Option Menu Example Let's see how to create menu in android. Let's see the simple option menu exampl e that contains three menu items. activity_main.xml We have only one textview in this file. File: activity_main.xml <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android" 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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> menu_main.xml It contains three items as show below. It is created automatically inside the re s/menu directory. File: menu_main.xml <menu xmlns:androclass="http://schemas.android.com/apk/res/android" > <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"/> </menu> ________________________________________ Activity class This class displays the content of menu.xml file and performs event handling on clicking the menu items. File: MainActivity.java package com.jm.optionmenu; 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);//Menu Resource, Menu return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { 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); } } } ________________________________________ ________________________________________ ________________________________________
Option Menu with Icon
You need to have icon images inside the res/drawable directory. The android:icon element is used to display the icon on the option menu. You can write the strin g information in the strings.xml file. But we have written it inside the menu_ma in.xml file. File: menu_main.xml <menu xmlns:androclass="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:icon="@drawable/add" android:title="Item 1"/> <item android:id="@+id/item2" android:icon="@drawable/minus" android:title="Item 2"/> <item android:id="@+id/item3" android:icon="@drawable/delete" android:title="Item 3"/> </menu>
Android Context Menu
Android context menu appears when user press long click on the element. It is al so known as floating menu. It doesn't support item shortcuts and icons. Android Context Menu Example Let's see the simple example of context menu in android. activity_main.xml Drag one listview from the pallete, now the xml file will look like this: File: 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" 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" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="66dp" android:layout_marginTop="53dp" > </ListView> </RelativeLayout> ________________________________________ Activity class Let's write the code to display the context menu on press of the listview. File: MainActivity.java package com.jm.contextmenu; import android.os.Bundle; import android.app.Activity; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { ListView listView1; String contacts[]={"Ajay","Sachin","Sumit","Tarun","Yogesh"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView1=(ListView)findViewById(R.id.listView1); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simp le_list_item_1,contacts); listView1.setAdapter(adapter); // Register the ListView for Context menu registerForContextMenu(listView1); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuIn fo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Select The Action"); menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title menu.add(0, v.getId(), 0, "SMS"); } @Override public boolean onContextItemSelected(MenuItem item){ if(item.getTitle()=="Call"){ Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show(); } else if(item.getTitle()=="SMS"){ Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).sho w(); }else{ return false; } return true; } }