why the actionBar is placed at the bottom? - android

I was following some tutorials to learn how to create an activity with ActionBar, I followed the tutorial literally, but in the end, I found that the action bar is unlike the action bar resulted in the tutorial, mine is placed at the bottom of activity and appears only when I press the optionsMenueButton, while the result from the tutorial is at the top. And my activity extends ActionBarActivity Any idea why that happens.
Java_Code:
public class ActionBarActivityTest00 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_bar_activity_test00);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_bar_activity_test00, menu);
return true;
}
xml_file:
<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.actionbaractivitytest.ActionBarActivityTest00" >
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/search"
android:title="Search"/>
<item
android:id="#+id/action_copy"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/copy"
android:title="Copy"/>
<item
android:id="#+id/action_share"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/share"
android:title="Share"/>

Related

How to show menu item in action bar?

I want the menu items to show on action bar. There is a lot of space on the ActionBar, but still they don't show up. Every item shown as three dots, why?
.java file
public class GalleryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/tools">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/shareId"
android:title="share"
android:icon="#drawable/share"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/delete"
android:title="delete"
android:icon="#drawable/delete"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/searchId"
android:title="search"
android:icon="#drawable/search"
MissionAndroid:showAsAction="ifRoom"/>
</menu>
Currently, you're using "http://schemas.android.com/tools" which does not offer the functionality you're looking for and your showAsAction modifiers are being ignored.
Try updating your main_menu.xml with the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/apk/res-auto">
...
</menu>
Better yet, to follow convention, replace MissionAndroid in this file with app:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
app:showAsAction="always"/>
...
</menu>

Icon not able to show on action bar Android

I want to show a icon button the right side of my actionbar.
This is my menu_drawing 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=".DrawingActivity">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_delete_white_36dp"
android:title="tick"
android:orderInCategory="0"
app:showAsAction="always" />
</menu>
This is my activity
public class pollActivity extends Activity {
Button SEND;
Button CANCEL;
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_drawing, menu);
return super.onCreateOptionsMenu(menu);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poll_layout);
}
}
This is my style
<style name="MyActionBarTheme" parent="android:Widget.Material.Light.ActionBar">
<item name="android:background">#255e7c</item>
<item name="android:titleTextStyle">#style/TitleBarTextColor</item>
<item name="android:homeAsUpIndicator">#mipmap/backbtn</item>
</style>
What is the problem? The icon is not showing and only title appear as "word" on my overflow button?

SearchView Widget not showing up in Action Bar

This can be considered as a extension of this Question
I have added following to the code to get a searchView Widget
res/menu/main_activity_actions.xml
<?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/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Mainactivity.java
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.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
System.out.println("TESTING: "+searchView);
return super.onCreateOptionsMenu(menu);
}
---
---
}
The app shows up fine but on touching the search button nothing shows up.
Have set the Sdk versions as follows:(4.0.3 -4.2.2)
android:minSdkVersion="15"
android:targetSdkVersion="17"
Change you menu to,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="always" />
</menu>
You're missing "collapseActionView" in your menu:
app:showAsAction="ifRoom|collapseActionView"

actionbar menu item onclick?

I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
I inflate the action bar menu with this on each activity I use 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.main2, menu);
return true;
}
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
Note:
ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import
import com.actionbarsherlock.view.MenuItem;
and not
import com.android.view.MenuItem;
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
which #adneal mentions.
In my opinion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreateDialog(getTaskId());
}
});
}
<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:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/add_text_id" android:title="Add"
android:icon="#drawable/ic_add_btn"
android:orderInCategory="100" app:showAsAction="ifRoom" />

MultiSelect Menu in Action Bar Sherlock

I wan to have one action bar with one "menu" button. (Until here I know how to do) But Now I want to do that, when the users Clicks on any subitem of the menu, don't close this menu.
Said in other words, I want to enable the multiselect menu in action bar sherlock, but I dont know how to do it.
Can someone explain me some way to achive it?
Thats what I have
public class MainActivity extends SherlockActivity{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setTitle("Testing");
actionBar.setSubtitle("test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
And this is the XML Menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu"
android:orderInCategory="0"
android:title="Menu"
android:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/subitem1"
android:title="SubMenu1"/>
<item
android:id="#+id/subitem2"
android:title="SubMenu2"/>
<item
android:id="#+id/subitem3"
android:title="SubMenu3"/>
<item
android:id="#+id/subitem4"
android:title="SubMenu4"/>
</menu>
</item>
</menu>

Categories

Resources