I only want to use text in the tabs of my TabLayout. Unfortunately, by default, the Tabs are very large and the text is very small. I see no way of adjusting the size of the text or height of the tabs, without using TabSpec.setIndicator(View); which would be very undesirable because then I would have define a specific layout, selector, selected image, unselected image, etc... so my tabs would not fit the look and feel of each device. Is there a convenient way to customize the Tab appearance?
mTabHost.addTab(mTabHost.newTabSpec("system").setIndicator("My first tab").setContent(R.id.fileBrowserTabHostSystemList));
mTabHost.addTab(mTabHost.newTabSpec("recent").setIndicator("My Second tab").setContent(R.id.fileBrowserTabHostRecentList));
I have managed to change the tab height in a nice way:
final int tabChildrenCount = tabWidget.getChildCount();
View currentTab;
for (int i = 0; i < tabChildrenCount; i++) {
currentTab= tabWidget.getChildAt(i);
currentTab.getLayoutParams().height = 32;
}
tabWidget.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
tabWidget.requestLayout();
I also tried to get the height of the title in the tab and to set tab's height to it:
currentTab.findViewById(android.R.id.title).getHeight();
// also tried with getMeasuredHeight()
but it returns 0 since it was in the onCreate() method and the view seems to not know its size at that moment. LayoutParams.WRAP_CONTENT is no good for the tab height, because it acted as FILL_PARENT.
Note, however, that this solution only assumes that the title of the tab will fit in 32 pixels.
Still the safest guess is to manually do the layout (as already posted http://joshclemm.com/blog/?p=136). Get the resources from Android Repository and customtize :)
Related
Hi currently I'm working on a puzzle that slice and randomize one image. Upon setting up I have manually set the padding into 2 for all corners then after the puzzle is finished padding should be set to 0 on all corners as well to display the image in full. Problem is there is an odd thing that happen wherein only the right side of the puzzle removes the padding completely while the other side still has it's padding on the bottom part. This only happens on devices with lower specs. (Nexus One and alike)
Here's my code for the setting of padding after completing the puzzle:
if(true){
enable_puzzle = false;
for (int i=0; i < parent.getChildCount(); i++){
LinearLayout cont = (LinearLayout) parent.getChildAt(i);
for (int c = 0; c < cont.getChildCount(); c++){
View v = cont.getChildAt(c);
v.setClickable(false);
v.setPadding(0,0,0,0);
v.postInvalidate();
}
cont.postInvalidate(); //Tried this didn't work
}
parent.postInvalidate(); //Tried this didn't work
}
for better understanding of how I placed my layout it's pretty simple. I have a linearLayout that in horizontal orientation which holds the imageViews which will be places into the parent container in vertical orientation.
Here's the screenshot for sample.
The puzzle on create (see odd padding on the bottom part on left side)
And upon completing:
This is the screenshot on my Nexus S emulator. Same thing happen on real device with lower specs.
UPDATE:
Upon checking it appears that the main cause was the width of the container for the images. Since I have a 4x4 puzzle and uses weight instead of fixed sizes the width for each images is not evenly distributed causing a 1px/dp difference. Now the only problem is on how can I get the width of the parent horizontal layout to be divided and round up to get an even result for each imageviews?
There is a workaround for this, you can create a listener something like onPuzzelCompleteListener(), something that you are doing even now to remove the padding. Here you can show an ImageView with the complete image. This should solve your padding issue as for now.
I have a ListView with an adapter attached. The data behind it is a couple of articles. The articles have a title and a subtitle, the length of both varies.
Sometimes the text of either one is so long that the TextView doesn't fit the View created by the adapter which has a fixed height.
Is there a possibility to find out if both TextViews are completely visible within the View?
I know I would have to wait for the layout to be drawn, would do it with getViewTreeObserver().addOnGlobalLayoutListener(...)
It seems like you can only completely fit two lines per title and subtitle. If I were you, I'd measure the length of the text with the paint that's used to draw it and see if it's less than (width of line) * (number of lines).
For example, consider the following:
boolean doesTitleFitBounds = titleTextView.getPaint().measureText(titleText) < (TITLE_LINE_WIDTH * TITLE_NUM_ROWS);
where TITLE_LINE_WIDTH is the available width for your text in pixels (accounting for paddings/margins etc) and TITLE_NUM_ROWS is the number of rows you have per title.
Similarly, you can do a check for the subtitle to see if it fits its own bounds.
How to increase/reduce space between elements of the indicator in ViewPagerIndicator?
I have used CirclePageIndicator. I was able to make more space between two indicator by the step:
Open the source code of CirclePageIndicator and find the variable mRadius
At the line number around 235, you will find a line like below:
final float threeRadius = mRadius * (some value here)
Change this some value, and play with it. I used 5 for my case, it gave me a good result.
Hope it solved your problem.
There's no documented way to do this without some digging in.
I think #Md_Omar's answer is wrong as this increases the size of the indicator but not their separating distance.
And you could just use app:radius="5dp" instead if thats what you wanted.
See sample here
https://github.com/JakeWharton/ViewPagerIndicator/blob/master/sample/res/layout/themed_circles.xml
You can set space by LayoutParams:
int numOfIndicators = 5;
int space = 20;
tabLayout.setupWithViewPager(myViewPager, true);
ConstraintLayout.LayoutParams params =
(ConstraintLayout.LayoutParams)tabLayout.getLayoutParams();//ConstraintLayout is my layout manager
params.width = numOfIndicators * space;
tabLayout.setLayoutParams(params);
(I used tabLayout with ViewPager as in this example:
Android ViewPager with bottom dots)
If you're using a vector drawable, you can fiddle with the viewportWidth and viewportHeight to create spacing between TabLayout indicators if using the dots style.
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.
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.