Set custom frame of tab in Android - android

Is it possible to set height and width of tabs in Android? I tried setting customized textview as the indicator of tabspec, setting its height and width to different values, but no matter what its height and width value is, it's always set to the default height and width value.

Yes, but it is quite a bit of a hassle. The layout for the tab widgets uses a fixed height, so you'd have to change that layout directly. One way of doing that is to extend TabWidget, override the addView method and change the widgets according to your needs.

Related

Set Padding of TextView depending on Content height of TextView

I currently have a textview with a paddingBottom of 20dp.
I want to reduce this padding if the text within the textView is long enough to otherwise get cut off at the bottom.
Here's a diagram illustrating the behavior I want:
However, I'm not sure where to call setPadding().
Can someone recommend me an approach for getting this behavior of a dynamic bottom padding?
I ended up creating a custom Layout that contained my textView, and overrode its onMeasure() method. The custom layout was set with height='wrap_content'.
The system passes in the available height to this layout's onMeasure() method. So here, I measured my child textView's height with
textView.onMeasure()
int preferredHeight = textView.getMeasuredHeight()
If its preferred height was larger than what my custom view had available to it (minus the bottom padding), I removed the bottom padding with setPadding(). Otherwise, I added the padding.

custom view with height based on own width

I am currently developing an Android App for Tablets, landscape mode only.
My question:
How can I display a View with a height based on the width?
A LinearLayout holds my View so that I can use the weight attribute.
My height always has to be 1/3 of my width while the width always has to be a specific percentage of the parent, in this case 80%(sum weight is set to 1, weight is 0.8) How can I achieve this?
Thanks in advance.
You have to override the onMeasure() method of view and customize this functionality. Alternatively you can use the library
https://developer.android.com/reference/android/support/percent/PercentRelativeLayout

Basing a custom View's width off of its height

I have a custom View I'm making which has a fixed proportion between width and height. I want the programmer to set the height and I would like the View to set its own width according to a formula based on height.
The View is going to be a child of a RelativeLayout so I can't use the height of the parent element to calculate anything.
How can I accomplish this? I don't know how to do it in the onMeasure hook because the View's width and height are not available at that time.
By the way, I am making this view solely programmatically, no XML involved.
use View.setlayoutParams()
There's an example using an adView here.

Android - How can I determine an appropriate height for my ListView items?

I am building an Android app with a ListView. Coming from iOS I am used to setting fixed pixel heights for list view items, since the screen sizes of the used devices are always the same. Now for Android, I am wondering what is a good way to dynamically set the heights of ListView items so that it it looks nice on all screen sizes?
In android there are two famous properties. They are:
MATCH_PARENT formerly FILL_PARENT using this property for layout width or height will expand the view to the parents width or height minus margins
WRAP_CONTENT using this property for layout width or height will allow the view to take as much space required or available(if it exceeds screen dimension exception is inside scrollable views)
So for your tag set both width and height to match_parent. And in the custom row that you might be populating set the root layout width to match_parent and height to wrap_content.
Note: in android while we give fixed height at times but it is generally not a good practice.

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.

Categories

Resources