Thursday, November 15, 2012

ScrollView inside ScrollView Scrolling problem


While designing rich layouts you might need to use two scrollview in your app.
Well ideally its not advised to use two scrollview in a view. So try to avoid it.

Why this problem occurs ? :

When you put two scrollview android just get confused which scroll view is touched. So sometimes it gets unable to deliver touch event.
But even if the requirement forces you to make such layouts. Try this…
Say case is somewhat like this….
<ScrollView android:id=”@+id/parent_scroll” 
            android:layout_width=”fill_parent”
            android:layout_height=”wrap_content”
            android:layout_weight=”1″
            android:background=”@drawable/dotted_bg”
            android:focusableInTouchMode=”false”>
                        <LinearLayout   />
                        <LinearLayout   />
                        <LinearLayout  >
                        <ScrollView android:id=”@+id/child_scroll”  
                        android:layout_width=”fill_parent”
                        android:layout_height=”fill_parent”
                        android:background=”@drawable/text_box_bg”>
                    <TextView android:id=”@+id/text_description”
                        android:layout_width=”fill_parent”
                        android:layout_height=”fill_parent”
                        android:textColor=”@color/gray”
                        android:textSize=”12dip”
                        android:padding=”5dip”
                        android:scrollbars=”vertical”/>
                    <!–ScrollView>
                  </LinearLayout>
</ScrollView>

Step 1 : Provide unique id to both the scrollview.
Step 2 : get reference of that two scrollview in your activity.

     parentScroll=(ScrollView)findViewById(R.id.parent_scroll);
     childScroll=(ScrollView)findViewById(R.id.child_scroll);

Step 3: Now set touch listeners for both.

            parentScroll.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    Log.v(TAG,”PARENT TOUCH”);
                    findViewById(R.id.child_scroll).getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            });
            childScroll.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) 
                {
                    Log.v(TAG,”CHILD TOUCH”);
                                        // Disallow the touch request for parent scroll on touch of child view
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });
Done …

Wednesday, August 8, 2012

Code for Previous most Activity directly with out re calling the Activity



Code for
FirstActivity   ---> SecondActivity  ---> ThirdActivity

backPress: Thied ---> Second --->  First

When you click the GoToMainActivity button in ThiredActivity it will directly go to MainActivity by finishing the SecondActivity also

MainActivity ;
--------------


public class MainActivity extends Activity {
private Button next;

public static boolean back = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        next = (Button) findViewById(R.id.next);
    next.setText("First");
    }
   
    public void nextActivity(View view){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}  
   
}


SecondActivity :
----------------

public class SecondActivity extends Activity {
private Button next;

public static boolean back = false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        next = (Button) findViewById(R.id.next);
        next.setText("Second");
    }
   
    public void nextActivity(View view){    
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
}
   
@Override
    public void onResume(){
        super.onResume();
        /*
         * check the cundition wether user click the recomencer button if click colose this activity else resume the activity
         */
       
        if(back!= false){      
back();
}
    }
   
    public void GoBack(View view){
back();
}
/*
* this ethode is usefull to finish this activity when click recomencer button click
*/
public void back(){
finish();
back = false;
}
   
}



ThirdActivity ;
--------------


public class ThirdActivity extends Activity {
private Button next;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        next = (Button) findViewById(R.id.next);
    next.setText("Go to MainActivity");
    }
   
    public void nextActivity(View view){
    SecondActivity.back = true;
    finish();
}  
   
}

main.xml:
----------


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:onClick="nextActivity"
        android:text="first" />

</RelativeLayout>







Wednesday, May 30, 2012

How to play embed you tube video in web view

How to play embed you tube video in web view  

Note: this is applicable in HTML-5 browser supported devices only 

myWebView = (WebView) findViewById( R.id.webview_compontent );

String playVideo= "<html><body>Youtube video .. <br> <iframe class=\"youtube-player\" type=\"text/html\" width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\"></body></html>"

myWebView.loadData(playVideo, "text/html", "utf-8");