I have a problem, I want to add item in a toolbar (material design) programatically .
This is a my menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
I know change text , color , background, and #override listener
toolbar.setBackgroundColor
toolbar.setTextColor
toolbar.setNavigationIcon
toolbar.setText
I don't know how I can add a menu ítem with a category " android:orderInCategory="300"
Thanks.
Note : I have all fragment, without 1 Activity
Tree - > Activity - > Fragment1(here add menu item) - > Fragment2(add/remove menu item) - > Fragmentx ..
Try this Toolbar has option getMenu() which return menu
private static final String placeholder1="Something";
private static final int FIRST_ID=Menu.FIRST;
private static final int SECOND_ID=Menu.FIRST+1;
//To add an item
toolbar.getMenu().add(Menu.NONE,FIRST_ID,Menu.NONE,R.string.placeholder1);
toolbar.getMenu().add(Menu.NONE,SECOND_ID,Menu.NONE,R.string.placeholder2);
//and to remove a element just remove it with id
toolbar.getMenu().removeItem(FIRST_ID);
First of all put the item you want to display/hide in your xml file.
Let's say it's "action_settings" as mentioned in your code, override the "onCreateOptionsMenu"
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
MenuItem item = menu.findItem(R.id.action_settings);
if (yourConditionToShow) {
item.setVisible(true);
} else {
item.setVisible(false);
}
return true;
}
If you do not have access to the toolbar, you can do the following:
1 - Add the item to the menu through onCreateOptionsMenu():
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_item_id, menu);
return true;
}
2 - Create a boolean equal to true if the item must be visible
3 - Hide or show the item according to the value of your boolean in onPrepareOptionsMenu()
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mi = menu.findItem(R.id.my_item_id);
if(displayItem)
mi.setVisible(true);
else
mi.setVisible(false);
return super.onPrepareOptionsMenu(menu);
}
In your onCreateOptionmenu, try this code
//menu.add("groupId", "ItemId", "OrderID", "title")
MenuItem item = menu.add(1, 100, 300, "Settings");
item.setIcon(R.drawable.ic_launcher);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
you can also add setting like xml. You can check by itemid("100") in onOptionsItemSelected.
If you don't want to use 100 as ItemId, you can create ids resource file. And can use like this,
//R.id.action_settings is from ids resource file
MenuItem item = menu.add(1, R.id.action_settings, 300, "Settings");
1 Solution :
#Chitrang Thanks to idea, I solved with this line :
toolbar.inflateMenu(R.menu.menu_activity1);
toolbar.inflateMenu(R.menu.menu_activity2);
toolbar.inflateMenu(R.menu.menu_activityx);
Options select R.menu.item1,2,x
2 Solution :
Create custom toolbar : into "android.support.v7.widget.Toolbar" and can you managment all icons.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right"
android:layout_marginRight="15dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:id="#+id/imageView10"
android:src="#drawable/sun"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
In your menu XML file, you can add a menu item not visible (android:visible=false) and render visible when you want.
Related
I've created a custom ActionBar with a simple TextView, the text is supposed to be aligned to the center. Then I added a MenuItem with onCreateOptionsMenu method. The result was that the each of the components were at each side of the ActionBar, the menu-item was placed at the left corner and the text-view was placed at the right corner. How can I make the text-view remain centered despite adding menu-items to the action-bar?
Here is my custom action_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/app_name"
android:textColor="#ffffff"
android:id="#+id/action_bar_text"
android:textSize="18sp"/>
</LinearLayout>
Here is my menu.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.rashish.myapplication.MainActivity">
<item
android:id="#+id/back_action"
android:orderInCategory="100"
android:title="#string/back"
app:showAsAction="always"/>
</menu>
Here is my onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.menu_main, mMenu);
MenuItem item = menu.findItem(R.id.back_action);
item.setVisible(true);
return true;
}
and this is how I use my custom action bar in onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);
//MORE CODE
I want to add some buttons on top of the activity layout (marked it in the picture) but could not find how to do this. What phrases should i search for?
The buttons that appear there (both text and icons) are items in what's called the Options Menu. Developer guides for creating options menus are here: https://developer.android.com/guide/topics/ui/menus.html#options-menu
As Ben P said, thats called Menu.
You need to create an XML with the options, and in the activity render the XML.
Example, lets call this menu_test.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=".MainActivity" >
<item android:id="#+id/action_download"
android:title="#string/download_information"
android:orderInCategory="100"
android:icon="#drawable/ic_file_download_white_24dp"
app:showAsAction="always" />
</menu>
As you can see on the guide, showAsAction will display the icon if have one, or the title if it hasnt. If you remove that line, its added to the three points button.
Now in the activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_test, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_download) {
//YOUR METHOD HERE
return true;
}
return super.onOptionsItemSelected(item);
}
Hope it helps.
You need to remove actionbar from activity. You can set NoActionBar theme for the activity. And in your layout xml, you can add toolbar which includes buttons like following code.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131313"
android:minHeight="?attr/actionBarSize">
<LinearLayout
android:layout_width="wrap_content"
android:background="#00aaaaaa"
android:layout_gravity="right"
android:layout_height="match_parent">
<Button
android:text="Delete"
android:layout_width="wrap_content"
android:background="#000"
android:textColor="#fff"
android:layout_height="wrap_content"
android:textSize="16dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
And in onCreate() function, you can add following code:
Toolbar topToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
ActionBar actionBar = getSupportActionBar();;
actionBar.setDisplayHomeAsUpEnabled(true);
Since i'm new here i was wondering how could i add a TextView in the ActionBar
It should be shown in the right corner of the action bar.
The text for example is "Tell a friend"
Thank you for your help.
You just usethis function in your activity you can use the Custom layout for Activity...
sActionBar = getActionBar();
TabActivityPacs.sActionBar.setDisplayShowHomeEnabled(false);
TabActivityPacs.sActionBar.setDisplayShowTitleEnabled(false);
TabActivityPacs.sActionBar.setDisplayShowCustomEnabled(true);
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.custom_title,
null);
TabActivityPacs.sActionBar.setCustomView(actionBarLayout);
You can design watever you want in the Custom title layout
All the best..
First create a layout.xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/tell_a_friend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tell a Friend"
/>
</LinearLayout>
then in menu.xml write :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/tell_a_friend"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/layout"
/>
</menu>
then in Activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
then in onOptionsItemSelected() :
public boolean onOptionsItemSelected (MenueItem item){
switch (item.getItemId()){
case R.id.tell_a_friend:
Toast.makeText(this, "Tell a friend", Toast.LENGTH_LONG).show();
break;
}
return true}
hope this may work for you..
In my action bar, I have defined a menu item that can show text "DONE" by the code below:
Menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_register_text"
android:actionLayout="#layout/action_done_text"
android:title="#string/action_done"
android:showAsAction="always"/>
</menu>
action_done_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/expand_activities_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"
android:addStatesFromChildren="true">
<TextView
android:id="#+id/register_action_bar_done"
android:layout_width="53dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginRight="10dip"
android:gravity="center"
android:text="DONE" />
</FrameLayout>
I have onCreateOptionsMenu implement properly in the code, and the view can show the text correctly, but just when I tap on the DONE text, onOptionsItemSelected is not called. To me, it seems like the click event is not recognized.
I was wondering if the above way is not a good way to add a text menu item?
Use this as shown in onOptionsItemSelected not called when using actionLayout (SherlockActionBar)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.map_menu, menu);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.menu_more) {
itemChooser = item.getActionView();
if (itemChooser != null) {
itemChooser.setOnClickListener(this);
}
}
}
return super.onCreateOptionsMenu(menu);
}
My android application has an action bar item to add employees.
So here what I did is call custom layout named add_employee when this action bar item clicked.
<item android:id="#+id/action_add"
android:icon="#drawable/ic_action_add_person"
android:title="#string/action_add"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/add_employee"/>
Following image shows that custom layout:
This is a loyout xml file named add_employee. Add and cross marks are image views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_marginRight="12dp"
android:src="#drawable/ic_action_cancel" />
<EditText
android:id="#+id/editText1"
android:layout_width="228dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="40dp"
android:layout_toLeftOf="#+id/imageView1"
android:ems="10"
android:inputType="numberSigned" />
<ImageView
android:id="#+id/addemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_marginRight="26dp"
android:layout_toLeftOf="#+id/imageView2"
android:src="#drawable/ic_action_add_person" />
</RelativeLayout>
Now I want to get the entered value to this edit text field when add image view clicked. This operation should be handled in the same java class where I set the option menu also.
public class MainPortal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main_portal, menu);
return super.onCreateOptionsMenu(menu);
}
}
I have used setOnItemClickListener for imge view but it ends with a forced closed error. How can I do this task?
Please help me...
Thanks in advanced.
Did you create the actions in onOptionsItemSelected? Where is the code showing the implementation of the Action bar tabs?
this is how to implement an option on actionbar
#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_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
there you can implement whatever and however...
plus showing more code and LogCat would help understanding your exact issue