How to scroll a few pixels down programatically Android Studio - android

I made a Ctrl+F search box with next ocurrence button, when i click next it scroll to next word, but the word is out of screen , if i scroll a few pixels down , you are able to see it.
so after the method find next is called i would like to scroll a little more so the highlighted word is visible.
mFindNextButton = (ImageButton) findViewById(R.id.find_next);
mFindNextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mCurrentWebView.findNext(true);
//scroll a little more()
}
});

Maybe something like this:
scrollView.scrollTo(0, scrollView.getScrollY()+10); // Scroll down 10 more pixels

Related

Android scroll up

I have two buttons Button 1, Button 2 placed horizontally like tabs. On Click of these button I am adding Linear Layout in Scroll View.
Now suppose I am clicking Button 1 and scroll down and immediately switch or click to Button 2 that scroll applies on newly added layout. I want every time on click of button content should scroll to top.
I have tried scrollview.scrollT0(0,0) and scrollview.scrollTo(0,scrollview.getTop()) but none of them is working.
I've ran into these types of issues before.
I suggest trying the following:
scrollview.post(new Runnable() {
#Override
public void run() {
scrollview.scrollTo(0, 0);
}
});
The reason this works is forces the action in the Main Thread which is where all UI Activity happens (safely, of course).
Try this . Just it is sample
private void scrollMyListViewToBottom() {
your_list.post(new Runnable() {
public void run() {
if (!running) {
// Select the last row so it will scroll into view...
your_list.setSelection(your_list.getCount() - 1);
running = true;
}
}
});
Note :
Before call this method just change Boolean running = false;

EditText's selectAll() does not select the text, instead cursor is moved to position 0

My situation is: I have a EditText, and under it I have a button, called "select all". It's purpose is to let the user select all the text by pressing it.
When the button is clicked, I invoke selectAll() on the EditText, but instead of selecting all text, in some cases (generally, when the cursor is already positioned within the text somewhere), the cursor is moved to position 0 (start of text) and text remains unselected. Second click on the button will then select all. But of course it should happen on first click.
From this issue: Android EditText selectAll() doesn't works if one taps on the same field in android 4.x only it seems that this is a bug in android 4.0 and above. (Found no mention in Google issue tracker).
Does anyone know about a way to overcome this problem? It seems that the probelm is also in other selection methods and not only selectAll().
(p.s. This question is sort of duplicate of the issue I mentioned above. But I opened another question, because the author of that issue was satisfied and selected a partial answer (of setting android:selectAllOnFocus="true"), while in my case and scenario it does not help me and does not solve the problem.
Thanks.
Problem caused by IME. If showed cursor drag pointer then selection must be zero width.
You need cancel drag pointer. It can be doned by change text. For example replace:
Editable text = edit.getText();
if (text.length() > 0) {
text.replace(0, 1, text.subSequence(0, 1), 0, 1);
edit.selectAll();
}
We replace first symbol of text with same symbol. It cause cancel drag pointer and allow make selection without bug.
I was having a similar issue. I solved it (using Xamarin in C#, but the equivalent Java will work) with the following code:
private void InitFocus()
{
Java.Lang.Runnable rable = new Java.Lang.Runnable(()=>
{
EditText maleCount = FindViewById<EditText>(Resource.Id.txtMaleCount);
maleCount.RequestFocus();
maleCount.SelectAll();
});
new Handler().Post(rable);
}
I have 2 controls: One is a dropdown that lets you select a chicken house and then a set of buttons for each day of the week. Whenever you change the day of week or the chicken house, I want to set focus in the txtMaleCount EditText and then I want the value in that box selected (since it's a number and they're presumably going to replace it).
Clearly, the non-intuitive part was the need to Post it. Doing it directly (on the UI thread) didn't seem to have any effect.
Try this:
yourEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
});
Or This:
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
#Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.setSelection(editText.getText().length()-1); // selects all the text
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.performLongClick();
}
});
Hope this helps .. :)

changing visibility of text android

Hi i have two textViews that i initially set its visibility to gone then animate in and become visible. now i want to make the invisible again but for some reason they're still showing on screen does anyone no why?
in my onCreate() i make the view gone
register = (TextView)findViewById(R.id.register);
register.setVisibility(View.GONE);
forgotpassword = (TextView)findViewById(R.id.forgotpw);
forgotpassword.setVisibility(View.GONE);
then later on i make it visible
public void run()
{
animations();
loginForm.setVisibility(View.VISIBLE);
register.setVisibility(View.VISIBLE);
forgotpassword.setVisibility(View.VISIBLE);
}
and then when a user presses a button i want the text views to become invisible so that they retain their layout but they stay visible on screen
signInBtn = (Button) findViewById(R.id.signin);
signInBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
signInProcess();
}
});
public void signInProcess() {
register.setVisibility(View.INVISIBLE);
forgotpassword.setVisibility(View.INVISIBLE);
setuploader.setVisibility(View.VISIBLE);
}
In Android when you animate something, It's just drawn somewhere else. The actual element is not moved. So when you animate signInBtn it's drawn somewhere else, but the actual button is not moved from the original position. So when you click the button the click handler is not called.
To avoid this set fillAfter = True in your animation so the button will actually get moved at the end of your animation.
Also, after animating a view in Android make sure you call View.clearAnimation() before trying to change its visibility.

How do I select all text when clicking on a EditText view?

so I tried android:selectAllOnFocus and of course I'm using android:hint.
The app loads, requestFocus triggers and the full text is selected.
The problem is that when I click in the EditText the selection is lost.
I've already read:
Select all text inside EditText when it gets focus
For some reason, it worked (on Jelly Bean) only if I posted it with a Handler:
new Handler().post(new Runnable() {
#Override
public void run() {
paidView.selectAll();
}
});
Set the selection in an View.OnClickListener like so:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(0, editText.getText().length() - 1);
}
}
Set the selection in an View.OnClickListener like so: (with the 0 and text length opposite to the previously approved response) - this will ensure that when the user starts typing the content will be overridden.
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(editText.getText().length() - 1, 0);
}
}
I realize this is an old post, but I had this same problem. For me, the reasoning was that I like to move the keyboard out of the way (dismiss it) to get my bearings (or hit buttons to add or remove rows of data) and when i touched back on the EditText I was previously editing, it was annoying to have the keyboard pop back up and the text un-select, forcing me to either work to get the cursor to where I wanted to start deleting, or touch another EditText and then touch back on the original to re-select everything. I just want to have the keyboard pop back up and have the text selected and ready to overwrite whenever I go to that EditText.
There are easy solutions to this:
1) Long tap does this for you on most occasions.
2) If you're already using setSelectAllOnFocus(true), you can just throw a simple clearFocus() and requestFocus() in your onClick listener:
etMyEditText.setSelectAllOnFocus(true);
etMyEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.clearFocus();
view.requestFocus();
}
});
This way the EditText has everything selected when you tap on it, regardless of soft keyboard status.
Additional bonus:
Add android:windowSoftInputMode="adjustPan" inside your <activity .../> tag in your AndroidManifest.xml file to force the selected EditText to stay in sight when the soft keyboard pops up.
try This
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.selectAll();
//or this.selectAll();
}
To select all the text.
Do the code below after you did findViewById and you are good to go:
edtProductPrice.setSelectAllOnFocus(true);
edtProductPrice.requestFocus();
edtProductPrice.selectAll();

Continuously Auto-Scrolling ListView

I have a Listview in Android. I want that the Listview continuously scrolls from top to bottom by itself. It should happen infinitely
And obviously I want to capture the click on any of the items of the Listview, post that the scroll will continue
Anybody having experience with such an implementation. Please help !!
http://groups.google.com/group/android-developers/msg/753a317a8a0adf03
To scroll automatically, you can use this: listView.smoothScrollToPosition(position);
private void scrollMyListViewToBottom() {
myListView.post(new Runnable() {
#Override
public void run() {
// Select the last row so it will scroll into view...
listView.smoothScrollToPosition(myListAdapter.getCount() - 1);
// Just add something to scroll to the top ;-)
}
});
}

Categories

Resources