How to add action bar to Android app? - java

I just start with Android. Want to add action bar following the tutorial. I was added res/menu/main_activity_actions.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
NecoSeparator:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
In MainActivity.java i do:
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);
}
But Eclipse says that cant find : R.menu.main_activity_actions.
What's wrong with this code? I have to do something more with this xml file?
UPDATE
MainActivity.java
package com.example.necoseparator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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);
}
}

I Think main problem is to saving your menu layout, if you have an error in file its might happened. please change your menu file with my code and save that and import R file (make sure that is not android.R and its must be YourPackageName.R) then clean your project and run.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="filtered"
/>
<item
android:id="#+id/menu_load"
android:icon="#drawable/ic_luncher"
android:orderInCategory="200"
android:showAsAction="always"
android:title="update"/>
</menu>

Remove this line xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto and try again
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>

You can use Toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
and add list of menus

Related

Changing the popup menu size

Currently, I am attempting to creating a popup menu on an ImageButton which works, though I want the size (width) of the menu to be smaller as well to inline them side by side? But that is not the main issue it is the size(width) I would like to reduce either for a png or just the word itself. I'm hoping you guys could help me figure this out. I just spent 3days figuring out the menu. Also, I have tried adding some padding and the android: width/android:layout_width. None of these have worked and I'm hoping there is a way around this issue.
Main.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class MainActivity extends AppCompatActivity
{
public WebView webView;
public EditText search_bar;
private ImageButton options_menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
//Open in Equinox instead of Deafult Browser
webView.setWebViewClient(new
WebViewClient());
//WebView - JavaScript-WebViewSettings-HomePage
final WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.loadUrl("https://www.Google.ca");
//OptionsBtn
options_menu = (ImageButton) findViewById(R.id.options_menu);
options_menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, options_menu);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
if(webView.canGoBack()){
webView.goBack();
}
else{
webView.reload();
}
return true;
case R.id.forward:
if(webView.canGoForward()){
webView.goForward();
}
else{
webView.reload();
}
return true;
case R.id.refresh:
webView.reload();
default:
return false;
}
}
});
popup.show(); //showing popup menu
}
}); //closing the setOnClickListener method
//"Go" on Keyboard with search
search_bar=(EditText)findViewById(R.id.search_bar);
search_bar.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if(arg1 == EditorInfo.IME_ACTION_GO)
{
if (search_bar.getText().toString().contains(".com")) {
webView.loadUrl("https://".concat(search_bar.getText().toString()));
} else {
webView.loadUrl("https://www.google.ca/search?q=".concat(search_bar.getText().toString()));
}
}
return false;
}
});
}
public void onBackPressed(){
if (true){
webView.goBack();
}
else{
webView.reload();
}
}
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/back"
android:title="#string/back" />
<item
android:id="#+id/forward"
android:title="#string/forward" />
<item
android:id="#+id/refresh"
android:title="#string/refresh" />
I can reduce the height and text size of the menu items with the help of below codes:
res/values/styles.xml:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:dropDownListViewStyle">#style/PopupMenuListView</item>
<item name="android:actionBarWidgetTheme">#style/PopupMenuTextView</item>
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<!-- Change Overflow Menu ListView Item Height & Property -->
<item name="android:listPreferredItemHeightSmall">10dp</item>
<item name="android:listPreferredItemPaddingLeft">5dp</item>
<item name="android:listPreferredItemPaddingRight">5dp</item>
</style>
<!-- Change Overflow Menu ListView Divider Property -->
<style name="PopupMenuListView" parent="#android:style/Widget.Holo.ListView.DropDown">
<item name="android:divider">#FF0000</item>
<item name="android:dividerHeight">2dp</item>
</style>
<!-- Change Overflow Menu ListView Text Size and Text Size -->
<style name="PopupMenuTextView" parent="#style/android:Theme.Holo.Light">
<item name="android:textColor">#00FF00</item>
<item name="android:textSize">5sp</item>
</style>
<!-- Change Overflow Menu Background -->
<style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#888888</item>
</style>
</resources>
Add the above codes in styles.xml(v11) and styles.xml(v14).
For More Detail:
Refer Action Bar Overflow Menu Customization

Items in menu not showing action bar, rather being displayed without any icons in three dots

Previously my app name was being displayed hence I had to use getSupportActionBar().setDisplayShowTitleEnabled(false) to remove the text, but now in its place a blank action is displayed with three dots on the side showing the list items that I have defined.
I want Refresh and Back actions to be displayed directly into the action bar with their respective icons
eventdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
app:showAsAction="always"
android:title="Refresh"
android:icon="#drawable/ic_action_refresh"
/>
<item
android:id="#+id/action_settings"
android:title="Settings"
app:showAsAction="ifRoom"
>
</item>
<item
android:id="#+id/action_back"
android:title="Back"
android:icon="#drawable/ic_action_back"
app:showAsAction="always"
/>
</menu>
EventDetails.java
public class EventDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarEventDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.eventdetails, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_back:
Intent action_back = new Intent(EventDetails.this, EventView.class);
startActivity(action_back);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Your xmlns:app is not setup properly
change:
xmlns:app="schemas.android.com/apk/res-auto"
to
xmlns:app="http://schemas.android.com/apk/res-auto"

Cant use Appbar with Buttons

I started working with the Holo Theme, and I want to build my app with this Theme (without using the Compact)
The problem is that my App bar is whithout the Buttons. (I cannot use the ifRoom option for showing the menus icons).
my menu code:
<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=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="105"
app:showAsAction="ifRoom"
android:title="#string/action_search"
/>
<item
android:id="#+id/action_edit"
android:icon="#drawable/ic_action_edit"
android:orderInCategory="110"
android:title="#string/action_edit"
app:showAsAction="ifRoom" />
and my Java Class:
package myfirsttry.another;
import android.app.Activity;
import android.content.Intent;
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.EditText;
public class MainActivity extends Activity{
public final static String EXTRA_MESSAGE = "linir.key";
#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);
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);
}
public void sendMessage(View view){
Intent intent = new Intent(this, secondActivity.class);
EditText editText = (EditText)findViewById(R.id.etInput);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}
Thanks
The app:showAsAction is only applicable if you use AppCompat as is strongly recommended for compatibility and consistency across all API levels.
If you'd like to only use the system provided action bar, then you'd replace all instances of app:showAsAction with android:showAsAction.

Android Action Button not shown in action bar

I want to display an icon on the action bar. But it always comes in the overflow list. Could you please help me up with a solution. This is my code.
CreateScheduleActivity.java
package com.mpeers.ui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class CreateScheduleActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_schedule);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, 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);
}
}
action_bar.xml in menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/plusicon"
android:title="CS"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
If you are using the appcompat support library, you need to define
xmlns:app="http://schemas.android.com/apk/res-auto"
in the parent tag and then use app:showAsAction for controlling the visibility of the action items.
If you are not using the appcompat support library, you can use the android:showAsAction tag for controlling the visibility of the action items.
define xmlns:app="http://schemas.android.com/apk/res-auto" in parent tag.
then use app:showAsAction="ifRoom"
Following is the example code.
<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.taazi.android.MainActivity" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_right_drawer"
android:icon="#drawable/ic_action_play_over_video"
android:title="#string/action_right_drawer"
app:showAsAction="ifRoom"/>
</menu>

No actions or icons showing on the actionbar; only in overflow

I'm not really new to programming but a complete fresh noob regarding android and java.
I recently made the decision to programm an Android App using this tutorial.
I got till the actionbar and can even display some actions like search and settings in the overflow. What I cant manage to do is to let an action appear on the actionbar(with or without icon).
I managed to it once with this tutorial but I somehow cant repeat it on my actual "Tutorialapp".
Here is my code:
main.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.newapp.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:orderInCategory="1"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
</menu>
MainActivity.java:
package com.example.newapp;
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.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.newapp.MESSAGE";
#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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() {
Toast.makeText(this, "This is a Search Button", Toast.LENGTH_SHORT).show();
}
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);
}
}
try swapping the menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="101"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
I dont know why, but you need to specify in java code the action always. I had the same problem, and I solved it like that:
#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);
menu.findItem(R.id.actionSearch).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
Hope it solve your problem. :D!! Good Luck!

Categories