How to slide to imageView in ScrollView - android

I have a layout in which i have two button:
1)back
2)help
and ScollView in which i place two image Views:
1) firstImageVeiw
1) SecondImageVeiw
first image View will be displayed on front and second is placed beneath the 1st image.
Now i want that if i click on help button the the scrollView automatically slides to 2nd image view.
Any help or suggestion.

You can use scrollview's scrollTo() method
helpbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
yourscrollview.scrollTo(0, yourImageView.getTop());
}
});
At runtime you can get the location of every view object with
getleft()
getTop()
getRight()
getBottom()

Related

TranslateAnimation in Android with adjacent view

I have a view with TransaleAnimation which works very fine. The problem is view has a android:layout_toRightOf="#id/iv_hook" to an ImageView which doesn't animate with view. Is there any way to achieve this?
The ImageView is for show/hide. when the view slides out i want 'Show' image to display.
Try something like this:
// yourImageView is hidden => its visibility is View.GONE
// animate your view with translation => slide out effect
viewToAnimate.animate().translationX(newTranslationX).setDuration(duration).withEndAction(new Runnable() {
#Override
public void run() {
// now, as the animation has just finished, we want to show the image
yourImageView.setVisibility(View.VISIBLE);
}
}).start();

Android - listview - scroll programmatically to specific layout

Here's my question :
I have a listview (with adapter) which contains multiple inflated layout for each row and I want to smooth scroll programmatically to a specific layout.
Exemple :
this is one listview row:
------------------
- -
- layout1 -
- -
- layout2 -
- layout3 -
- etc... -
------------------
I know that in a Scrollview we can use getY() of the layout view but when i did this for my specific layout in listview it return me 0. But I can't use scrollview because I need to get SwipeRefreshLayout (Or we can get it in Scrollview ?)
I hope I was clear enough, if this is not the case, please tell me.
If you are triggering scroll by action like onClick.
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View row = listView.getChildAt(0);
View layout = row.findViewById(R.id.layout_to_scroll_to);
listView.smoothScrollToPositionFromTop(rowPositionToScrollTo, layout.getTop());
}
});
If you are triggering scroll on page load. Add this in onCreate or onActivityCreated for Activity or Fragment, accordingly.
getListView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
View row = listView.getChildAt(0);
View layout = row.findViewById(R.id.layout_to_scroll_to);
listView.smoothScrollToPositionFromTop(rowPositionToScrollTo, layout.getTop());
}
});
Change smoothScrollToPositionFromTop to setSelectionFromTop for no scroll animation.
Hope this helps !

How I can increase the touch area for controls (ToggleButton)?

I have a ToggleButton. I want to have more place for click on this button. If I add layout_width, layout_height etc. the image looks bad. I also tried using android:padding but it did not help me.
It is needed for the convenience of users.
Easy solution : If you have image for the button, then create transparent area around it (i.e. for touch area).
The grey area can be transparent to increase the touch area.
Or use TouchDelegate
You can also increase touch area by setting touch delegates Android Developer Training Blog Post
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the parent view
View parentView = findViewById(R.id.parent_layout);
parentView.post(new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before you call getHitRect()
#Override
public void run() {
// The bounds for the delegate view (an ImageButton
// in this example)
Rect delegateArea = new Rect();
ImageButton myButton = (ImageButton) findViewById(R.id.button);
myButton.setEnabled(true);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"Touch occurred within ImageButton touch region.",
Toast.LENGTH_SHORT).show();
}
});
// The hit rectangle for the ImageButton
myButton.getHitRect(delegateArea);
// Extend the touch area of the ImageButton beyond its bounds
// on the right and bottom.
delegateArea.right += 100;
delegateArea.bottom += 100;
// Instantiate a TouchDelegate.
// "delegateArea" is the bounds in local coordinates of
// the containing view to be mapped to the delegate view.
// "myButton" is the child view that should receive motion
// events.
TouchDelegate touchDelegate = new TouchDelegate(delegateArea,
myButton);
// Sets the TouchDelegate on the parent view, such that touches
// within the touch delegate bounds are routed to the child.
if (View.class.isInstance(myButton.getParent())) {
((View) myButton.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
}
Use TouchDelegate for your ToggleButton as ammar26 have commented you.
Or
Try this:
Make one parent layout like LinearLayout or RelativeLayout that cover the ToggleButton. And now put margin to that Parent layout.
Now, on click of that Parent layout do action for the toggle button.
Hope it will help you to increase touch area for your view.
Happy Coding.
Instead of putting the touch event in button put it in the layout containg the only the button..and fix the size of the layout as ur wish
increase the values of android:padding:
<SeekBar android:id="#+id/seek" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:paddingTop="5dp" android:paddingBottom="5dp"
android:progressDrawable="#drawable/green_scrubber_progress_horizontal_holo_ligh‌​t"
android:thumb="#drawable/thumb" />
Take the margin (place of padding) of your Button from its parent layout and then perform opration on your Layout
mLayout.setonTouchListener(View.onTouchListener{
// here your working code
});

Multi-View Android ScrollView scrollTo Popover not working

My app opens up in View A. In View B, I have made a custom popover who's view contains:
LinearLayout
ScrollView
LinearLayout1
LinearLayout2
.
.
.
LinearLayoutN
What I wish to do is From View A, Move Into View B having set the Vertical Scroll position to a specific LinearLayout from the ScrollView within the Popup.
I have the scrollview being assigned in code to a variable and
variable.scrollTo
is NOT working.
I have also tried to put
sView.post(new Runnable() {
#Override
public void run() {
sView.scrollTo(0, myNum);
}
});
within the routine I run to setup View B, Tried it Outside in the OnCreate routine. Nothing seems to be working to scroll the ScrollView.
The
sView.post(new Runnable() {
#Override
public void run() {
sView.scrollTo(0, scrollYPos);
}
});
was EXTREMELY useful, however its placement was even more important. Do to the scrollview being inside of a popover view, the code above actually had to be placed AFTER the code that truly brought that popover onto screen.

Image in ImageView not scaling properly

I have an ImageView, ther are 2 Images for a particular imageview, but the problem is when i click it (the clicked image is bigger then the previous one). So the layout gets shifted upwards, i dont want layout to move upwards, i just want image to extend upwards.
// addnews button the 1st screen
final ImageView addnewsback = (ImageView)findViewById(R.id.newslistback);
ImageView addnews = (ImageView)findViewById(R.id.newslistbtn);
addnews.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
addnewsback.setImageResource(R.drawable.bgbig);
vf.setDisplayedChild(0);
}
});
The correct view should be,

Categories

Resources