I am new on android.
What I want to do is, when I press the icon on Actionbar the LinearLayout is showing.
Here's the picture
As you can see, there's a magnifier icon.
when I press that icon, the search bar below it will gone.
And when i press the icon again, it'll show again.
I already trying use setVisibility, but its show error.
Here's my Java code
MenuItem searchBar;
searchBar = (MenuItem) findViewById(R.id.search);
searchBar.setVisibility(View.GONE);
}
public void toggle_contents(View v){
if(searchBar.isShown()){
Fx.slide_up(this, searchBar);
searchBar.setVisibility(View.GONE);
}
else{
searchBar.setVisibility(View.VISIBLE);
Fx.slide_down(this, searchBar);
}
}
And Here's my XML code
<LinearLayout
android:id="#+id/search"
android:background="#color/gray74"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="7dip"
android:paddingBottom="7dip" >
<EditText
android:id="#+id/search_global"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/searchbackground"
android:text="Search"
android:textColor="#color/gray74" />
</LinearLayout>
Thanks before
First: A MenuItem is not a View and consequently it's not part of your layout. Therefore you shouldn't be using:
MenuItem searchBar = (MenuItem) findViewById(R.id.search);
But instead:
MenuItem searchBar = menu.findItem(R.id.search);
Second: When you make changes to your menu, you should call invalidateOptionsMenu().
By doing this you basically tell the menu that some changes have been made to it. This will make the menu recreate itself and request that the system call onPrepareOptionsMenu().
Related
I know how to set floating action button like shown in bellow image. But i need to show different layout on click or anytime when i want to show that layout. second layout is attached
How can i achieve this?
Refer this link Floating Action Button Toolbar https://github.com/AlexKolpa/fab-toolbar
There you need to add dependency in the project.
compile 'com.github.alexkolpa:floating-action-button-toolbar:0.5.1'
In your XML file,
<com.github.alexkolpa.fabtoolbar.FabToolbar
android:id="#+id/fab_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
tb:tb_anim_duration="500"
tb:tb_button_gravity="end"
tb:tb_container_gravity="center"
>
<ImageView
android:id="#+id/attach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_attachment_white_48dp"
android:layout_marginLeft="#dimen/icon_margin"
android:layout_marginRight="#dimen/icon_margin"
/>
<!-- More buttons can be added here -->
To hide or show the Toolbar, simply call hide() and show().
FabToolbar fabToolbar = ((FabToolbar) findViewById(R.id.fab_toolbar));
findViewById(R.id.attach).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fabToolbar.hide();
}
});
Is it possible to put the toggle drawer button on the right side of the action bar? The toggle drawer button is the three bar icon on the left side. It turns into an arrow when drawer is opened (hence the arrow icon). I've already set my drawer on the right, but the button is still on the left. Or should I just make a custom one like that? But how can I use the same animation?
Steps that you have to follow :
Make a menu.xml in menu folder :
Inflate this menu in your activity and implement action over it :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflating menu
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.drawer :
//Open Drawer from right side
drawerLayout.openDrawer(Gravity.Right);
// or
drawerLayout.openDrawer(rightSlider); //right slider is right listview
break;
}
return super.onOptionsItemSelected(item);
}
Finally in your layout xml file and inside drawer layout do like this :
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/right_slidermenu"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/white"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#android:color/transparent"
android:overScrollMode="never" />
Try this....it will help you..:)
This question already has answers here:
Actionbar not shown with AppCompat
(3 answers)
Closed 9 years ago.
I am using appcompat in my app. I want the menu items to show on actionbar or at least the overflow(3 dots) to show them when there is no room. There is lot of space on the actionbar, but still they don't show up. The menu flow raises from the bottom and that too only when menu button is pressed.
menu_activity.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_lang"
android:showAsAction="always"
android:title="#string/menu_lang"
android:icon="#android:drawable/ic_input_lang"/>
</menu>
activity:
#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_activity, menu);
return true;
}
This post says that it not works when hardware menu button is present. But other apps are able to show items on the same device. So, that answer seems to be incorrect. Can someone please help on this?
You seem to be using the wrong menu:
Your file is named "menu_activity.xml" and you inflate the menu with the Resource-Id: R.menu.reminder_menu
The Resource name of the menu should be the same as the file name, i.e.: R.menu.manu_activity
Try it with this again - I ran into this too once and it drove me nuts...
Update
After clarification that the above part was for obfuscation, please make sure that:
You extend ActionBarActivity.
You use (or extend) one of the Theme.AppCompat themes for the activity (or whole app)
Because on older devices, the Actionbar related attributes are not present, make sure that all these attributes in the XML are used with a custom namespace. Here that would be the showAsAction attribute, so that the compatibility library can pick them up.
You already had the custom namespace defined ("app", in the menu tag). You need to change the android:showAsAction tag to app:showAsAction according to the Android guide.
Your corrected menu_activity.xml would then look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_lang"
app:showAsAction="always"
android:title="#string/menu_lang"
android:icon="#android:drawable/ic_input_lang"/>
</menu>
The extended Android guide for actionbar covers these new and nasty traps...
Okay I think I found a solution for you. It is called Overflow Menu and you need to call the below method in your onCreate method.
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this and let me know if it works for you.
EDIT:
I think I understood your problem finally. I can give you one idea. If you do not want to use overflow menu and just display menu items as displayed in the screen-shot you have posted, you can create a layout on top of your activity.
You can take a linear layout, give a background that looks the same as action bar and place whatever icons you want to put there, and give functionality to them with onClickListener. Hope this will give you some idea. This way you can create your own custom menu. Try the layout below and replace ic_launcher drawable with menu icons. Then just set onClickListeners to them and perform whatever functions you want to perform inside onClick method.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#66000000" >
<ImageView
android:id="#+id/xMenuBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#drawable/ic_launcher" />
<TextView
android:id="#+id/xMenuTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chats"
android:textSize="18sp"
android:layout_toRightOf="#+id/xMenuBtn1"
android:layout_centerVertical="true" />
<ImageView
android:id="#+id/xMenuBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/xMenuBtn3"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/xMenuBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
Try in your activity onCreate() :
ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.app_name);
and then :
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getSupportMenuInflater().inflate(R.menu.menu_activity, menu);
return super.onPrepareOptionsMenu(menu);
}
I use ActionBarSherlock, in which I set the navigation mode to 'list'
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, this);
Is it possible to dynamically show a second spinner, depending on which item is selected?
Use a custom action bar layout.
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.action_bar_custom);
action_bar_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/action_bar_spinner_collection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
<Spinner
android:id="#+id/action_bar_spinner_collection_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
</LinearLayout>
Use this one..This may works...
Code:
getMenuInflater().inflate( R.menu.main, menu );
mSpinnerItem = menu.findItem( R.id.menu_spinner );
setupSpinner( mSpinnerItem );
Menu XML:
<item
android:id="#+id/menu_spinner"
android:actionViewClass="android.widget.Spinner"
android:visible="false"
android:showAsAction="always"/>
NEVER use NAVIGATION_MODE_LIST and onNavigationItemSelected it is not worth it !
#Override
public boolean onNavigationItemSelected(int position, long itemId)
You also cannot use menu's to do this:
#Override public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
due to inflation ordering.
Reasons:
(1) it generates a "hidden" spinner, for which you can not get the id.
(2) you cannot customize this spinner
(3) you save 30 lines of code, but are permanently limited if you want to add a second bi-directional spinner
(4) not even in the special case of "simple code" (one spinner), you lose to much.
(5) you cannot use tabs.
the key is actionBar.setCustomView(R.layout.action_bar_custom);
and spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()...
for each spinner.
Trust me I lost hours trying each solution.
I have in my activity ActionBarSherlock action bar. It is created with method onCreateOptionsMenu(Menu menu). How to leave this menu available like actionbar and create ordinary menu with some buttons?
Create a layout for your application. For example, create a button for your menu. And you can also use graphical UI android eclipse plugin to position it.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/testTitle"/>
</RelativeLayout>
In your activity class, which extends com.android.Activity,
Button testButton;
testButton = (Button) findViewById(R.id.testButton);
testButton .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code to perform on click of the menu button
}
});