How to create this UI ( android xml ) in Java code - android

I have this main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#android:color/white"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#drawable/barslide"
android:layout_marginLeft="500dp"/>
</LinearLayout>
</FrameLayout>
I want to create the same UI like in main.xml in java code. I try to code it but it doesn't work, it's not same as the xml. Here is the code:
void createUI(){
LayoutParams params1 = new FrameLayout
(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
LayoutParams params2 = new android.widget.FrameLayout.LayoutParams(
android.widget.FrameLayout.LayoutParams.FILL_PARENT,
android.widget.FrameLayout.LayoutParams.FILL_PARENT,
Gravity.BOTTOM);
FrameLayout f1 = new FrameLayout(this);
f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LinearLayout l2 = new LinearLayout(this);
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(params2);
view1 = new page1(getBaseContext());
view360 = view1.img1(getBaseContext());
view360.setBackgroundDrawable(getResources().getDrawable(R.drawable.black1));
view360.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
slidebar = view1.img3(getBaseContext());
slidebar.setBackgroundResource(R.drawable.barslide);
slidebar.setLayoutParams(new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT,Gravity.BOTTOM));
f1.addView(view360);
f1.addView(l2);
l2.addView(slidebar);
addContentView(f1, params1);
}

Your code doesn't quite replicate the xml layout(this was to big for a comment):
// replace THIS(from the constructors) with your context reference
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
ImageView imv1 = new ImageView(this); // the first ImageView
imv1.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
imv1.setImageResource(R.drawable.ic_launcher);
fl.addView(imv1);
LinearLayout ll = new LinearLayout(this);
FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
llp.gravity = Gravity.BOTTOM;
ll.setLayoutParams(llp);
ll.setBackgroundColor(Color.WHITE);
ImageView imv2 = new ImageView(this); // the second ImageView
DisplayMetrics metrics = getResources().getDisplayMetrics();
float width = 300f;
int pWidth = (int) (metrics.density * width + 0.5f);
LinearLayout.LayoutParams imvlp = new LinearLayout.LayoutParams(pWidth,
FrameLayout.LayoutParams.WRAP_CONTENT);
float margin = 500f;
int pMargin = (int) (metrics.density * margin + 0.5f);
imvlp.leftMargin = pMargin;
imv2.setLayoutParams(imvlp);
imv2.setBackgroundResource(R.drawable.food_image1);
ll.addView(imv2);
fl.addView(ll);
You'll have to modify it to use your object page1(I don't know exactly what are you trying to do there).
Also, are you sure you have to use the addContentView()(maybe setContentView?) method?(This method will add the views to the already present layout)

Related

Make child of scrollview 1/3 size

I have this:
my problem is that i have a Linearlayout wrapper with a scrollview and a Linearlayout inside.. i want every item to be 1/3 of the scrollviews height no matter how many items i put into it..
When i add items to the Linearlayout it is linearlayouts, if i add 3 'items' it is almost fine.. but if i add 8 items, they are all quite small and scrollable. This is what i want, but i want them to have the height of 1/3 of the scrollview/linearlayout.
i want it to align so its as big as the buttons in the 2nd layout i have.
i just cant seems to make it right, hope its just a simple solution i have refused to see.
my code so far:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="75">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:layout_gravity="center_horizontal"
android:fillViewport="true"
android:scrollbarSize="5dp"
android:fadeScrollbars="false"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/coats"
android:orientation="vertical"
android:weightSum="75">
</LinearLayout>
</ScrollView>
</LinearLayout>
This is my way of adding 'children' to the layout wrapped by the scrollview
LinearLayout w0 = new LinearLayout(this);
LinearLayout w1 = new LinearLayout(this);
ImageView w2 = new ImageView(this);
TextView w3 = new TextView(this);
TextView w4 = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 25);
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));
w0.setLayoutParams(params);
w0.setOrientation(LinearLayout.HORIZONTAL);
w0.setClickable(true);
w0.setBackgroundColor(Color.argb(100, 100, 100, 100));
w0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < LLCoats.getChildCount(); i++) {
View vv = LLCoats.getChildAt(i);
vv.setSelected(false);
vv.setBackgroundColor(Color.argb(100, 100, 100, 100));
}
LinearLayout LL = (LinearLayout)LLCoats.getChildAt(i);
TextView TV = (TextView)LL.getChildAt(1);
TV.requestFocus();
if(TV.getError() != null)
{
Snackbar snackbar = Snackbar
.make(findViewById(android.R.id.content), ""+TV.getError(), Snackbar.LENGTH_LONG);
snackbar.show();
}
v.setSelected(true);
v.setBackgroundColor(Color.argb(255,255,255,255));
}
});
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2);
w1.setLayoutParams(params2);
w1.setOrientation(LinearLayout.VERTICAL);
w1.setClickable(false);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w2.setLayoutParams(params3);
w2.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w3.setLayoutParams(params4);
w3.setTextSize(25f);
w3.setGravity(Gravity.CENTER);
w3.setClickable(false);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w4.setLayoutParams(params5);
w4.setTextSize(45f);
w4.setGravity(Gravity.CENTER);
w4.setClickable(false);
w1.addView(w2);
w1.addView(w3);
w0.addView(w1);
w0.addView(w4);
LLCoats.addView(w0);
UPDATE!:
Now its getting really weird, as i have found a way to calculate the height and add it with:
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, pixelToDp((int)(display.heightPixels* .8 * 0.25)));
This works really well on my PI64 and a ASUS tablet, but my phone makes the height 100% the same as the scrollview, how is happening??
I made it works like this, getting the screen size *.8 because the screen is used as a popup and then 0.25 as items are 1/4 of the screen and in the end -10 for my margins
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(display.heightPixels* .8 * 0.25 - pixelToDp(10)));
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));

how can we divide screen in two equal width in android programatically

i was developing android application but based on the screen sizes element size getting changed.is there is any solutions to divide the screen sizes equally.i am getting different sizes in different screens like 5inches and 3.7 inches
My Code is...
lView.setBackgroundColor(Color.parseColor("#FFFFFF"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 220);
GradientDrawable gdtitle = new GradientDrawable();
gdtitle.setCornerRadius(5);
ImageView title = new ImageView(Main2Activity.this);
title.setImageResource(R.drawable.logo);
title.setLayoutParams(layoutParams2);
title.setBackgroundDrawable(gdtitle);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title);
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("#FFFFFF")); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
TextView uname1 = new TextView(Main2Activity.this);
uname1.setText("Hello , " + Sessionname);
uname1.setGravity(Gravity.CENTER);
uname1.setTextColor(Color.parseColor("#003366"));
uname1.setTextSize(20);
uname1.setWidth(width);
uname1.setLayoutParams(l2);
et1 = new TextView(Main2Activity.this);
et1.setHeight(Height);
et1.setWidth(width);
et1.setTextColor(Color.WHITE);
et1.setHintTextColor(Color.WHITE);
et1.setGravity(Gravity.CENTER);
et1.setHint("Select Date");
et1.setBackgroundDrawable(gd3);
et1.setTextSize(15);
et1.setLayoutParams(l2);
LinearLayout.LayoutParams l4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
l4.gravity = Gravity.CENTER;
lHorizontalView1.setOrientation(LinearLayout.HORIZONTAL);
lHorizontalView1.setLayoutParams(l4);
lHorizontalView1.addView(uname1);
lHorizontalView1.addView(et1);
lView.addView(lHorizontalView1);
GradientDrawable gd4 = new GradientDrawable();
gd4.setCornerRadius(30);
gd4.setColor(Color.parseColor("#5CB85C"));
gd4.setStroke(3, 0xFFFFFFFF);
Intime = new TextView(Main2Activity.this);
Intime.setHint("In Time");
Intime.setTextColor(Color.WHITE);
Intime.setHintTextColor(Color.WHITE);
Intime.setTextSize(15);
Intime.setHeight(Height);
Intime.setWidth(width);
Intime.setGravity(Gravity.CENTER);
Intime.setLayoutParams(l2);
Intime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
Outtime = new TextView(Main2Activity.this);
Outtime.setHint("Out Time");
Outtime.setTextSize(15);
Outtime.setHeight(Height);
Outtime.setWidth(width);
Outtime.setGravity(Gravity.CENTER);
Outtime.setTextColor(Color.WHITE);
Outtime.setHintTextColor(Color.WHITE);
Outtime.setLayoutParams(l2);
Outtime.setBackgroundDrawable(gd4);
lView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout lHorizontalView = new LinearLayout(Main2Activity.this);
LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
l3.gravity = Gravity.CENTER;
lHorizontalView.setOrientation(LinearLayout.HORIZONTAL);
lHorizontalView.setLayoutParams(l3);
lHorizontalView.addView(Intime);
lHorizontalView.addView(Outtime);
lView.addView(lHorizontalView);
You should use DisplayMetrics
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
Example
RelativeLayout rl_firstObj=(RelativeLayout)findViewById(R.id.rl_first);
rl_firstObj.getLayoutParams().width=DeviceTotalWidth*50/100;
RelativeLayout rl_secondObj=(RelativeLayout)findViewById(R.id.rl_second);
rl_secondObj.getLayoutParams().width=DeviceTotalWidth*50/100;
For buttons parent view use linearlayout and set
param.weight = 1
for each button.
Using LinearLayouts and assigning weights to the views inside it will divide the seperate the views with the same ratio no matter the screen size. Here, I've assigned weight 1 to both the views, thus they will be shown side my side divided equally on the screen.
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
android:textSize="16sp" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2"
android:textSize="16sp" />
</LinearLayout>
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
textView1.setLayoutParams(layoutParams);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
textView2.setLayoutParams(layoutParams1);
I have taken a linear layout with parameter weight as 1 and divide that in two views.Here i used textviews u can take any view in your code.Please let me know if face any issue

Programmatically add views to LinearLayout with both gravity and weight

I have LinearLayout generated in xml, this one:
<LinearLayout
android:id="#+id/available_themes_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:paddingTop="6dp"
android:paddingBottom="6dp"/>
I'm trying to add some custom views to this layout and place them evenly along layout:
BiColorCircleView lightOrangeCircleView = new BiColorCircleView(getCurrentActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f);
params.gravity = Gravity.CENTER;
lightOrangeCircleView.setLayoutParams(params);
availableThemesLayout.addView(lightOrangeCircleView);
But at the end I achieve that only weight is applied and my views is aligned to left side. Thought, if weight is not set - gravity works nice.
Could somebody point me to way of solving this problem?
LinearLayout availableThemesLayout = new LinearLayout(null);
ImageView lightOrangeCircleView = new ImageView(null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f);
FrameLayout frameLayout = new FrameLayout(null);
availableThemesLayout.addView(frameLayout, params);
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
frameParams.gravity = Gravity.CENTER;
frameLayout.addView(lightOrangeCircleView, params);
Try this.

TextView doesn't wrap text

I need dynamically create next xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="a lot of text will be here"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
</LinearLayout>
and here is my code:
LinearLayout ll = new LinearLayout(getActivity());
ll.setLayoutParams(layoutLP);
TextView title = new TextView(getActivity());
title.setText("a lot of text will be here");
title.setLayoutParams(textLP);
EditText editText = new EditText(getActivity());
editText.setLayoutParams(ediLP);
editText.setSingleLine();
ll.addView(title);
ll.addView(editText);
layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutLP.gravity = Gravity.CENTER;
textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
However, on device I get problem: text inside textView is not wrapped and partly covered by editText.
Why does this happen?
And here is how it must looks like:
Your Activity should be
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutLP.gravity = Gravity.CENTER;
layoutLP.weight = 2;
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
LinearLayout.LayoutParams editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
// Add textview 1
TextView textView1 = new TextView(this);
textView1.setLayoutParams(textLP);
textView1.setText("programmatically created TextView1");
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(textView1);
// Add textview 1
EditText editText = new EditText(this);
editText.setLayoutParams(editLP);
editText.setHint("programmatically created TextView1");
// editText.setBackgroundColor(Color.GRAY); // hex color 0xAARRGGBB
editText.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(editText);
linearLayout.addView(linearLayout1);
Your xml file should be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
Try your phone or a phone with similar dpi-resolution properties as yours on the AVD, see if it is the same problem. Maybe the resolution on your phone is the problem.

Not able to centre a Linear Layout and therefore the imageView inside it

I want to create a ImageView dynamically and place it at the center of the center. I tried doing it in xml and I was able. I tried to convert the same thing to code but the imageView always appears at the top left corner of the screen. Can somebody point out what the mistake is ? I'm still new to Android and finding my way. There might be blunders as well.
Thanks
The XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/myimg"
android:layout_width="100dip"
android:layout_height="100dip"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal"
android:background="#drawable/exmp"
/>
</LinearLayout>
The code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Resources res = getResources();
String pluginPackageName = this.getApplicationContext().getPackageName();
int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(getApplicationContext());
iv.setLayoutParams(imageViewParams);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageResource(drawable_spinner);
layout.addView(iv, imageViewParams);
addContentView(layout, imageViewParams);
}
To make it exactly as in the XML layout file you have to change addContentView(layout, imageViewParams); to setContentView(layout);
EDIT:
full code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Resources res = getResources();
String pluginPackageName = this.getApplicationContext().getPackageName();
//int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
//int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
//int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(getApplicationContext());
iv.setLayoutParams(imageViewParams);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageResource(R.mipmap.ic_launcher);
layout.addView(iv, imageViewParams);
setContentView(layout);
}
You have this as the last line in onCreate:
addContentView(layout, imageViewParams);
Perhaps you meant this?
addContentView(layout, layoutParams);
Alternatively, maybe this:
setContentView(layout, layoutParams);
Use layoutGravity insted of gravity.
imageViewParams.layoutGravity = Gravity.CENTER_HORIZONTAL;
You've to set the layout_gravity. You can do this way:
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);

Categories

Resources