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);
}
Related
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);
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();
Why navigation drawer icon gets disappeared on setting customView by
actionbar.setCustomView(R.layout.blah);
How can this be resolved?
OK, you don't post any code, so I have to assume stuff here. So if you can update your question and show actual code!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Depending on what your custom Action Bar will do, you might want to disable these!
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.custom_actionbar, null);
// Here is how you can get specific items from your custom view!
TextView titleTextView = (TextView) customView.findViewById(R.id.title_text);
titleTextView.setText("My Own Title");
...
// MAKE SURE THESE ARE SET!! BOTH OF THEM!!
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
}
The biggest issue is probably the code at the very end. Please make sure you have that code!
I am trying to make an activity have swipable tabs, with each tab having a different fragment (I dont know yet how many fragments I am going to have, so assume they will be at least 5).
So I am having problems make the parent activity with the tabs (if I said that correctly) and then the fragments themselves have some dynamically added views (text views and one image view) which get their stuff from different async tasks, executed when the fragment is showed.
Don't know if I explained it correctly, but here is my code and I'll ask you please to add in it the needed stuff.
So here is the parrent activity which must host the tabs and the fragments:
public class SecondActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
and here is one of the fragments (the others are similar):
public class Fragment1 extends Fragment {
LinearLayout layout;
ImageView iv;
String anotherURL;
ArrayList<InfoStuff> ci;
public Fragment1() {
// Empty constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
layout = (LinearLayout) rootView.findViewById(R.id.layout);
iv = (ImageView) rootView.findViewById(R.id.ivPortrait);
Bundle b = this.getArguments();
ci = b.getParcelableArrayList("infoStuff");
regionUrl = b.getString("someURL");
createViews();
return rootView;
}
public void createViews() {
TextView tv;
tv = new TextView(v.getContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("le text");
layout.addView(tv);
tv = new TextView(v.getContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("some text");
layout.addView(tv);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(v.getApplicationContext()).build();
ImageLoader.getInstance().init(config);
String imgUrl = "someURL";
ImageLoader.getInstance().displayImage(imgUrl, iv);
}
}
I am also having troubles making the ImageView work, as it is from an additional library (forgot the name, heres the import though import com.nostra13.universalimageloader.core.ImageLoader;)
EDIT: Sorry, forgot to mention the tabs must be swipeable
The article Creating Navigation tabs using TabHost and Fragments in Android may help you.
In this article, we will develop an Android application containing navigation tabs. There are many ways by which we can add navigation tabs to an application.
Beginning with Android 3.0, the standard way to create navigation tab is using action bar. [...]
For Android 2.x and above, we can use Action bar Sherlock library to create navigation library. [...]
In this application, we are creating navigation tabs using TabHost and Fragments. [...]
This application is developed in Eclipse 3.7.2 with ADT plugin (20.0.2) and Android SDK ( R20.0.1 ) .
I just started doing something very similar to what you want to do. I followed this tutorial on Android Hive, which was very helpful in getting the tabs, swiping, and fragments down. (The fragments in this example are just textviews with different colors, but you can easily figure out where to add in your own views).
Hope this helps - it did for me!
I want to remove from the arrow image from the HomeAsUpButton.
I tried removing the arrow's ImageView from the layout (nothing happens) and also tried using SupportActionBar.SetDisplayHomeAsUpEnabled(false); removes the button functionality entirely.
I'm using johnkil's SideNavigation code. Any suggestions?
Using YouTube's App as example:
With the ActionBar Sherlock, inside your Activity's onCreate method, you just need to do the following:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
If the up image does not disappear, it might be something related to the library that you referred. In my app, I use the SlidingMenu library and it works just fine (source: https://github.com/jfeinstein10/SlidingMenu)
EDIT: With the SlidingMenu library, the Activity would look like this:
public class MainAct extends SlidingFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Sliding menu
// Here I set the menu's layout
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
MenuListFrag frag = MenuListFrag.newInstance(getSlidingMenuItems());
t.replace(R.id.menu_frame, frag);
t.commit();
// Customizing the SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setFadeDegree(0.35f);
// Hiding the ActionBar's up button
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
}
}
You can disable the action bar using this method
actionBar.setHomeButtonEnabled(false);
I was able to hide the "arrow" by setting a transparent image.