Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this code: </p> <p><em>activity_main.xml</em></p> <blockquote> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="100dip" tools:context=".MainActivity" &gt; &lt;HorizontalScrollView android:id="@+id/hsv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:fillViewport="true" android:measureAllChildren="false" android:scrollbars="none" &gt; &lt;LinearLayout android:id="@+id/innerLay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" &gt; &lt;LinearLayout android:id="@+id/asthma_action_plan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/action_plan" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/controlled_medication" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/controlled" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/as_needed_medication" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/as_needed" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/rescue_medication" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/rescue" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/your_symptoms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/symptoms" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/your_triggers" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/triggers" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/wheeze_rate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/wheeze_rate" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/peak_flow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/peak_flow" /&gt; &lt;TextView android:layout_width="0.2dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/ln" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &lt;/HorizontalScrollView&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="0.2dp" android:layout_alignParentRight="true" android:layout_below="@+id/hsv" android:background="@drawable/ln" /&gt; &lt;LinearLayout android:id="@+id/prev" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:paddingLeft="5dip" android:paddingRight="5dip" android:descendantFocusability="blocksDescendants" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/prev_arrow" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:paddingLeft="5dip" android:paddingRight="5dip" android:descendantFocusability="blocksDescendants" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/next_arrow" /&gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> </blockquote> <p><em>grid_item.xml</em></p> <blockquote> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" android:layout_height="100dp" android:src="@drawable/ic_launcher" /&gt; &lt;/LinearLayout&gt; </code></pre> </blockquote> <p><em>MainActivity.java</em></p> <pre><code>import java.util.ArrayList; import android.app.Activity; import android.graphics.Rect; import android.os.Bundle; import android.os.Handler; import android.view.Display; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; public class MainActivity extends Activity { LinearLayout asthmaActionPlan, controlledMedication, asNeededMedication, rescueMedication, yourSymtoms, yourTriggers, wheezeRate, peakFlow; LayoutParams params; LinearLayout next, prev; int viewWidth; GestureDetector gestureDetector = null; HorizontalScrollView horizontalScrollView; ArrayList&lt;LinearLayout&gt; layouts; int parentLeft, parentRight; int mWidth; int currPosition, prevPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); prev = (LinearLayout) findViewById(R.id.prev); next = (LinearLayout) findViewById(R.id.next); horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv); gestureDetector = new GestureDetector(new MyGestureDetector()); asthmaActionPlan = (LinearLayout) findViewById(R.id.asthma_action_plan); controlledMedication = (LinearLayout) findViewById(R.id.controlled_medication); asNeededMedication = (LinearLayout) findViewById(R.id.as_needed_medication); rescueMedication = (LinearLayout) findViewById(R.id.rescue_medication); yourSymtoms = (LinearLayout) findViewById(R.id.your_symptoms); yourTriggers = (LinearLayout) findViewById(R.id.your_triggers); wheezeRate = (LinearLayout) findViewById(R.id.wheeze_rate); peakFlow = (LinearLayout) findViewById(R.id.peak_flow); Display display = getWindowManager().getDefaultDisplay(); mWidth = display.getWidth(); // deprecated viewWidth = mWidth / 3; layouts = new ArrayList&lt;LinearLayout&gt;(); params = new LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT); asthmaActionPlan.setLayoutParams(params); controlledMedication.setLayoutParams(params); asNeededMedication.setLayoutParams(params); rescueMedication.setLayoutParams(params); yourSymtoms.setLayoutParams(params); yourTriggers.setLayoutParams(params); wheezeRate.setLayoutParams(params); peakFlow.setLayoutParams(params); layouts.add(asthmaActionPlan); layouts.add(controlledMedication); layouts.add(asNeededMedication); layouts.add(rescueMedication); layouts.add(yourSymtoms); layouts.add(yourTriggers); layouts.add(wheezeRate); layouts.add(peakFlow); next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Handler().postDelayed(new Runnable() { public void run() { horizontalScrollView.smoothScrollTo( (int) horizontalScrollView.getScrollX() + viewWidth, (int) horizontalScrollView.getScrollY()); } }, 100L); } }); prev.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Handler().postDelayed(new Runnable() { public void run() { horizontalScrollView.smoothScrollTo( (int) horizontalScrollView.getScrollX() - viewWidth, (int) horizontalScrollView.getScrollY()); } }, 100L); } }); horizontalScrollView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }); } class MyGestureDetector extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() &lt; e2.getX()) { currPosition = getVisibleViews("left"); } else { currPosition = getVisibleViews("right"); } horizontalScrollView.smoothScrollTo(layouts.get(currPosition) .getLeft(), 0); return true; } } public int getVisibleViews(String direction) { Rect hitRect = new Rect(); int position = 0; int rightCounter = 0; for (int i = 0; i &lt; layouts.size(); i++) { if (layouts.get(i).getLocalVisibleRect(hitRect)) { if (direction.equals("left")) { position = i; break; } else if (direction.equals("right")) { rightCounter++; position = i; if (rightCounter == 2) break; } } } return position; } } </code></pre> <p>Let me know if any issue enjoy...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload