i'm having problems with navigation drawer.
I setted the drawer layout and everything but when it comes to the real "drawer pane" and i want to open it, i get an exception saying:
java.lang.IllegalArgumentException: View
android.widget.RelativeLayout{699e897 V.E...... ........
0,0-1440,2464} is not a sliding drawer
So here is the code:
drawerPane=new RelativeLayout(activity);
RelativeLayout.LayoutParams drawerPane_params=new
RelativeLayout.LayoutParams(280, ViewGroup.LayoutParams.MATCH_PARENT);
drawerPane.setGravity(Gravity.START);
drawerPane.setLayoutParams(drawerPane_params);
drawerLayout=(DrawerLayout)activity.getRootView();
//getRootView is a custom function, returns the contentview (see updates below)
drawerLayout.addView(drawerPane);
in this code, the drawerlayout is the RootView of my activity (named: "activity")
i have a button which opens my drawer on buttonclick, and the code is this:
drawerLayout.openDrawer(drawerPane);
The error gets shown here.
Any suggestions? Thanks
UPDATE
this is the code i use to set drawerlayout as rootView:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myGestureDetector=new GestureDetectorCompat(this,this);
rootView=new DrawerLayout(this);
DrawerLayout.LayoutParams lp=new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT);
getRootView().setPadding(0,0,0,0);
rootView.setLayoutParams(lp);
setContentView(rootView,lp);
init(); //i made this function
ViewGroup container=(ViewGroup)getLayoutInflater().inflate(myLayout,null);
//myLayout is evaluated in init(), a function i created to set the CONTAINER of the app, that is, the first child after the drawlayout.
ViewGroup.LayoutParams lp2=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
container.setLayoutParams(lp2);
container.setPadding(0,0,0,0);
rootView.addView(container);
Related
I am developing android application with drawer layout in fragments. actually i am trying to display drawer layout in oncreate in fragments but it is not coming
i have oncreate and oncreateview override method in fragments and whenever application open that time only my drawer layout should come.
this is my oncreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test2, container, false);
mDrawerLayout = (DrawerLayout)rootView.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) rootView.findViewById(R.id.list_slidermenu);
mDrawerLayout.openDrawer(Gravity.LEFT);
navDrawerItems = new ArrayList<String>();
navDrawerItems.add("first");
navDrawerItems.add("second");
navDrawerItems.add("third");
adapter = new NavDrawerListAdapter(getActivity(),navDrawerItems);
mDrawerList.setAdapter(adapter);
return rootView;
}
My Requirements:-
My loadingswipe() function should execute whenever application open that is the reason i am giving loadingswipe() inside oncreate. if i give this function in oncreateview, it is executing while clicking back button also but my requirement is it should not execute while clicking back button. could you please help me to resolve this issues.
Update:-
Now i have given my drawer layout in my oncreateview Now my drawer layout is showing but the problem is inside test2 i have one button if click that enter into that page but whenever i click back button that time drawer layout also showing but for me that button only should show
I have an application, that at the moment only has 2 fragments. Fragment 1, this has the nav drawer and the title.
Fragment 2 requires a custom view as adding menu items won't work as I need alignment. So I add the view as follows:
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.LEFT);
//Remove nav drawer "hamburger"
mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
//Remove title from Toolbar
bar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View postToolBar = layoutInflater.inflate(R.layout.upload_content_toolbar, null);
mMainActivity.mToolBar.addView(postToolBar, params);
That is fine and it displays correctly. However, when I want to return to the previous fragment Fragment 1, I then call mMainActivity.mToolBar.removeView(postToolBar); I call this on return to Fragment 1 as the user can navigate either by the back button or by button in the postToolBar. However, the view is still in place. I can't get rid of it. I have now tried setting the visibility to GONE but that won't work either.
This was pretty simple with the Action Bar, however things seem to have gotten a bit complicated with the Tool bar.
I must add that in each of my two fragments I extend a BaseFragment in which I declare the toolbar view.
Can any one help or send me in the direction of a tutorial?
This is how i achieved it at this time:
in activity onCreateView();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowCustomEnabled(true); // enable overriding the default toolbar_home layout
getSupportActionBar().setDisplayShowTitleEnabled(false); // disable the default title element here (for centered title)
getSupportActionBar().setDisplayShowHomeEnabled(false);
cutomToolbarView=getLayoutInflater().inflate(R.layout.custom_toolbar_home, null);
getSupportActionBar().setCustomView(cutomToolbarView);
}
and here is two simple method to do the magic
public void setToolbarTitleEnabled(String title) {
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(title);
}
public void setCustomToolbarEnabled() {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
then simply when you can change actionbar like these:
when to select HomeFragment.java or whatever in your case
setToolbarTitleEnabled(CURRENT_TAG);
and when to select other fragment:
setCustomToolbarEnabled();
Problem:
The Fragment backstack is made so that traversing backwards through a stack of fragments in one activity does not revert the Action Bar to its original state in the previous fragment.
Why does this happen?
Turns out, the Action Bar is actually attached to the Activity itself, not the fragment! Remember, fragments are only modular bits of the UI, and have to explicitly specify control to other fragments, sections of the activity, or even the Action Bar.
Keep reading for the solution...
Solution:
I found that the best approach to this problem is done by what is generally described in Reto Meier's answer to a previous question. My solution will just expand more deeply on his answer.
What we want to establish though is that we don't want to re-create the action bar every time we switch to a different fragment, reason being it's not very efficient. I'm going to walk you through an I wrote for a student scheduling app. It's not very complicated, and it's onboarding experience is composed of multiple fragments held within an activity.
To make this work, we need to make sure we're using replace() to switch between fragments. This is better than layering fragments on top of each other, because it lets you configure the action bar separately for each fragment.
The first chunk of code comes from the activity's inner class, LoginOptionsFragment, in its onCreateView() method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login_options, container, false);
//LoginOptionsFragment will have its own action bar
setHasOptionsMenu(true);
//inject views. e.g: Button add_course
ButterKnife.inject(this, rootView);
add_course.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getFragmentManager().beginTransaction()
//exchange fragments. no messy clean-up necessary.
.replace(R.id.container, new AddCourseFragment())
.addToBackStack(null)
.commit();
}
});
return rootView;
}
Here, I not only make sure to call onCreateOptionsMenu() via the setHasOptionsMenu(true), but mainly, as soon as the "ADD COURSE" button is clicked to switch to the AddCourseFragment, the new fragment replaces the old fragment as the primary child of the activity. Next, after we override the onCreateOptionsMenu(), we come to onResume(), but we'll get to that later ;)
Secondly, we arrive at the AddCourseFragment, where we even inflate a custom done-cancel view for the action bar. So let's look at the code!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done/Cancel" custom action bar view.
final ActionBar actionBar = getActivity().getActionBar();
inflater = (LayoutInflater) actionBar.getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
//inflate custom action bar view
View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_cancel, null);
//set listeners to items in the view
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
// "Done"
//remove custom view from action bar
actionBar.setDisplayShowCustomEnabled(false);
getFragmentManager().popBackStack();
//add course to list
}
});
customActionBarView.findViewById(R.id.actionbar_cancel).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
// "Cancel"
//remove custom view from action bar
actionBar.setDisplayShowCustomEnabled(false);
getFragmentManager().popBackStack();
}
});
// Show the custom action bar view and hide the normal Home icon and title.
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayHomeAsUpEnabled(false);
// END_INCLUDE (inflate_set_custom_view)
View rootView = inflater.inflate(R.layout.fragment_add_course, container, false);
ButterKnife.inject(this, rootView);
return rootView;
}
The ONLY part that you need to pay attention to are the OnClickListener's added to the DONE and CANCEL buttons. In here, I use my previous reference to the parent Activity's action bar and tell it to stop displaying the custom view. Now in addition to this specific method, there are more setDisplayXEnabled() methods that you can pass in false to. After that, I pop the backstack to get to the previous fragment.
But how do I actually revert the action bar!?
Here's how. Remember that onResume() method that was hanging out in our LoginOptionsFragment? Well, onResume() is called once a fragment gets back into focus from the backstack! So if we override it and re-enable the parts of the action bar that we want, we win right? Yes we do. Here is all you need to add into the onResume().
#Override
public void onResume() {
super.onResume();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowHomeEnabled(true); //show Home icon
actionBar.setDisplayShowTitleEnabled(true); //show title
// actionBar.setDisplayUseLogoEnabled(true); <--- more options
// actionBar.setDisplayHomeAsUpEnabled(true); <--- more options
}
And we did it all without recreating the action bar. Here's how it looks!
Thanks for reading, and happy coding!
I called the fragements as
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(context, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(context, Fragment2.class.getName()));
mPageAdapter = new PageAdapter(activity.getSupportFragmentManager(),fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(mPageAdapter);`
,but now in fragment class ,when trying to access the child button as
View v = inflater.inflate(R.layout.fragment1, container, false);
btn = (ImageButton) v.findViewById(R.id.btn);
btn.setOnTouchListener(new OnTouchListener() {}
,but the button is not generating click event,not able to understand the problem.
Secondly, how can I open a small window with viewpager and fragments ,above the present window,so user touches the back window front one goes off,and when front window is visible,onclick of one of fragment button ,user can swipe its elements,as here new in use of viewpager,kindly guide
It looks like you are not completely understand the meaning of inflater.inflate, it was the same problem in your previous question.
When calling inflater.inflate it creat new View object from the xml you give it.
In this code:
View v = inflater.inflate(R.layout.fragment1, container, false);
btn = (ImageButton) v.findViewById(R.id.btn);
btn.setOnTouchListener(new OnTouchListener() {}
You created new view object, than you found it's button and add a touch listener event to it. Unless you are going to add this view to the fragment, the event will never fire.
I think what you meant to do is finding your view using findViewById() inside your fragment class.
I'm developing an Android app and I want to have 2 ActionBar (one on the top and another on the bottom) like this: http://developer.android.com/design/media/action_bar_pattern_considerations.png
I'm using Actionbar Sherlock and for now I created only the top ActionBar. I serched in the web, but I didn't find a solution yet, only some piece of code.
Can anyone help?
Take a look at the following links:
http://developer.android.com/guide/topics/ui/actionbar.html
How can I force the Action Bar to be at the bottom in ICS?
Changing position of Android Action Bar
try this...
public class MainActivity extends SherlockActivity {
private View contentView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(contentView);
LinearLayout layout = (LinearLayout) contentView.getParent().getParent();
View view = layout.getChildAt(0);
layout.removeViewAt(0);
layout.addView(view);
}