I have an app screen, where I have a webview, and a linear layout of buttons at the bottom of the screen. I have a scroll listener attached to the webview, which is giving me values when scrolled. What I really want to do, is gradually hide the bottom linear layout when scrolling down, and show when scrolling up, as seen in a fair few apps, but all the examples I can see are toolbars at the top of the screen.
I have tried a number of things, I have tried getting the height of the linear layout, and then on scrolling, move it in the y axis to hide and show, and this moved the buttons within but not the actual bar.
The main thing I tried, is when scrolling, decreasing the height of the bar and when it reaches 0 hiding it, and then showing it again, but this is squashing the buttons rather than scrolling them off the bottom of the screen without changing their size.
This is the layout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/buttonBar"
android:background="#403152"
android:visibility="gone"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/facebookLink"
android:src="#string/button1"
android:layout_weight="1"
android:onClick="openLink"
android:background="#android:color/transparent"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/linkedinLink"
android:src="#string/button2"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="openLink"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/twitterLink"
android:src="#string/button3"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="openLink"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/instagramLink"
android:src="#string/button4"
android:layout_weight="1"
android:background="?android:selectableItemBackground"
android:onClick="openLink"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#mipmap/ic_overflow"
android:layout_weight="1"
android:layout_gravity="center"
android:background="?android:selectableItemBackground"
android:onClick="showPopup"
android:id="#+id/aboutoption"/>
</LinearLayout>
<holidays.ObservableWebView
android:id="#+id/scrollableWebview"
android:layout_width="fill_parent"
android:clickable="false"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/buttonBar"
android:visibility="gone"/>
and then this is the code I have tried so far, which includes changing the height, etc - Hoping someone may be able to point me in the right direction of what I am doing wrong, or what may be a better way to tackle this.
mWebView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback(){
public void onScroll(int l, int t){
int height = mWebView.getMeasuredHeight();
Log.d("HEIGHT", Integer.toString(height));
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenHeight = size.y;
Log.d("Screen HEIGHT", Integer.toString(screenHeight));
LinearLayout layout = (LinearLayout)findViewById(R.id.buttonBar);
View contentsView = findViewById(R.id.buttonBar);
if(firstScroll == true){
mWebView.setLayoutParams(new RelativeLayout.LayoutParams(mWebView.getMeasuredWidth(), mWebView.getMeasuredHeight() + 10));
/*originalButtonBarHeight = findViewById(R.id.buttonBar).getHeight();
currentButtonBarHeight = originalButtonBarHeight;
contentsView.getLocationOnScreen(screenBarPosition);
contentsView.setY(screenBarPosition[1] + 1);
firstScroll = false;
layout.getLayoutParams().height = t + 1;
layout.requestLayout();
directionVal = t;*/
}
else if(firstScroll == false){
if(directionVal < t){
mWebView.setLayoutParams(new RelativeLayout.LayoutParams(mWebView.getMeasuredWidth(), mWebView.getMeasuredHeight() + 10));
/* Log.d("DOWN", Integer.toString(t));
if(currentButtonBarHeight <= 0){
//do nothing
}
else{
currentButtonBarHeight = currentButtonBarHeight - 5;
if(currentButtonBarHeight <= 0){
currentButtonBarHeight = 0;
layout.getLayoutParams().height = 0;
layout.requestLayout();
findViewById(R.id.buttonBar).setVisibility(View.GONE);
}
else{
contentsView.getLocationOnScreen(screenBarPosition);
contentsView.setY(screenBarPosition[1] + 1);
layout.getLayoutParams().height = currentButtonBarHeight;
layout.requestLayout();
}
}
/*if(t + 5 < originalButtonBarHeight) {
}*/
}
else if(directionVal > t){
Log.d("UP", Integer.toString(currentButtonBarHeight));
/*decreasedAmount = t - 5;
if (decreasedAmount < originalButtonBarHeight){
layout.getLayoutParams().height = t - 5;
layout.requestLayout();
}*/
}
directionVal = t;
}
}
});
You can do this one by CustomWebView:
CustomWebView.java:
public class CustomWebView extends WebView {
private GestureDetector gestureDetector;
public CustomWebView(Context context) {
super(context);
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}
public void setGestureDetector(GestureDetector gestureDetector) {
this.gestureDetector = gestureDetector;
}
}
web_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:orientation="vertical">
<com.customview.CustomWebView
android:id="#+id/customWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true" />
// Your other Linear layout
</LinearLayout>
CustomeGestureDetector clss for Gesture Detection (I have added in Fragment):
private class CustomeGestureDetector extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1 == null || e2 == null) return false;
if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
else {
try {
if(e1.getY() - e2.getY() > 20 ) {
// Show Your Linear Layout
customWebView.invalidate();
return false;
}
else if (e2.getY() - e1.getY() > 20 ) {
// Hide Your Linear Layout
customWebView.invalidate();
return false;
}
} catch (Exception e) {
customWebView.invalidate();
}
return false;
}
}
}
WebFragment.java:
private CustomWebView customWebView;
customWebView= (CustomWebView) view.findViewById(R.id.customWebView);
customWebView.setGestureDetector(new GestureDetector(new CustomeGestureDetector()));
Hope it would help you.
Make your root view as CoordinatorLayout.
Wrap your content into NestedScrollView.
Add following to your bottom View:
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior">
Related
I know that we can't access clipboard content from background services in 10+ but there is an application that shows copied text on a floating widget in Android 10+ devices using foreground service here's that app. I want to achieve that functionality somehow! please help me.
My app is working fine in android P and lower devices!
FloatingViewService.java
public class FloatingViewService extends Service implements View.OnClickListener {
private WindowManager wm;
private View floatv;
View collapsed;
View expanded;
CharSequence copiedText;
EditText editText;
public FloatingViewService(){
}
#Override
public void onCreate() {
super.onCreate();
// getting the widget layout from xml using layout inflater
floatv = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget,null);
// setting window manager layout parameters
// set foucus able ,, wrap content, set on lock screen etc...
final WindowManager.LayoutParams wrules = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
// getting window service and adding floating view to it
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(floatv,wrules);
//adding click listener to close button and expanded view
collapsed = floatv.findViewById(R.id.layoutCollapsed);
expanded = floatv.findViewById(R.id.layoutExpanded);
editText = floatv.findViewById(R.id.pastehere);
// adding an touch listener to make drag movement of the floating view
floatv.findViewById(R.id.relativeLayoutParent).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float touchX;
private float touchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
initialX = wrules.x;
initialY = wrules.y;
touchX = event.getRawX();
touchY = event.getRawY();
return true;
/*as we implemented OnTouchListener() to the root view, OnClickListner() won’t work. So, to detect clicks we will detect clicks in MotionEvent.ACTION_MOVE in the OnTouchListener() using below code:*/
case MotionEvent.ACTION_UP:
int diffX = (int) (event.getRawX() - touchX);
int diffY = (int) (event.getRawY() - touchY);
if(diffX < 10 && diffY < 10){
if (isViewCollapsed()){
collapsed.setVisibility(View.GONE);
expanded.setVisibility(View.VISIBLE);
wrules.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
wm.updateViewLayout(floatv,wrules);
}else {
collapsed.setVisibility(View.VISIBLE);
expanded.setVisibility(View.GONE);
wrules.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wm.updateViewLayout(floatv,wrules);
}
}
return true;
case MotionEvent.ACTION_MOVE:
// moving the widget across the screen
wrules.x = initialX + (int) (event.getRawX() - touchX);
wrules.y = initialY + (int) (event.getRawY() - touchY);
wm.updateViewLayout(floatv,wrules);
return true;
}
return false;
}
});
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
#Override
public void onPrimaryClipChanged() {
copiedText = clipboardManager.getPrimaryClip().getItemAt(0).getText();
if(copiedText != null){
editText.setText(String.valueOf(copiedText));
wm.updateViewLayout(floatv,wrules);
}
Toast.makeText(getBaseContext(), copiedText, Toast.LENGTH_SHORT).show();
}
});
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
if(floatv != null) wm.removeView(floatv);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.layoutExpanded){
collapsed.setVisibility(View.VISIBLE);
expanded.setVisibility(View.GONE);
}else if(v.getId() == R.id.buttonClose){
stopSelf();
}
}
private boolean isViewCollapsed() {
return floatv == null || floatv.findViewById(R.id.layoutCollapsed).getVisibility() == View.VISIBLE;
}
}
layout_floating_widget.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relativeLayoutParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- this is the collapsed layout -->
<RelativeLayout
android:id="#+id/layoutCollapsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<ImageView
android:id="#+id/collapsed_iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_baseline_menu_book_24" />
<ImageView
android:id="#+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="50dp"
android:src="#drawable/ic_baseline_close_24"
android:background="#color/teal_200"/>
</RelativeLayout>
<!-- this is the expanded layout -->
<LinearLayout
android:id="#+id/layoutExpanded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#a0c3d7"
android:orientation="horizontal"
android:padding="8dp"
android:visibility="gone">
<ImageView
android:id="#+id/buttonSimplifiedCodingExpanded"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_baseline_markunread_24"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/buttonSimplifiedCoding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="Dictionary"
android:textAlignment="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/pastehere"
android:textAlignment="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
see these images
text should appear in editText, working in android 9
I have a tablayout with viewpager and a recyclerview inside... I've override the onMeasure method... The first time I open the fragment, the fragment inside the viewpager not show, but if i change tab it appear and if i go back to the tab that doesn't show, it begin to show... Can some one help me please!!!
I Think i saw is the viewpager get visible on screen if it fit on the screen, if i need to scroll the screen to see the viewpager it begin hided until i go to another tab and back again to see the content.
I got diferent sizes of contents inside the viewpager, that's why i need to use a custom view pager and override the onMeasure...
The source code below...
THE VIEW PAGER
public class CustomPager extends ViewPager {
private int mCurrentPagePosition = 0;
public CustomPager(Context context) {
super(context);
}
public CustomPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
View child = getChildAt(mCurrentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
} catch (Exception e) {
e.printStackTrace();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void reMeasureCurrentPage(int position) {
mCurrentPagePosition = position;
requestLayout();
}
}
THE SCROLLVIEW
public class CustomScrollView extends ScrollView {
private GestureDetector mGestureDetector;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, new YScrollDetector());
setFadingEdgeLength(0);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev)
&& mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return (Math.abs(distanceY) > Math.abs(distanceX));
}
}
}
THE LAYOUT
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="br.com.ole.oleconsignado.ui.fragment.main.LastEntriesFragment">
<View
android:id="#+id/vw_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/dimen_mdpi"
android:layout_marginLeft="#dimen/dimen_mdpi"
android:layout_marginRight="#dimen/dimen_mdpi"
android:layout_marginEnd="#dimen/dimen_mdpi"
android:layout_marginStart="#dimen/dimen_mdpi"
android:background="#color/colorGrey"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/colorLightBlue"
app:tabSelectedTextColor="#color/colorLightBlue"
android:layout_marginLeft="#dimen/dimen_mdpi"
android:layout_marginRight="#dimen/dimen_mdpi">
<android.support.design.widget.TabItem
android:id="#+id/tab_national"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_national" />
<android.support.design.widget.TabItem
android:id="#+id/tab_international"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_international" />
</android.support.design.widget.TabLayout>
<br.com.ole.oleconsignado.util.CustomScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<br.com.ole.oleconsignado.util.CustomPager
android:id="#+id/vp_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimen_mdpi" />
</br.com.ole.oleconsignado.util.CustomScrollView>
</LinearLayout>
I realy don't know why it happen... Please, help me!
Here some examples similar...
Set Viewpager height inside Scrollview in android
getchildat() method returns null for last position (3rd Fragment in my case) Fragment for WarpContent ViewPager Android
ViewPager wrap_content not working
Android - ViewPager tab not showing first time
Sorry I couldn't understan your use-case completely but I think I can help.
Are You Trying to get any sort Dimensional-Measurements from the activity?
If Yes Then unfortunately the activities and fragments are not completely generated until onPause()
but you can try to do this-
Create a variable of your parent layout in your activity ;
RelativeLayout canvas;
canvas = (RelativeLayout) findViewById(R.id.canvas);
canvas.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
canvas.getViewTreeObserver().removeOnGlobalLayoutListener(this);
/* Code to get dimensional measurements */
}
});
Hope It Helps :)
i have one scrollview that is compulsory need it . within scroll view i have one Listview . listview's row item have one framlayout . i am adding nativeexpreesview to fram layout when ad is ready .
now problem is if list have 10 items when ad is not loaded its works okay but when ad is loaded then it occupy some height because i am adding it to frame layout then scroll view scrolls only height of list that is before ad loaded.
but i want scroll with entire item as well ads
My acivity'slayout is below
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/svStrategy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/app_bg"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lytBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center"
android:text="#string/str_use_spells_best"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Large"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/ivBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:adjustViewBounds="true" />
</LinearLayout>
<com.example.SummonersWar.classes.NestedListView
android:id="#+id/lvStrategy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:cacheColorHint="#00000000"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
android:fadingEdge="none"
android:focusable="false"
android:scrollbars="none" />
</LinearLayout>
</ScrollView>
Row_layout of Nestedlistview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/lytStrategyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/ivStrategyIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.4"
android:scaleType="fitXY" />
<TextView
android:id="#+id/tvStrategyTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:layout_weight="0.6"
android:ellipsize="end"
android:maxLines="4"
android:minLines="4"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
android:textColor="#android:color/white" />
</LinearLayout>
<LinearLayout
android:id="#+id/rlads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lytStrategyRow"
android:orientation="vertical"
android:visibility="gone">
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#android:color/transparent" />
<FrameLayout
android:id="#+id/fr_adview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
</LinearLayout>
</RelativeLayout>
I am programmatically adding Nativeexpressview to framlayout when ads is loaded.
My nestedlistview below
public class NestedListView extends ListView implements OnScrollListener // OnTouchListener
{
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 100;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
// setOnTouchListener(this);
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null
&& getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null,
this);
// now it will not throw a NPE if listItem is a ViewGroup
// instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
newHeight += getPaddingBottom() + getPaddingTop();
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}}
so my problem is scrollview scrolls only height of list when ads are loaded .so please help me what should i do now for scrolling entire list with ads?
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.ScrollView;
public class VerticalScrollview extends ScrollView {
public VerticalScrollview(Context context) {
super(context);
}
public VerticalScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalScrollview(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
return false;
default:
break;
}
return false;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return true;
}
}
I am using Horizontal Scroll View, dynamically i will add items in to that. if no. of items are more than displaying items on the screen i want to show image(arrow) at the edges ( like scroll view shows fading edges). How can i do it.
just look at below image, i want create like that.
this is the xml code
<HorizontalScrollView
android:layout_width="wrap_content"
android:scrollbars="none"
android:id="#+id/app_category"
android:layout_below="#+id/top_layout"
android:background="#drawable/minitopbar"
android:layout_height="30dp">
<LinearLayout
android:orientation="horizontal"
android:id="#+id/app_category_scroll_layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
</HorizontalScrollView>
A. You have to create your own class, that extends HorizontalScrollView:
public class ExtendedHorizontalScrollView extends HorizontalScrollView {
private IScrollStateListener scrollStateListener;
public HorizontalScrollViewForMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public HorizontalScrollViewForMenu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HorizontalScrollViewForMenu(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
prepare();
}
private void prepare() {
if (scrollStateListener != null) {
View content = this.getChildAt(0);
if (content.getLeft() >= 0)
scrollStateListener.onScrollMostLeft();
if (content.getLeft() < 0)
scrollStateListener.onScrollFromMostLeft();
if (content.getRight() <= getWidth())
scrollStateListener.onScrollMostRight();
if (content.getLeft() > getWidth())
scrollStateListener.onScrollFromMostRight();
}
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (scrollStateListener != null) {
if (l == 0)
scrollStateListener.onScrollMostLeft();
else if (oldl == 0)
scrollStateListener.onScrollFromMostLeft();
int mostRightL = this.getChildAt(0).getWidth() - getWidth();
if (l >= mostRightL)
scrollStateListener.onScrollMostRight();
if (oldl >= mostRightL && l < mostRightL)
scrollStateListener.onScrollFromMostRight();
}
}
public void setScrollStateListener(IScrollStateListener listener) {
scrollStateListener = listener;
}
public interface IScrollStateListener {
void onScrollMostLeft();
void onScrollFromMostLeft();
void onScrollMostRight();
void onScrollFromMostRight();
}
}
B. Use it defining of layout:
<LinearLayout
.....>
<ImageView
android:id="#+id/navigation_left"
..... />
<your.custom.view.package.ExtendedHorizontalScrollView
android:id="#+id/scroller"
android:layout_width="0px"
android:layout_weight="1"
android:fadingEdge="none"
....>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</your.custom.view.package.ExtendedHorizontalScrollView>
<ImageView
android:id="#+id/navigation_right"
..... />
</LinearLayout>
C. Add logic for disabling arrows when you can't scroll any more.
((ExtendedHorizontalScrollView)findViewById(R.id.scroller)).setScrollStateListener(new IScrollStateListener() {
public void onScrollMostRight() {
((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.INVISIBLE);
}
public void onScrollMostLeft() {
((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.INVISIBLE);
}
public void onScrollFromMostLeft() {
((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.VISIBLE);
}
public void onScrollFromMostRight() {
((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.VISIBLE);
}
});
An easy alternative is to make a scrollview without fading edges and then add the image with arrow below or on top of the scrollview.
Your xml something similar:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:fadingEdge="none"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View
android:layout_height="6dp"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:background="#drawable/background_effect"/>
</RelativeLayout>
</FrameLayout>
I want to implement a slider, which is basically two lines, one vertical and one horizontal, crossing where the screen is touched. I have managed to make one but I have to issues:
The slider is not very smooth, there is a slight delay when I'm moving the finger
If I place two sliders it is not multitouch, and I'd like to use both of them simultaneously
Here is the code:
public class Slider extends View {
private Controller controller = new Controller();
private boolean initialisedSlider;
private int sliderWidth, sliderHeight;
private Point pointStart;
private Paint white;
private int mode;
final static int VERTICAL = 0, HORIZONTAL = 1, BOTH = 2;
public Slider(Context context) {
super(context);
setFocusable(true);
// TODO Auto-generated constructor stub
}
public Slider(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
pointStart = new Point();
initialisedSlider = false;
mode = Slider.BOTH;
}
#Override
protected void onDraw(Canvas canvas) {
if(!initialisedSlider) {
initialisedSlider = true;
sliderWidth = getMeasuredWidth();
sliderHeight = getMeasuredHeight();
pointStart.x = (int)(sliderWidth/2.0);
pointStart.y = (int)(sliderHeight/2.0);
controller = new Controller(pointStart, 3);
white = new Paint();
white.setColor(0xFFFFFFFF);
}
canvas.drawLine(controller.getCoordX(),0,
controller.getCoordX(),sliderHeight,
white);
canvas.drawLine(0, controller.getCoordY(),
sliderWidth, controller.getCoordY(),
white);
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
if(isInBounds(X,Y)) {
updateController(X, Y);
}
break;
case MotionEvent.ACTION_MOVE:
if(isInBounds(X,Y)) {
updateController(X, Y);
}
break;
case MotionEvent.ACTION_UP:
if(isInBounds(X,Y)) {
updateController(X, Y);
}
break;
}
invalidate();
return true;
}
private boolean isInBounds(int x, int y) {
return ((x<=(sliderWidth)) && (x>=(0))
&& (y<=(sliderHeight)) && (y>=(0)));
}
private void updateController(int x, int y) {
switch(mode) {
case Slider.HORIZONTAL:
controller.setCoordX(x);
break;
case Slider.VERTICAL:
controller.setCoordY(y);
break;
case Slider.BOTH:
controller.setCoordX(x);
controller.setCoordY(y);
break;
}
}
private class Controller {
private int coordX, coordY;
Controller() {
}
Controller(Point point, int width) {
setCoordX(point.x);
setCoordY(point.y);
}
public void setCoordX(int coordX) {
this.coordX = coordX;
}
public int getCoordX() {
return coordX;
}
public void setCoordY(int coordY) {
this.coordY = coordY;
}
public int getCoordY() {
return coordY;
}
}
}
And the XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<com.android.lasttest.Slider
android:id="#+id/slider"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
<com.android.lasttest.Slider
android:id="#+id/slider"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
<com.android.lasttest.Slider
android:id="#+id/slider"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
</LinearLayout>
How to implement a SeekBar
Add the SeekBar to your layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SeekBar
android:id="#+id/seekBar"
android:max="100"
android:progress="50"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Notes
max is the highest value that the seek bar can go to. The default is 100. The minimum is 0. The xml min value is only available from API 26, but you can just programmatically convert the 0-100 range to whatever you need for earlier versions.
progress is the initial position of the slider dot (called a "thumb").
For a vertical SeekBar use android:rotation="270".
Listen for changes in code
public class MainActivity extends AppCompatActivity {
TextView tvProgressLabel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set a change listener on the SeekBar
SeekBar seekBar = findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);
int progress = seekBar.getProgress();
tvProgressLabel = findViewById(R.id.textView);
tvProgressLabel.setText("Progress: " + progress);
}
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// updated continuously as the user slides the thumb
tvProgressLabel.setText("Progress: " + progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// called when the user first touches the SeekBar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// called after the user finishes moving the SeekBar
}
};
}
Notes
If you don't need to do any updates while the user is moving the seekbar, then you can just update the UI in onStopTrackingTouch.
See also
SeekBar Tutorial With Example In Android Studio
Android provides slider which is horizontal
http://developer.android.com/reference/android/widget/SeekBar.html
and implement OnSeekBarChangeListener
http://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html
If you want vertical Seekbar then follow this link
http://hoodaandroid.blogspot.in/2012/10/vertical-seek-bar-or-slider-in-android.html
For future readers!
Starting from material components android 1.2.0-alpha01, you have slider component
ex:
<com.google.android.material.slider.Slider
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:valueFrom="20f"
android:valueTo="70f"
android:stepSize="10" />
Release notes
Material Design Spec
Docs