Menu and options menu together - android

how can I create a mainmenu in the actionbar and also an "options" menu that will open on hardware button click?
Like Whatsapp. There's an actionbar with avatar, name and and icon and if you press "menu" button, it will appear another menĂ¹ with "Show contact, media, search" etc.
This is my actual code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calendar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_legend) {
// Open dialog
} else if(id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
} else if(id == R.id.action_add) {
startActivity(new Intent(this, AddActivity.class));
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
// Here open the options menu
return true;
}
return super.onKeyDown(keycode, e);
}

Related

Action on item in Toolbar?

I have ToolbarCustom that has two items and an item can be clicked to open or close the layout. I am wondering how can I use the same item for opening and closing layout?
This my Code, but the variable openLayout is not defined:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
linearLayout = findViewById(R.id.show);
boolean openLayout=true;
if (id == R.id.action_categure) {
if (openLayout) {
linearLayout.setVisibility(View.VISIBLE);
openLayout=false;
} else {
linearLayout.setVisibility(View.GONE);
openLayout =true;
}
}
if (id==R.id.action_card){
return true;
}
return super.onOptionsItemSelected(item);
}
boolean firstTimeClick = true; //global variable
boolean secondTimeClick = false; //global variable
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
linearLayout = findViewById(R.id.show);
if (id == R.id.action_categure) {
if (firstTimeClick) {
firstTimeClick = false;
secondTimeClick= true;
linearLayout.setVisibility(View.GONE);
} else if (secondTimeClick){
firstTimeClick= true;
secondTimeClick= false;
linearLayout.setVisibility(View.VISIBLE);
}
}
if (id==R.id.action_card){
return true;
}
return super.onOptionsItemSelected(item);
}
Is that what you want to achieve?Hope I helped you.

App Constant in application

I created Listview and menu options in android application with App Constants to minimize the number of classes
Ex:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,prgmNameList));
lv.setOnItemClickListener(this);
}
OnClickListener
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
Intent intent = new Intent(this, First.class);
intent.putExtra(AppConstants.MAIN_ACTIVITY_TAG,lv.getItemAtPosition(position).toString());
startActivity(intent);
break;
}
But i need apply app constants to Menu Options too
But i don't know how to do that:
Here is the code for Menu Options
// create action options for options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_about) {
Intent intent = new Intent(this,Extras.class);
startActivity(intent);
}
if (id == R.id.action_advertise) {
return true;
}
if (id == R.id.action_contact) {
return true;
}
if (id == R.id.action_help) {
return true;
}
return super.onOptionsItemSelected(item);
}
I want to do same as listview but for menu options.
Please help me solve this problem.
Just put your string in string.xml .
<string name="menu_name">Show result</string>
and you directly access it in xml .
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ContainerActivity">
<item
android:id="#+id/menu_action_show"
android:layout_width="wrap_content"
android:icon="#drawable/youricon"
android:title="#string/menu_name"
app:showAsAction="always"/>
You can also access all resources using java code with context :
String menu_name=getString(R.id.menu_name);

how to get my android USSD program to work

am building a simple dialler to help me check my account balance but for some unknown reasons am getting some errors, i have a button on my xml which i have set its onclick element to sendMessage1 , but am getting error on my code with the phoneNum[1] telling me cannot reslove symbol phoneNum[1]. this is my code
/**Called when the user clicks the Send button */
public void sendMessage1(View view){
//example phoneNum[1] = "*556";
String encodedHarsh = Uri.encode("#");
startActivity(new Intent
("android.intent.action.DIAL",
Uri.parse("tel:"+ phoneNum[1]+ encodedHarsh)));
//Do something in response to button
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mtn);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_mtn, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
You have to do it doing this :
String encodedHarsh = "*" + "556" + Uri.encode("#");
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + encodedHarsh)));
Note, don't forget to add uses-permisions on manifest

Android back option in action bar

When I created an activity it automatically added a back to main activity option in the action bar. It looks like those are the functions that do it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app_settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Is there a way to make it go back using finish()? Right now it looks like it resets some values that are saved and I want to keep them. When I tried working with finish() it didn't do it but i'm not sure how to use it in the action bar.
This is simpler and it works perfect:
public class BackButtonExample extends Activity {
#SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.duahs);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0B4C5F")));
bar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home {
finish();
return true; }
else{
return super.onOptionsItemSelected(item);
}
}

Menu click item event

I added 3 menu items: Settings / About and Exit.
As you can see in the code, I made the Exit button to close the app, and I want that when I press the About button to open a new activity. How do I do that?
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#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);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.Exit){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
if(id == R.id.action_masterz_yuris_about){
Intent openAboutActivityIntent = new Intent(this, MasterZYurisAboutActivity.class);
startActivity(openAboutActivityIntent);
}
try this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.Exit){
finish();
return true;
}else if (id == R.id.about){
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}

Categories

Resources