ActionBar onClick listener- this is possible? - java

How to get ActionBar onClick listener? I mean- global listener for whole view, not only ActionBar child views

Try to use a custom view.
Create the layout you want for your action bar
Add the android:onClick attribute in the xml
android:onClick="handleClick"
inflate it somewhere in your code
use ActionBar.setCustomView() method
Implement the onClick method in your Java
public void handleClick(View view) {
// code after click here
}
That should work !

Related

I have a Navigation Drawer and want open another isolated fragment

I have a navigation drawer and when i click on one button i want to go to other fragment where i can't access to the navigation drawer and anything else. I just want have a black arrow in the top-left to go back and return to the navigation drawer with all the fragments.
I tried somethings but don't works like i want normally navigator continues accessible and see bot layouts one over the other.
MAIN FRAGMENTS CONTROLLER
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawerLayout=findViewById(R.id.drawer);
NavigationView navigationView=findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
appBarConfiguration= new AppBarConfiguration.Builder(R.id.nav_home,
R.id.nav_user_data,R.id.nav_join_group,
R.id.nav_user_groups,R.id.nav_search_documents)
.setDrawerLayout(drawerLayout).build();
navController = Navigation
.findNavController(this,R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
}
#Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController,appBarConfiguration)
|| super.onSupportNavigateUp();
}
I'm trying with a botton on my ActionBar, when i click it should go to my new fragment (with a generic app back but with one back button)
You simply should specify your action in navigation.xml file like others.
It will change hamburger icon by itself because you synchronize your navigation controller with NavDrawer in this two lines of code:
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
We need to add one fragment in our navigation.xml (the file where we define our fragments associated with the Fragment and the layout) for the Fragment where we want to go. Add there an action with a unique id and where we want to go in the fragment where we are.
Sorry, i know it's a little bit confused.
NAVIGATION
We go from there
<fragment
android:id="#+id/nav_user_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.SeeAndModifyUserDataFragment"
android:label="User Data"
tools:layout="#layout/fragment_see_and_modify_user_data"
>
<action
android:id="#+id/action_nav_user_data_to_nav_modify_data"
app:destination="#+id/nav_modify_data" />
</fragment>`
To there
<fragment
android:id="#+id/nav_modify_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.ModifyUserDataFragment"
android:label="Modify User Data"
tools:layout="#layout/fragment_modify_user_data"
/>
With the action that we defined in the origen fragment.
ORIGEN FRAGMENT
And in the Fragment when you click on one button just happen this.
Navigation.findNavController(getView()).navigate(R.id.action_nav_user_data_to_nav_modify_data);

Adding a menu to an activity than a fragment

I have a MainActivity along with a fragment. I have a refresh menu option that will refresh the data (Using AsyncTask). Is there any difference in inflating the refresh menu option in the fragment than in the MainActivity?
Note: This is in context with Udacity's Developing Android Apps, Lesson 2.
Yes, if i understand right, you want to inflate your activity toolbar menu from the the fragment. If that is the case you can do it like this
Yout Fragment class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setHasOptionsMenu is important
//it's telling the parent activity that he wants to participate in inflation of the menu
setHasOptionsMenu(true);
}
//Rest of your methods (onCreateView, onPause, onResume etc...)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflate the menu file
inflater.inflate(R.menu.your_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//handle click
return true;
}
return super.onOptionsItemSelected(item);
}
You can also use setMenuVisibility(boolean) if you want to hide/show menu in certain childfragments
If you inflate the menu from within the activity, it will be shown in all of the fragments you load inside the activity, but the action will be available to activity only (This is good if you want to do some general stuff with your menu action like starting new activity, displaying information popup etc). If you inflate the menu from within the fragment you will be able to handle menu items from withing the fragment, which will allow you to create more specific menu actions based on which fragment is currently active. For example if you have viewpager with 3 different fragments lets say:
FragmentOne for image browsing
FragmentTwo for video browsing
FragmentThre for Text browsing
Lets say you want to allow users to upload the images only, and you want that upload button to be located in the menu.
If you inflate the the menu from within the activity your upload button will be visible inside all of your fragments, and you would have to create a custom/logic for showing hiding menu items. If you create a menu from within the fragment, you will be able to handle and show the menu for the fragment you need
Long story short i think this depends on the use case of the activity/fragment and what you want to achieve with it

Android - Get Other Layout Widget

I've been trying to get a widget from another layout for past hours but still no luck. Can someone tell what is wrong with my current code?
public void refreshClicked(View view){
tab1 = LayoutInflater.from(view.getContext()).inflate(R.layout.tab_1, null);
Button b = (Button) tab1.findViewById(R.id.refresh);
b.setText("Updating...");
}
I've hooked my button onClick to refreshClicked(View view) method.
This method is under my MainActivity.java and I'm trying to get the Button with id refresh from tab_1.xml layout but the button text is not updating.
Thanks!
You seems to inflate a layout but never attach it to a parent view as the ViewGroup parameter is null. Then this layout inflated must not even been displayed anywhere.
If the button b already exists, no need to inflate the layout and retrieve the button form there, it will a different button.
Just retrieve the button from the actual existing layout.

Replace FrameLayout content from a custom view

I'm currently working on creating a custom view which uses a xml layout file to render the view on the canvas. In this file I have a FrameLayout that is meant for dynamic content. I'm trying to change the content and in the custom view class with this method.
private void setFragment(Fragment fragment)
{
((ActionBarActivity) getContext()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_container, fragment)
.commit();
}
The problem with this is that I'm using the FragmentManager from the Activity this view is embedded in. Because of this the FragmentManager is referencing the Activity's layout file which does not contain my FrameLayout. I need this to be able to modify the content in the custom view's layout file so it can access the FrameLayout and change the content.
How can I go about accomplishing what I want? Any idea?
use callback pattern like here. But define an interface in your view class and implement it in activity.

modify Action Bar Sherlock theme to have no action bar

I am using ABS for my application. But I do not want my start screen (the first activity) to have action bar. How should I modify the theme to achieve this?
Many thanks in advance.
You could set your activity's theme attribute in AndroidManifest.xml file to one of the following based on your requirement:
android:theme="#style/Theme.Sherlock.Light.NoActionBar"
android:theme="#style/Theme.Sherlock.NoActionBar
call hide() on your ActionBar object, inside the onCreate() of your FirstActivity
if you are using Sherlock
getSupportActionBar().hide();
add following code in your activity :
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
ab.hide();

Categories