Up Navigation - transition does not slide from right to left - java

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.

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.

Removing Title from the actionbar in Android 3.0

I have a requirement in which I have to make an application that works from Android 3.0 onwards. Naturally I didnt go for ActionBarSherlock.
Now I replaced the background of the actionbar using the following code from the official google resource .
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/navigation_bar</item>
</style>
As a result , I now have the proper action bar .
Now the problem is , when I press the home icon ( ic_launcher.png) , when the icon is in pressed state I see the activity title and a carrot sign appear . I do not want such. I just want the home button to be clickable and would like to see nothing while the button is pressed .
I did succeed in removing the activity title that appears by using
getActionBar().setDisplayShowTitleEnabled(false);
however the default "<" sign still appears when the home icon is pressed .
Question 1 : How can I remove the "<" sign when the home icon is in pressed state
Question 2 : How can I remove the default options icon (on the right side , the three dots) ?
Thanks a bunch
For home Icon remove
getSupportActionBar().setIcon(R.color.transfrant);
Remove the title bar
in manifest under the activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
Change the action bar name. Android manifest under the activity
android:label="#string/app_name"
change to
android:label=" "
Question 2 : How can I remove the default options icon (on the right side , the three dots) ?
remove this method from your Activity class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}

Android Action Bar Search

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

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