Unable to programmatically change width of horizontal progressbar - android

I would like change the width of a Horizontal Progressbar programmatically (initial size is set in XML using RelativeLayout...but I would like to dynamically change it based on certain values).
I have tried setMinimumWidth(50) in my code, but that did not make a difference. I have also tried setting 'android:layout_width="wrap_content", but that did not work either.
Here is my XML:
<ProgressBar android:id="#+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
Thanks in advance for any assistance!

What layout is your ProgressBar in? This makes a difference as to how the layout_width and height are interpreted.
However, I think you are seeing a conflict between the View's width, and the layout_width. Each time the view is rendered, its preferred size is determined based on the minWidth, width, etc. set on it. The layout that the view sits in can then update the actual size to render based on the layout_width information. See How Android Draws Views and View size documentation for more info on the two phase process.
In your case, you have the layout_width set to 100, and the minimum width set to 50, so I would think it would always show 100.
Try setting the layout_width to wrap_content, and then updated the preferred width for the ProgressBar.

I would imagine the setmindwidth would be met when you give it an initial value of 100. the system would receognize it exceeds 50 and then continue to do as the rest of what it's told. try making the min width 150, set the xml for an initial value of 75 and in your onCreate fire off a Log.d("progbar", String.valueOf(myprogress.width));
disclaimer - written when in front of work computer without development environment for reference.

Related

control dimensions of custom alert dialog in different screen orientations

I want to set dimensions for my custom alert dialog, based on screen orientation. My intention is to swap height and width values to keep the box look like being the same size, yet handled various screen sizes of various devices, thanks to Android fragmentation it seems difficult to achieve the same effect on all devices. Android's auto-resizing seems weird to me.
Portrait:
alertDialog.width=screen.width*0.8
alertDialog.height=screen.height=0.5
Landscape:
alertDialog.width=screen.width*0.5;
alertDialog.height=screen.height*0.8
Please note that the Custom Alert Dialog must use the same code and support Android versions from JellyBean (at least 4.2) to Nougat (7).
i am using android.support.v7.AlertDialog (the latest available thing)
i assume android.app.Dialog should be avoided now (being old)
Also, i need a black border and white background for the same. i am unable to achieve same effect on all devices, (i need transparency for rounded corners)
i have used android:windowMinWidthMajor and android:windowMinWidthMinor but they affect both layouts (portrait and landscape) and seem to be ignored if content does not fit within the specified constraints.
I wish there was android:windowMaxWidthMinor & android:windowMaxWidthMajor
This is something that I've used for resizing specific views on a page:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
item.height = myView.getHeight();
});
Applying it to what you want to do:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
LayoutParams params = (get the layout params according to the layout);
params.width = myView.getWidth() * 0.5;
params.height = myView.getHeight() * 0.8;
myView.setLayoutParams(params);
});
You have to use the listener because the object will be added to the view tree, will be measured and prepared for display, but won't yet be displayed, so you can change its dimensions before it is visible.
Update:
I could achieve it by setting android:layout_centerInParent="true" for my Layout. and layout values for each component in the style to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
to achieve the center alignment.
XML Code available at https://github.com/computingfreak/CFTest/blob/master/app/src/main/res/layout/alert_popup.xml
Java Code available at
https://github.com/computingfreak/CFTest/blob/master/app/src/main/java/net/cf/sms/cftest/MainActivity.java
I wonder this line of code is redundant
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
and thus left it to Android System to decide dimensions and resizing based on content. It works for my case, but will fail in case of long text, maybe some kind of Scroll Layout will help. Cheers!

Android seekbar goes out of its track towards the end

I am having an issue with a kind of custom seekbar What I have done is created a new object that extends SeekBar.
Then in onSizeChanged function, I am calculating the width, and setting it up, and the pass that size on to super.onSizeChanged() function.
Now this gives me perfectly correct width of the SeekBar track, however, when I push and drag the SeekBar thumb, more it moves towards right, thumb moves further a head of the seekbar track filling color area. So much so that it moves to the edge of the available screen where as the color filled width of the track is behind.
I hope some kind soul can guide..There is nothing fancy its simple manual intervention to change the width in onSizeChanged and thats it.
Here is the XML that I am using for this
<com.test.SeekBar
android:id="#+id/seek_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:progress="0"
android:max="100"
android:progressDrawable="#layout/progressbar"
android:secondaryProgress="0"
/>
Thanks
I figured out the solution to my problem. My solution is a mix of xml and setting up layout parameters programmatically in the main activity.
I enclosed the seekbar xml element in another linear layout element(now I had only one control in this linear layout element
Then in code I just calculated the desired width as I wanted and set the layout params
as below
int newWidth = getSeekBarWidthAsNeeded();
int newHeight = getDesiredHeight();// smaller for lareger screens
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(newWidth,newHeight);
mySeekBar.setLayoutParams(llp);
And this finally took care of my struggle with working with onSizeChanged and other methods, which I had soo much trouble with

Android - Button height not decreasing to wrap text

I set the button's height via XML. Suppose it to be X
<Button
android:layout_height="wrap_contnet"
android:textSize = " <X> dp"
....
....
/>
If I set X to greater than 15 dp, the button's height grows to fit the text. But when I set it to something lower like 2dp, the button's height doesn't change and I can see a thick border around it as shown below:-
As shown above, text size is so small but still height doesn't decrease!
How can I force it to be of appropriate height?
Thanx in advance!
Font size does not depend on button height. If you set it too small to fit the text, you will get the result you observe
EDIT
wrap_content would usually do the trick, however Button class sets some layout parameters like margin, background so it takes more space. You may get rid of Button and use i.e. TextView class, styled by hand to match your design. onClickListener will work perfectly fine with it and you will get result you want and full control over your button
EDIT 2
As this is bit related - there's AutoFitTextView widget on Github that deals with automatic text size scalling: https://github.com/grantland/android-autofittextview
Just Set android:minHeight="xxdp"

How to dynamically set height and width of an ImageView in an Android widget to "match_parent"?

There is one ImageView in my homescreen widget. In the layout file, I am setting the height and width to wrap_content. But depending on user's input, I have to change its height and width to match_parent. How can this be done ?
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Now I tried to use the setInt() method on this RemoteViews object like this to just check if it is possible to change height and width:
rv.setInt(R.id.widgetImageView, "setMinimumHeight", 300);
But this is what I get on the logcat:
couldn't find any view, using error view
android.widget.ImageView can't use method with RemoteViews: setMinimumHeight(int)
So how do I change its height and width to match_parent ?
For those wondering how this can be done, I used two ImageView in the layout. One with height and width set to wrap_content and another with height and width set to match_parent. Depending on the user's input, I called the setVisibility to hide the ImageView which was not required through RemoteView.
rv.setInt(R.id.widgetImageView1, "setVisibility", View.GONE);
There are a very few ImageView methods which we can call through RemoteView.
Use:
ImageView view = new ImageView();
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
First parameter of LayoutParams is for width, second for height.
Here, I have created a new ImageView for explanation. You should use your own ImageView variable from your code, to do the above. I hope that's clear.
[Update to code]: Sorry, the MATCH_PARENT variable is under LayoutParams not under View. I've updated the code.
[Update]: Above answer does not work for RemoteViews as RemoteViews cannot have Layout Params. Based on the answer given in this SO Question, there is no way to set or find dimensions for RemoteViews by normal methods.
I found this Android Widget Tutorial, where under the section Widget Size, the author says:
A Widget will take a certain amount of cells on the homescreen. A cell is
usually used to display the icon of one application. As a calculation rule
you should define the size of the widget with the formula:
((Number of columns / rows)* 74) - 2.
These are device independent pixels and the -2 is used to avoid rounding issues.
As of Android 3.1 a Widgets can be flexible in size, e.g. the user can make it
larger or smaller. To enable this for Widgets you can use the
android:resizeMode="horizontal|vertical" attribute in the XML configuration file
for the widget.
From this, I can suggest you that instead of setting a specific integer size value directly, why not set the number of rows and columns to resize? For example, if your initial widget size was 4 Columns and 1 Row, then on user input, you can change it to 4 Columns and 4 Rows, thus making it occupy the whole screen (Max size for widgets is 4 Rows and 4 Columns)
I know the above method is not what you wanted, but this seems like the only way to me. Try and see if it's helpful.

Image at the specific screen position

Can I using xml layout attributes put image to the following position? Or I must calculate position on src?
http://i.stack.imgur.com/gox4d.png
You can do it using the XML attributes.
Just use follwing attribute in your ImageView:
Android:layout_marginTop="20dip"
Android:layout_height="20dip"
Only use DIP as these are density independent Pixels
dont use PX.
Yes, you can, for the simple example you posted I would use a vertical LinearLayout, with a FrameLayouts to 'fill' the blank space. Set the LinearLayout weightSum to 1, then set the first FrameLayout weight attribute to 0.2 (1/5), and imageview to 0.2. Also set both the frame layout and imageview layout_height values to 0px.
While this solution works, I'm sure there is a better and cleaner way out there. Hopefully someone will post it.
You can use AbsoluteLayout, calculate exact coordinates based on screen size and orientation and then position the image with absolute coordinates.
You can just use weights for different containers in a vertical linear layout.

Categories

Resources