I am a newbie in Android. I have a requirement where I have to add the imageViews dynamically onto a linear layout and then have to animate each image individually.
I am not aware of the way to add the imageViews dynamically onto a linear layout. Plz help me out.
Thanks
You can add view dynamically like this.
LinearLayout.LayoutParams imParams =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imSex = new ImageView(context);
imSex.setImageResource(getmyImage());
mainlayout.addView(imSex,imParams);
You can give this piece of code a try. It also has the bit for setting the dimensions for the ImageView along with a margin between multiple ImageView's. The int dimens = 45 and the int dimensMargin = 4; are pixel values and are being converted into dp.
The LinearLayout linlaLikes has to be in your layout XML and then cast in your activity.
LinearLayout linlaLikes = (LinearLayout) findViewById(R.id.linlaLikes);
ImageView imgUsers = new ImageView(getApplicationContext());
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 45;
float density = getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens = new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgUsers.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgUsers.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 4;
float densityMargin = getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin = new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
// SET YOUR IMAGER SOURCE TO YOUR NEW IMAGEVIEW HERE
// ADD THE NEW IMAGEVIEW WITH THE PROFILE PICTURE LOADED TO THE LINEARLAYOUT
linlaLikes.addView(imgUsers, imgvwMargin);
see this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.rainbow);
ImageView tv1 = new ImageView (this);
tv1.setImageresorce(R.drawable.image1);
ImageView tv2 = new ImageView (this);
tv2.setImageresorce(R.drawable.image2);
ImageView tv3 = new ImageView (this);
tv3.setGravity(Gravity.CENTER);
tv3.setImageresorce(R.drawable.image3);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tv3);
setContentView(ll);
}
see this usefull data
http://mobile.tutsplus.com/tutorials/android/android-layout/
You could use fragment for this http://developer.android.com/guide/components/fragments.html
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment imageFragment = new ExampleFragment();
fragmentTransaction.add(R.id.image_container, imageFragment);
fragmentTransaction.commit();
Related
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 )
I'm using the CameraPreview example API demo. I need to add some views (button, etc..) overlaying the SurfaceView.
For this, I'm trying to set their parameters, but they appear all the time on the top-left side of the screen.
This is the onCreate method of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
btnTakePhoto = new Button(this);
btnTakePhoto.setBackgroundResource(android.R.drawable.ic_menu_camera);
/*Set container*/
mPreview = new Preview(this);
setContentView(mPreview);
/*Set button params and add it to the view*/
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
addContentView(btnTakePhoto, buttonParams);
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
defaultCameraId = i;
}
}
}
Have to say that the values here
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
are updating well if I change them. What doesn't change is what contains the addRule() method
Finally solved. When doing setContentView() and addContentView(), I was placing the views in a DecorView which is a FrameLayout. So, LayoutParams referencing RelativeLayout won't work, as for a FrameLayout only generic features of LayoutParams will work.
So, the thing is to first create a relativeLayout, set the params and set it as the content:
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(relativeLayout, rlp);
But, now, every time I want to add a view, I have to add it to this relativeLayout this way:
relativeLayout.addView(View, Params);
Just this.
are you wanna to add the btnTakPhoto to the right center of the view? if so, have a try:
btn.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
Try This
// create main linearLayout and set properties
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
// Create camera layout params
LinearLayout.LayoutParams cameralayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// set layout params
linearLayout.setLayoutParams(cameralayoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
int m_Height=ScreenHeight(this);
int buttonWH = 80;
// Create button for capture, save and discard
captureButton = new Button(this);
captureButton.setBackgroundResource(R.drawable.camera);
captureButton.setWidth(buttonWH);
captureButton.setHeight(buttonWH);
saveButton = new Button(this);
saveButton.setBackgroundResource(R.drawable.save);
saveButton.setVisibility(View.INVISIBLE);
saveButton.setWidth(buttonWH);
saveButton.setHeight(buttonWH);
discardButton = new Button(this);
discardButton.setBackgroundResource(R.drawable.discard);
discardButton.setVisibility(View.INVISIBLE);
discardButton.setWidth(buttonWH);
discardButton.setHeight(buttonWH);
// Create layout for controls
controlLayout = new LinearLayout(this);
controlLayout.setOrientation(LinearLayout.VERTICAL);
controlLayout.setBackgroundColor(Color.BLACK);
// Create params for control layout
LinearLayout.LayoutParams controlLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
// Set layout params
controlLayout.setLayoutParams(controlLayoutParams);
int buttonMargin = ((m_Height - (buttonWH * 3)) / 3) / 2;
// Create params for capture button
LinearLayout.LayoutParams buttonCaptureParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
buttonCaptureParams.setMargins(10, buttonMargin, 10, buttonMargin);
LinearLayout.LayoutParams buttonSaveParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
buttonSaveParams.setMargins(10, buttonMargin, 10, buttonMargin);
LinearLayout.LayoutParams buttonDiscardParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
buttonDiscardParams.setMargins(10, buttonMargin, 10, buttonMargin);
// Add button to main layout
controlLayout.addView(discardButton, buttonDiscardParams);
controlLayout.addView(captureButton, buttonCaptureParams);
controlLayout.addView(saveButton, buttonSaveParams);
// Make it full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set main layout as content view
setContentView(linearLayout);
Method to get screen height and width
public static int ScreenHeight(Context ctx) {
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;
}
public static int ScreenWidth(Context ctx) {
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.widthPixels;
}
try to set the parent of btnTakePhoto to mPreview
btnTakePhoto.setParent(mPreview);
which will cause the button to appear above the preview.
ScrollView sv = new ScrollView(this);
this.addContentView(sv, new RelativeLayout.LayoutParams(screen.widthPixels, screen.heightPixels));
RelativeLayout FORM = new RelativeLayout(this);
sv.addView(FORM, new RelativeLayout.LayoutParams(screen.widthPixels, screen.heightPixels));
You must be targeting an API older than 14, setParent should work with newer API's. Above code is an alternative way, however you would have to place everything by code, but it works. Hope this helps.
FORM is the container, and you would have to add your other components like FORM.addView(btn) with relative layout params.
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);
I want to add three linear layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programmatically. I could do this within xml, but I want to do this in program.
here is what I want:
Here its the solution
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
See Full Solution
LinearLayout ll1, ll2, ll3;
/* Find these LinearLayout by ID
i.e ll1=(LinearLayout)findViewById(R.id.ll1);
*/
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
ll1.setLayoutParams(lp);
ll2.setLayoutParams(lp);
ll3.setLayoutParams(lp);
Use new LinearLayout.LayoutParams(int width, int height, float weight) to set weights when setting layout params to the subviews
Do this way..
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNote = (LinedEditText) findViewById(R.id.txtNote);
lnr = (LinearLayout) findViewById(R.id.lnr);
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
LinearLayout l3 = new LinearLayout(this);
l1.setBackgroundResource(android.R.color.holo_green_light);
l2.setBackgroundResource(android.R.color.holo_orange_dark);
l3.setBackgroundResource(android.R.color.holo_blue_bright);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1);
lnr.addView(l1, param);
lnr.addView(l2, param);
lnr.addView(l3, param);
}
You can do it by setting layout weight property for your individual linear layouts, pass it in LinearLayout - LayoutParams constructor:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParam(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1);
or
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
0,
LayoutParams.MATCH_PARENT, 1);
Hope it may help you !
I am dynamically creating a linearlayout and adding some imageviews and textviews to it.
Code
ScrollView sv = new ScrollView(this);
LinearLayout llay = new LinearLayout(this);
for(int i =0;i<ns;i++){
TextView tv = new TextView(this);
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LinearLayout.LayoutParams(100,100,50));
llay.addView(tv);
llay.addView(iv);
}
On running the application
1st device : The imageviews are of appropriate size.
2nd device : the imageviews are too small.
Is there any way, I can get away with setting the size of the imageview explicity so that it becomes device independent?
Try This....
If your app for diff size density use display height/width instead of fix value.
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight()/10;
int width = display.getWidth()/5;
ScrollView sv = new ScrollView(this);
LinearLayout llay = new LinearLayout(this);
for(int i =0;i<ns;i++){
TextView tv = new TextView(this);
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LinearLayout.LayoutParams(width,height,0));
llay.addView(tv);
llay.addView(iv);
}
It's recommend to use dp instead of px.
For more information about dp vs px you can visit Support multiple screens
In your case, you set the value in pixels. If you want to work with dp you can use the following:
float x = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());