android set width and height to 100% of a dynamically created button - android

that might be a silly question but i didn't find any answer on the internet.
I want to create a button that fills the whole screen (width = 100%, height = 100%) when pressing another button.
Therefore i tried this:
Button button = new Button();
button.setText("fills screen");
button.setWidth(100);
button.setHeight(100);
the problem is that the setWidth and setHeight methods are dealing with px and not with percent.
What do i need to add to use percent values instead of px values?

I'll link here a post that details how to set a width programmatically
programmatically set button width to 50 of parent linearlayout
You can utilize that method (except do not divide by 2 JohnEye demonstrates in his answer)
Or you can utilize the LayoutParams class and create a new instance that will have the parameters set and pass those parameters to your object.
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
button.setLayoutParams(lp); // Assuming you've already initiated it of course as above
MATCH_PARENT will fill up the view. If the root view is the highest view on the hierarchy then this will work, as it will fill up the entire view. If not, the button would need to be moved there. (It may be your linearLayout or some other view)
Hope this helps.

Related

how to set a button layout_marginTop/layout_marginBottom in runtime?

I am using a relative layout with 4 buttons (in the center, one below the other).
My problem is to adjust the gap between all buttons so it will be the same between all buttons.
I want to do it dynamically according to the height of the screen (i use Display class to get the actual height).
what is the best way to do it?
Thanks,
Amigal
You can do this via modifying the LayoutParams of your View
Button b;// your button
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)b.getLayoutParams();
lp.leftMargin=10;//your left margin here
lp.topMargin=10;//your top margin here and do this for other 2 margins if you need to
sometimes you need to call
b.setLayoutParams(lp);
to have the changes applied
also i dont know how you get the screen dimensions, but this works at every API:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

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.

Layouts and Button

TableLayout relativeLayout = (TableLayout)findViewById(R.id.ll1);
TableRow row =new TableRow(this);
Button button ;
int counter = 0;
for(int i = 0;i<=13;i++){
counter++;
button = new Button(this);
button.setText(counter);
row.addView(button);
}
relativeLayout.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
relativeLayout.computeScroll();
XML File
<TableLayout android:id="#+id/ll1" android:shrinkColumns="true"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="#id/button1"
xmlns:android="http://schemas.android.com/apk/res/android"></TableLayout>
But whenever i execute my application it just move my buttons in a single row and after 5 buttons all the buttons move out from the screen. i dont' want to add more rows as i don't want to hard-code anything in my code like placing some code if (counter%5==0) then do this and that.
Is there any other way to calculate the width that if the buttons are equivalent to the width of screen then do this or that or any other way like any property in table Layout which simply wrap content of a row?
Yes there is a way to get the exact width of any component in your app. Look at my answer to my question asked a while ago here. As you can see, I instantiate a Paint object, but that's not necessary unless you wish to get the width of the text itself. However you must use the density multiplier:
float scale = getContext().getResources().getDisplayMetrics().density;
That will convert any function that gets the width of a component (in this case, your button) to the actual pixel width on any given devices. So you would get the button width (via invoking the getWidth() function on your Button object) and then multiply by that scaling factor.
Use FlowLayout instead:
http://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/
GIT project:
https://gist.github.com/1073863
Using Contex's method getResources() you can access all the resources for your application's package. The method returns Resources and you can see here all properties that are available.

How do I achieve square layout and buttons in main.xml?

I would like to have a layout with 5 times 5 buttons. Each of them should have the same width and height (they should be square). And I want the whole matrix to use the screen width (or height, depending on rotation).
I currently do it "by hand" in the Java code:
for (int y=0; y<5; y++) {
TableRow tr = new TableRow(this);
for (int x=0; x<5; x++) {
Button b = new Button (this);
...
tr.addView(b, 60, 60);
}
layout.addView(tr);
}
This can be improved by obtaining screen width first and then dividing by 5 to get rid of this literal 60. But I'm wondering how I can do this in the res/layout XML file?
How can I specify for the height to be the same as the width? (I can set the width to match_parent.)
You are best off just doing this as a custom layout manager. Subclass from ViewGroup and implement the desired layout code. It is quite simple to implement a custom layout manager if you are trying to do a general-purpose layout algorithm (which you aren't).
See for example the platform's AbsoluteLayout as an example of a simple layout manager: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/AbsoluteLayout.java
I've two ideas.
Both are pretty similar to suggestion from hackbod
Instead of implementing subclass from ViewGroup, you can create something like SquareButton extending Button or SquareTableLayout extending TableLayout.
Override constructor class, so that you will replace the width or height value with the smallest value of them both. I'm not sure, but i guess, you'll be able to use new Layouts in XML-Description.
Probably it's easier to create just a SquareTableLayout
Then just set width and height of all elements within TableLayout to 0dip and the weight of all of them to 1.
Assuming that you have NxN elements in your Table, they all will get then the same width and the same height because of the same weigth.
Use Tables... that way all the buttons are same width. you can change height accordingly
You'll want to check out a TableLayout.

Changing the height of an existing, visible TextView

Hi I'd like to programatically increase the height allocated to a TextView, and have the activity layout redrawn accordingly (the text view has a maximum height until the user clicks it, then it takes up all height required, wrap_content).
setHeight() isn't working, even coupled with invalidate() or postInvalidate(). I am able to change the contents of the TextBox with setText() but it isn't altering the existing specified height.
Android 1.5 under the 1.6 SDK.
Didn't test that , but try to create new Layout params and assign it to a view
This is for a button, but idea is same.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 0;
shareBtn.setPadding(50, 0, 50, 0);
shareBtn.setLayoutParams(params);

Categories

Resources