Imageview on a random position [duplicate] - java

This question already has answers here:
How can I dynamically set the position of view in Android?
(13 answers)
Closed 6 years ago.
I want myImageView to locate itself somewhere on a random position inside the screen, without pushing textviews away. I found this code in another thread, but I can't get it to work.
myImageView = (ImageView) findViewById(R.id.myImageView);
LinearLayout game = (LinearLayout)findViewById(R.id.game);
int width = game.getWidth();
int height = game.getHeight();
random = new Random();
int x = width;
int y = height;
int imageX = random.nextInt(x-50)+50;
int imageY = random.nextInt(y-100)+100;
myImageView.setX(imageX);
myImageView.setY(imageY);

You can give margin programmatically between certain range, may be you can use device width or height for range. Check this links for setting margin dynamically on run time and get screen dimension.

I Think first get programmatically device size then set image view width & height
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
hear you get width & height now set image view size
android.view.ViewGroup.LayoutParams layoutParams =myImageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
myImageView.setLayoutParams(layoutParams);
and if you still have a problem to change it's size try to put it inside of a LinearLayout and manipulate the imageView size using the layout params:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
myImageView.setLayoutParams(layoutParams);

Related

set bitmap in the center of screen in span

I am trying to put the bitmap in the center of my layout (Center-horizontal) but my bitmap always starts with the left and not centered. I assign the x value to setbound which center my image but shrink it rather the maintaining the ratio I selected.
private void setImageinText(Bitmap myBitmap){
myBitmap = Util.scaleBitmapToFitWidth(myBitmap, 1360, true);
Drawable d = new BitmapDrawable(getResources(), myBitmap);
Point outSize = new Point();
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
display.getSize(outSize);
int[] widthAndHeight = new int[2];
widthAndHeight[0] = outSize.x;
int x = (widthAndHeight[0]- d.getIntrinsicWidth())/2;
SpannableString ssd = new SpannableString("\n \n");
d.setBounds(x, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ssd.setSpan(span, 1 , 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
texto.setTransformationMethod(null);
texto.getText().insert(texto.getSelectionStart(), ssd);
}
take ImageView below TextView and set it Centerhorizontal with Visibility GONE, then when you capture image set it in that ImageView and VISIBLE it.

How can I create a certain amount of edit texts staying next to each other programmatically in Android?

I'm trying to create a number of edit texts programmatically without using XML, but I faced some problems while trying this out. Here is a piece of code:
private void createEditText(int l, int topMargin){
ViewGroup.MarginLayoutParams vlp = (ViewGroup.MarginLayoutParams) editText.getLayoutParams();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(vlp.width, vlp.height);
layoutParams.setMargins((vlp.leftMargin + l) - vlp.width - vlp.height - 25, topMargin, vlp.rightMargin, vlp.bottomMargin);
final EditText newEditText = new EditText(this);
newEditText.setBackgroundResource(R.drawable.rounded_edittext);
newEditText.setLayoutParams(layoutParams);
linearLayout.addView(newEditText);
}
private void numberOfEditTexts(){
String[] words = Answers.answers[getCurrentLevel()].split(" ");
int l = 0, topMargin = 450;
for(int i = 0; i < words.length - 1; i++){
createEditText(l, topMargin);
l += editText.getLayoutParams().width + 5;
if(i == 0) topMargin -= 560;
}
}
The first function creates an edit text and I send 2 parameteres to it, of which the topMargin is the variable that defines the altitude coordinates of the edit text. So the problem is that this edit text will appear differently on different devices because of their screen sizes.
How can I fix it? How can I avoid setting the position manually?
Thanks in advance.
I think you need to get the screen width and length. Use the percentage of screen size as your margin size.
to get the screen size by pixels,
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Your margin can be some percentage of the height: like 5% or 10% of the screen height. Then it is adjustable.

On Android: How to calculate row height prior to TextView creation?

Can anyone explain correlation between Paint.setTextSize() and TextView.setTextSize() ?
I have code like this:
Rect bounds = new Rect();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(48);
paint.getTextBounds("W", 0, 1, bounds);
int rowHeight = bounds.height();
LayoutParams params1 = new RelativeLayout.LayoutParams(300, rowHeight);
TextView tv = new TextView(getContext());
tv.setLayoutParams(params1);
**tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 48);**
but final text size in TextView is way bigger than rowHeight calculated.
I guess, my question is:
Is there any way to calculate rowHeight prior to TextView creation so that text fits by HEIGHT not width?
Thanks

Android, set WebView dimensions with px in Java

Currently I am using the following to set height and width:
//landsapeWebView is a WebView, and deviceHeight and deviceHeight are ints
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceHeight, deviceWidth));
This seems to work fine on some devices but not others. The only conclusion I've come to is that this is setting the height and width like this:
<WebView android:layout_width="(deviceWidth)dp"
android:layout_height="(deviceHeight)dp" />
when what I want is this:
<WebView android:layout_width="(deviceWidth)px"
android:layout_height="(deviceHeight)px" />
How can I specify the unit of measure?
Extra disclosure for #collusionbdbh
display = getWindowManager().getDefaultDisplay();
if(getResources().getConfiguration().orientation == 1){
deviceWidth = display.getWidth();
deviceHeight = display.getHeight();
}else{
deviceWidth = display.getHeight();
deviceHeight = display.getWidth();
}
Try this:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Are you sure it is not an orientation issue?
Regardless of the orientation of the device the width is the horizontal and the height is the vertical.
I would try this:
display = getWindowManager().getDefaultDisplay();
deviceWidth = display.getWidth();
deviceHeight = display.getHeight();
I am pretty sure width should be before height:
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceHeight, deviceWidth));
should be:
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceWidth, deviceHeight));

Android: Changing ImageView size is not working

I am trying to change my ImageView size, but I can't seem to change both - width and height. If I just change one of them, then all works correctly, but when I try to change both, only width is changed.
I have the size in dp: width 22 and height 38, and I convert them to px. Is there another way to do so? I have tried many other ways, but they don't seem to work either.
Could someone tell me, what I should do differently?
Here is my code:
public GridLayout gridLayout;
public GridLayout.Spec row = GridLayout.spec(0);
public GridLayout.Spec col = GridLayout.spec(3);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = (GridLayout) findViewById(R.id.gameGrid);
createBlock();
}
public void createBlock() {
ImageView block = new ImageView(this);
block.setImageResource(R.drawable.red);
if (gridLayout != null) {
gridLayout.addView(block, new GridLayout.LayoutParams(row, col));
int width_in_dp = 22, height_in_dp = 38;
final float scale = getResources().getDisplayMetrics().density;
int width_in_px = (int) (width_in_dp * scale + 0.5f);
int height_in_px = (int) (height_in_dp * scale + 0.5f);
ViewGroup.LayoutParams layoutParams = block.getLayoutParams();
layoutParams.width = width_in_px;
layoutParams.height = height_in_px;
block.setLayoutParams(layoutParams);
}
}
Have you tried playing with the block's scaleType? The default is fitCenter but I usually choose centerCrop or fitXY. Here's a good webpage that describes the differences.
You can set the scaleType through ImageView's setScaleType method.

Categories

Resources