Android - Adding horizontal scroll in a layout with multiple Items - android

I want to create a layout which has multiple views in it. Currently I have 3tabs, in one of which I want to be able to scroll a specific part of the layout which contains two textviews and an Image.
I have tried using ViewPager , HorizontalScrollView but I havenot been able to scroll this part of my layout. Here is a design of my layout
[some images][scroll view]
[table 1]
[table 2]
[tab1][tab2][tab3]
the code that I have ran for gesture detection
public class RegionView extends HorizontalScrollView implements View.OnTouchListener{
private static final int SWIPE_MIN_DISTANCE = 5;
private static final int SWIPE_THRESHOLD_VELOCITY = 300;
private ArrayList<ArticleList> mItems= new ArrayList<ArticleList>();
private GestureDetector mGestureDetector;
private int mActiveFeature = 0;
public RegionView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setItems();
// TODO Auto-generated constructor stub
}
public RegionView(Context context) {
super(context);
internalWrapper = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
internalLayout = (LinearLayout) findViewById(R.id.regionll1);
featureLayout = (LinearLayout) View.inflate(myContext,R.layout.regionview1,null);
setItems();
}
public void setItems(){
showToast("setItem Called");
internalLayout.setOnTouchListener(gestureListener);
for(int i = 0; i< mItems.size();i++){
internalLayout.addView(featureLayout);
}
setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL ){
int scrollX = getScrollX();
int featureWidth = v.getMeasuredWidth();
mActiveFeature = ((scrollX + (featureWidth/2))/featureWidth);
int scrollTo = mActiveFeature*featureWidth;
smoothScrollTo(scrollTo, 0);
return true;
}else{
return false;
}
}
});
mGestureDetector = new GestureDetector(new MyGestureDetector());
}
class MyGestureDetector extends SimpleOnGestureListener {
public boolean isRightToLeft = false;
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int featureWidth = getMeasuredWidth();
mActiveFeature = (mActiveFeature < (mItems.size() - 1))? mActiveFeature + 1:mItems.size() -1;
smoothScrollTo(mActiveFeature*featureWidth, 0);
isRightToLeft=true;
return true;
}else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int featureWidth = getMeasuredWidth();
mActiveFeature = (mActiveFeature > 0)? mActiveFeature - 1:0;
smoothScrollTo(mActiveFeature*featureWidth, 0);
showToast("Gesture on right swipe");
isRightToLeft=false;
return true;
}
} catch (Exception e) {
Log.e("Fling", "There was an error processing the Fling event:" + e.getMessage());
}
return false;
}
}
}
My XML file with the tabhost , I have splitted the code in 2 parts to show the place where I am implementing the Horizontalscroll view
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_bckground"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/Orange"
>
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--First Tab-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabyourcity"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="0dip"
android:paddingRight="0dip" >
</LinearLayout>
<!—Second Tab-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabnepal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:id = "#+id/nepalpageview"
android:orientation="vertical">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/loadContainernepal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#414141"
android:orientation="horizontal" >
<TextView
android:id="#+id/loadingTextnepal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Data is Loading...."
android:textColor="#FFF"
/> <ProgressBar
android:id="#+id/progressBar2Nepal"
android:layout_width="25dip"
android:layout_height="25dip"
android:indeterminate="true"
android:visibility="visible"
android:layout_marginLeft="160dip"
style="#android:style/Widget.ProgressBar.Small"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativelayouttop"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_sunrisetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_sunrise"
android:layout_alignRight="#+id/tv_sunrise"
android:layout_below="#+id/tv_sunrise"
android:gravity="center"
android:text="rise time"
android:textColor="#ffffff" />
<TextView
android:id="#+id/tv_sunrise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/img_SUNRISE"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="SUNRISE"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/Orange"
android:textSize="18sp" >
</TextView>
<ImageView
android:id="#+id/img_SUNRISE"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignLeft="#+id/tv_sunrise"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/tv_sunrise"
android:paddingTop="5dp"
android:src="#drawable/sunriseicon" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/img_sunset"
android:layout_alignRight="#+id/img_sunset"
android:layout_below="#+id/img_sunset"
android:gravity="center"
android:text="SUNSET"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/Orange"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_sunsettime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_sunrise"
android:layout_alignRight="#+id/tv_sunrise"
android:layout_below="#+id/TextView02"
android:gravity="center"
android:text="SET time"
android:textColor="#ffffff" />
<ImageView
android:id="#+id/img_sunset"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignLeft="#+id/tv_sunrisetime"
android:layout_alignRight="#+id/tv_sunrisetime"
android:layout_below="#+id/tv_sunrisetime"
android:src="#drawable/sunseticon" />
<RelativeLayout
android:layout_width="130dp"
android:layout_height="120dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:gravity="right" >
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
the code works fine when I give all the image and text field in this layout,
but since I need to check the value and then place the texts and image here while scrolled I’m having a problem implementing this
<!-- <LinearLayout
android:id="#+id/scroll_linearlayout "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/regionll1 "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_regionname"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hilly Region"
android:textColor="#ffffff"
android:textSize="18sp" />
<ImageView
android:id="#+id/img_region"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:src="#drawable/iconsresized5" />
<TextView
android:id="#+id/tv_regiontemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="#+id/regionll2 "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/regionll3 "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout> -->
</HorizontalScrollView>
</android.support.v4.view.ViewPager>
</RelativeLayout>
</RelativeLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:background="#color/Orange"
android:gravity="center" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="25dp"
android:layout_height="20dp"
android:src="#drawable/rainfall_stastastics" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Rainfall Statistics"
android:textColor="#ffffff"
android:textSize="16sp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tbl_background" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="4dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total this month to date "
android:textColor="#000000" />
<TextView
android:id="#+id/tv_totalrainfall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</TableRow>
<TableRow>
</TableRow>
</TableLayout>
<TableLayout>
</TableLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
I want a scrollview like the one we get with iphone by viewpager

Related

Android ListView onScroll Fires Multiple Times

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;
}

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);
}
}
}
};

Swipe between two layouts overlapped on each other in Framelayout

I want to swipe between two linear layouts which are wrapped in frame layout. At one time, only one of the linear layout is visible. I am using button to show/hide them. I want to implement swipe functionality in them. I tried ViewFliper but didn't work out on finger swipe. Is it possible to do it using ViewPager?
Layout XML is
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" >
<LinearLayout
android:id="#+id/layTopUp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/yellowspace"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="visible" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etTopUpCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:hint="Code"
android:inputType="number"
android:maxLength="3" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/etTopUpNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7"
android:hint="Number"
android:inputType="number"
android:maxLength="7"
android:shadowColor="#000000" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<LinearLayout
android:id="#+id/layConv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvConvertedAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="XXX "
android:textColor="#000000" />
<ProgressBar
android:id="#+id/pbConversion"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
<ImageButton
android:id="#+id/bTopUpSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="#drawable/sendrechbotton" />
</LinearLayout>
<LinearLayout
android:id="#+id/layScratchCard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/yellowspace"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="gone" >
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etRechargeEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/bSendCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/sendcardbotton" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
Solution:
You need to use GestureDetector to get notified whenever swipe occurs on your LinearLayout
Please have a look at GestureDetector for reference.
As of now,I have applied swipe in your code,but I did not have your drawables so I have just used mine.rest of the thing is ready for you.so just use your xml file instead of mine.
Example:
MainActivity.java
package com.mehuljoisar.swipe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private GestureDetector gesturedetector = null;
private Intent i;
private FrameLayout flContainer;
private LinearLayout ivLayer1,ivLayer2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acti);
flContainer = (FrameLayout) findViewById(R.id.flContainer);
ivLayer1 = (LinearLayout)findViewById(R.id.layTopUp);
ivLayer2 = (LinearLayout)findViewById(R.id.layScratchCard);
gesturedetector = new GestureDetector(new MyGestureListener());
flContainer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
gesturedetector.onTouchEvent(event);
return true;
}
});
}
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return gesturedetector.onTouchEvent(ev);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 20;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float dX = e2.getX() - e1.getX();
float dY = e1.getY() - e2.getY();
if (Math.abs(dY) < SWIPE_MAX_OFF_PATH &&
Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
if (dX > 0) {
Toast.makeText(getApplicationContext(), "Right Swipe",
Toast.LENGTH_SHORT).show();
//Now Set your animation
if(ivLayer2.getVisibility()==View.GONE)
{
Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_right_in);
ivLayer2.startAnimation(fadeInAnimation);
ivLayer2.setVisibility(View.VISIBLE);
}
} else {
Toast.makeText(getApplicationContext(), "Left Swipe",
Toast.LENGTH_SHORT).show();
if(ivLayer2.getVisibility()==View.VISIBLE)
{
Animation fadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left_out);
ivLayer2.startAnimation(fadeInAnimation);
ivLayer2.setVisibility(View.GONE);
}
}
return true;
} else if (Math.abs(dX) < SWIPE_MAX_OFF_PATH &&
Math.abs(velocityY) >= SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dY) >= SWIPE_MIN_DISTANCE) {
if (dY > 0) {
Toast.makeText(getApplicationContext(), "Up Swipe",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Down Swipe",
Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}
}
acti.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/flContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/black" >
<LinearLayout
android:id="#+id/layTopUp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/darker_gray"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="visible" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etTopUpCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:hint="Code"
android:inputType="number"
android:maxLength="3" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/etTopUpNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7"
android:hint="Number"
android:inputType="number"
android:maxLength="7"
android:shadowColor="#000000" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="XXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnTopupAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
<LinearLayout
android:id="#+id/layConv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvConvertedAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="XXX "
android:textColor="#000000" />
<ProgressBar
android:id="#+id/pbConversion"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
<ImageButton
android:id="#+id/bTopUpSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="#drawable/icon" />
</LinearLayout>
<LinearLayout
android:id="#+id/layScratchCard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/icon"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:padding="25dp"
android:visibility="gone" >
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeOperator"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#000000" />
<EditText
android:id="#+id/etRechargeEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
android:textColor="#000000" />
<Spinner
android:id="#+id/spnRechargeAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/bSendCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/icon" />
</LinearLayout>
</FrameLayout>
<!-- <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd" />
-->
</LinearLayout>
slide_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="800"/>
</set>
slide_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="800"/>
</set>
I hope it will be helpful !!
Create an Activity extending FragmentActivity.
Create two fragments - each for the linear layouts
Create your View pager adapter extending FragmentStatePagerAdapter.
Supply a list of fragments to yout View pager adapter.

Is it possible to check if a specific "TAB" in TabWidget is visible in current View or not?

I have a TabWidget with 5 tabs in it. Tabs : "One","Two","Three","Four","Five". The TabHost is in a HorizontalScrollView. Now I've one arrow (< and >) each on each side of the TabWidget to indicate that there are more tabs in that direction(which are currently not visible in view).
Now what I want to do is, if the first tab is not visible(in current view) then the arrow to left (<) should be visible and if the last tab is not visible(in current view) then the arrow to right should be visible and if both tabs are not visible(in current view) then both arrows should be visible. I tried doing it with:
if (horizontablscrollview.getLeft == 0)
{
arrowRight.setVisiblity(View.VISIBLE)
arrowLeft.setVisiblity(View.INVISIBLE)
}
else if( horizontablscrollview.getRight == 0)
{
arrowLeft.setVisiblity(View.VISIBLE)
arrowRight.setVisiblity(View.INVISIBLE)
}
else if ( horizontablscrollview.getRight == 0 && horizontablscrollview.getLeft == 0)
{
arrowRight.setVisiblity(View.VISIBLE)
arrowLeft.setVisiblity(View.VISIBLE)
}
But this doesn't work, my both tabs are invisible in the start:
my xml looks like this..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<RelativeLayout
android:id="#+id/RL_Header"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_marginBottom="3dip"
android:background="#drawable/gradient" >
<ImageButton
android:id="#+id/btn_Home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="#android:color/transparent"
android:contentDescription="#string/description_home"
android:onClick="onHomeButtonClick"
android:src="#drawable/title_home" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:background="#android:color/transparent"
android:contentDescription="#string/description_about"
android:onClick="onClickAbout"
android:src="#drawable/title_about" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/FL_Tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/RL_Header" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/FL_Tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/RL_Header" >
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<ImageView android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentLeft="true"/>
<RelativeLayout
android:id="#+id/rl_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentLeft="true"
android:visibility="invisible"/>
<ImageView android:id="#+id/arrow_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentRight="true"
android:visibility="invisible"/>
<HorizontalScrollView
android:id="#+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_toRightOf="#+id/arrow_left"
android:layout_toLeftOf="#+id/arrow_Right">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true"/>
</HorizontalScrollView>
</RelativeLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="65dip" >
<RelativeLayout
android:id="#+id/myTabContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip" >
<TableLayout
android:id="#+id/tabSubs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btntestno1"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 1"
android:textColor="#000000" />
<Button
android:id="#+id/btntestno2"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 2"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btntestno3"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 3"
android:textColor="#000000" />
<Button
android:id="#+id/btntestno4"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 4"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btntestno5"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 5"
android:textColor="#000000" />
<Button
android:id="#+id/btntestno6"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 6"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</RelativeLayout>
</FrameLayout>
</TabHost>
</RelativeLayout>
</RelativeLayout>
Any help would be greatly appreciated.
One solution would be to make you own HorizontalScrollView class and show the ImageViews based on the scroll(although I didn't tested that much the code below should work). Your layout will look like this:
//...
<RelativeLayout
android:id="#+id/rl_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentLeft="true"
android:visibility="gone"/>
<ImageView android:id="#+id/arrow_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentRight="true"
/>
<com.your.package.here.SpecialScroll
android:id="#+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true"/>
</com.your.package.here.SpecialScroll>
</RelativeLayout>
//...
The SpecialScroll class will look like this:
public class SpecialScroll extends HorizontalScrollView {
public interface PositionListener {
public void onLeftArrowRequired(boolean required);
public void onRightArrowRequired(boolean required);
public View implementScrolledView();
}
private PositionListener listener;
public void setPositionListener(PositionListener listener) {
this.listener = listener;
}
public SpecialScroll(Context context) {
super(context);
}
public SpecialScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SpecialScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (l == 0) {
listener.onLeftArrowRequired(false);
} else {
listener.onLeftArrowRequired(true);
}
View v = listener.implementScrolledView();
Rect r = new Rect();
v.getDrawingRect(r);
if ((r.width() - l) == getWidth()) {
listener.onRightArrowRequired(false);
} else {
listener.onRightArrowRequired(true);
}
}
}
Then in your activity's onCreate method you'll do:
SpecialScroll hsv = (SpecialScroll) findViewById(R.id.my_scrollView);
hsv.setPositionListener(this); // I made the activity implement the SpecialScroll.PositionListener interface
and the activity's implementation of the PositionListener interface is:
#Override
public void onLeftArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.GONE);
}
}
#Override
public void onRightArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.GONE);
}
}
#Override
public View implementScrolledView() {
return findViewById(android.R.id.tabs);
}
This isn't an elegant solution, but you could display the left arrow if the x coordinate of the leftmost tab was less than zero and display the right tab if the rightmost tab's x plus its width was greater than the screen width.

how can i put the framelayout in the bottom of the application?

i want to put a framelayout in the bottom by doing this :
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/darkblue"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom"
android:textSize="30px"
android:textColor="#color/white"
android:layout_gravity="center"
/>
</FrameLayout>
But it doesn't work . It's staying in the middle of my application.
This is the whole thing :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
>
<FrameLayout
android:id="#+id/first"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/darkblue"
>
<ImageView
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="#drawable/back4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="30px"
android:textColor="#color/white"
android:layout_gravity="center"
/>
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:id="#+id/second"
android:layout_below="#id/first"
>
<ImageView
android:id="#+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chuck"
/>
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:id="#+id/third"
android:layout_below="#id/second"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/sum"
android:text="Summary "
android:textSize="25px"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/s_btn"
android:src="#drawable/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/sum"
android:layout_alignTop="#id/sum"
/>
<TextView
android:id="#+id/blank"
android:text=" "
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/sum"
/>
<TextView
android:id="#+id/airdate"
android:text="Air Date : "
android:textSize="25px"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/blank"
/>
<TextView
android:id="#+id/blank2"
android:text=" "
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/airdate"
/>
<TextView
android:id="#+id/airtime"
android:text="Air Time : "
android:textSize="25px"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/blank2"
/>
</RelativeLayout>
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:id="#+id/fourth"
android:layout_below="#id/third"
android:layout_alignParentBottom="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom"
android:textSize="30px"
android:textColor="#color/white"
android:layout_gravity="center"
/>
</FrameLayout>
</RelativeLayout>
In the bottom of what? What is your parent layout? If you're using a RelativeLayout, remove the layout_gravity attribute, and add the alignParentBottom="true" attribute instead.
Not sure this is totally correct but I'm working on an similar app with a bunch of frame layouts that get dynamically generated and had to switch to relative layouts to use the android:layout_below="#id/some_id" to 'stack' my layouts. I'm using fragments and it seems to be working well.
Example layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bg">
<!-- Content Foo -->
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/middle_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#id/top_layout"
android:background="#drawable/bg">
<!-- Content Foo -->
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#id/middle_layout"
android:background="#drawable/bg">
<!-- Content Foo -->
</RelativeLayout>
This is full programmability method :
EKRAN MY_EKRAN;
context = getApplicationContext();
MY_EKRAN = new EKRAN( contex );
YOUR_FRAMELAYOUT = findViewById(R.id.PUT_ID_FOR_YOUR_FRAMELAYOUT);
YOUR_FRAMELAYOUT.post(new Runnable() {
#Override
public void run() {
YOUR_FRAMELAYOUT.getLayoutParams().width = MY_EKRAN.W(105);
YOUR_FRAMELAYOUT.getLayoutParams().height = MY_EKRAN.H(10);
// banner.getLayoutParams().width = ( MY_EKRAN.H(10));
// banner.getLayoutParams().width = ( MY_EKRAN.H(10));
YOUR_FRAMELAYOUT.setY( MY_EKRAN.H(90) );
// also like this
// YOUR_FRAMELAYOUT.setLayoutParams(new LinearLayout.LayoutParams( MY_EKRAN.W(100) ,MY_EKRAN.H(30) ));
}
});
Here's ekran class :
public class EKRAN {
DisplayMetrics dm = new DisplayMetrics();
Point size_ = new Point();
static int width;
static int height;
EKRAN(Context CONTEXT_) {
dm = CONTEXT_.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
height = dm.heightPixels;
width = dm.widthPixels;
}
public static int WIDTH() {
return width;
}
public static int HEIGHT(){
return height;
}
public int W( int PER_ ){
return width/100*PER_;
}
public int H( int PER_ ){
return height/100*PER_;
}
//////////////////
//extras
/////////////////
public int GET_PIX_FROM_DP ( float DP_VALUE )
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP_VALUE , dm );
}
public int GET_PIX_FROM_DP2 ( float DP_VALUE )
{
float res = DP_VALUE * ( dm.ydpi / 160f);
return (int) res;
}
}

Categories

Resources