I have a relative layout:
RelativeLayout ll = new RelativeLayout(this);
How can I set a background to the layout (dynamically not in xml) and have it stretch to fill the entire screen?
Thanks
Try
ll.setBackgroundResource(R.drawable.background);
Look here for methods you need.
Related
I have Image in my layout and I want when user clicked on image, 4 drawable (corner button) added in corner of the image. How can I do this?
Add id field to the relative layout in the xml. Use that id in the class to create view dynamically.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.YourID);
imageView.setImageDrawable(R.drawable.image);
//Add View to Layout:
relativeLayout.addView(imageView);
well there are many ways to that.
Using layoutparams you can set layout params programatically.
like
LayoutParams param = new LayoutParam(LayoutPara.width, LayoutParam.height);
//your rules...
imageView.setlayoutParams(param);
here you can set margintop, bottom, left and right and alignParentLeft.... so on
I am trying to achieve a dynamic list of textviews like in the image below :-
Here is my code :-
LayerDrawable dashboardResShape_community= (LayerDrawable) getResources().getDrawable(R.drawable.upcomingtask_tags_shape);
// The background effect is by the layer list drawable from the above code
LinearLayout tags_view2=(LinearLayout)findViewById(R.id.tags_view);
LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 2, 2, 2);
TextView[] tx = new TextView[15];
for(int i=0; i<15; i++) {
tx[i] = new TextView(getActivity());
tx[i].setPadding(8, 4, 8, 4);
tx[i].setBackground(dashboardResShape_community);
tx[i].setLayoutParams(lp);
tx[i].setText("Tag"+i);
tags_view2.addView(tx[i]);
}
and in my xml there is only a linear layout :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tags_view"
android:orientation="horizontal" >
</LinearLayout>
This is what i achieve :-
When i am adding 15 textviews, only 8 are shown like below, the rest should come in the next line but they are not.
If i add more textviews, it goes out of screen but i want to add the textview in the second line when the first line is full. What i am doing wrong here?
Its LinearLayout's limitation.
If you want the explained behavior than
You have to make your own Layout/View refer this link or
Impliment LinearLayout Horizontal orientation with wrapping children like this
you cannot get more text views on next line after linear layout is filled( screen width ), you already the made linear layout orientation as horizontal. Better solution add one more linear layout or use relative (do some child count coding and set parameters). The best solution i prefer for u is table layout. Easier to code code and handle
What you can do is add as many textviews as will fit on the screen to your linearlayout, but then when a textview would go off the screen, you could add another linearlayout below the one that you already had, and then add on to that. You could keep doing that and you would end up with no textviews goind off the screen. You could also try using a gridview.
Here is what this layout looks like:
http://developer.android.com/guide/topics/ui/layout/gridview.html
And here is the documentation:
http://developer.android.com/reference/android/widget/GridView.html
I have a linearlayout as a container for two relativelayouts. Both relativelayouts appear on the screen but they are side by side. I want them to be top and bottom. It looks as if the linearlayout initialization defaults to Horizontal. I have tried using setorientation to Vertical but the screen blanks out.
The following code is an example of what I am trying to do:
LinearLayout layoutContainer = new LinearLayout(this);
layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//layoutContainer.setOrientation(LinearLayout.VERTICAL);
// Arguments here: width, height, weight
LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, 1);
layoutTop = new RelativeLayout(this);
layoutContainer.addView(layoutTop, childLp);
layoutBot = new RelativeLayout(this);
layoutContainer.addView(layoutBot, childLp);
layoutTop.setBackgroundColor(GREEN);
layoutBot.setBackgroundColor(Color.BLUE);
setContentView(layoutContainer);
It looks as if the linearlayout initialization defaults to Horizontal.
That is exactly right.
I have tried using setorientation to Vertical but the screen blanks out.
You do need to set orientation to vertical to get this effect. As for it "blanks out", I see several things wrong such as setting the width to "0". There is no width so it won't show anything. I think you would want something like LinearLayout.WRAP_CONTENT. Also, you are using LinearLayout params for your RelativeLayout which may or may not make a difference in this case.
If there isn't a necessary reason to create the layout in Java it is much easier to do this in the xml.
I think you are complicating your layouts by trying to programmatically manipulate them. Set the orientation to Vertical and do the following:
<LinearLayout
------
------
android:orientation="vertical">
Your first Relative layout
<RelativeLayout
android:id="#+id/rel1"
android:layout_alignParentTop="true"
----------------------
---------------------
/>
Your next Relative layout
<RelativeLayout
android:id="#+id/rel2"
android:layout_below="#id/rel1"
----------------------
---------------------
/>
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!
Is it possible to create a Button, whose background drawable is a layout?
For example, I would like to create a layout with some horizontal images and some textviews, and all the resulting view should be clickable all at once as a single button.
Would it be possible, and how?
Thank you.
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_nested_layout);
yourLayout.setOnclickListener(...);