I have a multi-line EditText/TextView, say 1000 line.
The contents can't be shown on one page, actually there will automatically be a vertical scroll bar.
I want to make a button that can jump to line 500, and the result view start from exactly at line 500.
Anybody know how to achieve this?
You should be able to calculate the scroll in the Y direction using TextView.getLineHeight(), TextView.getLineCount() and TextView.getHeight().From there you can call TextView.scrollTo(0, calculatedY).I'm assuming you always want to have the left side of the view visible. Of course, if your lines were longer than the current View could display, you could similarly calculate an X scroll position also.
Related
Using Android views, I've stumbled upon a recurring issue and thought it was about time to ask if anyone has a solution.
I have a parent ConstraintLayout, the blue one in the picture.
This layout contains two views: a green view constrained to the left of the parent, and the red view constrained to the center of the parent.
Both red view and green view are text views, and their actual size may vary depending on the language.
My wish is for the red view to always stay centered, so to grow symmetrically left and right until it reaches the green view. At its maximum width, the red view will touch the green view on the left, and there will be empty space on the right of the same width as the width of the green view.
Problem is that I can't find a way using regular XML layouts to do it. I can think of several hacks to do it, but thinking there should be a clean way.
Any idea?
Not sure but just an idea, maybe you can try to add two more barriers. One at the end of the green box at left. And one for it's symmetric. Because you said
and there will be empty space on the right of the same width as the
width of the green view
So with these two barriers you can mark the borders of the red one.
And you can set the constraints of the red to the barriers, and with 0dp width may work. Let us know :)
If you are certain that the width of the red view will not ever need to go to two lines because it runs out of space (maybe it is truncated, marque'd or ellipsised) then you would simply constrain the start and end of the red view to a guideline set in the center of the ConstraintLayout.
However, if you can't guarantee that the red view will never need two lines then you are stuck with a hack. The simplest hack would be to create an invisible view on the right that has the same width as the green view. (It could simply be another TextView with the same text and characteristics.) You would then constrain the start of the red view to the end of the green view and the end to the start of the invisible view.
I would like to have my 2-line text view in android-xml centered horizontally with the smallest width necessary. At the moment it's like the first line is completely filled and the second with the rest of the words.
Does anyone know how to achieve that both lines are filled kind of equally? Below are 2 images, the first showing the default multi-line text view, the second what I would like to achieve. As you can see, the second image requires much less width than the first and is more in the center of the view.
Thanks in advance!
Try setting android:breakStrategy attribute of textView to balanced
android:breakStrategy="balanced"
Add this line to your textView's xml code, this will balance the lines.
I have a number of custom views that can expand on a click event and they push down views below them.
This may result in some views completely or partially out of screen. I want to detect these occurrences and move them to column two of the screen.
Both columns take half of the screen size.
I do not want to use Listview or anything scroll-able, just move views from column 1 to column 2 and back if space is free.
EDIT: I am trying to override each views onDraw, so it can check if bottom coordinates is below screen y coordinates and then reparent them to a pre-defined Layout. But so far no success.
EDIT2: I managed to detect if a view is out of screen space.
For future reference: i have overriden the dispatchDraw of my custom view class, where i calculated its bottom position and the screen height with: context.getresources().getdisplaymetrics().heightpixels;
I have an ImageButton that I need aligned to the bottom of the screen, and when I click and drag it, it moves with my finger by a certain distance, beyond which it will not move further from the origin but continue following the direction my finger is from the origin.
When I let go, the button has to slide back to the origin.
What I have managed to do so far is to have the button follow my finger via this answer: Moving buttons via Touch
However, how should I get the origin of the button at initialization for it to bounce back after I release?
Originally I implemented the alignment of the button by having a android:layout_gravity="center|bottom" to place it at the bottom, but it somehow messes with the position of the button relative to my finger (It follows my finger movements but it's off center).
Hence, I used mButton.setY() and mButton.setX() to place it at the bottom of the screen, but it seemed hackish to use the screen dimensions to place it. Can't think of a better way.
You can set the width and height of your layout in XML using
layout_width and layout_height.
Then in your MainActivity, instantiate the layout (I will use LinearLayout as an example) and get its width and height.
LinearLayout l = (LinearLayout)findviewbyid(R.id.l1);
int height = l.getHeight();
int width = l.getWidth();
Now, whenever you release the button, you can set your its position in the following way:
mButton.setY(height - 50)
This will position the ImageButton 50 pixels above the bottom of your layout. You can also set the x position of the button as you want in the same way. Also, you should probably store height - 50 as a global variable (origin) that you can call from anywhere in your code.
I have the following requirement.
Initially I want to show the textview half on the screen. This textview when pulled out/clicked should show a complete one.
I just want few hints as to how to get over such animation.
You must use a translation on the X axis animation.
This should give you enough to start working on it:
textview.setX(5000); //TODO: change to (current X + textview.witdh)
textview.animate().translationX(0).setDuration(1000).start();