Sunday, 15 September 2013

Context Menu

Lets create a CONTEXT MENU. This menu appears n screen when you long click an UI component.

To begin you need to make a UI component on whose long click the context menu would appear.

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="145dp"
        android:text="Context Menu"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Menu which appears on long click"
        android:textAppearance="?android:attr/textAppearanceMedium" />


Then create 2 menu items to be shown in the context menu.

 <item
        android:id="@+id/item1"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Menu Item 1"/>

    <item
        android:id="@+id/item2"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Menu Item 2"/>


Now in the onCreate(), you need to register the component for the context menu after getting its reference from the layout file. And then write the code to make the menu working.

package com.codingredefined.androidcontextmenu;

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

public class MainActivity extends Activity 
{
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
registerForContextMenu(tv);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.item1:
Toast.makeText(this, "Menu Item 1 Selected", Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(this, "Menu Item 2 Selected", Toast.LENGTH_LONG).show();
break;
}
return true;
}
}



After completing the above mentioned steps, your output should look like...



Wednesday, 11 September 2013

Menu in Action Bar

Android Action Bar is same as that of Menu but the only difference is that in this the menu items appear on the action bar and not in  the menu.

Create new project having:
Application Name : AndroidMenu
Project Name : AndroidMenu
Package Name : com.codingredefined.androidmenu
Click NEXT


 Click NEXT
 You may change the icon of your app then click NEXT
 Click NEXT
 Click NEXT

 You may or may not change the code of the layout file as it has nothing to do with this example.
For those who want to change:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Android Menu Item" />


 Now to create the menu items, goto menu file in the menu folder again in res folder and paste the following code:

<item
        android:id="@+id/item_1"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="item 1"/>

    <item
        android:id="@+id/item_2"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="item 2"/>


Then in order to make the menu items working use the below code after the onCreate()

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.item_1:
Toast.makeText(this,"Menu Item 1",Toast.LENGTH_SHORT).show();
break;
case R.id.item_2:
Toast.makeText(this,"Menu Item 2",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}

Pasting the above code will produce a toast on your emulator screen as shown in below figures.

 After pasting the code run your application and observe the output

Monday, 9 September 2013

Menu

Create new project having:
Application Name : AndroidMenu
Project Name : AndroidMenu
Package Name : com.codingredefined.androidmenu
Click NEXT
 Click NEXT
 You may change the icon of your app then click NEXT
 Click NEXT
 Click NEXT
 You may or may not change the code of the layout file as it has nothing to do with this example.
For those who want to change:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Android Menu Item" />

 Now to create the menu items, goto menu file in the menu folder again in res folder and paste the following code:

<item
        android:id="@+id/item_1"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="item 1"/>

    <item
        android:id="@+id/item_2"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="item 2"/>



Then in order to make the menu items working use the below code after the onCreate()

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.item_1:
Toast.makeText(this,"Menu Item 1",Toast.LENGTH_SHORT).show();
break;
case R.id.item_2:
Toast.makeText(this,"Menu Item 2",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}

Pasting the above code will produce a toast on your emulator screen as shown in below figures.


 After pasting the code run your application and observe the output.

Monday, 2 September 2013

Intent

This exercise is about Intents which is used to create new activities.
Open Eclipse IDE.
Create new project with details:
Application Name: AndroidIntent
Project Name: AndroidIntent
Package Name: com.example.androidintent
Click NEXT.
 Click NEXT.
 Click NEXT.
 Click NEXT.
 Now make changes in the layout file.
Add an EditText(Text Box used to get data input) and a Button.
This EditText and Button will be used to get input from user and send the data to the next activity.

<EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et"
        android:layout_centerHorizontal="true"
        android:text="Click Me" />

 Now save the input from user into a String and send the data to the next activity using put() of Intent.

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
{
EditText et;
Button b;
String s;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.et);
b=(Button)findViewById(R.id.button1);
intent=new Intent(this,Abc.class);*
b.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
s=et.getText().toString();
intent.putExtra("s",""+s);
startActivity(intent);
}
});
}

}

*while creating the new activity remember the name provided. otherwise the program will generate error.
 Now we need to create another activity which would start when we will click the button.
In the Package Explorer right click your project.
Click New>Other.
 Select Android Activity and click NEXT.
 Click NEXT.
 Give the name to the Activity provided in the previous java class and click FINISH.(Abc in this case)
 Now Create a TextView which would display the data received from the previous activity.

<TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

 Use the function get() to get the data and then set it to the textview.

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class Abc extends Activity
{
TextView tv;
Bundle bundle;
String s;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abc);
tv=(TextView)findViewById(R.id.tv);
bundle=getIntent().getExtras();
s=bundle.get("s").toString();
tv.setText(""+s);
}

}


 Now your app is ready to be tested.