I am developing an application that contains many activities
and i created my own menu (i don't want to use the built in menu button) with the Sliding Drawer
as the sliding drawer is at the bottom of the screen and contains my menu buttons
what i need is to make that sliding drawer to appear in all my activities
i tried to create an activity and set it's content view to the xml file that includes the drawer and then extends that activity in all other activities but this solution doesn't work
so any suggestions ?
Extending is the right way. Just override setContentView in the right way.
Here's the working example, but instead of drawer, I use a created a custom tabbar:
Define a layout with your drawer like this:
this is act_layout.xml
<LinearLayout
...
android:orientation="vertical"
>
<YourDrawer
...
/>
<FrameLayout
android:id="#+id/act_content"
...
>
// Here will be all activity content placed
</FrameLayout>
</LinearLayout>
This will be your base layout to contain all other layouts in the act_content frame.
Next, create a base activity class, and do the following:
public abstract class DrawerActivity extends Activity {
protected LinearLayout fullLayout;
protected FrameLayout actContent;
#Override
public void setContentView(final int layoutResID) {
// Your base layout here
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null);
actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
// Setting the content of layout your provided to the act_content frame
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
// here you can get your drawer buttons and define how they
// should behave and what must they do, so you won't be
// needing to repeat it in every activity class
}
}
What we do, is basically intercept all calls to setContentView(int resId), inflate our layout for drawer from xml, inflate our layout for activity (by reId provided in method call), combine them as we need, and set as the contentView of the activity.
EDIT:
After you've created the stuff above, just proceed to write an app as usual, create layouts (without any mention of a drawer) create activities, but instead of extending simple activity, extend DrawerActivity, like so:
public abstract class SomeActivity extends DrawerActivity {
protected void onCreate(Bundle bundle) {
setContentView(R.layout.some_layout);
}
}
What happens, is that setContentView(R.layout.some_layout) is intercepted. Your DrawerActivity loads the layout you provided from xml, loads a standart layout for your drawer, combines them and then sets it as contentView for the activity.
Finally after 3 years, here's the complete solution to this important question for whom may have not been completely guided by Mr. Orlov's answer.
His method for making a hierarchy view was completely OK but there was some small mistakes that may mislead beginner developers.
As Gabe also mentioned, you can get rid of abstract in declaration.
There's no need to wrap both child with a FrameLayout. The parent could be anything RelativeLayout, LinearLayout, etc. But the most important part is that you must declare the FrameLayout before the Slider.
So the 100% working soloution would be like this :
activity_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/drawer_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<YourDrawer
android:id="#+id/drawer_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</YourDrawer>
</RelativeLayout>
DrawerActivity.java
public class DrawerActivity extends Activity {
protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;
#Override
public void setContentView(int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_drawer, null);
frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(fullLayout);
//Your drawer content...
}
}
MainActivity.java
public class MainActivity extends DrawerActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Don't forget to declare DrawerActivity in manifest.
Hope it helped.
I know this is two years late but for those who would like an answer here it is. Mr Orlov had an appropriate response, just a few adjustments needed.
delete the abstract after public. (this is what causes an error)
wrap BOTH your sliding drawer and FrameLayout with another FrameLayout and set the width and height to match_parent. (This allows BOTH layouts to appear).
ALSO: If you want your sliding drawer to appear on top of your layout (duh!) then place the frameLayout that is after the slidingDrawer before the slidingDrawer.
Related
I am preparing to do an android demonstration of sorts and one of the first apps that i would like to write would be a screen filled with different widgets(which of course are views) but would like to put them on the screen without any layout built to hold them. is this possible or do you have to use a layout to put more than one view(widget) on the screen at once?
So right now i can do something like:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView view1 = new TextView(this);
view1.setText("I am view one");
setContentView(view1);
}
}
In this case i really havent specified a layout but there doesnt seem to be a way to position multiple widgets on the screen without setting a layout. The purpose of this would be to show why you would want to use layouts. perhaps there is a way to display widgets on the screen without having to call the setContentView method.
You can only add multiple widgets/views to something called a ViewGroup. If you take a look at the documentation you'll see - not surprisingly - that basically all layouts extend this class. Similarly, if you look up the documentation on e.g. a TextView, you'll find that it doesn't extend ViewGroup (it does inherit from View, just like ViewGroup, which means it's on a different branch in the hierarchy tree).
In other words: you will need some sort of a layout in order to display more than a single widget/view at a time. You will also always need an explicit call to setContentView(), unless you use something like a ListActivity or ListFragment that by default creates a layout with a ListView as root.
That being said, your example is actually just a programmatical way of setting the following layout on the activity:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="I am view one" />
You can do it like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
TextView view1 = new TextView(this);
view1.setText("I am view one");
frameLayout.addView(view1);
// add more widgets into ViewGroup as you want
// then set the viewgroup as content view
setContentView(frameLayout);
}
want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.
I want to do this in the same activity without creating or starting another one.
Here I show you part of the code:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
The mains only have some images, text views and the buttons.
But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.
For example:
Main XML including two other layouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="#+id/buildinvoice_step1_layout" layout="#layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
<include android:id="#+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="#layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>
Code to switch between layouts:
findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.
I have a program where I want to add two views in one activity, like
public class AnimationActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicsViewForBitmap(this));
setContentView(new GraphicsView(this));
}
}
where GraphicsViewForBitmap & GraphicsView are two classes extends view.
so I want at a time two views should set to an activity.
Is it possible?
Plz give me answer.
Thanks
setContentView() will display only the view that you have set . If you want to display more than one view then you can add both the view in your layout XML file inside any Layout like LinearLayout,RelativeLayout etc. Then you can use setContentView(R.layout.yourXML).
Here is how you can do it in your XML...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.yourpkg.GraphicsView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.yourpkg.GraphicsViewForBitmap
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Yes but first you have to put them inside a ViewGroup, for example a LinearLayout, and then set that ViewGroup with setContentView. Because with the existing code you're not just appending the second view with the first, but you are setting another content.
Add the second view to the first view.
LinearLayout childLayout = new LinearLayout(this);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
childLayout.addView(graphicsView);
parentLayout.add(childLayout);
Another way is to create a 2nd layout XML, say main2.xml (the 1st being main.xml). Then you can swap from one to another via, e.g. an ActionBar button, etc. as follows:
setContentView(R.layout.main2); // Pass from layout #1 to layout #2
setContentView(R.layout.main); // Pass from layout #2 back to layout #1
(You can create as many views as you like ...)
I have a navigation bar in my app, the thing is that I want the navbar to be available in all activities. I suppose that I have to set contentView two times, but that doesn't work, of course.
I've been looking at and but I dont get it to work. I have a super class, can I set this second layout from my super class?
You should include the nav bar via <include> tag from the other layouts. Setting the content layout twice will not work, as Android is in the callbacks basically always using what the user has told last. So
setContentLayout(R.layout.nav);
setContentLayout(R.layout.main);
will result in only the main layout being used.
Have a look at this article which gives an example of using the include tag.
You can extend standard activities (Activity, ListActivity, etc.. if you use any others) and use them as a base for including a nav_bar.
For example:
Define a layout with nabar like this
<LinearLayout
...
android:orientation="vertical"
>
<YourNavBarComponent
...
/>
<FrameLayout
android:id="#+id/nav_content"
...
>
// Leave this empty for activity content
</FrameLayout>
</LinearLayout>
This will be your base layout to contain all other layouts in the nav_content frame.
Next, in create a base activity class, and do the following:
public abstract class NavActivity extends Activity {
protected LinearLayout fullLayout;
protected FrameLayout navContent;
#Override
public void setContentView(final int layoutResID) {
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.nav_layout, null); // Your base layout here
navContent= (FrameLayout) fullLayout.findViewById(R.id.nav_content);
getLayoutInflater().inflate(layoutResID, navContent, true); // Setting the content of layout your provided in the nav_content frame
setContentView(fullLayout);
// here you can get your navigation buttons and define how they should behave and what must they do, so you won't be needing to repeat it in every activity class
}
}
And now, when you create a new activity, where you need a nav bar, just extend NavActivity instead. And your nav bar will be placed where you need it, without repeating it in every layout over and over again, and polluting the layouts (not to mention repeating a code to control navigation in every activity class).
Try merging layouts, as described on Android Developers Blog.
I'm new to android,I'm using tabHost adding some tabs to it,its working quite fine but when i rotate my device in landscape mode it also work there fine but i don't need tab bar there because it covers much space and i also have google ads so both of them cover half of the screen and leave a little space for user to interact.All i need is a solution to somehow hide tab bar just like we can do it in iphone to make a bit room for user to interact.I need some solution urgent.Thanks
I think you should wrap your tab widget in any ViewGroup such as LinearLayout or RelativeLayout, and create a static function in your tabActivity to show/hide this wrapper, Here's a little code might be helpful for you.
<LinearLayout
android:id="#+id/popupTabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<TabWidget android:id="#android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></TabWidget>
</LinearLayout>
Now your tab activity should do something like this.
public class TabsView extends TabActivity {
public static LinearLayout popupTabs ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
popupTabs = (LinearLayout) findViewById(R.id.popupTabs);
// Your other code
//............
//............
}
// Show Tabs method
public static void showTabs(){
popupTabs.setVisibility(ViewGroup.VISIBLE);
}
// Hide Tabs method
public static void hideTabs(){
popupTabs.setVisibility(ViewGroup.GONE);
}
}
Now you can call this method statically from any location in your code like this
// hide tab from any activity
TabsView.showTabs();
// hide tab from any activity
TabsView.hideTabs()
For Hide
mTabHost.getTabWidget().setVisibility(View.GONE);
For Visible
mTabHost.getTabWidget().setVisibility(View.VISIBLE);
The simplest way would be to create a second version of your layout.xml file which doesn't include the TabHost and put it in a resource folder named 'layout-land' (the 'land' suffix is short for 'landscape'). Please see this SDK article for more information.
Apart from doing what Reuben is telling you would be to animate the transition between both so that the change would be a bit smoother.