I want to display two screen (One main and another at bottom). Following is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
showBottomScreen();
}
private void showBottomScreen() {
setContentView(R.layout.bottom_layout);
LinearLayout layout = (LinearLayout)findViewById(R.id.bottom_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
params.rightMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
params.gravity = Gravity.CENTER;
layout.addView(new bottomProvider(this).generateInLineView(),params);
}
Problem is that now i am able to see bottom screen not main screen. Please give some suggestion?
What is happening now:
Using setContentView() twice only sets the content of the activity to whichever is set the last.
So:
setContentView(R.layout.seekbar); and after that
setContentView(R.layout.bottom_layout);
Just sets the bottom_layout.xml as the content view of your activity.
What you want:
There are alternatives to achieve what you want, depending on your requirement.
A simple way of doing it:
You can try including all elements of your screen as one, in either seekbar.xml or bottom_layout.xml
If working your way with various layouts - relative, linear, frame and a scrollview.
Or
Go for the multi-pane UI and related development by using Fragments in Android app,
for reference:
1. Multi-pane development in Android with Fragments
2. Android Fragments Example
3. Imp read:How to use Fragments? and its Life cycle
I think what you're looking for are fragments.
Related
I used to create android app with basic layouts in android such as linear layout. I'm a student working on an android project and now my teacher want me to create an app with different graphic interface, so I got up with an idea but I don't know exactly how I can do it.
I want 2 seperate screens (let's call it Menu1 and Menu2), the Menu2 has some buttons, when I click on those button, Menu2 will become submenu1, or submenu2, submenu3. But during the transition of Menu2, Menu1 is still remain. Is there any way I can do it? I'd be really appreciate if you can give me a link to a tutorial or somethings like that.
I try to explain my idea in the picture below.
Here is the link to the picture
Yes, you can use Layoutmanager to do this,
check this out...
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
LinearLayout layoutmanager = new LinearLayout(this);
layoutmanager.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutmanager);
LayoutInflater inf = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layleft = (RelativeLayout)inf.inflate(R.layout.firstxml,null);
RelativeLayout layright = (RelativeLayout)inf.inflate(R.layout.secondxml,null);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutmanager.addView(layright, 250, 450);
layoutmanager.addView(layleft, relParam);
You can use LinearLayout Vertical instead of Horizontal and make sure both the xml you use must have only relative layouts ..I hope It would solve your problem
I have the following problem:
The code below successfully adds my TextView to my custom RelativeLayout:
RectF rectRecord = getItemRect(trCurrent);
TextView tv = new TextView(this.getContext());
tv.setLeft((int)rectRecord.left);
tv.setRight((int)rectRecord.right);
tv.setTop((int)rectRecord.top);
tv.setBottom((int)rect.bottom);
addView(tv);
Unfortunately the methods ("setLeft,setRight,setTop,setBottom") aren't available on Android older than 3.0.
So I tried to add my TextView the alternative way:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins((int)rectRecord.left, (int)rectRecord.top, (int)rectRecord.right, (int)rectRecord.bottom);
//tv.setLayoutParams(tv);
addView(tv, params);
Doing it that way does not show a child control ...
I already tried to change the class my host control derives from ViewGroup to LinearLayout, RelativeLayout and the deprecated AbsoluteLayout but always the same.
Also removed my custom onDraw and onMeasure and "setWillNotDraw(false);" but that didn't solved my problem.
Snippet 1 is displaying my child views.
Snippet 2 does not show a child.
Can anyone point to the solution for this problem?
Your margins are set to 1234 - that's 1234 dip (density independent PIXELS) and that is huge. Your control doesn't show up on the screen because margins are too big, and your control has no place to show on the screen.
Right, this is a strange problem I have been toying with for a while now, hopefully maybe I am missing something you guys can draw my attention to!
LinearLayouts seem to be disappearing once I add any spacing using views and defining the weight (a method which works elsewhere in the project).
I have a custom Dialog (extends Dialog). In the onCreate() I use the method setContentView(generateDialog()) which returns a vertical LinearLayout.
The LinearLayout has three elements, one row of four custom category buttons (LinearLayouts), one row of sorting buttons (also LinearLayouts) and one ListView which populates the rest of the dialog and refreshes based on which button is pressed.
All is functional and working fine. Except when I attempt to space the buttons out evenly using my spacer method:
Dialog.java:
LinearLayout catBtns = new LinearLayout(context);
catBtns.setOrientation(LinearLayout.HORIZONTAL);
catBtns.setBackgroundResource(R.drawable.btn_cat_gradient_bg);
catBtns.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
catBtns.addView(space(1));
cat1Btn = new CatButton(context,this,act,"CAT1");
catBtns.addView(cat1Btn);
catBtns.addView(space(1));
cat2Btn = new CatButton(context,this,act,"CAT2");
catBtns.addView(cat2Btn);
catBtns.addView(space(1));
cat3Btn= new CatButton(context,this,act,"CAT3");
catBtns.addView(cat3Btn);
catBtns.addView(space(1));
cat4Btn = new CatButton(context,this,act,"CAT4");
catBtns.addView(cat4Btn);
catBtns.addView(space(1));
The space() method:
private View space(int space) {
View view = new View(context);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(0,0);
p.weight = space;
view.setLayoutParams(p);
return view;
}
What confuses me is that I have been using this method throughout the project and can't find as to why the category LinearLayout DISAPPEARS COMPLETELY when I add the spacers in between each button.
I use the same technique for the sorting buttons and it works perfectly! I use the same technique in another part of the project using slightly different versions of the same buttons (they are different class files though, because the onClickListener and some other stuff is slightly different)
Anyone have any clue?
I tried to build this in XML and it works fine. A possible difference is, that you do not set any LayoutParams for your buttons. Try something like that for every button:
cat1Btn = new CatButton(context,this,act,"CAT1");
catBtns.addView(cat1Btn, new LinearLayout.LayoutParams(0,0));
I'm trying to put a mobclix ad banner in a cocos2D game. I have the ad banner showing up on top of the openGL view. However, I can not figure out how to place it at the bottom of the screen which is what we want. The example from mobclix shows the use of a LinearLayout with gravity set to bottom. I tried this in the GameActivity which is the main activity on startup:
adview_banner = new MobclixMMABannerXLAdView(this);
adview_banner.addMobclixAdViewListener(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
this.addContentView(adview_banner, params);
adview_banner.bringToFront();
adview_banner.getAd();
adview_banner.setRefreshTime(30000);
No matter what I do here the banner always shows up on the top of the screen. Any help would be greatly appreciated.
OK! I think I finally have it! Those layouts are tricky at first programmatically but they do match up with what the example had in the xml. I just needed to embed the banner in a layout object and send it to the bottom of that layout.
In the main Activity:
#Override
public void onStart() {
super.onStart();
RelativeLayout layout = new RelativeLayout(this);
adview_banner = new MobclixMMABannerXLAdView(this);
adview_banner.addMobclixAdViewListener(this);
RelativeLayout.LayoutParams childParams = new RelativeLayout.LayoutParams(320, 50);
childParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adview_banner, childParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(layout, params);
//adview_banner.bringToFront();
adview_banner.getAd();
adview_banner.setRefreshTime(30000);
}
Now the only issue is that I get a "can't get the viewWidth after the first layout error" on the Logcat from "webcore". And the ad banner displays "An error has occurred. Click here for more details."
I think I'll consult the Mobclix support team on that one.
Use Absolute Layout.
android:layout_width="40px"
android:layout_marginRight="5px"
android:layout_height="40px"
etc
Linear Layouts can only set gravities to each individual child.
Was it useful?
I'm trying to build an android application that features a graphical display drawn within a RelativeLayout. I want to place "+" and "-" buttons next to several of the parameters, which are drawn at various points on the canvas. The positions are free-form don't seem to conform to any of the standard XML layouts.
I know how to create the buttons programmatically, but I don't know how to place them over the canvas where I need them to be. I'm assuming that this would be done in the view thread's doDraw() method, after all the graphics have been drawn, but how?
I struggled with the same problem, and found out great solution.
RelativeLayout rules like "leftOf" or "rightOf" can be implemented programmatically like this:
RelativeLayout container = new RelativeLayout(getApplicationContext());
Button weight = new Button(getApplicationContext());
final int WEIGHT_ID = 0;
weight.setId(WEIGHT_ID);
weight.setText("0.0");
LayoutParams wrapBoth =
new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
container.addView(weight, wrapBoth);
Button increaseWeight = new Button(getApplicationContext());
increaseWeight.setText("+");
// Note the difference: RelativeLayout.LayoutParams in spite of LayoutParams
RelativeLayout.LayoutParams toBeRightOfWeight =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
container.addView(parameter,wrapBoth);
// Sweet part
clearAirParams.addRule(RelativeLayout.RIGHT_OF, WEIGHT_ID);
container.addView(increaseWeight, toBeRightOfWeight);
So, in code you can create a 'container' RelativeLayout, then add several Views with unique ID's and, finally, create RelativeLayout.LayoutParams object to achieve sweet-like-sugar methods for alignment, like in XML.