Android Action Bar Search - java

I am a newcomer to android and am using Eclipse to learn to write action bars.
Code for res/menu/main:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
Code for MainActivity.java:
public boolean OnOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, there is a bug regarding R.id.action_search. It is said that it cannot be resolved or is not a field. How can I solve it?
One more question, am I going to define openSearch() and openSettings by myself or they are defined?
Thank you very much.

I believe that your project have to be built automatically to have the R.id working properly.
For your second question, if you want to open the system settings you should look here: How to open Settings of Android Phone on a button click in our Android App
For the search, I don't know if you're looking for a search bar or opening Google search, but for a search bar you should look at the section Add an action view on this page: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

Related

How to make navigation drawer text appearance colored?

How to make navigation drawer text appearance like below image?
I want to design navigation drawer like this.
How to change text color, icon color and background color when I click on navigation drawer item?
Please follow the below steps:
Create a selector drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is used when the Navigation Item is checked -->
<item android:color="#E91E63" android:state_checked="true" />
<!-- This is the default text color -->
<item android:color="#android:color/black" />
</selector>
Now add the below 2 attribute to your navigation view.
app:itemIconTint="#drawable/navigation_menu_selector"
app:itemTextColor="#drawable/navigation_menu_selector"
You can manually tell which item should be checked (Make sure your menu group should have single checkableBehaviour):
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.any_id:
menuItem.setChecked(true);
break;
case R.id.any_id:
menuItem.setChecked(true);
break;
case R.id.any_id:
menuItem.setChecked(true);
break;
case R.id.any_id:
menuItem.setChecked(true);
break;
}
return false;
}
Hope it will work for you.
Thanks.
use the following attribute to set the colour to whatever you like:
drawerLayout.setScrimColor(Color.PURPLE);
EDIT: I must have misunderstood your question. Do you have a drawer menu and want to change the shadow colour, or do you not have a drawer menu at all? If yes to the former, my previous answer should cover it, if yes to the latter, have a look at the following from the developers website:
https://developer.android.com/training/implementing-navigation/nav-drawer

How to Select any other button in Bottom navigation bar instead of first by Default

I'm creating android app which contain bottom navigation bar with 4 buttons. I would like to Preselect the 3rd button when initially app is launches rather then the First tab by which is selected by Default. I have used replace frgment to load Third Fragment initially with below code
secondFragment = new second();
replaceFragment(secondFragment);
But still, the first tab is active by default instead of the third although third fragment loads successfully
Here is the xml file from menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/bottom_action_home"
android:icon="#drawable/ic_visibility_black_24dp"
android:title="I"
app:showAsAction="always|withText" />
<item
android:id="#+id/bottom_action_notif"
android:icon="#drawable/ic_visibility_black_24dp"
android:title="II"
android:checked="true"
app:showAsAction="always|withText" />
<item
android:id="#+id/bottom_action_account"
android:icon="#drawable/ic_visibility_black_24dp"
android:title="III"
app:showAsAction="always|withText"/>
<item
android:id="#+id/bottom_action_chat"
android:icon="#drawable/ic_visibility_black_24dp"
android:title="IV"
app:showAsAction="always|withText"/>
</menu>
and activity xml file is
<android.support.design.widget.BottomNavigationView
android:id="#+id/mainBottomNav"
android:background="#color/colorblog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/bottom_menu"
android:layout_alignParentBottom="true"/>
and my java file is
mainbottomNav = findViewById(R.id.mainBottomNav);
BottomNavigationViewHelper.removeShiftMode(mainbottomNav);//disable BottomNavigationView shift mode
// FRAGMENTS
mainbottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.bottom_action_home:
return true;
case R.id.bottom_action_notif:
return true;
case R.id.bottom_action_account:
return true;
case R.id.bottom_action_chat:
default:
return false;
}
}
});
I would like to have Third Button active when application is launched i.e case R.id.bottom_action_account:
Thanks in advance.
Add this line to your code
mainbottomNav.setSelectedItemId(R.id.bottom_action_account);
If you want that part of your navigation bar to be highlighted then use as mayank said:
navBar.setSelectedItemId(R.id.menuItem);
but if you're talking about when the activity starts, either change your setContentView to the correct activity, or use setActivity to complete change to a new intent/class.
In your Java file, you can add code that looks like so:
mainBottomNav.setSelectedItemId(R.id.bottom_action_account);
You can add this to your onCreate at the end so that way it is made after the view is made so the end-user will see the third tab selected.
Cheers.

Up Navigation - transition does not slide from right to left

I've created some custom transitions so when a button is clicked the pages slide in and out. This works perfectly for when a page is entered (slides from left to right) and when the back button is clicked (slides from right to left) but when the "up" button is clicked in the action bar, the page slides from left to right when I want it to behave like pressing the back button. Here's is the code I am using in the styles.xml file:
<style name="YourAnimation.Activity" parent="#android:style/Animation.Activity">
<item name="android:activityCloseEnterAnimation">#anim/slide_in_left</item>
<item name="android:activityCloseExitAnimation">#anim/slide_out_right</item>
<item name="android:activityOpenEnterAnimation">#anim/slide_in_right</item>
<item name="android:activityOpenExitAnimation">#anim/slide_out_left</item>
</style>
Any help would be appreciated. Thanks!
You could try using overridePendingTransition() but you'd have to insert it everytime you switch activities.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
overridePendingTransition(R.anim.enterAnim, R.anim.exitAnim);
return true;
...
You'd also might want to check how you implement onClick() for the up-button. Here I'm calling super.onBackPressed() which calls finish() on the activity.

How to hide menubar but still access it through menubutton

So this is the deal, I have the following
And I want to remove it but still be able to access the menu item through the menu button, is this possible?
Here is my menu xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/updateShares"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Refresh"/>
</menu>
And here is my code creating the menu
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_action_bar_main, menu);
return true;
}
EDIT_____________________________________________________________________
I still want the "refresh" menu to appear when you press the menu button on the phone, I just want to remove the menu bar, thats why I can't set any option menu to visible=false because then I can't see them when pressing the menu bar.
I am not sure with the mark166 answer. You can try this link also. And another thing what i have done and you can try is....
Just don't inflate the layout on click of Menu. Even dont create menu.xml file.
You can try with the following code in java file
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
Toast.makeText(getApplicationContext(), "Do what you want to show here", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
It works for me and using same thing in app.
You just comment your code for the menu and try this one.
Hope it will help you.
You can just add in your onCreate following snippet:
this.requestWindowFeature(Window.FEATURE_NO_TITLE); so your title bar will gone.
This may help you with removing menu item https://stackoverflow.com/a/13099201/1045579
as per your second screenshot change the target SDK version to 4.0.3 so the menu ... will not visible.

Android Hardware Menu button code not working properly! need help

I love this site all you guys are awesome! but here is another problem I have:
In my app I have a webview that displays a website in the entire screen, I have made a code to show a menu through pushing the phone's menu button from where i want 2 things to happen 1st menu item Go back to main screen of the app, 2nd menu item quit the app or exit the app.
First problem:
after pressing the menu button it shows the menu... if I press it again it shows the two choices twice, if I press it again now both items shows 3 times and so on!
Second Problem:
after choosing any of the two choices nothing happens!
here is my code please tell me what I'm doing wrong!
Thanks
menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menuToMenu"
android:title="Menu Principal"
/>
<item
android:id="#+id/menuToSalir"
android:title="Salir"
/>
</menu>
Backtomain.java
import android.app.Activity;
import android.os.Bundle;
public class Backtomain extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and where I call the menu:
public boolean onPrepareOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mostrar = getMenuInflater();
mostrar.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuToMenu:
startActivity (new Intent("my.app.BACKTOMAIN"));
return true;
case R.id.menuToSalir:
finish();
System.exit(0);
return true;
}
return false;
}
You are calling super.onCreateOptionsMenu() from onPrepareOptionsMenu(). And, you are inflating the same options into the menu in onPrepareOptionsMenu(). Rename onPrepareOptionsMenu() to onCreateOptionsMenu(), and it will probably behave better.
Also:
If you think the my.app.BACKTOMAIN activity is running, you probably want to add FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_CLEAR_TOP to the Intent.
Get rid of the menuToSalir menu choice. No well-written Android application will call System.exit(0). Users leave your application by pressing the HOME button, no different than they might in a Web app.
To fix your first problem try onCreateOptionsMenu(), rather than onPrepareOptionsMenu().
I am not positive how to fix second problem, I usually create my menus all in java instead of using xml like you have.
I found the problem it works! I was missing the "s" at...
public boolean onOptionItemSelected(MenuItem item){
the right way is
public boolean onOptionsItemSelected(MenuItem item){
thanks for your help Tim and CommonsWare
I ran into this problem. In my case I had set the background color of the activity screen to black. When the menu popped up it had a transparent background and black text so I didn't see it working.

Categories