How to set a desired layout for an animating fragment? - android

I have a fragment which is animating from right to left. I want to set it to stop at like 100 units from the left part of the screen. This is what i did so far.
RelativeLayout tempLi;
Display display = MyActivity.context.getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
tempLi.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT));
This line of code is getting the layout 100 units short from the right side. I tried doing -width + 100 , it didn't work.
Any suggestions will be appreciated. TIA

You can set the left-margin to the layout as follows:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(100, 0, 0, 0);
tempLi.setLayoutParams(layoutParams);

Related

Have an ImageView with a default height, change height when an image is put into it

So I am making an app that holds multiple user-chosen pictures. I have default ImageViews with pre-set heights of 300dp. I want this height to change to wrap_content once an image has been placed into the ImageView. The only way I know to do this is to remove the image from the layout and then re-add it with a new LayoutParams, but this messes up the order of the other views in my layout. Can I change the height without removing it?
Essentially:
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
final float scale = getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams mTestImgParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(int) Math.ceil(scale * 300)
);
final ImageView createdView = new ImageView(this);
mainLayout.addView(createdView, mTestImgParams);
//onLongClick listener, get picture, set the picture into the imageview, etc.
I somehow want to change the
(int) Math.ceil(scale*300)
to
LinearLayout.LayoutParams.WRAP_CONTENT
without removing and re-adding the ImageView, and only after the image has been placed. Help please.
You could try to get the current layout params and change it.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) createdView.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
createdView.setLayoutParams(params);

How to set dynamic size for Android's view?

I am trying to set the minimum sizes, but still one view is taking over completely. My normal view looks like this:
These two values are coming from the server. But if the size of the "hello" changes to something longer then the number string disappears.
My code looks like this:
if (jsonDataViewType.get(i).toString().equals("editBox")) {
editText = new EditText(context);
editText.setMinLines(1);
editText.setMinimumWidth(10);
linearLayoutHorizontal.addView(editText);
editText.setText(jsonDataValue.get(i));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
editText.setGravity(Gravity.CENTER_HORIZONTAL);
editText.setLayoutParams(params);
}
else if(jsonDataViewType.get(i).toString().equals("button")) {
helloButton = new Button(context);
// helloButton.setMinLines(1);
linearLayoutHorizontal.addView(helloButton);
helloButton.setText(jsonDataValue.get(i));
// testing
// helloButton.setText("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllooooooooooo");
}
I am setting both the minLines and minWidth, but as you can see its still not working.
Try to set layout_width=0 for both EditView andButton`. Then set layout_weight to 0.8 and 0.2
So theese two view will fill parents width. Now your EditView is out of the screen left side.
So your problem is that setting setMinLines(1) is a lower bound setting meaning that at minimum make this one line long. If you want to control how much is being shown you need to setMaxLines(1); and then the same for width setting setMinWidth(10) means at least let this be 10 wide so do setMaxWidth(10)
Then you may want to consider adding ellipsize to the buttons text if you want it to show that there is more text but its cut off.
Update
If you want them to sit next to each other:
// Linear Layout has weight sum of 3
linearLayoutHorizontal.setWeightSum(3.0f);
// Lets EditText take up 2/3 of view
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 2.0f);
editText.setLayoutParams(params);
// Button gets the other 1/3
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
helloButton.setLayoutParams(params2);

How to add an ImageView to the center of the screen, by ratio

My code:
Display display = getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
final int height = display.getHeight();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200,200);
relativeLayout.addView(img1,params);
params.leftMargin = width/2;
params.topMargin = height/2;
setContentView(relativeLayout);
Could somebody help me to fit my img1 to the center of the screen, at every resolution?
I think that the display.getwidth value is not associated with params.leftmargin.
Please instruct me to get the margin value to any use by dividing it.
try this
RelativeLayout.LayoutParams params =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
img1.setLayoutParams(params);
relativeLayout.addView(img1);
EDIT : if u want to add more views then u should create layoutparams for every view and use addRule method I m just giving u an example how to place a view under a view u can place that view right , left or above accoridngly change this RelativeLayout.ALIGN_BOTTOM and u can use RelativeLayout.LAYOUT_BELOW instead of RelativeLayout.ALIGN_BOTTOM
p.addRule(RelativeLayout.ALIGN_BOTTOM, ur_view.getId());
if u create it in xml it will be much more easier
if u want to set margin for any view do something like this
p.setMargins(left margin, top margin, right margin, bottom margin);
left margin, top margin, right margin, bottom margin are Integer
value, which value u want to set for ur view

Android negative margin does not work

I have encountered a problem when i try to give a negative left margin to a LinearLayout.
The negative margin does not appear.
Here is my code
HorizontalScrollView hview = new HorizontalScrollView(context); // HorizontalScrollView is the outer view
RelativeLayout.LayoutParams hs_lot_params = new RelativeLayout.LayoutParams(164, 164);
hs_lot_params.setMargins(100, 100, 0, 0); // set the positions
ImageView image = new ImageView(context);
image.setBackgroundResource(R.drawable.leder);
LinearLayout.LayoutParams img_lot_params = new LinearLayout.LayoutParams(164, 164);
img_lot_params.setMargins(0, 0, 0, 0);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(164, 164);
layoutParams.setMargins(-132, 0, 0, 0);
ll.addView(image, img_lot_params);
hview.addView(ll, layoutParams);
Note: my plan is to scroll the image from left to right.
First, the left part of the image is hidden and can scroll to right to see the full image
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams)view.getLayoutParams(); params.topMargin = -100;
Negative margins should work in LinearLayout and RelativeLayout. What you probably need, is to scroll the HorizontalScrollView with scrollBy(int x, int y) or scrollTo(int x, int y) to achieve the "peek and scroll" effect you described.
Also keep in mind that using raw pixel units is generally a bad idea as the actual size will depend on the pixel density of the screen. Prefer dp measurements instead.

cant set minimum width of dynamically created seekbar

here is the code that i am using to generate my view object:
// creates the seekBar
sBGauge = new SeekBar(this);
sBGauge.setMax(depthL - 1);
sBGauge.setMinimumWidth(150);
sBGauge.setId(2000 + i);
sBGauge.setOnSeekBarChangeListener(this);
reguardless of what i set the sBGauge.setMinimumWidth(); to it always appears to only be about 20 wide.
any thoughts?
thanks
Edit: to give some more info I am using this seekbar in between two textViews in a tablerow.
bump
#Amit Hooda your solution did not solve my problem but it did lead me down the right path to finding a solution.
What I had to do to fix this issue was to change from a dynamically created a TableRow to a LinearLayout within my TableLayout. I was then able to get my screen width and subtract out my textviews and then set my width of the seekbar with the resulting number.
// gets the width of the screen to set the seekbar width
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
width = width - 170;
LinearLayout lL = new LinearLayout(this);
// creates the seekBar
sBGauge = new SeekBar(this);
sBGauge.setMax(depthL - 1);
sBGauge.setId(2000 + i);
sBGauge.setLayoutParams(new LinearLayout.LayoutParams(width, 50, 1f));
sBGauge.setOnSeekBarChangeListener(this);
Try this
LayoutParams lp = new LayoutParams(150, 150);
sBGauge.setLayoutParams(lp);
you can change the value 150 and 150 according to your need.

Categories

Resources