I have the sequent layout:
<com.example.CustomSwipeRefreshLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/include" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</com.example.CustomSwipeRefreshLayout>
with fixed header and a scrollable list. CustmoSwipeRefreshLayout is a class that extends SwipeRefreshLayout:
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
private ListView childListView;
public CustomSwipeRefreshLayout(Context context) {
super(context);
}
public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setChildListView(ListView childListView) {
this.childListView = childListView;
}
#Override
public boolean canChildScrollUp() {
return childListView.getFirstVisiblePosition() != 0;
}
(where childListView is the widget with id lv). This code doesn't work as i want. In fact, progress circle appear in two cases:
- i swipe listview to top
- i make swipe gesture on header and listview has first element visible.
I would that progress circle appear both if i swipe listview to top (as works now), and also if i make swipe gesture on header and listview is not to top element
I had the same problem, my solution was set android:clickable="true" inside my parent layout. try it like this:
<com.example.CustomSwipeRefreshLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/include" >
<LinearLayout
android:clickable="true" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</com.example.CustomSwipeRefreshLayout>
Related
I'm trying to use 2 recycler views in 1 linear layout.
A recycler view will generate its own scroll but other data remains intact on the screen, so how do I combine everything into 1 scrolling area?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:weightSum="100">
<LinearLayout
android:id="#+id/sliderhome"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="70">
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
/>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="30"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout 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="vlabs.chorbazaar.HomeFragment"
android:background="#color/white"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
</LinearLayout>
I'm trying to make my slider and recycler view to scroll as one, but the slider is stuck at a constant place while the recycler view scrolls as usual.
change xml file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:weightSum="100">
<LinearLayout
android:id="#+id/sliderhome"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="70">
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
/>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="30"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
viewpager adapter:
private class MyPagerAdapter extends FragmentPagerAdapter {
ArrayList<String> arrayList
public MyPagerAdapter(FragmentManager fm, ArrayList<String> arrayList ) {
super(fm);
this.arrayList = arrayList;
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance(arraylist);
case 1: return SecondFragment.newInstance(arraylist);
}
#Override
public int getCount() {
return 2;
}
}
}
in activty:
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), arrayList));
fragment in xml file:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
i want to implement Globe-Weis File Folder like picture below which by clicking on green button it scrolling smoothly up and by clicking on the green button again ,the folder page scrolling down smoothly.
what i try so far was putting three scrollView in a frameLayout , and to open and close it
if (!underIsOpen) {
ObjectAnimator
.ofInt(sv_Under, "scrollY",
findViewById(R.id.ll_under_botton).getTop())
.setDuration(1000).start();
underIsOpen = true;
} else {
ObjectAnimator
.ofInt(sv_Under, "scrollY",
findViewById(R.id.ll_under_s_part).getTop())
.setDuration(1000).start();
underIsOpen = false;
}
but it just work on the scrollView which is on top
my code is:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test3);
ll_under_s_part = (LinearLayout) findViewById(R.id.ll_under_s_part);
sv_Under = (ScrollView) findViewById(R.id.scrollView1);
sv_Mid = (ScrollView) findViewById(R.id.scrollView2);
sv_Top = (ScrollView) findViewById(R.id.scrollView3);
}
public void onClick_under_left(View v) {
Toast.makeText(getApplicationContext(), "under layout",
Toast.LENGTH_SHORT).show();
if (!underIsOpen) {
ObjectAnimator
.ofInt(sv_Under, "scrollY",
findViewById(R.id.ll_under_botton).getTop())
.setDuration(1000).start();
underIsOpen = true;
} else {
ObjectAnimator
.ofInt(sv_Under, "scrollY",
findViewById(R.id.ll_under_s_part).getTop())
.setDuration(1000).start();
underIsOpen = false;
}
}
public void onClick_mid_left(View v) {
Toast.makeText(getApplicationContext(), "mid layout",
Toast.LENGTH_SHORT).show();
}
public void onClick_top_left(View v) {
Toast.makeText(getApplicationContext(), "top layout",
Toast.LENGTH_SHORT).show();
if (!topIsOpen) {
ObjectAnimator
.ofInt(sv_Top, "scrollY",
findViewById(R.id.ll_top_botton).getTop())
.setDuration(1000).start();
topIsOpen = true;
} else {
ObjectAnimator
.ofInt(sv_Top, "scrollY",
findViewById(R.id.ll_top_s_part).getTop())
.setDuration(1000).start();
topIsOpen = false;
}
}
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:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_under_s_part"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#ccff00"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/ll_under_botton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#519B57"
android:onClick="onClick_under_left"
android:orientation="vertical"
android:text="B T N" />
<LinearLayout
android:id="#+id/ll_under_p_part"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#edf5ee"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/ll_under_1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/ll_under_2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1000dp"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_mid_s_part"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#00000000"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/ll_mid_3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/ll_mid_botton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#088da5"
android:onClick="onClick_mid_left"
android:orientation="vertical"
android:text="B T N" />
<LinearLayout
android:id="#+id/ll_mid_p_part"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#e6f3f6"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/ll_mid_1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
<ScrollView
android:id="#+id/scrollView3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_top_s_part"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#00000000"
android:orientation="vertical"
android:visibility="visible" >
</LinearLayout>
<LinearLayout
android:id="#+id/ll_top_3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/ll_top_2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/ll_top_botton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#D11141"
android:onClick="onClick_top_left"
android:orientation="vertical"
android:text="B T N" />
<LinearLayout
android:id="#+id/ll_top_p_part"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#fae7ec"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
the second way i tried was using animation but no success to result i am looking for :
public void SlideUP(View view, Context context) {
view.startAnimation(AnimationUtils.loadAnimation(context,
R.anim.slid_up));
}
public void SlideDown(View view, Context context) {
view.startAnimation(AnimationUtils.loadAnimation(context,
R.anim.slid_down));
}
any other way that i can implement this?
I don't think adding so may layout on a single screen with scrollbars is a good idea, you can apply simple frame layouts and list them accordingly and when clicking on any of the desired folder icons just animate it towards top, and it will also show the content of the folder.
https://github.com/kikoso/Swipeable-Cards
you could customize this library to do that, I have done a similar kind of app and applied the same library with my custom changes.
I have RelativeLayoutin top Scroll View, and now i want when pull down RelativeLayoutin hint and pull up RelativeLayoutin show.
How i do?
My xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:id="#+id/Rlayout_top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" />
</RelativeLayout>
<ScrollView
android:id="#+id/Scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/Rlayout_top"
android:paddingBottom="20dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
Thank you!
try this :
private float lastPosition=0;
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY(); //for verticalScrollView
if(scrollY >lastPosition){
// hide your RelativeLayout
}else {
//show your view
}
lastPosition=scrollY
});
in navigation drawer, when i push a button or anything else, drawer gets closed. What is the problem?
I search on internet but I didn't find anything that is different than my codes. Maybe there are some mistakes but I have not found them.
codes:
index.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbb9b9b9">
<ListView
android:id="#+id/listViewDrawer"
android:layout_width="240dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:background="#fff"
android:padding="3dp"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:divider="#android:color/transparent">
</ListView>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="10">
<ImageButton
android:layout_width="0dp"
android:layout_height="fill_parent"
android:id="#+id/btnMalt"
android:layout_weight="1.5"
android:src="#drawable/mal" />
<EditText
android:layout_width="0dp"
android:layout_height="fill_parent"
android:id="#+id/legerin_ett"
android:layout_weight="5.5"
android:hint="#string/legerin_text" />
<ImageButton
android:layout_width="0dp"
android:layout_height="fill_parent"
android:id="#+id/btnLegerint"
android:layout_weight="1.5"
android:src="#drawable/legerin" />
<ImageButton
android:layout_width="0dp"
android:layout_height="fill_parent"
android:id="#+id/btnDerkevet"
android:layout_weight="1.5"
android:src="#drawable/derkeve" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/sernavt"
android:gravity="center"
android:text="sepana tirşikê ji bo telefonên jîr"
android:textStyle="bold"
android:textColor="#ff000000"
android:textSize="20dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/listViewt"
android:layout_gravity="center_horizontal"
android:layout_weight="9"
android:layout_marginBottom="5dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="0dp"
android:text="#string/peyameke_binivise"
android:id="#+id/buttonPeyamBinivise"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout >
</android.support.v4.widget.DrawerLayout>
in main.java related codes:
NuceMijarAdapter adapter1 = new NuceMijarAdapter(Index.this,R.layout.list_mijar_layout,arr);
lvDrawer.setAdapter(adapter1);
private class NuceMijarAdapter extends ArrayAdapter<String> {
private ArrayList<String> items;
public NuceMijarAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_mijar_layout, null);
}
final String o = items.get(position);
if (o != null) {
String htmlText = o.replace("<a href=\"index.php?","<a href=\"http://www.tirsik.net/index.php?")
.replace("padding-left:8px","padding-left:1px")
.replace(" » ","");
final NuceMijarIro nuce = mijarIroAction(htmlText);
Button tvMijar = (Button) v.findViewById(R.id.tvMijar);
Button tvHejmar = (Button) v.findViewById(R.id.tvHejmar);
tvMijar.setText(nuce.mijar);
tvHejmar.setText(nuce.hejmar);
tvMijar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("url: ", nuce.link);
if(nuce.link.contains("index.php?mijar")) {
urlIndex = nuce.link;
if(teketin) mijarRasthatiTeketi("bnr");
else mijarRasthati("bnr");
}
}
});
}
return v;
}
}
I had the same problem once.
I had a RecyclerView NavigationDrawer and by just moving the Navigation code to the bottom of the xml file my problem got solved.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//some code here for the body of your activity
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerView"
android:layout_width="350dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
hope it works for you too...
For more explanation,
<ListView
android:id="#+id/listViewDrawer"
android:layout_width="240dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:background="#fff"
android:padding="3dp"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:divider="#android:color/transparent">
</ListView>
Is going to be your Content and other contents gonna be your NavigationDrawer actually.
Please see how it works:
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Which said:
the following layout uses a DrawerLayout with two child views: a
FrameLayout to contain the main content (populated by a Fragment at
runtime), and a ListView for the navigation drawer.
And use NavigationDrawer which that comes from with new SupportLibrary.
So I have this XML Problem is when I open it it gives me forces close. When I remove some image view let say only 7 total images views in XML it works. Any idea why? I don't know why its not working when there are more images on my xml
App3.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/JCBG"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Products"
android:textColor="#color/JCLogo"
android:textSize="30dp"/>
<ViewFlipper
android:id="#+id/fliping"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- The child Views/Layout to flip -->
<!-- Layout 1 for 1st Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal|center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/gr" />
</LinearLayout>
<!-- Layout 2 for 2nd Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/calvitcdetails" />
</LinearLayout>
<!-- Layout 3 for 3rd Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/kapedetails"/>
</LinearLayout>
<!-- Layout 4 for 4th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/glutafitdetails"/>
</LinearLayout>
<!-- Layout 5 for 5th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/pgtdetails"/>
</LinearLayout>
<!-- Layout 6 for 6th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/hilifedetails"/>
</LinearLayout>
<!-- Layout 7 for 7th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/organicbarleyjuicedetails"/>
</LinearLayout>
<!-- Layout 8 for 8th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/barleydetails"/>
</LinearLayout>
<!-- Layout 9 for 9th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/spirulinadetails"/>
</LinearLayout>
<!-- Layout 10 for 10th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView10"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/omnisoapdetails"/>
</LinearLayout>
<!-- Layout 11 for 11th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView11"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/omnipinkishdetails"/>
</LinearLayout>
<!-- Layout 12 for 12th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView12"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/omnidaydetails"/>
</LinearLayout>
<!-- Layout 13 for 13th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView13"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="#drawable/omnitonerdetails"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
Activity3.java
package com.example.jcw;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
public class activity3 extends Activity
{
private ViewFlipper vf;
private float lastX;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app3);
vf = (ViewFlipper) findViewById(R.id.fliping);
}
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (vf.getDisplayedChild() == 0)
break;
vf.setInAnimation(this, R.anim.in_from_left);
vf.setOutAnimation(this, R.anim.out_to_right);
vf.showNext();
}
if (lastX > currentX) {
if (vf.getDisplayedChild() == 1)
break;
vf.setInAnimation(this, R.anim.in_from_right);
vf.setOutAnimation(this, R.anim.out_to_left);
vf.showPrevious();
}
break;
}
}
return false;
}
}
ImageView widgets use Bitmaps which consume a lot of heap space. Therefore if you try to load to many images at the same time you will get OutOfMemoryError which is probably in your Logcat.
In case bitmaps resolutions are higher than really needed for display, you can optimize loading them, here you will find Google's example how to do it.
If it's not the case then you have to design your app different way.
Add this in thatthe appmanifest:
android:largeHeap="true"
you can change your <ViewFlipper ... </ViewFlipper> to this <AdapterViewFlipper />
for this you need chaange your layout to this one:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/JCBG"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Products"
android:textColor="#color/JCLogo"
android:textSize="30dp"/>
<AdapterViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</LinearLayout>
then you need to create new layout and put below code to that:
mLayout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"/>
</LinearLayout>
in your main activity after getting adapterViewFlipper view set adapter for it:
adapterViewFlipper.setAdapter(new BAdapter(this,yourImageResourceArray));
after this you should create custom adapter (for example extend base adapter) and in it's get view method,inflate mLayout.xml and findView of ImageView and then set your Image for it.
this is sample of customAdapter that you need:
public class Badapter extends BaseAdapter{
Context context;
ArrayList<Integer> imageResources;
public Badapter(Context context,ArrayList<Integer> imageResources) {
this.context = context;
this.imageResources = imageResources;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return imageResources.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return imageResources.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater
.inflate(R.layout.mLayout, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
imageView.setBackgroundResource(imageResources.get(position));
//if you use view Holder you will save more memory
return rowView;
}
}
if you do this ,view will recycle and using less memory.
NOTE: if you use image with more than 50Kb size ,in some device your app will crash.so you should use image with lower size.