I'm an Android beginner. I'm using Android Studio, and I create my activity with the NavigationDrawer.
So I have a ListView, but I want to add some other things to my Drawer.
I created a custom layout and inside NavigationDrawerFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout insideDrawer = (RelativeLayout) inflater.inflate(
R.layout.fragment_fragment_inside_drawer, container, false);
return insideDrawer;
}
And it works.
I have a ListView and a button inside the drawer.
I tried to access the list view with getActivity.findViewById(R.id.mylistview) but it doesn't work.
So how can I access my objects inside the drawer?
Thanks!
You are searching for your view from the activity, but because the view you are looking for is inside a fragment (we know there's a fragment here, because the onCreateView), you must search from the view that you inflate in that method.
After you return a view in onCreateView, you can use the getView() method of the fragment, then call .findViewById().
Or if you need to search for the view before its returned in onCreateView, after inflating it, just call insideDrawer.findViewById() -- since insideDrawer is your relativeLayout you inflate in that method.
Related
Like any other dynamic view we can add that view dynamically into layout, is it possible with recyclerview that we can add/create dynamically and attach into a viewgroup?
Thanks
RecyclerView.LayoutParams lparams = new RecyclerView.LayoutParams(
RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
RecyclerView recyclerView = new RecyclerView(this);
recyclerView.setLayoutParams(lparams);
rootview.addView(recyclerView);
Here rootview is linear layout or any other view group of your layout XML file after that you can add Adapter to this recycle view as usual. Here you can add any number of recycle view using rootview.addView(recyclerView);
I'm trying to inflate a layout with theme different from app default, and doing it like this:
final View view = context.getLayoutInflater().cloneInContext(new ContextThemeWrapper(context, getThemeForView())).inflate(R.layout.some_layout, root, false);
First time it's done view doesn't have theme I want, but main app theme. When view is recreated after few minutes, it has correct theme. Why is theme different when view is first created?
EDIT:
I can't set android:theme because I get theme dinamically.
I first created view in Activity.onCreate. With some testing I found out that if I create view in Activity.onWindowFocusChanged(boolean hasFocus) when hasFocus == true works fine. I'm still not sure why.
You can add
android:theme="#style/some_theme"
to the activity in the manifest file.
I am developing an app and have successfully added a floating action button using the library shown here. The floating button displays well but when i navigate to another fragment through the navigation drawer the button still displays instead i want the button to only display in the activity i created it. I checked for those that had similar issues online and i saw comments...saying that i have to set the View by modifying this line of code found within the method show below.
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
Please can tell me how to achieve this, thanks in advance.
Library Method
public FloatingActionButton create() {
final FloatingActionButton button = new FloatingActionButton(activity);
button.setFloatingActionButtonColor(this.color);
button.setFloatingActionButtonDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
return button;
}
if you can access the FAB view using findViewById you can simply do:
button.setVisibility(VISIBILITY.GONE);
to hide it, that way you can set visibility back to visible when you go back to that activity.
or if you can access view called 'root' (or can access button via getActivity().findViewById for Fragments)
root.removeView(button);
Set the FloatingActionButton to be invisible by default in the XML file:
<ImageButton android:id="#+id/button" android:visibility="invisible"/>
and make it visible by using the following code in the required activity class:
button.setVisibility(View.VISIBLE);
In PreferenceFragment, the view is inflated like so
addPreferencesFromResource(R.xml.preferences);
and not by creating a view object and inflating it. So now, if I want to implement a toolbar in my settings fragment, which extends PreferenceFragment, I need to do this:
addPreferencesFromResource(R.xml.preferences);
toolbar = (Toolbar) findViewById(R.id.toolbar);
((ActionBarActivity)getActivity()).setSupportActionBar(toolbar);
but without a view object, how am I supposed to use findViewById?
You could just use your activity to get the view by id.
getActivity().findViewById(...)
You can read more about getViewByid and activities' methods here
In my fragment1 I add view like this:
CheckBox chb = (CheckBox)new CheckBox(getActivity());
linearLayout.addView(chb);
When I load fragment2 to activity's container and then return back to fragment1, there is no added view.
Thanks for help.