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
Related
I am creating a relativelayout programmatically in onCreate like this:
RelativeLayout rl = new RelativeLayout();
//I add all the views here.
RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
rp.addRule(RelativeLayout.CENTER_IN_PARENT);
setContentView(rl, rp);
And i am currently switching from one to another with:
RelativeLayout rn = (RelativeLayout) Framework.layouts(rlo);
if (rn!=null) {setContentView(rn);}
Works fine and everything, now, i am trying to add a crossfade effect between the two layouts.
Is there a way to do it programmatically without xml files and without the android.R.anim animations?
For every single question i have made i end up getting answers like better solutions or something, but I have good reasons for this, thanks.
I'm confronted with the Problem of slow Performance...
Just take a case:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myrlLayout);
//Adding now 100 Buttons with 100 TextViews below each Button(just a example)
for(i=0;i<100;i++) {
Button btn = new Button(this);
btn.setId(i+1); //Should be a positive integer
//set LayoutParams for Button
RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams....
....
myLayout.addView(btn, btn_layoutparams);
TextView mytv = new TextView(this);
mytv.setid(101+i);
//set LayoutParams for Button with referenced to the Button(because the Textview Needs to be
of Button)
....
myLayout.addView(mytv, tv-layoutparams);
}
Regarding to the high amount of Views programmatically created, my app starts really slow...
I think it's not because of creating a new View, but because of setting the LayoutParamters each time for the view. I can't find a Workaround because my LayoutParams for the TextView for example Need to reference to the button created before. Due to that i'm not really able to create a XML-layout-file or XML-style-file because i can't reference the tv's layoutparameters anchor in the XML-file to the button which does not exist at the Moment. At least i didn't find a way. I hope somebody got an idea how to appreciable improve the Performance when creating such a amount of views at runtime. Every advise is welcome.
Update regarding answere from rom4ek
The Problem is, that i Need to calculate how much views can i add per row before the Screen-width is fully used. That means i Need second LayoutParams to add the next Button below the first Button from the first row. And i also Need to reference to the img-Button added before in the LayoutParams.. so it's not possible to reference LayoutParams to a Button which doesn't exist before the for-loop.Maybe i completely miss something.. Do you have an idea/solution? Thank you for your respond.
If you're setting the same LayoutParams, what if you move RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams.... before the cycle? So you will initialize it one time, and then no need to create new LayoutParams every step.
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.
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 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.