Android layout creation dynamic runtime - android

I need to create a layout dynamically like this through code instead creating through XML.
I am able design either in vertical or horizontal buttons. But I need to create both vertical and horizontal in a same layout.
Please help me to do it in easy way.
Thanks in advance

Its simple, First create the parent linear layout
LinearLayout parentLayout = new LinearLayout(this);
set the parameters and orientation for this parent layout:
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)
parentLayout.setOrientation(VERTICAL OR HORIZONTAL);
parentLayout.setLayoutParams(layoutParam);
Thats it , your parent layout has been generated. Now create new layout and start adding it to this layout. For ex.:
LinearLayout firstLayout = new LinearLayout(this);
LinearLayout secondLayout = new LinearLayout(this);
LinearLayout thirdLayout = new LinearLayout(this);
parentLayout.addView(firstLayout);
parentLayout.addView(secondLayout);
parentLayout.addView(thirdLayout);
Also you need to set the parameters for all the layout seperatly. You can optimise the code according to your need.
Hope it works!

Related

adding linear layout to current view

I'm sure this has come up often, but I couldn't find an exact solution for my issue. I'm simply trying to programmatically create a linear layout with a textview and 2 buttons and add it to the bottom of my fragment (which is also a linear layout). I.e. I want it to appear below the other views on the screen not the actual bottom. However, when I try to add it, it always appears at the top corner and not the end. Here's my code:
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setBackgroundColor(Color.CYAN);
TextView noteView = new TextView(getActivity());
LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
noteView.setLayoutParams(lparams);
noteView.setText(noteText);
layout.addView(noteView);
getActivity().addContentView(layout, lparams);
}
Have you tried to add gravity to the LinearLayout?
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
layout.setLayoutParams(params);
If your main goal is to have your views line up relative to each other, perhaps it would be worth considering using a Relative Layout to accomplish a similar task.
If that's not an option for whatever reason, I think LayoutParams has a gravity option that can be set to bottom which will align views to the bottom of the parent view. I can't guarantee this will work though as I usually do not accomplish tasks like these programmatically.

Fail to execute code with nested layouts in android

Hi here is the code in question (I've verified that everything that is not here is working, as far as I can see):
Now SchemeLayout, CityLayout, MastermidLayout, HQLayout and HandLayout are all Linear Layouts with specific dimensions for my screen. Each of them have a different color and hence I know exactly their size and where they are placed. Then I create a handfull of secondary layouts just so that the screen will be split as I want:
LinearLayout schemeandcity = new LinearLayout(this);
LinearLayout masterandhq = new LinearLayout(this);
LinearLayout cards = new LinearLayout(this);
LinearLayout cardsandecks = new LinearLayout(this);
LinearLayout Complete = new LinearLayout(this);
schemeandcity.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
masterandhq.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
cards.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
cardsandecks.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Complete.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
schemeandcity.setOrientation(LinearLayout.HORIZONTAL);
masterandhq.setOrientation(LinearLayout.HORIZONTAL);
cards.setOrientation(LinearLayout.VERTICAL);
cardsandecks.setOrientation(LinearLayout.HORIZONTAL);
Complete.setOrientation(LinearLayout.VERTICAL);
schemeandcity.setOrientation(LinearLayout.HORIZONTAL);
masterandhq.setOrientation(LinearLayout.HORIZONTAL);
cards.setOrientation(LinearLayout.VERTICAL);
cardsandecks.setOrientation(LinearLayout.HORIZONTAL);
Complete.setOrientation(LinearLayout.VERTICAL);
schemeandcity.addView(SchemeLayout);
schemeandcity.addView(CityLayout);
masterandhq.addView(MastermindLayout);
masterandhq.addView(HQLayout);
cards.addView(HandLayout);
cards.addView(masterandhq);
cards.addView(schemeandcity);
//cardsandecks.addView(cards);
//cardsandecks.addView(DecksLayout);
//Complete.addView(cardsandecks);
Complete.addView(cards);
//Complete.addView(CBarLayout);
setContentView(Complete);
Each Now for the code that fails. In the code above I only show the cards sublayout. The code:
cards.addView(HandLayout);
cards.addView(masterandhq);
Shows me both layouts but
cards.addView(masterandhq);
cards.addView(HandLayout);
Only shows me the first (masterandhq). Its as if after I add a compound layout then I cannot add anything else. Oh.. and of course none of the versions show me schemeandcity.
Thank you for any help.
masterandhq has layout_height and width set to match_parent so when you add it to your linear layout it take the whole space anything you add after that will be out of screen.

Relative layout change through java code

I went through several links yet i am not able to find if it is possible to set other attributes of relative layout, like android:gravity,android:orientation through java code.
I want to change the positions of the some of the views through java code and not through xml.
e.g, i want to set these attributes through java code for a particular view. android:gravity="center_vertical" android:orientation="vertical"
how can i do so? please help.
as Daan said you can use Layout params for relative layout in android
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.alignWithParent = true;
params.bottomMargin = 5;
params.height = 50;
params.leftMargin = 50;
But as you said you want to set following attributes for particular view, you cannot assign these attributes to RelativeLayout as these are belongs to LinearLayout and other views
android:gravity="center_vertical" android:orientation="vertical"
For setting gravity for any view please follow the code
tv = (TextView)findViewById(R.id.tv01);
tv.setGravity(Gravity.CENTER_VERTICAL);
You cannot set Orientation of any view.. instead you can set orientation to LinearLayout
like this
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Hope this will help you
Within a relative layout you have to use other layout params:
Layout params within relative layout

Aligning a Linear Layout within a RelativeLayout to the Center and to the Left

Currently I have tried aligning my Linear Layout to the left and center but currently cannot get this to work. See code below:
CustomLinearLayout sideMenu = new CustomLinearLayout(this);
RelativeLayout.LayoutParams sideMenuParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
sideMenuParams.addRule(RelativeLayout.CENTER_VERTICAL);
sideMenuParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
sideMenu.setLayoutParams(sideMenuParams);
sideMenu.setOrientation(LinearLayout.VERTICAL);
sideMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sideMenu.setBackgroundColor(Color.TRANSPARENT);
//sideMenu.setGravity(Gravity.CENTER);
sideMenu.addView(AppSoups);
sideMenu.addView(salads);
sideMenu.addView(ribs);
sideMenu.addView(favorites);
sideMenu.addView(sandwiches);
sideMenu.addView(sides);
sideMenu.addView(desserts);
RelativeLayout screenLayout = new RelativeLayout(this);
screenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
screenLayout.setBackgroundResource(R.drawable.body_bkgd);
screenLayout.addView(sideMenu);
setContentView(screenLayout);
CustomLinearLayout is just a class that extends LinearLayout to do some custom drawing. Anyway what I am doing wrong here? So far it aligns left and to the top of the relative layout but i cannot get it to center. Just as a side note both or either or of my rules do not work in conjuction with setting the LinearLayout to the center(i have it commented out in the code). Finally the views that i am adding to the linear layout are just textviews - just FYI(code not shown)
I think you are "overriding the layout params you've set in line 5 with new layout params you are setting in line 7, so
sideMenuParams.addRule(RelativeLayout.CENTER_VERTICAL);
sideMenuParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
are not really taking affect
In here
screenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
try changing the WRAP_CONTENT to FILL_PARENT

Added Views appear at top of screen

I've been trying to add multiple textviews and buttons when onClick, and this is the best code I have found that actually works:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.RelativeLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(lp);
tv.setText("Shift" + mShiftCount);
EditText edittv = new EditText(getApplicationContext());
edittv.setLayoutParams(lp);
relative.addView(tv);
relative.addView(edittv);
This seems to be the best code to add additional items that are alike to that of what I already have in my main.XML file.
My issue is that when these are added, they appear at the top of the screen and I am unsure how to add further parameters to the objects.
How would I go about placing the textview and edittext below my other elements specified in the XML?
What you need to do is create a RelativeLayout.LayoutParams (refer here) when you add the views like tv and edittv to the RelativeLayout. In your RelativeLayout.LayoutParams, you need to define the same parameters you would give to your other RelativeLayout objects. Here is an example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.idOfWhatYouWantToBeBelowOf);
addView(tv, params);
First off there are definitely some static components to your view and some dynamic components. can you please specify what is your layout file so that i can give you a much more detail in your application.

Categories

Resources