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.
Related
I'm trying to make a variant of the default "Scrolling View" interface on android. Basically the exact same, but with the blue page on top starting at 90% height, and a few options in the bottom white page. To do this, since I read you can't put heights in percentages, I used this code in the main and only activity:
//To fix the height to the right level
Resources res = getResources();
AppBarLayout app_bar = findViewById(R.id.app_bar);
float heightDp = (float)getResources().getDisplayMetrics().heightPixels / 100 * 87; //The percentage
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)app_bar.getLayoutParams();
lp.height = (int)heightDp;
So the app project is just the default Scrolling Activity sample (CoordinatorLayout and NestedScrollView) + the above code.
Problem:
it flickers and sometimes bounces quite a lot when flinging up and down on many devices. Especially on Huawei ones, for some reason.
It doesn't get better adding "snap" in app:layout_scrollFlags.
It also happens when using demo applications like cheesesquare.
I also tried converting the NestedScrollView to a ConstraintLayout, but that made it so you can't scroll up from the bottom page (from the white space) anymore.
Any wisdom to share?
Sorry for the long question. Thanks in advance.
I Displayed my program's output via TableRow in TableLayout using array of Textview.Actually the output is of 2d array.
I displayed every element of 2d array without any problems.
But what the problem is I cant have space between TextViews(Columns).
Below is the image which explains this clearly.I want to do this programmatically
Please help me to solve this
Remember that almost any property that can be set on your XML layout, can also be set programmatically!
The way to do it is:
view.setPadding(0,padding,0,0);
This will set the top padding to padding-pixels. If you want to set it in dp instead, you can do a conversion:
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
I go this from here .
You can set padding programmatically on the textview by using this method:
txt.setPadding(left, top, right, bottom);
set integer value for left,top,right,bottom according to your condition.Hope it will help...
I have a very short problem. I have a custom control that is based upon LinearLayout. I inflate it's view from xml', in which the root is set as element.
Now when I try to set the padding of this custom control by "this.setPadding(x,x,x,x)" it does not affect the TextView and ImageView I add to this control in a few lines of code later.
Currently I am bypysing it, by setting the margin of each control separately, to achieve the global padding of the whole custom control, but it would be nice to know if there is a catch in using setPadding dynamicaly, or maybe a mistake is mine.
To simplify, my case looks like that:
setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams lp = new
LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT); setLayoutParams(lp);
setPadding(0, 30, 0, 30); //I know it's in px
Afterwards I'm adding a large image, which shrinks due to it's size, but the padding of LinearLayout(which I try to set dynamicaly) does not affect it, only if I set margin on it directly.
Any tip would be greatly appriciated.
After a little of digging, found an answer through other StackOverflow question:
https://stackoverflow.com/a/13363318/905938
Basicaly if one person sets a background reasource with a selector xml given (as in my case) it overrides completely the previous padding setting. So the padding I was setting within the custom control initialization was lost as soon as it was set.
Now that I know the problem, I basicaly just intercept the call to this method in my custom control like this:
#Override
public void setBackgroundResource(int resid)
{
int paddingLeft, paddingTop, paddingRight, paddingBottom;
paddingLeft = getPaddingLeft();
paddingTop = getPaddingTop();
paddingRight = getPaddingRight();
paddingBottom = getPaddingBottom();
super.setBackgroundResource(resid);
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
That solves the problem, and I hope will solve a problem for anybody who will find this question with a similar problem.
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 :)
I want to fill the screen with a 100 different letters in random positions. On the iPhone I just created a bunch of UILabels set their x and y positions and then used animations to move them about.
On Android it doesn't look like I can add a TextView to my view and specify its X and Y. Is there a way to do this?
View gameView = findViewById(R.id.gameboard);
tv = new TextView(gameView.getContext());
tv.setText("A");
tv.setWidth(w); tv.setHeight(h);
// How to set the X and Y?
EDIT: The solution was to use AbsoluteLayout:
AbsoluteLayout al = (AbsoluteLayout)findViewById(R.id.gb_layout);
tv = new TextView(this);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,10);
params.x = 50;
params.y = 50;
al.addView(tv, params);
and to move it base on MotionEvent me:
AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,(int)me.getX(), (int)me.getY());
mTV.setLayoutParams (p);
I think you are making a game and for this you should look into SurfaceView here is an good example then
1...if you look into code there is onDraw method where you will get the width of screen and height
2...Now taking this width and height as seed for random generator get x and y position.
3...Iterate through point 2 100 times and you will get the desired result
You can use a FrameLayout for this. Set each TextView's background to transparent and add it to the FrameLayout. Make each TextView, and the FrameLayout, fill their parent. The FrameLayout places all its child views at (0,0). To move letters around, just change the top and left padding of the corresponding TextView.
In android positioning child views is the responsibility of the layout class.
You need to create a custom.layout and overide the onLayout method. In this method you can iterate through all the child views calling View.layout(left,top,right,bottom) on each child.
i found this example code whilst trawling the net :
https://gist.github.com/882650
You should also check out view.setTranslationX (and Y) which might be exactly what you need
I'm not entirely sure how you'd go about doing this in code, but you need to use an absolute layout as your root element. (edit: Do not use absolute layout, as it was pointed out that it is deprecated. The closest alternative seems to be RelativeLayout.)
The properties in the XML format for the absolute layout coordinates are layout_x and layout_y.
edit: A little research is saying you need to be using setLayoutParams, but my Eclipse IDE is not working properly, so unfortunately I can't test exactly what you're looking for.