ShrikantAndroidInfo
Sunday, 19 April 2020
Sunday, 10 February 2013
AlertBox in Android
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"
tools:context=".MainActivity" >
<Button
android:id="@+id/exitbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="22dp"
android:layout_marginRight="28dp"
android:text="Exit" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="131dp"
android:src="@android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Developed by Shrikant"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Mainactivity.java
package com.shrikant.alertbox;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.exitbtn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder build=new AlertDialog.Builder(MainActivity.this);
build.setTitle("Exit");
build.setMessage("Are You Sure");
build.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
build.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = build.create();
alert.show();
}
});
}
}
Menu in Android
main_activity.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="268dp"
android:layout_height="wrap_content"
android:layout_x="30dp"
android:layout_y="56dp"
android:text="Context Menu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="6dp"
android:text="Developed by Shrikant"
android:textAppearance="?android:attr/textAppearanceMedium" />
</AbsoluteLayout>
Under menu folder add following xml files:
contextitems.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item5" android:title="Option5"></item>
<item android:id="@+id/item6" android:title="Option6"></item>
<item android:id="@+id/item7" android:title="Option7"></item>
<item android:id="@+id/item8" android:title="Option8"></item>
</menu>
items.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1" android:icon="@drawable/ic_launcher" android:title="Option1"></item>
<item android:id="@+id/item2" android:icon="@drawable/ic_launcher" android:title="Option2"></item>
<item android:id="@+id/item3" android:icon="@drawable/ic_launcher" android:title="Option3"></item>
<item android:id="@+id/item4" android:icon="@drawable/ic_launcher" android:title="Option4"></item>
</menu>
Mainactivity.java
package com.shrikant.menumyself;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.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button contextbtn=(Button)findViewById(R.id.button1);
registerForContextMenu(contextbtn);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.item, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(), "option1 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(), "option2 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(getApplicationContext(), "option3 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(getApplicationContext(), "option4 selected", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.contextitems, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item5:
Toast.makeText(getApplicationContext(), "option5 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item6:
Toast.makeText(getApplicationContext(), "option6 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item7:
Toast.makeText(getApplicationContext(), "option7 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item8:
Toast.makeText(getApplicationContext(), "option8 selected", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}
Subscribe to:
Posts (Atom)