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);
Related
I need to add TextViews to a Layout (RelativeLayout or LinearLayout I don't care) programatically.
I want something like this:
After the textview 4 there is no more space on the right side so the next TextView will be placed in a new Line bellow.
This is what I have done:
Create a LinearLayout in the xml file:
<LinearLayout
android:id="#+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
In my code, I loop over a list of Objects and add a new TextView for each objekt:
LinearLayout myLinearLayout = (LinearLayout) itemView.findViewById(R.id.my_linear_layout);
for(MyObject object : myObjectList) {
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0); // llp.setMargins(left, top, right, bottom);
final TextView textView = new TextView(context);
textView.setText(object.getName());
textView.setLayoutParams(params);
myLinearLayout.addView(textView, params);
}
But this is the result I get:
I have thought adding LinearLayouts vertically instead of TextViews, but still my problem is that I don't know when I need to add the next the next one, I mean, when there is no more space on the right side.
I have also tried it with TableLayout but still the same problem, I don't know when I have to add the next row.
Any ideas? Maybe there is a View that already solves that problem and I don't know...
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....!
I am trying to add TextView into a view which extends LinearLayout. When I set its width of layoutparams to fill_parent or wrap_content, the text inside TextView will display vertically such like the following.
e.g.
T
E
X
T
However, when I set the width to some fixed number, it will display normally or as I expected.
e.g. TEXT
My question is why this happens and how to solve it, i.e. how I should set programmatically so that TextView can write text horizontally without setting a fixed width to it such as setting fill_parent or wrap_content?
Here is setting of XML code of the parent ilGallery which extends LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.illib.ilgallery.view.ILGallery
android:id="#+id/galleryLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
The following is the code how i initialize the children inside it:
ilViewPager = new ILViewPager(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ilViewPager.setLayoutParams(params);
ilgallery = this;
//initialize default header and footer view
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
headerView = new TextView(context);
headerView.setLayoutParams(params2);
((TextView)headerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)headerView).setText("hello");
footerView = new TextView(context);
footerView.setLayoutParams(params2);
((TextView)footerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)footerView).setText("hello2");
There appears to be only a single item inside your LinearLayout. Please set the orientation of the LinearLayout to "horizontal", like this:
android:orientation="horizontal"
Also, whenever possible instead of creating new LayoutParams objects from scratch, you should get the LayoutParams from the current layout and modify that, like this:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ilViewPager.getLayoutParams();
lp.<change-any-values>;
ilViewPager.setLayoutParams(lp);
Context: I have a TableLayout (created using XML), which has one TableRow, which has one TextView. The code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
android:id="#+id/mytable"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:id="#+id/add_alarm"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:gravity="center"
android:text="New\nItem"
android:textSize="30sp"
/>
</TableRow>
</TableLayout>
</ScrollView>
In my Activity's onCreate() method, I am trying to add another View to the TableRow dynamically. Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
View mainLayout = inflater.inflate(R.layout.main, null);
TableLayout tl = (TableLayout) mainLayout.findViewById(R.id.mytable);
TableRow tr = (TableRow) tl.getChildAt(0);
Log.d(TAG, "tr class = " + tr.getClass().getName() + " | width = " + tr.getWidth() + "\n");
final RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.alarm_widget, null);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
tr.addView(rl, lp);
tl.invalidate();
setContentView(mainLayout);
}
Question: This code is not having the intended effect of displaying both the Views (the one in the XML layout already & the other added dynamically) in a columns of equal width.
With the code given above, the dynamically added View has a width of '0' and is therefore invisible.
If I change the code to tr.addView(rl) (i.e. without reference to LayoutParams), the dynamically added view is visible, but the columns are not equal in width.
How can I achieve this?
Edit: I changed the code based on the comments to the following. It still doesn't work as expected:
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1f);
tr.addView(rl, lp);
The problem is this behavior defined for TableRow:
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.
Rather than add your text views directly to the TableRow, have the TableRow hold a horizontal LinearLayout and add the second view to that holder.
(Also, using LinearLayout.LayoutParams for something that's going into a TableRow is wrong. You should have been using TableRow.LayoutParams. But that wouldn't be the way to get equal-width TextViews. Use a LinearLayout holder.)
I think it's not a good idea to dynamically add an item to a TableRow - it defeats the purpose of using a table. Imagine if you add an item to the first row of the table, but not on the second, meaning the first row has more elements. It wouldn't look much like a table.
But if you insist,
From developer guide:
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.
You may need to start looking on the layout_weight of each element. Try adding layout_weight=1 on the row's static elements, and then setting your dynamic RelativeLayout's weight to 1 before adding it to the row.
rl.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
The last parameter is weight.
Set height like this to the layout containing the ScrollView.
It solved my own problem where tablelayout does not show last lines.
android:layout_height="0dp"
I have this code
View item = View.inflate(context, R.layout.item_layout, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
layout.addView(item, params);
my item_layout: (note the part android:layout_marginTop="2dip")
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_width="fill_parent">
<ImageView android:src="#drawable/pic_unknown" android:id="#+id/image1"
android:layout_height="50dip" android:layout_width="50dip"
android:padding="5dip"></ImageView>
</RelativeLayout>
and then in my layout I see the list of items inflated but with no margin in-between them. I tried with margintop=10dip still nothings happen my point is that the value I put in the layout it is not taken in the calculation with or without the margin top the layout is the same.
How can I add some empty space between the items ?
How can I inflate a empty space between the items ?
Is it possible to inflate something like gap or some space ?
or I must use workaround like inflating some empty layout with 2dip height or something
Thanks
The last parameter of the inflate method is the parameter to which you add the inflated view. In your case it is null. Try this instead:
View item = View.inflate(context, R.layout.item_layout, layout);
Try Padding the RelativeLayout instead if your margins apply to the outside.
You can add margin to layout which you inflated like below:
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.topMargin = 10;