It inflate the view without the margin - android

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;

Related

Unable to set layout width, height and weight of inflated view in XML in Android

I am developing an Android app. In my app, I need to inflate list of views dynamically. I added them and working. The problem is with setting the width and height of layout. Now I will demonstrate my problem with a simple project. Actually, my project is much more complicated than this simple project.
I am inflating views to this layout.
<LinearLayout
android:orientation="horizontal"
android:id="#+id/cm_photos_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
I am looping through a list of bitmap and adding view dynamically as follow
for(Bitmap bmp : bitmaps)
{
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
previewImageView.setImageBitmap(bmp);
container.addView(preview);
}
Please note, in the above code, container is a LinearLayout added dynamically to the parent XML in the above.
container = new LinearLayout(this);
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(params);
parentLinearLayout.addView(container);
This is my item_cm_preview_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_weight="1"
android:layout_height="400dp"
android:layout_width="0dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:scaleType="centerCrop"
android:id="#+id/item_cm_preview_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
As you can see, I set the layout height to 400dp, width 0 and layout_weight to 1 in the XML. So all image height must be same and width must be equal because of layout_weight. But the result is not as expected. You can see screenshot below.
As you can see in the screenshot, both layout_weight and height are not working for all inflated views. But if I add extra ViewGroup dynamically and inflate the view to that layout, it is working. Below is my code
//This happening in for loop
LinearLayout wrapper = new LinearLayout(this);
wrapper.setLayoutParams(new LinearLayout.LayoutParams(0,500,1));
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
previewImageView.setImageBitmap(bmp);
wrapper.addView(preview);
container.addView(wrapper);
This is the result:
As you can see both layout_weight and height working when I use an extra dynamic linear layout. Why is setting layout weight and height in XML not working? Why second way is working? How can I set weight and height in XML layout file? Is it possible?
If you inflate layout using method
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
it skip its width and height parameters..., but if you will use:
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,parent,false);
it should work correct, for example if you inflate view in activity as parent you can provide (ViewGroup) getView():
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,(ViewGroup) getView(), false);

listView items is not match parent n linear layout

I need to create LinearLayout, which has ListView. I did it with this code:
this.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
final ListView listView = (ListView) LayoutInflater.from(getContext()).inflate(R.layout.chat_timer_rollout, null);
addView(listView);
My chat timer rollout is:
<ListView
xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="wrap_content"
a:background="#color/white"
a:cacheColorHint="#color/white"
a:paddingBottom="12dp"
a:paddingTop="12dp">
After creating i've tried to add items in list view like this:
final ArrayAdapter<TimeInterval> adapter = new ArrayAdapter<>(getContext(),
R.layout.chat_timer_item,
R.id.timer_label, TimeInterval.INTERVALS);
listView.setAdapter(adapter);
Chat timer item has code:
<TextView
a:id="#+id/timer_label"
xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="wrap_content"
a:paddingBottom="11dp"
a:paddingLeft="#dimen/timer_label_margin_right"
a:paddingTop="11dp"
a:textColor="#color/primary_text"
a:textSize="#dimen/timer_text_size"/>
After creating this i see my linear layout on screen, but when i click item it has width wrap_content, not match parent? how to fix it? I've tried to set one more linear layout for inflated with params match_parent, but it didnt help. Thank you!
The second parameter in the LayoutInflater#inflate() method is the (optional) parent View of the inflated layout. When you pass null for the parent, the Inflater doesn't know which type of LayoutParams are appropriate, so it uses a default ViewGroup.LayoutParams, which has ViewGroup.WRAP_CONTENT for both dimensions.
Since your posted code is in your LinearLayout subclass, you can pass this as the second argument in the inflate() call. Also, when you provide the parent View in that two-parameter method, the inflated layout is added to it automatically, so you don't need the addView() call.
final ListView listView = (ListView) LayoutInflater.from(getContext())
.inflate(R.layout.chat_timer_rollout, this);

Adding a widget in scrollView programmatically

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);

create and set margin programmatic for relative layout android

Hi I am developing android application in which I am creating relative layout programmatic and tried to set margin for that and Added it into linear layout which have orientation linear.
So here is my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/screen_background"
tools:context=".ChooseChannelsFragment" >
<LinearLayout
android:id="#+id/main_outer_llt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
</LinearLayout>
</RelativeLayout>
and inside fragment I am adding relative layout like this
RelativeLayout relativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(200, 80);
relativeParams.setMargins(20, 20, 20, 20);
relativeLayout.setLayoutParams(relativeParams);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
linearLayout.addView(relativeLayout);
It create layout with given color and size but not accepting margins. Am I doing something wrong? How to do this? Need Help. Thank you.
The LayoutParams type you use on a view should actually be from its parent.
So, if you're adding a RelativeLayout to a LinearLayout, the LayoutParams you set to your RelativeLayout should actually be a LinearLayout.LayourParams, and not a RelativeLayout.LayoutParams.
As others suggested, layout_margin# is the space between the parent's # edge and your view.
# replaces "Left", "Right", "Top" or "Bottom"
Getting/setting margins worked for me with:
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
params.topMargin += 20;
mView.requestLayout(); // important
Of course, my View was indeed a ViewGroup and the parent was a ViewGroup as well. In most cases, you should cast your layout params to the parent's View class LayoutParams (in this case it's ViewGroup and RelativeLayout)
In this case, the father is LinearLayout. So you should use:
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 20, 20, 20);

TextView write text vertically when setting its layoutparams to fill_parent

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);

Categories

Resources