I want to position a View horizontally centered in a Layout. My code below fails. How can I fix this?
See my code:
LinearLayout LLT = new LinearLayout(context);
LLT.setOrientation(LinearLayout.VERTICAL);
LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//get booktheme by bookID
theme = db.getthemeByID(id);
String themePath = theme.getFilepath();
int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
//imageView
ImageView imageTheme = new ImageView(context);
imageTheme.setLayoutParams(new LayoutParams(500, 700));
imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageTheme.setPadding(0, 20, 0, 10);
imageTheme.setAdjustViewBounds(true);
imageTheme.setImageResource(resid);
LLT.addView(imageTheme);
// add view
VF.addView(LLT);
Use Gravity feature in your LayoutParams as following:
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
imageTheme.setLayoutParams(layoutParams);
Replace your code with this one and try:
LinearLayout LLT = new LinearLayout(context);
LLT.setOrientation(LinearLayout.VERTICAL);
LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//get booktheme by bookID
theme = db.getthemeByID(id);
String themePath = theme.getFilepath();
int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
//imageView
ImageView imageTheme = new ImageView(context);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
imageTheme.setLayoutParams(layoutParams);
imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageTheme.setPadding(0, 20, 0, 10);
imageTheme.setAdjustViewBounds(true);
imageTheme.setImageResource(resid);
LLT.addView(imageTheme);
// add view
VF.addView(LLT);
have you tried using relative layout instead of linear layout ?
EDIT :
if u want to use relative layouts ,
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)imageview.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageview.setLayoutParams(layoutParams);
( did not test the code btw but i used it in one of my app so this shld do the job )
Related
This question already has answers here:
ImageView without xml definition
(2 answers)
Closed 7 years ago.
I want to create an Imageview programmatically without using XML. Is there any way to create it using the height, width and margin property.
Is it possible to add multiple component like this to different places of the screen using the height, width and margin property?
I wrote some code you can use. It relies on the fact that your container is a RelativeLayout:
ImageView myImageView = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.topMargin = myTopMargin;
params.leftMargin = myLeftMargin;
myImageView.setLayoutParams(params);
myImageView.setImageResource(R.id.my_image_resource);
myRelativeLayout.addview(myImageView);
Hope this helps :)
try this..
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//setting image margin
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
imageView.setLayoutParams(layoutParams);
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
Hope this will help..
Use this:
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.something);
//setting image position
//set the margin to the layoutparams
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
//add view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
I am doing an android app that have images which are located besides each other,
every image has its own width, I want to add these images in one line to fit the screen width,
with the same ratio for every one
I wrote code to add this images but still to know how to set the width programmatically
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
LinearLayout my_root = (LinearLayout) findViewById(R.id.root_layout);
LinearLayout child = new LinearLayout(this);
child.setOrientation(LinearLayout.HORIZONTAL);
child.addView(view);
child.addView(view1);
child.addView(view2);
my_root.addView(child);
only image 1 and 2 appear but the third didn't appear because the width of screen finished
Any help !!
Thank you :)
You must need to use weight parameter to do this.
Try below code:
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
view1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
view2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Use Layout Parameters to set height and Width of views at runtime.
like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
view .setLayoutParams(layoutParams);
here are my text and image and text views(i1, t1, t2). how can i format the width and height of these views dynamically in my class file.
ImageView i1 = new ImageView(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
RelativeLayout rl1 = new RelativeLayout(this);
RelativeLayout.LayoutParams innerLP1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
innerLP1.addRule(RelativeLayout.CENTER_IN_PARENT);
innerLP1.topMargin=(30);
t1.setText(name);
rl1.setLayoutParams(innerLP1);
rl1.addView(t1);
set layout parameters to those views
RelativeLayout.LayoutParams view_params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageview.setLayoutParams(view_params );
textview.setLayoutParams(view_params );
Use the txt.SetHeight() method.
Use setHeight() & setWidth() methods. For example:
ImageView i1 = new ImageView(this);
i1.setHeight(xxx);
i1.setWidth(xxx);
I have a RelativeLayout
RelativeLayout myLayout = new RelativeLayout(this);
Then, I add 2 ImageView in this Layout:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20, 20);
ImageView img1 = new ImageView(this);
// Here I give the position of img1
ImageView img2 = new ImageView(this);
// And here the position and img2
myLayout.addView(img1, params);
myLayout.addView(img2, params);
setContentView(myLayout);
And here I have a problem: I want to show and click on the 2 ImageViews, but only the img2 is visible on my screen.
Is there a problem with the z-index or something else?
Since you are using RelativeLayout and same params for both ImageView one will be overlapped with the other. So define params for both. Add rule to each param for positioning it. And then give addView().
For eg:
RelativeLayout myLayout = new RelativeLayout(this);
ImageView img1 = new ImageView(this);
RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftArrowParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ImageView img2 = new ImageView(this);
RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rightArrowParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
myLayout.addView(img1, firstImageParams);
myLayout.addView(img2, secImageParams);
setContentView(myLayout);
I have an image in "src\main\res\drawable\gate_logo.png" which looks like this :
My Java code looks like this :
LinearLayout Demo_Layout = new LinearLayout(context);
Demo_Layout.setId(View.generateViewId());
// Demo_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
Demo_Layout.setBackgroundColor(Color.rgb(98, 198, 238));
Demo_Layout.setHorizontalGravity(Gravity.CENTER);
Demo_Layout.setVerticalGravity(Gravity.CENTER);
addView(Demo_Layout, LinearLayout.LayoutParams.MATCH_PARENT,328);
TextView textView = new TextView(context);
textView.setId(View.generateViewId());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
textView.setText(Html.fromHtml("<P><Br><big>GATE Demo</big><P>[ Graphic Access Tabular Entry ]<Br><small>An Interception-resistant Authentication System</small>"));
// Demo_Layout.addView(textView, LinearLayout.LayoutParams.MATCH_PARENT, 328);
// Demo_Layout.addView(textView, 600, 328);
// int resID = getResources().getIdentifier("gate_logo.png", "drawable", "package.name");
int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(resID);
// gateLogoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// gateLogoView.setScaleType(ImageView.ScaleType.FIT_XY);
gateLogoView.setLayoutParams( new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
gateLogoView.setX(1);
gateLogoView.setY(1);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
gateLogoView.setLayoutParams(vp);
gateLogoView.setImageResource(resID);
// Demo_Layout.addView(gateLogoView, 600, 328);
Demo_Layout.addView(gateLogoView);
LinearLayout Button_Layout = new LinearLayout(context);
Button_Layout.setId(View.generateViewId());
Button_Layout.setBackgroundColor(Color.rgb(168, 98, 188));
Button_Layout.setHorizontalGravity(Gravity.CENTER);
Button_Layout.setVerticalGravity(Gravity.CENTER);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 138);
lp1.addRule(RelativeLayout.BELOW, Demo_Layout.getId());
addView(Button_Layout, lp1);
Button Registration_Button=new Button(context);
Registration_Button.setText("Registration");
Registration_Button.setAllCaps(false);
Registration_Button.setOnClickListener(Registration_Demo_Listener);
Button_Layout.addView(Registration_Button);
Button Login_Button=new Button(context);
Login_Button.setText("Login");
Login_Button.setAllCaps(false);
Login_Button.setOnClickListener(Login_Demo_Listener);
Button_Layout.addView(Login_Button);
Button Auto_Demo_Button=new Button(context);
Auto_Demo_Button.setText("Auto Demo");
Auto_Demo_Button.setAllCaps(false);
Auto_Demo_Button.setOnClickListener(Auto_Demo_Listener);
Button_Layout.addView(Auto_Demo_Button);
I also have a half size smaller version of the image in the same directory : gate_logo_s.png
I've tried different combinations, but why is the logo image not showing up ?
Screenshot look like this :
Replace these lines:
int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(resID);
with these:
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(R.drawable.gate_logo);