setDisplayHomeAsUpEnabled() + Up Arrow? - android

We can use the setDisplayHomeAsUpEnabled() method on an activity to display the "up" arrow on the action bar.
If I understand correctly, we're only really supposed to show the up arrow if we're not the root activity.
Clicking the "up" arrow should take us to the root activity. Is that all there is to it? I should then add this call to all my activities like so:
class RootActivity {
public void onCreate() {
setDisplayHomeAsUpEnabled(false);
}
}
class AppleActivity {
public void onCreate() {
setDisplayHomeAsUpEnabled(true);
}
}
class OrangeActivity {
public void onCreate() {
setDisplayHomeAsUpEnabled(true);
}
}
...

Yes, that's pretty much it.
Clicking the up-arrow should take you towards the root.
and don't forget to actually handle the click in onOptionsItemSelected by checking the item id against android.R.id.home

Related

Detecting when floating action button is hidden

I have a scrolling activity, and I want to be notified when the floating action button is being hidden.
In order to do so, I created MyFab which extends FloatingActionButton and an interface (to use as a callback) to call when the floating action button is being hidden, but I can't quite figure out what method is being called when I scroll and the floating action button disappears (I want to override the method, so I can call super and the callback that is implemented in the activity)
The interface:
public interface OnAppearanceChangedListener {
void onAppearanceChanged(int alpha);
}
My Floating Action Button:
public class MyFloatingActionButton extends FloatingActionButton {
private OnAppearanceChangedListener mListener;
public void setListener(OnAppearanceChangedListener mListener){
this.mListener = mListener;
}
/*insert code here to call mListener.onAppearanceChanged(),
probably by overriding some method */
}
The activity:
public class MainActivity extends Activity implements OnAppearanceChangedListener {
#Override
public void onAppearanceChanged(boolean visible) {
// I want this function to be called when Floating Action Button hide/show state is changed
}
}
--edit--
figured it out. animate() was the function I was looking for. It is being called to animate the floating action button shrinking action (what I was referring to as hiding)
Your question is quite ambiguous but if what you are looking for is to animate (hide) the fab on scroll you might want to refer to this tutorial. Otherwise explain your post to gain more help in community.
Cheers

Back Button in ActionBar of App

Generally switching from one activity to another activity hamburger icon is replaced with back arrow. I want to control the functionality of that arrow. I have seen many contents here but most of them were related to hardware's back button. How can I control that?
I am trying the functionality in case of fragments. Also I have Navigation drawer attached with the hamburger icon.
I tried this-
if(id == android.R.id.home){
getSupportFragmentManager().beginTransaction().replace(R.id.main_container, new AmbulanceMap()).commit();
getSupportActionBar().setTitle("Book A Ride");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
but doesnt work as I hoped.
I want my back button to change the fragmentto previous fragment.
I had the same problem once. Just like you, things like checking if Android.R.id.home is clicked didn't work.
But I solved it using that:
Set navigation listener to toolbar:
toolbar.setToolbarNavigationClickListener(v -> onBackPressed());
If it should be within fragment:
Create an public method in activity.
In fragment's onAttach (or later) cast getActivity() to your activity and call method you was defined previously.
Example:
// YourActivity
public void setHomeListener(OnLickListener listener){
toolbar.setToolbarNavigationClickListener(listener);
}
//Fragment's onCreate
((YourActivity)getActivity()).setHomeListener(v -> onBackPressed());
//Fragment's onDestroy
((YourActivity)getActivity()).setHomeListener(null);
And, of course, set home us up enabled to show back arrow.
EDIT
if you don't use labmdas u should use:
(YourActivity)getActivity()).setHomeListener(new OnClickListener() {
#Override
public void onClick(View v) {
YourFragment.this.onBackPressed();
}
});
The back button of the ActionBar is a menuItem so you need to override onOptionsItemSelected like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
//Your back button logic here
break;
}
return true;
}
Also donĀ“t forget to add getSupportActionBar().setDisplayHomeAsUpEnabled(true); after setting the Toolbar

Is it possible to set different actions for floating action button in differet fragments?

I am working or an app where I have 4 fragments in viewpager and floating action button (fab). When I click on fab there appear 3 subfabs. Each of them start different activities where user can search data from different fragments. Is it possible to set fab so that if I click on it while I'm in first fragment it'll open an activity for searching inside this fragment, onClick inside second - search for second and etc. The issue is that now clicking on sub fab while I'm in some fragment I can search data from another fragment too and it's a bit weird. I understand question is kinda strange, if something is unclear I'll explain further
You can make FAB button public and implement onClick listener in each fragment.
You should override setUserVisibleHint and put your code onResume so getActivity() will not return null. Fab button will have different actions in different fragments.
Here's an example, inside each fragment when you want Fab click listener:
#Override
public void setUserVisibleHint(boolean visible)
{
super.setUserVisibleHint(visible);
if (visible && isResumed())
{
onResume();
}
}
#Override
public void onResume()
{
super.onResume();
if (!getUserVisibleHint())
{
return;
}
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do what you want
}
});
}
You can check in onclick method of floating action button which fragment is currently opened and calling it.
Call findFragmentById() on FragmentManager and determine which fragment is in your R.id.frameTitle container.

BackButton in the Fragment cannot be disabled

I have a mainActivity with viewPager and three tabs, each tab has its respective fragment. i want when this mainActivity is launched and the backButton is pressed, nothing to happen.
to achieve this, i override onBackPressed() inside the mainActivity the one has viewPager with tabs, but when the mainActivity ia launched one of the tabs, which represents Fragment, by default appears, and when i press the backButton, i go back, which means the Backbuton is not disabled.
that is it, in the mainActivity, i have:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Log.w(TAG, "#onBackPressed().");
}
and he backbutton is not disabled in the Fragment, each time i press it, the logCat displays the message in the onBackPressed and i go back, and i do not want this, i want while i am in the Fragment "any one of the tabs", the backButton should has no effect.
please let me know how to achieve this?
Try not calling
super.onBackPressed();
Although the log will still print because the method will be called, this will stop Android performing it's logic and going back.
If you want the fragment tabs to do nothing, but the viewpager to handle it, do something like this
#Override
public void onBackPressed() {
if (getCurrentTab == 0) {
super.onBackPressed();
} else {
//This will stop the fragment tabs from doing anything when back is pressed
}
}
If you need avoid the back button, comment the super call //super.onBackPressed();

In Android, override a method using Button onClick

I've got a Main Activity which is a search interface, when the user clicks a result in the list, the result details are displayed in a Fragment. The Main Activity has an Action Bar, but in the details Fragment, the Action Bar is hidden. To hide the Action Bar in the Fragment, I'm calling a method in the Main Activity from the Fragment Activity like this: ((SearchInterface)getActivity()).hideABar();. Then when the user clicks the back button, they go back to the search interface and the Action Bar reappears. Doing that is a bit more complex than simply calling a method. I set up an Interface in the Main Activity and override the method in the Fragment like this:
Main Activity:
public interface Callback {
public void onBackPressedCallback();
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(fragBackPressed != null)
fragBackPressed.onBackPressedCallback();
checkActionBarState();
}
...
public void checkActionBarState(){
bar_AB = getSupportActionBar();
boolean barVisible = bar_AB.isShowing();
if(barVisible){
Toast.makeText(getApplicationContext(),"ActionBar WAS Visible",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"ActionBar WAS NOT Visible",Toast.LENGTH_LONG).show();
bar_AB.show();
}
}
Fragment:
#Override
public void onBackPressedCallback() {
Log.d("SSIVideoFrag", "checking action bar state");
}
So now, when the user presses the Back button while in the Details Fragment, they go back to Main Activity and the Action Bar reappears. Now I'd like to add a button in the Details Fragment that does what clicking the Back button does, but I can't figure out how to do it. I don't know how to override the onBackPressedCallback() method from within the onClick of a Button:
back_BTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//--- what to do here?
}
});
simple:
getFragmentManager().popBackStack();
https://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()
ps.: that's not an actual back press, your onBackPressed will not be called. But the fragment will be removed, just like in a back press.

Categories

Resources