It is not a duplicate question.
I have a xml layout which have alot of things and I'm including 2 layouts which have visibility gone.
<include layout="#layout/some_layout" />
Now I'm adding imageviews dynamically.
layout.addView(imageview);
But it is coming top in the hierarchy of layout.
i.e. Whenever I am setting visibility of my include layout as visible. The dynamically added imageviews overlaps it.
How can I add this down in the hierarchy?
Use layout.addView(view,index); to add imageview where index is less than the index of included layout.
Add id in your include layout like
<include android:id="#+id/include1" layout="#layout/some_layout" />
Use RalativeLayout rule like
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.BELOW, R.id.include1);
layout.addView(imageview, params1);
Related
I am having trouble adding a view in the scrollview. The structure of my xml is
// xml_layout.xml
<ScrollView>
<LinearLayout
android:id="#+id/container"
>
<LinearLayout>
<ListView
...
/>
</LinearLayout>
******Here I am trying to add an item *********
</LinearLayout>
</ScrollView>
I am trying to add a textview at the bottom programmatically as follows.
TextView v = new TextView(getActivity());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setText("TEST View");
v.setLayoutParams(params);
LinearLayout container = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.xml_layout, null).findViewById(R.id.container);
container.addView(v, 1);
The goal I am trying to achieve is making a tablelayout that contains two columns with the title and listview in the second column. Before I do this, I tried to display a simple textview so that I know I can add a widget. I have tried many different ways but did not work. Is there something wrong with my code??
you must first verify that your LinearLayout 'container' is 'vertical ' orientation, and...
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(v);
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 got a relative layout which contains another relative layout which I used to replace the "title", a linearLayout which I will use later as a "control panel", and a horizontalScrollView, where the horizontalScrollView contains a LinearLayout(let's name this linear layout - "hsc".
I also have another xml layout file named "entries" that contains an imageView.
My question is, how do i attach "entries" inside "hsc"? Or how to I populate "hsc" with multiple "entries"?
My main layout's structure looks something like this:
<RelativeLayout>
<relativeLayout1>
<linearLayout>
<horizontalScrollView1>
<hsc>
Thanks!
Try to use LayoutInflater. First get the hsc in code somehow like this
LinearLayout layout = (LinearLayout) findViewById(R.id.hsc_id);
Then you make new entrie
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View entrie = inflater.inflate(R.layout.entries,
null, false);
and put one into another
layout.addView(entrie);
you can add multiple views by repeating child view creation process.
If your are going to populate the view, you may want to use a ListView (FragmentList or ListActivity).
In which case you use the tag
<RelativeLayout>
<relativeLayout1>
<linearLayout>
<horizontalScrollView1>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
And then load the list with an Adapter.
http://www.vogella.com/articles/AndroidListView/
Your question seem to imply that you some-how need to traverse the xml tree, you don't, you just use android:id to find the resource from the code side.
create a linear layout dynamically and add some view in that.....
LinearLayout layoutContainer=new LinearLayout(your_activity.this); //create a linear layout dynamically
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.RIGHT;
layoutContainer.setLayoutParams(lp);//apply attributes to your linear layout
View viewOther = LayoutInflater.from(your_activity.this)
.inflate(R.layout.layout_to_add, layoutContainer);//add some view to your linear_layout.
hope it helps....!
scrollview = (ScrollView)findViewById(R.id.detailedScrollView);
for (Quotation quotation : object.quotes){
TextView quote = new TextView(this);
quote.setText(quotation.getQuote());
quote.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
scrollview.addView(quote);
}
Let's say there are three quotes, then I want to have three textViews. However, the code above crashes my app. Any obvious mistakes? Here's the error I'm getting:
11-06 17:35:53.214: E/AndroidRuntime(1430): java.lang.IllegalStateException: ScrollView can host only one direct child
You can't add views directly inside a scrollview. A scrollview can only contain a single layout object. What you have to do is to add a linearlayout in your scrollview, then add the textview to the linearlayout
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container. Please more detail
With best regards,
Psycho
You need to add a "LinearLayout" (or "RelativeLayout") inside ScrollView.
Say you have the layout xml as follows:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
And now you want to add the 'TextView' programmatically, which is as follows:
LinearLayout linearLayout =(LinearLayout) this.findViewById(R.id.linearlayout1);
for (Quotation quotation : object.quotes){
TextView quote = new TextView(this);
quote.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
quote.setPadding(4, 0, 4, 0); //left,top,right,bottom
quote.setText(quotation.getQuote());
linearLayout.addView(quote);
}