Friday, November 9, 2012

Android Edittext Seterror

In Some Devices , SetError text will not be visible..Solution For that..



int ecolor = getResources().getColor(R.color.textcolor); 

String errorString = getResources().getString(R.string.ErrorString);

ForegroundColorSpan textSpan = new ForegroundColorSpan(ecolor);

errortext = new SpannableStringBuilder(errorString);

errortext.setSpan(textSpan, 0, errorString.length(), 0);

Android ExpandableListView Page Scroll From Top

If you are using ExpandableList View , Sometimes page will be automatically scroll to middle of the list..will not be shown from top of the screen ..to resolve it..



scrollviewid.post(new Runnable() {
public void run() {
scroll.scrollTo(0, 0);
}
});

Android Add Contact to your Phone

Here is a sample code for adding contact to android phone


Intent intent = new Intent(Intent.ACTION_INSERT);

intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

intent.putExtra(ContactsContract.Intents.Insert.NAME, contactname);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, mobilenumber);

intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);

intent.putExtra(ContactsContract.Intents.Insert.POSTAL, address);

startActivity(intent);

Android Expandable ListView Inside ScrollView


When you r using listview inside ScrollView , we can't scroll properly..List view will not be shown properly..Here a solution for that..


import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ExpandAndCollapseActivity extends Activity {

private ExpandableListView expandableListView;

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.expand_and_collapse);
    expandableListView = (ExpandableListView)findViewById(R.id.ExpandAndCollapseView);            
     ArrayList<String> Groups = new ArrayList<String>();
     Groups.add("Group A");
     Groups.add("Group B");
     Groups.add("Group C");
     Groups.add("Group D");

     String[] GroupA = { "Uruguay", "Mexico", "Argentina", "Korea Republic" };
     String[] GroupB = { "England", "USA", "Germany", "Ghana" };
     String[] GroupC = { "Netherlands", "Japan", "Paraguay", "Slovakia" };
     String[] GroupD = { "Brazil", "Portugal", "Spain", "Chile" };

     ExpandAndCollapseAdapter ExpandableAdapter = new ExpandAndCollapseAdapter(
          this, this, Groups, GroupA, GroupB, GroupC, GroupD);

     expandableListView.setAdapter(ExpandableAdapter);

     setListViewHeight(expandableListView);

     expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

          @Override
          public boolean onGroupClick(ExpandableListView parent, View v,
               int position, long id) {
               setListViewHeight(parent, position);
               return false;
          }
     });
}


private void setListViewHeight(ListView listView) {
     ListAdapter listAdapter = listView.getAdapter();
     int totalHeight = 0;
     for (int i = 0; i < listAdapter.getCount(); i++) {
          View listItem = listAdapter.getView(i, null, listView);
          listItem.measure(0, 0);
          totalHeight += listItem.getMeasuredHeight();
     }

     ViewGroup.LayoutParams params = listView.getLayoutParams();
     params.height = totalHeight
          + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
     listView.setLayoutParams(params);
     listView.requestLayout();
}


private void setListViewHeight(ExpandableListView listView,
         int group) {
      ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
      int totalHeight = 0;
      int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
         MeasureSpec.AT_MOST);
      for (int i = 0; i < listAdapter.getGroupCount(); i++) {
             View groupItem = listAdapter.getGroupView(i, false, null, listView);
             groupItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
             totalHeight += groupItem.getMeasuredHeight();

        if (((listView.isGroupExpanded(i)) && (i != group))
           || ((!listView.isGroupExpanded(i)) && (i == group))) {
              for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                  View listItem = listAdapter.getChildView(i, j, false, null,
                      listView);
                  listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

                  totalHeight += listItem.getMeasuredHeight();
               }
        }
     }

     ViewGroup.LayoutParams params = listView.getLayoutParams();
     int height = totalHeight
          + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
     if (height < 10)
          height = 200;
     params.height = height;
     listView.setLayoutParams(params);
     listView.requestLayout();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.expand_and_collapse, menu);
     return true;
}
}


You can create your Own Adapter and Layouts..