Scroll to bottom when dynamically inflating view - android

I have a LinearLayout and I'm inflating a CardView in it like this:
final LinearLayout itineraryDetailLL = (LinearLayout) findViewById(R.id.itineraryDetailLinearlayout);
final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);
The inflation of the child view is done on a onclick of a button. I want to scroll to the bottom of the screen whenever a new cardview is inflated. I'm doing it like this:
ScrollView scrollview = ((ScrollView) findViewById(R.id.masterScrollView));
scrollview.fullScroll(View.FOCUS_DOWN);
but this scrolls to somewhere middle of the screen and not to bottom. What am I doing wrong?

You have to post an event to happen on the next frame, when this ScrollView would be laid out:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});

Related

Get nested parent

I have Views nested like this
FrameLayout
CardView
LinearLayout
LinearLayout
LinearLayout
ImageButton
In the listener for the ImageButton, I set the top FrameLayout invisible.
What's a better, more dynamic way than this?
public void onButtonClicked(View view) {
((FrameLayout)view
.getParent()
.getParent()
.getParent()
.getParent()
.getParent())
.setVisibility(View.GONE);
}
In the layout file, give an android:id="+#id/yourName" to the view that you want it hidden, and get the view using
View viewToHide = findViewbyId(R.id.yourName);
From there, you could set the viewToHide.setVisibility(View.GONE);

Animating a TextView appearing on activity creation

I want to show an animation of a TextView appearing when an activity is created. What I want is to show the activity without the TextView and then the TextView appearing (ideally, flying from outside) in its final position without user interaction.
I've tried to use the transition framework from API level 19 by having the TextView with visibility gone in the XML layout and setting it to visible in onCreate() with this code:
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
TransitionManager.beginDelayedTransition(layout, new ChangeBounds());
textView.setVisibility(View.VISIBLE);
This doesn't work. However, if I don't do this in onCreate() but as a response to a button click, it works. I think that the problem is that the layout is not created yet, so when I set the visibility to visible in the onCreate(), the layout is created with the final state and there aren't two scenes for the TransitionManager to work.
I've tried putting the code in onPause() but the result was the same. Any ideas how this should be done?
Try onWindowFocusChanged() like this
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
// start your animation here
}
}
You should do this in onResume() after the layout has been created. Initially the visibility would be Invisible and then the view will animate.
try like this:
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TransitionManager.beginDelayedTransition(layout, new ChangeBounds());
textView.setVisibility(View.VISIBLE);
}
});
because your view should be able to animate only after it's creation

Set Selection in HorizontalScrollView

I have multiple views in a horizontalscrollview, in an activity (view1, view2, view3 to viewN) for N-views. When i come to this activity, view1 is the the first visible item in the horizontalscrollview. But I want to start with view5 or view6 to be the first visible item in my horizontalscrollview, so when i swipe left to right, it shows up as all views view1, view2 upto view4 before that view5.
horizontalScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
horizontalScrollView.scrollTo(view5.getLeft());
}
});
This waits until the views have been measured, then scrolls the scrollview to the coordinates of view5.
Haven't tested this, but it should work.

How to move the control to the bottom of a TextView in ScrollView ( Scroll to bottom)

In my layout I have a TextView inside a ScrollView. But I am unable to scroll to the bottom of the TextView programmatically.
I have tried with setSelection(int) but the method is undefined for TextView. So can any one help me.
Thanks in advance.
Ok finally i got the work around.
TextView tv = (TextView)findViewById(R.id.textView);//my text view
ScrollView sv = (ScrollView) findViewById(R.id.scrollView);//my scrollview
String log ="a Very long text";
tv.setText(log);
sv.post(new Runnable() {
public void run() {
sv.scrollTo(0, tv.getHeight());
}
});

scrollTo, scrollBy, smoothScrollTo, smoothScrollTo don't work in ScrollView after adding view in child layout

I have HorizontalScrollView with child Layout inside. After adding view into child layout I can't scroll HorizontalScrollView to the right side of scroller.
scrollTo, scrollBy, smoothScrollTo, smoothScrollTo don't work.
solved:
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
final HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);
OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
hsv.scrollTo(100, 0);
}
};
ll.getViewTreeObserver().addOnGlobalLayoutListener(listener);

Categories

Resources