I have a problem, I would like to put in my Layout the Drawer Layout and the FlyOutContainer but in the error Log it shows an error:that DrawerLayout cannot be cast to FlyOutContainer what can I do to fix this problem. I need the Drawer Layout to go to the MainActivity.
Hope you can help me and sorry for my bad english.
The code in that demo inflates a layout and casts the root view of the Layout to FlyOutContainer. If you change your layout xml so that the root is now DrawerLayout, then that code no longer runs correctly and you are probably getting ClassCastException. You should do it this way instead (in onCreate()):
setContentView(R.layout.your_activity_layout);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_id);
FlyOutContainer flyOutContainner = (FlyOutContainer) findViewById(R.id.fly_out_container_id);
Of course, if you don't actually need a reference to these views/layouts, you can just stop after setContentView(...)
Related
I have trouble deciding where to set the image of an ImageView contained in the Android NavigationDrawer.
My Android app has a NavigationDrawer that contains an ImageView nested into a couple of other elements inside the NavigationDrawer header layout (app:headerLayout="#layout/navdrawer_header), and my navdrawer_header.xml has the following structure:
<LinearLayout android:id="#+id/navdrawer_header">
...
<ImageView android:id=#+id/image_view
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...
</LinearLayout>
I am setting the image (img.jpg in my drawable folder) via
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.img);
If called from OnCreate inside my MainActivity, the imageView will be null and the app crashes. The same if I move the above lines to OnPostCreate or even OnStart.
My understanding is that the view is not initialized yet at that point in time and therefore findViewById returns null. As a workaround, I have overwritten the OnDrawerOpened method and moved my code from above to that method.
This works just fine, but I am pretty sure it is not the best way to go since the method is called every time the drawer is opened. Instead, I would need a method that is only called once upon the creation of the drawer.
What is the best practice here, i.e. where should I set the resource of an ImageView that is contained in the NavigationDrawer?
I have tried omitting view. and experimented with different values of view, for example by assigning to it the NavigationView or DrawerLayout associated with my drawer, but it does not solve the issue.
Try this:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View view = navigationView.getHeaderView(0);
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.img);
I have an Activity which has Toolbars. When I move to Fragment A, the Toolbar needs to be changed specifically for Fragment A.
Fragment A contains 4 other Fragments as A is a FragmentPager.
I called the method of the Activity like :
((NewsfeedActivity)getActivity()).changeTitleBesideIcon("Purchases");
((NewsfeedActivity)getActivity()).changeIcon(getResources().getDrawable(R.drawable.purchase));
((NewsfeedActivity)getActivity()).changeBackground(getResources().getColor(R.color.purpleC));
((NewsfeedActivity)getActivity()).hideToolbarBottomMarketplace();
Somehow, the icon doesn't get changed, BUT the title does, along with the color.. I even set the method to setVisibility(View.VISIBLE); on the icon..
Is there a specific state where that kind of method needs to be called? Or am I just doing it wrong?
Update
This is my changeIcon method
public void changeIcon(Drawable imageDrawable){
toolbarImage.setVisibility(View.GONE);
toolbarIcon.setVisibility(View.VISIBLE);
toolbarIcon.setImageDrawable(imageDrawable);
}
toolbarImage is an ImageView located in the Toolbar
This is how it is :
<Toolbar>
<RelativeLayout>
<ImageView> //Toolbar Image
<ImageView>// Toolbar Icon
</RelativeLayout>
</Toolbar>
And I'm calling the methods in the onCreateView()..
try it.
public void changeIconToolbar(int resId){
getSupportActionBar().setHomeAsUpIndicator(resId);
}
it work for me
Well, now I can see that you possibly use Toolbar (rather than ActionBar) and once again I assume that the Toolbar is the toolbar added in API level 21 rather than AppCompat Toolbar.
In order to use this toolbar, I assume that you have called setActionBar (toolbar) to replace the ActionBar with the toolbar from your layout.
NOTE: Without calling this setActionBar (toolbar), your activity will continue using the default ActionBar (in onCreate()) and nothing will be shown.
Now, when the toolbar is in its place. I think your code will work.
PS: I recommend to use AppCompat Toolbar
I set up a toolbar in my main activity and when I go inside a fragment, I want to add a slider on it. If I had had the access to the Toolbar object, I would simply do:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Spinner mNavigationSpinner = new SpinnerTrigger(getSupportActionBar().getThemedContext());
toolbar.addView(mNavigationSpinner);
But if I get it using
((ActionBarActivity) getActivity()).getSupportActionBar()
I don't have any addView() method. So my question is, how can I add a view to the Toolbar in fragment if the Toolbar itself was created in an Activity.
I'm not sure if this is the best view of going about this, but I don't think I can have the Spinner in defined in the layout, because most of my fragments don't use it, they simply set a title to the toolbar. But at the same time, it would be great if I could define the toolbar once in the main activity and not redo it for every fragment.
Another way of achieving the same thing from Ellitz answer, inside the fragment access the toolbar (or any other view inside activity) directly:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
you can get it using
Toolbar refTool = ((NameOfClass)getActivity()).toolbar;
or, create an instance of your MainActivity, then, override onAttach(Activity activity) and assign your instance object of MainActivity to the activity in onAttach()
See toolbar main purpose is https://developer.android.com/reference/android/widget/Toolbar.html read here so there are nothing deference in toolbar and actionbar. so if you want to add view to toolbar before it set to Actionbar then toolbar.addView(your view); is fine but after apply apply to setactionbar(toolbar) or setSupportActionbar(toolbar) you can set view to actionbar.
ex. ((ActionBarActivity) getActivity()).getSupportActionBar().setView(Your view)
Thats it...
I would like to add a casting to what Budius said.
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
is the right way of doing it. Because
getActivity().findViewById(R.id.toolbar);
returns a view. This will give you error and you should cast it to Toolbar.
In my application I use DrawerLayout to show same help information. So I put it over actuall activity. Now I want to add TalckBack function and I cannot make that the any of view in DrawerLayout get the yellow rectangle (which marks focus in talkback mode).
The Drawerlayout view is loaded on demand by inflantign it.
drawerLayout = (DrawerLayout) ((Activity) mContext).findViewById(R.id.drawer);
drawerHelp = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.help, drawerLayout, false);
What I try:
in xml add:
android:focusable="true"
android:focusableInTouchMode="true"
android:importantForAccessibility="yes"
In code:
requestChildFocus()
requestFocus()
requestChildFocusFromTouch()
but still no luck.
So how to force the rectangle mark in talkback mode for ImageButton?
I solve my problem by adding
android:importantForAccessibility="no"
to all element in drawer layout and to other not nedeed elements in help layout.
I could not find a way to do a tablet multi-pane layout easily with NavigationDrawer. Play Music app does that.
I have used LOCK_MODE_LOCKED_OPENED but it opens the drawer on top of the content as expected and it cannot be closed. Therefore the content is not completely visible.
Do we have to do it manually?
The only way we found is to do it manually, we created a simple linear layout for tablet and check the view instance in activity:
View layout = findViewById(R.id.navigation_layout);
if (layout instanceof DrawerLayout) {
drawerLayout = (DrawerLayout) layout;
// initialization of drawer toggle etc.
...
} else {
// linear layout
getSupportActionBar().setHomeButtonEnabled(false);
}