Sunday, 6 October 2013

Radio Button Alert Box

In this post, we will create a Alert Box with radio Buttons.

Lets create a button which will trigger the alert box.

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


After creating the button, make changes in the java file.

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
Button b;
final CharSequence[] items={"Red","Green","Blue"};
AlertDialog.Builder builder;
AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
builder=new AlertDialog.Builder(this);
builder.setTitle("Pick a Color");
builder.setSingleChoiceItems(items,-1,new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Color : "+items[which],Toast.LENGTH_LONG).show();
}
});
alert=builder.create();
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
alert.show();
}
});
}

@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;
}

}


Output should look like



Friday, 4 October 2013

Button Alert Box

Now lets start with the Button Alert Box.
To start create a button on whose click the alert box would appear.

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


Now to make the alert box appear on click of the button and illustrate working of Button Alert Box.

public class MainActivity extends Activity
{
Button b;
AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
final CharSequence[] items={"Red","Green","Blue"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Pick a Colour.");
builder.setItems(items, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//TODO Auto-generated method stub
Toast.makeText(MainActivity.this,""+items[which],Toast.LENGTH_SHORT).show();
}
});
alert=builder.create();
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//TODO Auto-generated method stub
alert.show();
}
});
}
}


Now the output should look like this




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