Hi developers, here the simple example for the action bar control android.
menu_ctrl.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menuitem1"
android:showAsAction="always"
android:title="Add">
</item>
<item
android:id="@+id/menuitem2"
android:showAsAction="always"
android:title="Sub">
</item>
<item
android:id="@+id/menuitem3"
android:showAsAction="always"
android:title="Mul">
</item>
<item
android:id="@+id/menuitem4"
android:showAsAction="always"
android:title="Div">
</item>
</menu>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="20dp" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/str_firstnumber"
android:textSize="15dp" />
<EditText
android:id="@+id/FirstNumberET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/str_secnumber"
android:textSize="15dp" />
<EditText
android:id="@+id/SecondNumberET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/str_answer"
android:textSize="15dp" />
<EditText
android:id="@+id/AnswerET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:inputType="numberSigned"
android:textColor="#FF0000" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
private EditText etFirstName, etSecondName, etAnswer;
private Double intAns;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Initialisation();
}
public void Initialisation()
{
etFirstName = (EditText) findViewById(R.id.FirstNumberET);
etSecondName = (EditText) findViewById(R.id.SecondNumberET);
etAnswer = (EditText) findViewById(R.id.AnswerET);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_ctrl, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuitem1:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
+ (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
case R.id.menuitem2:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
- (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
case R.id.menuitem3:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
* (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
case R.id.menuitem4:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
/ (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
default:
break;
}
return true;
}
}
menu_ctrl.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menuitem1"
android:showAsAction="always"
android:title="Add">
</item>
<item
android:id="@+id/menuitem2"
android:showAsAction="always"
android:title="Sub">
</item>
<item
android:id="@+id/menuitem3"
android:showAsAction="always"
android:title="Mul">
</item>
<item
android:id="@+id/menuitem4"
android:showAsAction="always"
android:title="Div">
</item>
</menu>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="20dp" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/str_firstnumber"
android:textSize="15dp" />
<EditText
android:id="@+id/FirstNumberET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/str_secnumber"
android:textSize="15dp" />
<EditText
android:id="@+id/SecondNumberET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/str_answer"
android:textSize="15dp" />
<EditText
android:id="@+id/AnswerET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:inputType="numberSigned"
android:textColor="#FF0000" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
private EditText etFirstName, etSecondName, etAnswer;
private Double intAns;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Initialisation();
}
public void Initialisation()
{
etFirstName = (EditText) findViewById(R.id.FirstNumberET);
etSecondName = (EditText) findViewById(R.id.SecondNumberET);
etAnswer = (EditText) findViewById(R.id.AnswerET);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_ctrl, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuitem1:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
+ (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
case R.id.menuitem2:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
- (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
case R.id.menuitem3:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
* (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
case R.id.menuitem4:
if(etFirstName.length()==0||etSecondName.length()==0)
{
Toast.makeText(getApplicationContext(), "Enter First and Second Values", Toast.LENGTH_LONG).show();
}
else
{
intAns = (Double.parseDouble(etFirstName.getText().toString()))
/ (Double.parseDouble(etSecondName.getText().toString()));
etAnswer.setText(Double.toString(intAns));
}
break;
default:
break;
}
return true;
}
}