Wednesday, June 12, 2013

How to Change Size of Text in WebView in android


WebSettings settings= webview.getSettings();
settings.setTextSize(WebSettings.TextSize.NORMAL);

Sunday, June 2, 2013

Database Helper Class Example

public class DBHelper extends SQLiteOpenHelper
{
       private static final String DATABASE_NAME = "Student";
       private static final int DATABASE_VERSION =  1;
       private static final String TABLE_USER = "Marks";
       private SQLiteDatabase db;
       private static String DB_PATH = "";
       private Context mycontext;
       private String today;

       private static String CREATE_TABLE_USER = "create table Marks (ASid integer primary key autoincrement, Name text, Marks integer);";
      

public DBHelper(Context context) {
            
super(context, DATABASE_NAME, null, DATABASE_VERSION);
       DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}

@Override
public void onCreate(SQLiteDatabase db) {
       //db.execSQL(CREATE_TABLE_USER);
}

public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {}

public static boolean isTableExist(SQLiteDatabase db)
{

Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+TABLE_USER+"'", null);

       if(cursor!=null)
{
if(cursor.getCount()>0)
             {
                  cursor.close();
                  return true;
             }
             cursor.close();
            
    }
return false;
}
            
            
private static boolean isDataBaseExist()
{           
       File file =new File(DB_PATH + DATABASE_NAME);       
       return file.exists();
}

public DBHelper opendb() throws SQLException
{
isDataBaseExist();
       if (isDataBaseExist() == false)
       {
              db=getWritableDatabase();
              if (db.isOpen())
                    db.close();
       }
      
db = getWritableDatabase();
       boolean tableExsist = isTableExist(db);
      
if(! tableExsist)
       {
              db.execSQL(CREATE_TABLE_USER);
       }
       return this;
       }

       public void closedb() {
              db.close();
       }
      
       public void AddRecords(String Name, Integer Marks)
       {
              Date d = new Date();
              SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
              String today = curFormater.format(d);
                   
              try
              {
                    ContentValues initialValues = new ContentValues();
                    db =  getWritableDatabase();    
                    initialValues.put("Name", Name);
                    initialValues.put("Marks", Marks);
                    db.insert(TABLE_USER, null, initialValues);
                    }
                    catch(Exception e)
                    {
                           e.printStackTrace();
                    }
             }
            
       public boolean CheckASList(String Name, Integer Marks)
             {
                    db = getReadableDatabase();
                    boolean flg = false;
                    try
                    {
                           Cursor cursor = db.query(TABLE_USER,
                            null, "Name" + " = '" + Name +"'"  +" AND " + "Marks" + " = '" + Marks+"'", null,null, null, null);
                          
                    int size = cursor.getCount();
                    if(size > 0)
                    {
                           flg = true;               
                    }
                    }
                    catch(Exception e)
                    {
                           e.printStackTrace();
                    }
                    return flg;
             }
      
       public ArrayList<ListBean> SelectStudentList(Integer Marks)
       {
             db = getReadableDatabase();
            
            
             ArrayList<ListBean> Beans= new ArrayList<ListBean>() ;
             boolean flg = false;
             try
             {
                    Cursor cursor = db.query(TABLE_USER,
                            null, "Narks" + " > '" + Marks +"'"  , null,
                            null, null, null);
                   
                    int size = cursor.getCount();
                    if(size > 0)
                    {     
                           cursor.moveToFirst();
                           for(int i=0; i<size; i++)
                           {
                                  flg = true;
                                  String Name = cursor.getString(0);
                                  String Marks= cursor.getString(1);
                                  ListBean ASbean = new ListBean() ;
                                  ASbean.setName(Name);
                                  ASbean.Marks(Marks);
                                
Beans.add(i, ASbean);
                                  cursor.moveToNext();
                           }     
                           cursor.close();
                    }
             }
             catch(Exception e)
             {
                    e.printStackTrace();
             }
             return Beans;
       }
      
       public int getCount()
       {
             db = getReadableDatabase();
             Cursor cursor = db.query(TABLE_USER,
                     null, null, null,
                     null, null, null);
             int size = cursor.getCount();
             return size;
       }

}

Saturday, June 1, 2013

How to get JSON Object form url in android

String response = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("your url");
try
{
JSONObject json = new JSONObject();
       json.put("Fiedl1”,value1);
       json.put("Fiedl2",value2);
                              
       postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
       postMethod.setHeader( "Content-Type", "application/json" );
       response = httpClient.execute(postMethod,resonseHandler);
}
catch(Exception e)
{      e.printStackTrace();

}

How to Call Webservice in Android



import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
public class ListService {
               private static String METHOD_NAME = "GetList";
               private static String URL =Server.server + "/ServicesListService.asmx";
               private List<ListBean> Bens;
               public ListService() {
Bens = new Vector<ListBean>();
               }
               private List<ListBean> Beans;
               @Override
               public    List<ListBean> GetBean(PassBean bean)
               {              final String svalue = "PassBean";
                              SoapObject requestObject = new SoapObject(AppConst.NAMESPACE, METHOD_NAME);
                              requestObject.addProperty(svalue, bean);
                              final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                                            SoapEnvelope.VER11);
                              envelope.dotNet = true;
                              envelope.setOutputSoapObject(requestObject);
                              envelope.addMapping(AppConst.NAMESPACE,
                                                                           PassBean. PassBean_CLASS.getSimpleName(),
                                                            UserGetPassBean.UserGetPassBean_CLASS);
                              final Object response = new ServiceCaller().call(AppConst.SOAP_ACTION
                                                            + METHOD_NAME, envelope, URL);
                              if (response != null) {
                                             try {
                                                            if (response != null) {
                                                                           SoapObject classSoap = (SoapObject) response;
                                                                           int size = classSoap.getPropertyCount();
Beans = new ArrayList<ListBean>();
                                                                           for (int i = 0; i < size; i++) {
ListBean Bean = new ListBean(                                                                                                                                              (SoapObject) classSoap.getProperty(i));
                                                                         Beans.add(Bean);
                                                                                           }
                                                            }
                                             } catch (Exception e) {
                                                            e.printStackTrace();
                                             }
                                     }                            
                           return AnswerSheetBeans;
               }

}

Adapter For Listview in android


public class ListAdaptere extends BaseAdapter {
         private Context activity;
           private List<String> Beans;
           private static LayoutInflater inflater=null;
           public ImageLoader imageLoader;
           public String ListBn;
           

public ListAdaptere(Activity a,  List<String>Beans) {
               activity = a;
               this.Beans=Beans;
               inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               imageLoader=new ImageLoader(activity.getApplicationContext());
           }

           public int getCount() {
               return Beans.size();
           }

           public Object getItem(int position) {
               return position;
           }

           public long getItemId(int position) {
               return position;
           }
          
           public View getView(int position, View convertView, ViewGroup parent) {
               View vi=convertView;
               if(convertView==null)
                   vi = inflater.inflate(R.layout.list_row, null);

               TextView Name = (TextView)vi.findViewById(R.id.Name); // title
               TextView time = (TextView)vi.findViewById(R.id.marks);
               TextView marks = (TextView)vi.findViewById(R.id.time);
              
               //HashMap<String, String> song = new HashMap<String, String>();
               ListBn = Beans.get(position);
               JSONObject Anssht;
                    try {
                           Anssht = new JSONObject(ListBn);
                            JSONObject Detail = Anssht.getJSONObject("QuestionPaper");
                            String Name = Detail.getString("QuestionPaperName");
                            String TotalMrk = Detail.getString("TotalMarks");
                            String TotalTime = Detail.getString("TotalTime");
                            Name.setText( Name.toString());
                            time.setText("Total Time:  "+TotalTime.toString());
                            marks.setText( "Total Marks:  "+TotalMrk.toString());
                           
                    } catch (JSONException e) {
                          
                           e.printStackTrace();
                    }
             
                    return vi;
      }
}

==========================================================================
How To Use it From Activity Class


adapter = new PaperListAdapter(this, QuestionPaperListBeans);
list.setAdapter(adapter);