I'm trying to merge two LinearLayouts like this:
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
main.addView(namnLL,lp);
hscrolla.addView(ll_scrolla,lp);
scrolla.addView(hscrolla,lp);
main.addView(scrolla,lp);
setContentView(main, lp);
However, when I run the app, only the first "main" is visible.
Does anyone know what I've done wrong?
Edit: Here's some additional code:
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout ll_scrolla=new LinearLayout(this);
ScrollView scrolla=new ScrollView(this);
HorizontalScrollView hscrolla=new HorizontalScrollView(this);
LinearLayout namnLL = new LinearLayout(this);
LinearLayout textrutaView = new LinearLayout(this);
textrutaView.setOrientation(LinearLayout.VERTICAL);
//Everything named something with "tv" are TextViews
namnLL.addView(tvNamn,lp);
textrutaView.addView(tv1,lp);
textrutaView.addView(tv2,lp);
textrutaView.addView(tv3,lp);
textrutaView.addView(tv4,lp);
textrutaView.addView(tv5,lp);
namnLL.addView(namnTV);
ll_scrolla.addView(textrutaView);
change the LinearLayout orientation in VERTICAL, in this way:
main.setOrientation(LinearLayout.VERTICAL);
default it is HORIZONTAL
Related
Hello I am trying to make 2 TextViews apear above each other through code.
My question is:
How can I stack them over each other?
Sample Code
private TextView DurrationView;
private TextView DurrationViewoverall;
DurrationView = new TextView(getContext());
DurrationView.setText("");
addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("");
addView(DurrationViewoverall);
I am trying to have DurrationView appear above DurrationViewoverall.
The class they are in extends Linear Layout.
Add LayoutParams to the textview and make your linear layout as orientation vertical
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
DurrationView = new TextView(getContext());
LinearLayout.LayoutParams durrationViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationView.setLayoutParams(durrationViewParams);
DurrationView.setText("Text1");
linearLayout.addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
LinearLayout.LayoutParams durrationViewoverallParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationViewoverall.setLayoutParams(durrationViewoverallParams);
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("Text2");
linearLayout.addView(DurrationViewoverall);
addView(linearLayout);
Check that the LinearLayout is set to have a vertical orientation. If that is set properly, you may need to set the LayoutParams of the TextViews programatically as well.
I am trying to dynamically create a linear layout with a dynamic number of buttons based on certain parameters. So far I have some code that compiles but when it runs it does not display anything.
public void displayMenu()
{
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();
//Test Button
Button btn1= new Button(this);
btn1.setText("Dylan");
lin.addView(btn1);
for (int i =0 ; i < 5; i++){
btnList.add(new Button(this));
btnList.get(i).setText("Hello:"+i);
lin.addView(btnList.get(i));
Log.i("CategoryMenu","Adding btn to view");
}
}
What am i doing incorrectly? I'm sure that i'm missing some thing silly here.
you have to add lin to the current hierarchy of view, or creating a new one calling setContentView(lin);
It seems that you're not adding that linear layout to a viewgroup parent.
After all your code you should add something like
myParentViewGroup.add(lin);
Before lin.addView(btn1);
You should define layoutparams for Button like:
// Defining the layout parameters of the Button
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Setting the parameters on the Button
tv.setLayoutParams(lp);
You have to add lin too the layout.
At present you have just created a Linearlayout, but you have not attached it to any layout.
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();
I am trying to put a linearlayout vertical inside the linear layout programmatically, but it seems that its not working, the buttons doesnt appear, but the text view appear...
Here is my code:
(this is for a dialog..)
LinearLayout titleLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.VERTICAL);
m_titleView = new TextView(m_context);
m_titleView.setText(title);
LinearLayout horizontalLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
Button backward = new Button(m_context);
backward.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
backward.setText("Backwards");
Button newDirButton = new Button(m_context);
newDirButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newDirButton.setText("New folder");
horizontalLayout.addView(backward);
horizontalLayout.addView(newDirButton);
titleLayout.addView(m_titleView);
titleLayout.addView(horizontalLayout);
Thanks in advance!
Try setting LayoutParams on horizontalLayout.
Anyway, I'd suggest moving to xml world, as this code is not maintainable.
Edit:
Answer found by the author:
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
should be:
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
I need to create a button programatically and have it centered on the layout, both horizontally and vertically. I am trying with the following code:
LinearLayout ll = (LinearLayout)findViewById(R.id.layoutItem);
Button b = new Button(this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
b.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
b.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
ll.addView(b);
But it's not working. The button comes out on top all to the left.
Any clues on how to fix this?
I would do something like:
LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams)b.getLayoutParams();
ll.gravity = Gravity.CENTER;
b.setLayoutParams(ll);
see if that works.
Or you can use a RelativeLayout as your parent View and do the following:
this.testButton= (Button) this.findViewById(R.id.testButton);
RelativeLayout.LayoutParams testLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
testLP.addRule(RelativeLayout.CENTER_IN_PARENT);
this.testButton.setLayoutParams(testLP);
You can set several rules for the RelativeLayout.
I' m at this point stuck : I want to have a scrollview + a fixed button at the bottom, but Programatically Way ! I can' t go with XML for some technical reason.
Actually i have this :
//Is it really usefull Relative View?
RelativeLayout layout = new RelativeLayout(this);
ScrollView sv = new ScrollView(this);
sv.setId(2);
// What is it? RelativeLayout.LayoutParams?
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, sv.getId());
sv.setLayoutParams(new ViewGroup.LayoutParams(480, 800));
layout.addView(saveButton, lp);
layout.addView(sv);
I do the first 3 page Google on "fixed button and scrollview Android programatically"
Im beginner on Android, so, don' t hesitate to comment on my code some hints ;)
Thx for your help.
try this
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(lp);
ScrollView scroll = new ScrollView(this);
LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,0, 1.0f);
scroll.setLayoutParams(slp);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
layout.addView(scroll);
layout.addView(btn);
setContentView(layout);
If you are trying to create a view where the upper portion of the screen is a scrolling list, and the bottom portion is a button, put the scrollview inside a linearlayout, and put the button in the linearlayout below the scrollview.
http://developer.android.com/reference/android/widget/LinearLayout.html