Android ListView onScroll Fires Multiple Times - android

I have two views in my fragment. In the first view, I have a linear layout, which holds some controls and in the second view, I have a listview.
I want the linear layout in the first view to be collapsed / expand when I scroll up / down listview.
I tried to handle scroll up / down events of listview in the OnScrollListener and collapse / expand the listview.
But there is a problem with the onScroll method,i.e, when I scroll the listview, it fires many times when I scroll the listview one time. So expanding and collapsing process of linear layout is going crazy.
Here is - what is happening on the screen.
https://www.youtube.com/watch?v=U7KNwS6JlUk
What am I doing wrong?
What is the best way to handle scrol up / down events of listview and collapse / expand linear layout?
My Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.training.mehmetyilmaz.mywallet.TransactionsActivity.TransactionsFragment"
android:orientation="vertical">
<LinearLayout
android:id="#+id/transactions_filter_linear_layout"
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/blue">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/account_type_label"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<RadioGroup
android:id="#+id/transactions_account_type_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/transactions_rd_cash"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/cash"
android:checked="true"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
<RadioButton
android:id="#+id/transactions_rd_bank"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/bank"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white"
/>
</RadioGroup>
</LinearLayout><!-- Money Add Type-->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/currency_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<Spinner
android:id="#+id/transactions_spinner_currency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/entry_currency"
android:layout_gravity="right"
android:gravity="right|end"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
style="#style/DropDownColor"
android:background="#drawable/abc_spinner_mtrl_am_alpha"
>
</Spinner>
</LinearLayout><!-- Currency -->
</LinearLayout><!-- First Line-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cyan_ligth"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
>
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginTop="5dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/money_type_label"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<RadioGroup
android:id="#+id/transactions_money_type_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/transactions_rd_add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/add"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
<RadioButton
android:id="#+id/transactions_rd_sub"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/subtract"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
</RadioGroup>
</LinearLayout><!-- Money Type-->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/entry_date_label_all_dates"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<Spinner
android:id="#+id/transactions_spinner_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/entry_date"
android:layout_gravity="right"
android:gravity="right|end"
android:textSize="#dimen/transactions_filter_text"
style="#style/DropDownColor"
android:background="#drawable/abc_spinner_mtrl_am_alpha">
</Spinner>
</LinearLayout><!-- Date -->
</LinearLayout><!-- Second Line -->
</LinearLayout><!-- Filter -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.training.mehmetyilmaz.mywallet.ScrollDetectingListView
android:layout_width="match_parent"
android:layout_height="320dp"
android:id="#+id/transactions_list_view"
></com.training.mehmetyilmaz.mywallet.ScrollDetectingListView>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/transactions_total_textView"
android:text="#string/total"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="#dimen/abc_text_size_medium_material"
android:background="#color/green"
android:textColor="#color/white"
android:gravity="center"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</LinearLayout>
OnScrollListener
mTransactionListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//Log.d("Scroll State", "State : " + scrollState);
}
private int mInitialScroll = 0;
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("Scroll", "Called");
int scrolledOffset = mTransactionListView.getVerticalScrollOffset();
if (scrolledOffset != mInitialScroll) {
boolean scrollUp = (scrolledOffset - mInitialScroll) < 0;
if (scrollUp) {
Log.d("Scroll", "Up");
expand(mTransactionsFilterLinearLayout);
} else {
Log.d("Scroll", "Down");
collapse(mTransactionsFilterLinearLayout);
}
mInitialScroll = scrolledOffset;
}
}
});

I think the solution is not possible with the classic ListView of Android SDK.
I found a custom ListView called Android-ObservableScrollView and implemented it in my project and the result is success.
You can find it from here
https://github.com/ksoichiro/Android-ObservableScrollView/

try setting a booolean value like this as global.
boolean flag = true;
then
if (scrollUp) {
if(flag == false){
Log.d("Scroll", "Up");
expand(mTransactionsFilterLinearLayout);
}
flag = true;
} else {
if(flag == true){
Log.d("Scroll", "Down");
collapse(mTransactionsFilterLinearLayout);
}
flag = false;
}

Related

EditText OnFocusChangeListener() doesn't hide cursor and view

I made an EditText OnFocusChangeListener that supposedly hides the cursor and another view when it's not in focus (i.e. clicking anywhere on the screen aside from the EditText will hide those items). Clicking on the EditText is the only time that will display them.
This is my OnFocusChangeListener for my EditText pageTitle:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
I also made an OnclickListener:
pageTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
}
});
The OnClickListener works but not the OnFocusListener. Ideally, pageTitle's cursor and saveCancelBar are initially hidden and will only appear when pageTitle is clicked.
This my XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:ignore="missingPrefix"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="app.wanderast.activity.AddPhotoActivity">
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal">
<TextView
android:id="#+id/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/arrow_left"
android:textSize="20sp"
android:clickable="true"
android:textColor="#color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_below="#id/toolbar"
android:orientation="vertical">
<EditText
android:id="#+id/title"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="36dp"
android:background="#color/transparent"
android:maxLength="64"
android:privateImeOptions="nm"
android:inputType="textNoSuggestions|textMultiLine"
android:cursorVisible="false"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="16sp"
app:autoSizeMaxTextSize="24sp"
app:autoSizeStepGranularity="4sp"
android:textColor="#color/grey700"/>
<View
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_marginTop="3dp"
android:background="#color/grey700"
/>
<LinearLayout
android:id="#+id/sort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:layout_marginTop="20dp"
android:clickable="true"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="#+id/sort_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:text="List View"
android:textColor="#color/grey700"
android:textSize="14sp"/>
<TextView
android:id="#+id/sort_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chevron_circle_down"
android:textColor="#color/grey700"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/add_photo_layout"
android:layout_marginTop="150dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<TextView
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="30dp"
android:background="#null"
android:minLines="0"
android:text="Add your first travel moment to your story"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="#+id/capture_photo_button"
android:layout_marginBottom="15dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/green_pill_thick">
<TextView
android:id="#+id/capture_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/camera"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/capture_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Moment"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/add_photo_button"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/blue_pill_thick">
<TextView
android:id="#+id/add_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/photo"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/add_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add from Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/list_view_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="150dp"
android:paddingBottom="50dp">
<ListView
android:id="#+id/list_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll">
</ListView>
</RelativeLayout>
</LinearLayout>
<!--Navbar-->
<LinearLayout
android:id="#+id/photo_story_navbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#drawable/navbar"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingStart="15dp"
android:orientation="horizontal"
android:layout_marginEnd="10dp">
<!-- Take Photo Button -->
<LinearLayout
android:id="#+id/nav_capture_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/green_pill_button"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_capture_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/camera"
android:textColor="#color/green500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_capture_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
<!-- Add from Gallery Button -->
<LinearLayout
android:id="#+id/nav_gallery_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:background="#drawable/blue_pill"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_gallery_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/photo"
android:textColor="#color/blue500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_gallery_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingEnd="15dp"
android:orientation="horizontal">
<!-- Review button -->
<LinearLayout
android:id="#+id/nav_review_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/red_500_pill"
android:layout_gravity="right"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_review_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Review"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/red500"
android:textAlignment="center"
android:textSize="12sp"/>
<TextView
android:id="#+id/nav_review_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/angle_double_right"
android:textColor="#color/red500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingStart="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/save_cancel_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/linearLayout"
android:layout_alignParentStart="true"
android:background="#drawable/black_border_top"
android:visibility="gone">
<RelativeLayout
android:id="#+id/save_title_btn"
android:clickable="true"
android:layout_width="84dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:background="#drawable/blue_pill"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="fill"
android:text="Save Title"
android:textSize="12sp"
android:textColor="#2196F3"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/cancel_title_btn"
android:layout_width="84dp"
android:layout_height="32dp"
android:clickable="true"
android:layout_alignParentRight="true"
android:background="#drawable/grey_700_pill"
android:layout_marginRight="105dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cancel"
android:layout_gravity="fill"
android:textSize="12sp"
android:textColor="#616161" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
If you check the definition of onFocusChange(View v, boolean hasFocus) you will notice that your code must change like this:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
This is the code for checking the soft keyboard visibility in Android
public class MainActivity extends AppCompatActivity
{
public static String TAG = MainActivity.class.getSimpleName();
private TextView saveCancelBar;
private EditText pageTitle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListenerToRootView();
saveCancelBar = (TextView) findViewById(R.id.cancel_bar);
pageTitle = (EditText) findViewById(R.id.title);
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean hasFocus)
{
...
}
});
}
public void setListenerToRootView()
{
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = activityRootView.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.e(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) {
// 0.15 ratio is perhaps enough to determine keypad height.
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard open", Toast.LENGTH_SHORT).show();
} else {
// keyboard is closed
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard closed", Toast.LENGTH_SHORT).show();
saveCancelBar.setVisibility(View.GONE);
}
}
});
}
}
I think you forgot to add focusableInTouchMode. Add focusableInTouchMode to your editText to get focused on touching the view.
android:focusableInTouchMode="true"

Show/Hide top bar like chrome, but with some static content and a listview

First let me give a short introduction and then show you what end goal looks:
So far I've added a scrolllistener to my listactivity, so that I can detect scrolling, though it does not give me any animation on android:animateLayoutChanges="true". So when i change the margin's of the view programmatically, it just does these changes instantly, and not that well, any help is most welcomed.
As you can see in my xml, i place the item I'm going to hide slowly first so other will be above it on the Z-axis.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lector="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:background="#color/background_greyish">
<LinearLayout
android:id="#+id/disappearingHeaderLabel"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="60dp"
android:animateLayoutChanges="true"
android:layout_height="50dp"
android:background="#color/solid_white"
android:orientation="horizontal"
android:weightSum="7">
<TextView
android:id="#+id/ProductDetailLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dip"
android:layout_weight="4"
android:singleLine="true"
android:text="PRODUKTER"
android:textColor="#color/solid_red"
android:textSize="20dp" />
<Button
android:id="#+id/priceChooserButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_weight="3"
android:background="#drawable/buttonbackground"
android:clickable="true"
android:singleLine="true"
android:text="#string/retailprice"
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
<dk.myapp.mobile.views.PageHeaderView
android:id="#+id/header"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></dk.myapp.mobile.views.PageHeaderView>
<LinearLayout
android:id="#+id/expandingListContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:animateLayoutChanges="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="110dp"
android:layout_above="#+id/paginationBarContainer"
android:orientation="vertical">
<LinearLayout
android:id="#+id/headerLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:orientation="horizontal"
android:weightSum="11">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/sortAmountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:singleLine="false"
android:text="1354"
android:textColor="#color/solid_red"
android:textSize="15dp" />
<TextView
android:id="#+id/sortLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:text="Produkter"
android:singleLine="true"
android:textColor="#color/solid_red"
android:textSize="15dp" />
</LinearLayout>
<Button
android:id="#+id/filterBtn"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_weight="4"
android:background="#drawable/button_background_red"
android:clickable="true"
android:singleLine="true"
android:text="#string/retailprice"
android:textColor="#color/solid_white"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/sortSpinner"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/spinner_background"
android:drawSelectorOnTop="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:prompt="#string/sort" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#color/divider_color"></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#color/divider_color"
android:dividerHeight="1dip"
android:paddingLeft="15dip"
android:scrollbarStyle="outsideInset"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int marginRemoved = firstVisibleItem * 30;
if (marginRemoved > 110) {
marginRemoved = 110;
}
if (previousMarginRemoved != marginRemoved) {
previousMarginRemoved = marginRemoved;
LinearLayout disappearingHeaderContainer = (LinearLayout) findViewById(R.id.disappearingHeaderLabel);
RelativeLayout.LayoutParams disappearingHeaderContainerLayoutParams = (RelativeLayout.LayoutParams) disappearingHeaderContainer.getLayoutParams();
LinearLayout expandingListViewContainer = (LinearLayout) findViewById(R.id.expandingListContainer);
RelativeLayout.LayoutParams expandingListViewContainerParams = (RelativeLayout.LayoutParams) expandingListViewContainer.getLayoutParams();
if (disappearingHeaderStartMargin == -1 && expandingHeaderStartMargin == -1) {
disappearingHeaderStartMargin = disappearingHeaderContainerLayoutParams.topMargin;
expandingHeaderStartMargin = expandingListViewContainerParams.topMargin;
}
float disappearingHeaderTopMarginFloat = disappearingHeaderStartMargin - (marginRemoved * pixelDensity);
int disappearingHeaderTopMargin = Math.round(disappearingHeaderTopMarginFloat);
if (marginRemoved > 50) {
marginRemoved = 50;
}
float expandingContainerTopMarginFloat = expandingHeaderStartMargin - (marginRemoved * pixelDensity);
int expandingContainerTopMargin = Math.round(expandingContainerTopMarginFloat);
disappearingHeaderContainerLayoutParams.setMargins(disappearingHeaderContainerLayoutParams.leftMargin,
disappearingHeaderTopMargin,
disappearingHeaderContainerLayoutParams.rightMargin,
disappearingHeaderContainerLayoutParams.bottomMargin);
expandingListViewContainerParams.setMargins(expandingListViewContainerParams.leftMargin,
expandingContainerTopMargin,
expandingListViewContainerParams.rightMargin,
expandingListViewContainerParams.bottomMargin);
if ((lastChangeDate < System.currentTimeMillis()-100)) {
lastChangeDate = System.currentTimeMillis();
disappearingHeaderContainer.setLayoutParams(disappearingHeaderContainerLayoutParams);
expandingListViewContainer.setLayoutParams(expandingListViewContainerParams);
}
}
}
};

How to avoid ListView put rows with TextView transparent

I'm developing a Chat App, so I create a list of friends.
Take a look at my ListView declaration at layout.xml
<ListView
android:listSelector="#android:color/transparent"
android:id="#+id/usersList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:dividerPadding="2dp"
android:overScrollFooter="#null"
android:scrollingCache="false"
android:transcriptMode="normal"
/>
And the behavior of this ListView is annoying.
When I run my app in a phone with Android 2.3 the TextView itens of my ListView becomes transparent just like the image below:
Sometimes when a press the itens the color of TextView back to normality.
When I run my app in a phone with Android 4.1 this problem not happens.
My user item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:src="#drawable/quick_blox_user" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/layout_pending_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_message_pending_number"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#color/color_new_message_title"
android:padding="2dp"
android:text="12"
android:textColor="#android:color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="#string/pending_messages"
android:textColor="#color/color_new_message"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My Adapter:
public class UserAdapter extends BaseCustomAdapter<UserItem> {
public UserAdapter(Activity a, List<UserItem> d) {
super(a, d);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
view = LayoutInflater.from(activity).inflate( R.layout.user_quickblox_item, null);
}
UserItem user = data.get(position);
String userLogin = user.getUser().getLogin();
int index = userLogin.indexOf("#");
if(index != -1) {
userLogin = userLogin.substring(0, index);
}
int count = ChatMessageDAO.getInstance(activity).countMessagePendentBySenderId(user.getUser().getId());
View viewPendingMessages = view.findViewById(R.id.layout_pending_messages);
if(count == 0) {
viewPendingMessages.setVisibility(View.GONE);
} else {
viewPendingMessages.setVisibility(View.VISIBLE);
Button buttonMessageNumberIndicator = (Button)view.findViewById(R.id.button_message_pending_number);
buttonMessageNumberIndicator.setText(String.valueOf(count));
}
TextView textViewUser = (TextView)view.findViewById(R.id.text_view_user);
textViewUser.setText(userLogin);
TextView textViewUserLastActivity = (TextView)view.findViewById(R.id.text_view_user_last_activity);
long diff = new Date().getTime() - user.getUser().getLastRequestAt().getTime();
long diffInDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
if(diffInDays == 0) {
long diffInHours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
if(diffInHours == 0) {
long diffInMinutes = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
if(diffInMinutes < 0) {
textViewUserLastActivity.setText("Esta online agora");
} else {
textViewUserLastActivity.setText("Entrou hoje " + diffInMinutes+ " minuto(s) atrás");
}
} else {
textViewUserLastActivity.setText("Entrou hoje " + diffInHours+ " horas atrás");
}
} else if(diffInDays == 1) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(user.getUser().getLastRequestAt());
int hour = calendar.get(Calendar.HOUR_OF_DAY);
textViewUserLastActivity.setText("Entrou ontem as " + hour + " horas");
} else {
textViewUserLastActivity.setText("Entrou " + diffInDays + " dia(s) atras");
}
return view;
}
}
How to avoid this problem?
There is a chance that the TextViews are not showing at all. not the colour is changing. you can check that by setting a background for the textviews.
A few mistakes in the item xml I would like to point out:
You have two items in the horizontal layout. the linear layout that contains the image and the linear layout that contains textview. In this case you can't have them both match_parent. the total size would be screen size * 2.
when setting weight for a linear layout in a horizontal container you set the width to 0dp.
your weight sum is 1 + 0.5 = 1.5. means that the image will reserve 2/3 of screen size and textview 1/3. looks un-logical.
These un logical values could cause different layout display for different os version.
Solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
**android:layout_width="0dp"**
android:layout_height="match_parent"
**android:layout_weight="1"**
android:orientation="vertical" >
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:src="#drawable/quick_blox_user" />
</LinearLayout>
<LinearLayout
**android:layout_width="0dp"**
android:layout_height="match_parent"
**android:layout_weight="2"**
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/layout_pending_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_message_pending_number"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#color/color_new_message_title"
android:padding="2dp"
android:text="12"
android:textColor="#android:color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="#string/pending_messages"
android:textColor="#color/color_new_message"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
What solved my problem was android:background:
<LinearLayout
android:background="#android:color/white
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
At my LinearLayout which has the TextViews at my user_item.xml(The layout of itens of the List). I put background color as White. Its strange because the main ViewGroup of this layout is already set with background color White. Now My itens appears normally.
An Annoying solution for an Annoying problem.

View Visibility is not working on ACTION_MOVE

I am working on MultiDirectionSlidingDrawer motion events. I have a problem on Action_Move.
I am unable to set Visible or Gone layouts on Action_Move.
I have handle with two buttons. When I drag click on first button it should show the first view also the same for second button.
slidingDrawer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
int width = getActivity().getWindowManager()
.getDefaultDisplay().getWidth();
width = width / 2;
if (event.getX() < width) {
System.out.println("Visible view 1");
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
}else{
System.out.println("Visible view 2");
view1.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
}
}
}
My xml view :
<com.myproject.widget.MultiDirectionSlidingDrawer
xmlns:my="http://schemas.android.com/apk/res/com.lesmobilizers.chronoresto"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:content="#+id/content"
app:direction="topToBottom"
app:handle="#+id/handle" >
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:orientation="vertical"
android:visibility="gone" />
<LinearLayout
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical" />
</LinearLayout>
<LinearLayout
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/menufiche_content"
android:layout_marginTop="-24dp"
android:background="#drawable/btn_restaurant_pannel_switch_close"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:id="#+id/lt_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<com.myproject.widget.FontCustomTextView
android:id="#+id/menutext"
style="#style/font_button_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:drawableRight="#drawable/img_arrow_down"
android:text="Menu "
android:textColor="#color/gray"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/lt_fiche"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<com.myproject.widget.FontCustomTextView
android:id="#+id/fichetxt"
style="#style/font_button_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:drawableRight="#drawable/img_arrow_down"
android:text="Fiche "
android:textColor="#color/gray"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
</com.myproject.widget.MultiDirectionSlidingDrawer>
System.out are working fine. But the thing is view1, view2 Visibilities are not working as my code says.
I hope you are getting my problem. I need your suggestion about Action_Move.

Can't handle clicks on ScrollBar of Horizontal ScrollView

I had a Grid Layout inside a Horizontal Scroll View, I'm Trying to Implement a Custom Scroll bar for the Scroll View, So far i'm Able to customize the UI of scroll bar [thumb and seekbar] also set visibility permanent. But problem is i'm stuck at handling events on scroll bar of scrollview. Eg. If user clicks on scroll bar in between the ScrollView should scroll to 50% and so on..like click on 1st qurater scroll view should scroll by 25%.
It's pretty simple if i made my own seekbar and bind it to scroll view, also it provides me methods of it's own and click events and can create objects. But i want to do it using ScrollView's slider. How do i handle scroll view's scrollbar clicks, i can create object of ScrollView class. But How will it controll seekbar?? I do not have onseekbarchangelistner for Horizontall scroll view neither it return any achild view or scrollbar view. also it's seekbar not clickable.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarThumbHorizontal="#drawable/scrollbar_horizontal_thumb"
android:scrollbarTrackHorizontal="#drawable/scrollbar_horizontal_track"
android:visibility="visible"
android:fadeScrollbars="false"
android:id="#+id/scrollView">
<android.support.v7.widget.GridLayout
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="18"
android:orientation="horizontal"
android:rowCount="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
</android.support.v7.widget.GridLayout>
</HorizontalScrollView>
</RelativeLayout>
Sorry For Posting this Answer too late:
What is to be done is : Extend a Horizontal Scroll view and override a method onScrollChanged and u get two coordinates x1 and x2. just pass x1 which is real time value indicates how much disance on x axis has been scrolled , to your seekbar setProgress(x1) method. Make sure u set seebar's max value first [setMax()] which is equal to the max scrollable length of scrollView.
public class mHorizontalScrollView extends HorizontalScrollView{
private float progress = 0.0f;
private SeekBar mSeekBar = null;
private MyActivityClass mObj = null;
private float xMax = 0.0f;
public mHorizontalScrollView(Context context, SeekBar sBar)
{
super(context);
mObj = (MyActivityClass) context; //or just use a Static reference
mSeekBar = sBar;
}
#Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
// TODO Auto-generated method stub
int myX = x;
if(xMax == 0)
{
xMax = mObj.getMaxScrollWidth(); //Choose Your own Scrollable width or length
mSeekBar.setMax(Math.round(xMax));
}
mSeekBar.setProgress(myX);
super.onScrollChanged(x, y, oldX, oldY);
}
}
It Worked flawlessly. Now to Implement it reverse Scroll on scroll bar and HorizontallScroll View Should scroll in a proper Syncwith seekbar:
Use Below code in main Activity class :
seek_bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int currScrollPosX = progress;
mHSCV.scrollTo(progress , 0);
// mHSCV is object of mHorizontalScrollView Class
});
}
Regards.

Categories

Resources