Monday, June 15, 2015

Listview in Scrollview, How to avoid the scrolling of scrollview

Hi friends I know by reading the title of the post some guys will get the doubt, is this scenario will come in real time and why we required Scrollview if Listview is implemented. But I faced this situation in one of my application, there is no alternative way to avoid for solving this issue added the bellow code. I found this code by googling some hours it's worked very well for me.

I though if share same in blog that could be helpful for some guys like me. Below is piece of the code
 
mListView.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        // Disallow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        break;
                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
                // Handle ListView touch events.
                v.onTouchEvent(event);
                return true;
            }
        });
mListView: is the instance of the Listview

Monday, January 28, 2013

Custom Expandable Listview 2


After long back I started posting codes I found different types in my experience. For my blog I got most of the comments for Custom ExpandableListView only. Finally I decided I want to share my knowledge on ExpandableListView.

So many people asked me provide the source code of example as like in above image. Find the code here


 

Monday, June 27, 2011

Custom Expandable Listview

Hi expandable List-view is two level tree view provided by Android. In this view contains two types of categories. First type is Group-Elements and second one is Child-Elements. And also called parent and child elements. The main aim of this example is customise the expandable list-view as picture a shown bellow. Means I covered some important topics about expandable list-view when I face in my experience.

Below code is main.xml contains the expandable list-view.
main.xml

 
     
 
            

group_row.xml this is contains the layout for Expandable list view group view structure.
group_row.xml

         
         

child_row.xml this is contains the layout for Expandable list view group structure.
child_row.xml


    
         

First read the reference of the expandable listview from xml to activity class.
public class ExpList extends ExpandableListActivity
{
 /**
  * strings for group elements
  */
    static final String arrGroupelements[] = 
    {
   "India",
   "Australia",
   "England",
   "South Africa"
 };

    /**
  * strings for child elements
  */
 static final String arrChildelements[][] = 
 {
   {
  "Sachin Tendulkar",
  "Raina",
  "Dhoni",
  "Yuvi"
   },
   {
  "Ponting",
  "Adam Gilchrist",
  "Michael Clarke"
   },
   {
  "Andrew Strauss",
  "kevin Peterson",
  "Nasser Hussain"
   },
   {
  "Graeme Smith",
  "AB de villiers",
  "Jacques Kallis"
   }
    };

 DisplayMetrics metrics;
 int width;
 ExpandableListView expList;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        expList = getExpandableListView();
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        //this code for adjusting the group indicator into right side of the view
        expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
        expList.setAdapter(new ExpAdapter(this));
  
  expList.setOnGroupExpandListener(new OnGroupExpandListener()
  {
   @Override
   public void onGroupExpand(int groupPosition) 
   {
    Log.e("onGroupExpand", "OK");
   }
  });
  
  expList.setOnGroupCollapseListener(new OnGroupCollapseListener()
  {
   @Override
   public void onGroupCollapse(int groupPosition) 
   {
    Log.e("onGroupCollapse", "OK");
   }
  });
  
  expList.setOnChildClickListener(new OnChildClickListener()
  {
   @Override
   public boolean onChildClick(ExpandableListView parent, View v,
     int groupPosition, int childPosition, long id) {
    Log.e("OnChildClickListener", "OK");
    return false;
   }
  });
    }
  
    public int GetDipsFromPixel(float pixels)
    {
     // Get the screen's density scale
     final float scale = getResources().getDisplayMetrics().density;
     // Convert the dps to pixels, based on density scale
     return (int) (pixels * scale + 0.5f);
    }
}

For customising the Exp Listview main thing is adapter. Android provides BaseExpandableListAdapter for customising the view. Bellow is the code for design of Adapter.
/**
     * This is adapter for expandable list-view for constructing the group and child elements.
     */
 public class ExpAdapter extends BaseExpandableListAdapter {

  private Context myContext;
  public ExpAdapter(Context context) {
   myContext = context;
  }
  @Override
  public Object getChild(int groupPosition, int childPosition) {
   return null;
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) {
   return 0;
  }

  @Override
  public View getChildView(int groupPosition, int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {
   
   if (convertView == null) {
    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.child_row, null);
   }
   
   TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
   tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);
   
   return convertView;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
   return arrChildelements[groupPosition].length;
  }

  @Override
  public Object getGroup(int groupPosition) {
   return null;
  }

  @Override
  public int getGroupCount() {
   return arrGroupelements.length;
  }

  @Override
  public long getGroupId(int groupPosition) {
   return 0;
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded,
    View convertView, ViewGroup parent) {

   if (convertView == null) {
    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.group_row, null);
   }
   
   TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
   tvGroupName.setText(arrGroupelements[groupPosition]);
   
   return convertView;
  }

  @Override
  public boolean hasStableIds() {
   return false;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
   return true;
  }
 }
group_indicator.xml
This the code for changing the default indicator image.

    
    
    

 
 
Can download the source code from following link Custom Expandable Listview 2

 

Wednesday, June 22, 2011

Intent From NonActivity Calss

Passing intent from Activity class is very easy. Intent from is also very easy if we know.
Here is the example for passing intent to other activity in non-activity class with using Context. Every view class has constructor, In that constructor context reference is coming from Activity class. With using that context passing intent is very easy.

IntentDemo.java
public class IntentDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        CustomView viewCustom = new CustomView(IntentDemo.this, null);
    }
}

In bellow example context is coming from activity. With using that context get the startActivity(Intent). There is no problem for passing intent.

CustomView.java
public class CustomView extends View {

 public CustomView(Context context, AttributeSet attrs) {
  super(context, attrs);
  Intent intent = new Intent(context, Example.class);
  context.startActivity(intent);
 }
}

Tuesday, June 14, 2011

Current Location Using GPS

Here is example for finding the current location with using GPS. Initially declare the Location Manager for getting service from android OS(device). Using that location manager request the location updates from GPS provider.

LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new MyLocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
  double latitude = location.getLatitude();
  double longitude = location.getLongitude();
}

This is LocationListener interface for getting the location updates from method onLocationChanged.

public class MyLocationListener implements LocationListener{
 public void onLocationChanged(Location location) {
  if (location != null) {
  double lat = loc.getLatitude();
  double lon = loc.getLongitude();
  }
 }

 public void onProviderDisabled(String provider) {
 // TODO Auto-generated method stub
 }

 public void onProviderEnabled(String provider) {
  // TODO Auto-generated method stub
 }

 public void onStatusChanged(String provider, int status,
 Bundle extras) {
  // TODO Auto-generated method stub
 }
}

Monday, May 30, 2011

Android Tab Icons Designing

Android provide different types of drawables (drawable-ldpi, drawable-mdpi, drawable-hdpi...) depends on screen resolution. Here is the link provided for reference for design the icons of tab(images sizes).

http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html

Thursday, April 21, 2011

Double Tap Event using Gesture

Hi this is code for implementing custom Double Click event using the Gesture.

public class Main extends Activity implements OnGestureListener, OnDoubleTapListener
{    
    private LinearLayout main;    
    private TextView viewA;
    
    private GestureDetector gestureScanner;
 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        gestureScanner = new GestureDetector(this);
        
        main = new LinearLayout(this);
        main.setBackgroundColor(Color.GRAY);
        main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
        
        viewA = new TextView(this);
        viewA.setBackgroundColor(Color.YELLOW);
        viewA.setTextColor(Color.BLACK);
        viewA.setTextSize(16);
        viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
        main.addView(viewA);
        
        setContentView(main);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent me)
    {
     return gestureScanner.onTouchEvent(me);
    }
    
    @Override
    public boolean onDown(MotionEvent e)
    {
     viewA.setText("-" + "DOWN" + "-");
     return true;
    }
    
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
     viewA.setText("-" + "FLING" + "-");
     return true;
    }
    
    @Override
    public void onLongPress(MotionEvent e)
    {
     viewA.setText("-" + "LONG PRESS" + "-");
    }
    
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
     viewA.setText("-" + "SCROLL" + "-");
     return true;
    }
    
    @Override
    public void onShowPress(MotionEvent e)
    {
     viewA.setText("-" + "SHOW PRESS" + "-");
    }    
    
    @Override
    public boolean onSingleTapUp(MotionEvent e)    
    {
     viewA.setText("-" + "SINGLE TAP UP" + "-");
     return true;
    }

 @Override
 public boolean onDoubleTap(MotionEvent e) {
  viewA.setText("-" + "DOUBLE TAP" + "-");
  return true;
 }

 @Override
 public boolean onDoubleTapEvent(MotionEvent e) {
  viewA.setText("-" + "DOUBLE TAP EVENT" + "-");
  return true;
 }

 @Override
 public boolean onSingleTapConfirmed(MotionEvent e) {
  viewA.setText("-" + "SINGLE TAP CONFORMED" + "-");
  return true;
 }
}