There are a number of edit texts on
,
which I create dynamically using RelativeLayout (whis is defined in XML). The first edit text is also defined in XML, and the other ones are created based on this first edit text. The problem is that, as you can see the last edit text is changing its height and it's unlike the other ones, but how can I check it? How can I get the height of this edit text and add it to the next line on the screen?
you can try this to get size dynamically:
getMeasuredHeight() :please google it for more information.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText ed = (EditText) findViewById(R.id.edit1);
ed.setTextSize(20);
ed.setText("here stackoverflow");
ed.measure(0, 0);
int height = edit.getMeasuredHeight();
I usually use it because of this reasons, read carefully,it'll help you in future:
The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().
You can try something like this..
EditText editText = (EditText) findViewById(R.id.your_view_id);
int height = editText.getHeight(); // This will give height
EditText editTextLast = (EditText) findViewById(R.id.your_view_id);
editTextLast.setHeight(height); // this will set the height
Related
In the following code:
Text imageText = textX.getTextWithDefaultBoldLookAndFeel("Image:");
BufferedImage image = (pojo.getImage() != null) ? pojo.getImage() : imageX.getImageFromFileAsBufferedImage(GlobalVar.defaultImage);;
double maxHeight = 300;
double maxWidth = 150;
Pane imageViewPane = new Pane();
imageViewPane.setMaxWidth(maxWidth);
imageViewPane.setMaxHeight(maxHeight);
//Setting the image view
Image image = SwingFXUtils.toFXImage(image, null);
//Setting the image view
ImageView imageView = new ImageView(image);
//Setting the fit height and width of the image view
imageView.setFitWidth(maxWidth);
//imageView.minWidth(maxWidth); // this won't solve the problem
imageView.setFitHeight(maxImageHeight);
//imageView.minHeight(maxHeight); // this won't solve the problem
// this solves the problem but also enlarges image which I do not want
imageView.setPreserveRatio(false);
imageGridPane.add(imageText, 0, 3);
imageGridPane.add(imageView, 1, 3);
gridPane.addRow(1, imageGridPane);
I'm adding Image to ImageView which is then added to a GridPane. The images I'm adding are of different sizes - some are too small to fill the entire ImageView area (300 x 150). When this occurs it causes the parent GridPane box to be smaller than the other boxes. I do not desire this. I realize that when an image is too small I am able to resize the image by setting imageView.setPreserveRatio(false); however, while it creates uniform boxes, it also causes the image to become blurry.
I would like for the image to be displayed however big (up the max limit) or small it is (i.e, not to be enlarged if too small) but I still want a uniform size for all ImageViews so that the parent GridPane (box) are all the same size. Perhaps so sort of transparent border can be added when the image is too small so that the image is not enlarged but ImageView still takes up same space as other large images - forcing uniform box size. I was unable to accomplish this by setting minWidth and minHeight properties of parent GridPane.
How can this be achieved?
Thanks!
This is driving me crazy. I would like to be able to resize an xml vector drawable icon programmatically in order to use it in an ImageView.
This is what I've done so far which is not working
Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_marker,null);
drawable.setBounds(0,0,512,512);
imageVenue.setImageDrawable(drawable);
The vector icon ic_marker is not resized. It just keeps the hardcoded width and height values every time.
Any ideas?
You can change the width and height of your imageview programmatically. Since vector drawables will preserve the original quality of the image, this will make the desired output happen.
ImageView iv = (ImageView) findViewById(R.id.imgview);
int width = 60;
int height = 60;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(params);
I'm currently facing the same problem.
I'm trying something like this, cause ViewParent has actually height set explicitly, so I use match_parent and set margins. It doesn't work all the time though, cause I simply use this view in a viewholder for RecyclerView... Also I've noticed that sometimes I see scaled up version with artifacts, sometimes full size, sometimes there are margins, and bigger margins... But it still might work for you, if you use it in a simpler scenario.
mImageViewFront.setImageDrawable(vectorDrawable);
final int paddingLR = mImageViewFront.getWidth() / 4;
final int paddingTB = mImageViewFront.getHeight() / 4;
LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.setMargins(paddingLR, paddingTB, paddingLR, paddingTB);
mImageViewFront.setLayoutParams(params);
I created a BaseAdapter that implements ListAdapter. This BaseAdapter reads a JSON file and populate my ListView inflating a custom layout.
In this JSON file I have a boolean value (active = true or active = false). If active is true, I inflate the custom layout and everything is fine, but if active is false, I need to create a new View in this custom layout. Here's my code:
if (!active) {
JSONObject notifications = jsonData.getJSONObject("notifications");
String message = notifications.getString("message");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.height = 70;
TextView warning = new TextView(Controller.getContext());
warning.setText(message);
warning.setTextColor(Color.WHITE);
warning.setBackgroundColor(Color.RED);
warning.setGravity(Gravity.CENTER);
warning.setPadding(10, 0, 10, 0);
warning.setLayoutParams(params);
// contentFrame is a RelativeLayout where the view is created;
viewHolder.contentFrame.getLayoutParams().height = 320;
viewHolder.contentFrame.addView(warning);
}
My problem here is this:
params.height = 70;
viewHolder.contentFrame.getLayoutParams().height = 320;
I'm saying that my TextView is 70 pixels higher and my RelativeLayout is now 320 pixeis high.
It looks nice on my phone, but if I test on a phone with other resolution it will not look the same right?
Is there any way to create a View and define is Height or Width according to the screen resolution? Maybe create the View 10% the size of the screen resolution? Is that a good pratice? What's the best aproach for something like this?
Instead of using hardcoded pixel values, use DPs. DPs account for the device density so views look pretty much the same regardless of which device the app runs on.
See this for additional info.
So, define this in your dimens.xml:
<resources>
<dimen name="my_width">320dp</dimen>
</resources>
Then read it like this:
int width = getResources().getDimensionPixelSize(R.dimen.my_width);
You have to scale your size values according to the screen density:
float density = getResources().getDisplayMetrics().density;
That means you have to multiply all size values with density:
params.height = (int)(70 * density);
viewHolder.contentFrame.getLayoutParams().height = (int)(320 * density);
and the same with padding values:
warning.setPadding((int)(10 * density), 0, (int)(10 * density), 0);
You can define dimensions in XML like any other resource, and have those resources automatically selected by Android based on the current device configuration.
So you have two definitions, one for "normal" circumstances, and another for "bigger screen":
src/main/res/values/dimens.xml
src/main/res/values-sw600dp/dimens.xml
Each resource file can have the same dimension defined in dp. Android will pick the appropriate one. If you need to read the values programmatically as raw pixel sizes, you can do that with Resources.getDimensionPixelSize().
I would recommend against trying to define list items in terms of percentage of the size of the screen.
The text view i am using in the application is supposed to be single line and has a predefined width which may contain text fetched from database ranging from single word to maximum 3 words. I found a link in stack overflow for adjusting the text size according to height of the text view but i am looking for something more particular to width of text view.
This will work, but you might find faster ways posted. The incrementation in the loop does cost some time, so use sparingly in a single layout.
Display display = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; //Get screen width. There are other ways to do this too.
int tSize = 10;
tv.setTextSize(tSize); //tv is your TextView
while (tv.getPaint().measureText((String) tv.getText())
< width){
tSize++;
tv.setTextSize(tSize); //Increment up till screen width is filled.
}
I have a custom view that extends from FrameLayout. I'd like one of the children to have a size equal to the 20% of the total size as shown in the image.
How can I achieve the imageview to always have this size even if the custom view changes its size in runtime?
Lets say you have your view:
FrameLayout view = (FrameLayout)findViewById(R.id.customView);
Then you can get the layoutParams of the view calculate 20% of it, and then create a new view which will be 20% of the view.
For example:
ImageView img = new ImageView(this);
int imgSize = view.getLayoutParams().width * 0.2; //20 % of the width
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(imgSize, imgSize);
//params.gravity = Gravity.TOP;//How to set gravity programmatically
img.setLayoutParams(params);
//Set img attirubutes like src etc.
view.addView(img)
Also because you are using a FrameLayout it might be better to create an instance of a LayoutParam first and the set it in img.setLayoutParams(params), because iam sure you want to change the layout_gravity of the different views, which you can do in the params vairable