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
Related
So I have this app and in the toolbar, there is a drop-down menu and I am looking for a solution on how to show only specific items at specific fragments.
I know that I have to make a java class and link it to the menu, but how do I find out what fragment I am on?
You can move the implementation of setting the toolbar for your fragments in the activity that launches these fragments. In that case, you may have the toolbar in your activity and the activity should have a fragment container where you will put your fragment. Let us consider a layout like the following for your activity.
<RelativeLayout>
<!-- Your toolbar here in the activity -->
<ToolBar>
android:id="+#id/toolbar"
...
</ToolBar>
<!-- Your fragment container here -->
<FrameLayout>
android:id="+#id/fragment_container"
...
android:layout_below="#id/toolbar"
</FrameLayout>
</RelativeLayout>
Now, you have the ToolBar here in your activity and hence, you can set the toolbar when you are loading the fragments in the fragment container. Your activity might have the following functions.
public void launchFrag1() {
// replace the fragment 1 in the fragment container
loadToolbarForFrag1();
}
public void launchFrag2() {
// replace the fragment 2 in the fragment container
loadToolbarForFrag2();
}
Now call the specific function of your activity to load the fragment and the toolbar dynamically as well.
If you are trying to call the method from the fragment, you can always call those methods declared in your activity like the following.
((YourActivity) getActivity()).launchFrag2();
I hope you get the idea.
Please note that this is just a basic implementation of how you can get your dynamic toolbar working in your app. However, your implementation might vary based on the use case that you have.
I hope that helps!
I have an ImageView in the toolbar which I hide as follows:
// Show image in fragment 1
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.VISIBLE);
// hide image in fragment 2
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.GONE);
Try these codes in onCreateView after inflating your layout
I'm using a custom Toolbar widget in my app, I found that the label attribute in Manifest file doesn't work. Any suggestion except using "setTitle()" method in Activity/Fragment? Thank you.
<activity
android:name=".CartActivity"
android:label="#string/cart_activity_label"
android:parentActivityName=".CatalogActivity" />
I wish the title can be shown by using attributes in Manifest file.
use a textview inside the Toolbar widget or if you are using support library, use setsupportActionbar(toolbar); in your activity.
getActionBar().setTitle(R.string.logout);
or better,
getSupportActionBar().setTitle(R.string.logout);`
in onCreate method
Ok...I just found out that I forgot to link the View (Toolbar) object to the View id. Follwing is the complete setup for using a Toolbar as a default ActionBar:
private Toolbar toolbar;
toolbar = findViewById(R.id.toolbar); //I missed this
setSupportActionBar(toolbar);
Toolbar title explain:
You change the default system style to ".NoActionBar", the default ActionBar disappears.
You add a custom Toolbar to the Layout, an "ActionBar" without title appears.
There is no title on the Toolbar even though you've already set the label attribute in Manifest file, because the Toolbar is not recognized as default ActionBar by the system, that's why we need to use "setSupportActionbar()" method to make it work like a normal ActionBar.
Don't forget to link the View of the Toolbar.
I have an app with some behavior. As an upgrade I'm going to add a Toolbar to the app. I know, I can do it putting de XML on each activity. But I want to create a Layout XML for the action Bar and inflate on each activity on the OnCreate method.
This way I think I can add the bar whenever I need it without touching the XML,and maybe make the code more efficient:
Assumptions:
I have a XML with a layout with tag "< Toolbar >"
The app has the noActionBar theme
This is an extract of OnCreate()
android.support.v7.widget.Toolbar barra =(android.support.v7.widget.Toolbar) getLayoutInflater().inflate(R.layout.barra_herramientas,null);
LinearLayout layout_actividad = (LinearLayout) findViewById(R.id.contenedor_principal);
layout_actividad.addView(barra);
I have an android-studio app with custom toolbar. I use a layout for my toolbar with buttons and I call it with <'include> in every activity. I used android:onClick in the xml for the buttons.
The problem is, that I am not sure how I should connect my buttons (located in my toolbar layout) since I have no Java Class for it. If I create a Java Class I should extend it and I normally use Activity/AppCompatActivity, but my toolbar layout is not activity and the app crashes when I click the buttons in the emulator.
So how should I make the buttons work?
You have different ways to do it. I can show quickly 2 ways in pseudo code :
Solution 1 :
Layout activity :
<Coordinator>
<AppBarLayout>
<include layout="#layout/custom_toolbar"/>
</AppBarLayout>
</Coordinator>
Layout Toolbar :
<Toolbar id="#+id/toolbar">
<TextView id="#+id/tv_toolbar_title"/>
</Toolbar>
Activity :
onCreate() {
toolbar = (Toolbar)findViewById(R.id.toolbar);
tvTitle = (TextView) findViewById(R.id.tv_toolbar_title);
setToolbar(toolbar);
//handle click on view if you want
}
Solution 2 :
Layout activity
<Coordinator>
<AppBarLayout>
<CustomToolbar id="#+id/customToolbar""/>
</AppBarLayout>
</Coordinator>
Custom Toolbar Class => CustomView
CustomToolbar extends Toolbar {
//find your views
}
Layout custom toolbar :
<merge>
<TextView id="#+id/tv_toolbar_title"/>
//other views needed
</merge>
In your activity you will have the customtoolbar :
onCreate() {
toolbar = (CustomToolbar)findViewById(R.id.toolbar);
}
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.