So I recently trying to add another action button beside the overflow icon on the toolbar:
But by following a tutorial on adding action buttons, I can't get it to show on my toolbar.
Here's my menu/menu_notify.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="com.example.jovie.canteen.MenuNotify">
<item
android:id="#+id/action_notify"
android:icon="#drawable/ic_notifications_black_24px"
android:title="#string/action_notify"
app:showAsAction="always" />
</menu>
I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.
MenuNotify.java:
package com.example.jovie.canteen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
/**
* Created by Jovie on 1/28/2016.
*/
public class MenuNotify extends AppCompatActivity {
#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_notify, 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.
switch (item.getItemId()) {
//noinspection SimplifiableIfStatement
case R.id.action_notify:
startActivity(new Intent(this, Home.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Btw, my filter icon button is in my main class.
EDIT:
Thanks g2o for the help!
I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.
There is no need to create new class to add new action button.
Just add
<item
android:id="#+id/action_notify"
android:icon="#drawable/ic_notifications_black_24px"
android:title="#string/action_notify"
app:showAsAction="always" />
To your menu/main.xml and add
case R.id.action_notify:
startActivity(new Intent(this, Home.class));
return true;
to the switch of your Main class onOptionsItemSelected method.
Related
I am just getting started towards android and i am having trouble with my hello world app, The problem is first it said R cannot resolved which i was able to overcome because there was no R.java files which i later copied from the internet
now there are three errors
1. activity_main cannot be resolved or is not a field
2. menu cannot be resolved or is not a field
3. action_settings cannot be resolved or is not a field
I have already tries control+shift+o,
already tried clean and build.
package my.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#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;
}
return super.onOptionsItemSelected(item);
}
}strong text
You should not create or modify R.java file manually as it's autogenerated by build system. Try to remove one copied from Internet and just rebuild your project with layout and menu files located in appropriated directories of the res folder
Here is my menu resource file
<item
android:id="#+id/action_share"
android:title="#string/action_share"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider">
</item>
</menu>
And here is my java code
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider =(ShareActionProvider)menuItem.getActionProvider();
// setIntent("COOL NIKS");
return super.onCreateOptionsMenu(menu);
}
This is not showing share action provider. How do I fix it?
Here is how full working example should look like.This uses AppCompat library.
You need to add http://schemas.android.com/apk/res-auto schema to your menu items 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" >
<item
android:id="#+id/action_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"
android:title="Share" />
</menu>
Now you will have to update your MainActivity to use ShareActionProvider
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
#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.menu_main, menu);
// Get the menu item.
MenuItem menuItem = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
// Set share Intent.
// Note: You can set the share Intent afterwords if you don't want to set it right now.
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
// Create and return the Share Intent
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "http://google.com");
return shareIntent;
}
// Sets new share Intent.
// Use this method to change or set Share Intent in your Activity Lifecycle.
private void changeShareIntent(Intent shareIntent) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
I can't get an action button to appear in the action bar. I'm sure I'm just misunderstanding how this is supposed to be done. What am I doing wrong?
I'm going through the tutorial at developer.android.com and I've just added an item to the action bar. In the tutorial, the showAsAction for a Search button is set to ifRoom and it should show the button in the action bar, but it's only showing it in the overflow menu.
I thought there might not be enough room, so I've tried reducing the title of the app to a single letter. I've tried removing the title of the button. I've tried changing the showAsAction to every value except never.
Update 20150830_151538-0500: Changed android:showAsAction to app:showAsAction per recommendation in answers. The result is that the Search button doesn't show at all. Not even in the overflow. I also included the full MyActivity.java rather than just the snippet I included in the original question.
Update 20150831_222922-0500: There were two problems. The first was that I used android:showAsAction rather than app:showAsAction. This was pointed out right away in the first two answers. The second problem was that I was using an icon that was the wrong size. I was using one of the _48dp.png icons and that caused the item not to show at all because it was the wrong size for the action bar. This was pointed out in comments to the answer by #ci_.
Note: The original version of MyActivity.java, which was generated by Android Studio, inflated a menu_my.xml file for the menu. This menu file only included the Settings action using app:showAsAction="never". Following the tutorial, I replaced the content of the OnCreateOptionsMenu class. Unless I'm misunderstanding something this original menu_my.xml file isn't used at all, but since I started properly using app: instead of android: for the items that Settings item is the only one that's showing. I doubt it's related, but I wanted to mention it just in case.
MyActivity.java:
package com.ghodmode.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.ghodmode.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#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);
}
/**
* Called when the user clicks the Send button
*/
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
main_activity_actions.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_black_48dp"
app:showAsAction="ifRoom"
android:title="#string/action_search" />
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
app:showAsAction="never"
android:title="#string/action_settings" />
</menu>
This is a common mistake when using the ActionBarActivity or AppCompatActivity from the support library, which I'm assuming you're using here.
In this case a special appcompat namespace has to be used for showAsAction:
xmlns:app="http://schemas.android.com/apk/res-auto"
app:showAsAction="ifRoom"
If that still doesn't work for you, change the showAsAction back to never. If it now appears in the overflow menu, there is something wrong with your icon.
Try adding to your menu xml:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then use:
app:showAsAction="always"
I have one activity with 5 buttons. Each button should show up one activity (total 5 different activities). I tried many methods for multiple buttons but my app does not work. The first button of the activity starts its linked activity without any problem (btEtiologia and the activity is Etiology), but the second button still won't show up, and returns to the Main Activity (that is home layout). When I restart the app, I try with the second button, and third buttons and works, but the fourth button won´t show up and returns to the main activity. I have dowloaded the app on a samsung galaxy S2 and is the same than emulator. ¿Is anything wrong with my code?
Here is the java code of Inicio:
package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Inicio extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
Button btInformacion = (Button) findViewById(R.id.btInformacion);
btInformacion.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getBaseContext(), "Esta aplicación esta basada en el manejo de las infecciones de prótesis articulares de la IDSA", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btEtiologia).setOnClickListener(this);
findViewById(R.id.btAlgoritmo).setOnClickListener(this);
findViewById(R.id.btAntibioiv).setOnClickListener(this);
findViewById(R.id.btAntibioral).setOnClickListener(this);
findViewById(R.id.btRetirada).setOnClickListener(this);
}
public void onClick(View arg0){
// create a general intent
Intent intent = null;
// define an intent for all cases
switch(arg0.getId()){
case R.id.btEtiologia:
// Setting intent for first button
intent = new Intent(this,Etiology.class);
break;
case R.id.btAlgoritmo:
// Setting intent for second button
intent = new Intent(this,Algoritmo.class);
break;
case R.id.btAntibioiv:
// Setting intent for third button
intent = new Intent(this,Antibioiv.class);
break;
case R.id.btAntibioral:
// Setting intent for fourth button
intent = new Intent(this,Antibioral.class);
break;
case R.id.btRetirada:
// Setting intent for fourth button
intent = new Intent(this,Retirada.class);
break;
}
this.startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.inicio, 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);
}
}
The main activity code is:
package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btInicio = (Button) findViewById(R.id.btInicio);
btInicio.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Inicio.class);
startActivityForResult(intent, 0);
}
});
}
#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;
}
return super.onOptionsItemSelected(item);
}
}
As per you code, your images are dragging the code to OutOfMemory exception. Though the images which you are using of 120 dpi, their size is double to 640*480. please change image sizes then the problem will be solved. I have changed the all the 5 image sizes and now i am able to navigate to all the screens by clicking the buttons. Make the images to around 640*480 in ldpi folder.
I am new to android studio and I am reading the tutorials on i-programmer located here (http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html?start=2)
The two objects in the environment are a button and a large text widget instanced
in android studio's xml designer.
The problem: A method for setting text referenced in the tutorial is showing this error message when I try to run the code: error: cannot find symbol method setText(String)
And this error shows up in the text editor: Cannot resolve method 'setText(java.lang.String)'
Provided Source Code:
package com.example.helloworld1.helloworld1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
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;
}
return super.onOptionsItemSelected(item);
}
public void onButtonClick(View v){
Button button=(Button) v;
v.setText("I've Been Clicked!"); // This is where the error happens
}
}
I think you meant to use button instead of v.
button.setText("I've Been Clicked!");