Thursday, July 5, 2012

Retrieve database from mysql and displaying using dynamic table in android




Click here to download this program 


I Installed WAMP server on my local machine for excuting PHP programs.

I have programmed in windows 7.

If You need WAMP server, Click here to download.

Before run this program, start wampserver

programs --> wampserver --> start wampserver

see right side below of taskbar WAMPSERVER -server offline icon present.









This is the wampserver icon







 Left click icon --> start all services













package name: selva.select

project name: Dbselect

version :  1.5 ( support 1.5 and above versions)




main.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <Button
        android:id="@+id/button1"
        android:layout_below="@+id/e1"
        android:layout_width="276dp"
        android:layout_height="wrap_content"
        android:text="Button" />

   
 <ScrollView
         android:layout_below="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusableInTouchMode="true"
        android:focusable="true" >

   <HorizontalScrollView
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:focusableInTouchMode="true"
          android:focusable="true">
    <TableLayout 

    android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusableInTouchMode="true"
    android:focusable="true">
           
            
</TableLayout>

    </HorizontalScrollView>
     
   </ScrollView>

</RelativeLayout>


AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="selva.select"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />
      <uses-permission android:name="android.permission.INTERNET"/>
     
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".DbselectActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>





DbselectActivity.java




import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class DbselectActivity extends Activity
{
  
  
     public void onCreate(Bundle savedInstanceState)
    
     {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
          
            Button button = (Button) findViewById(R.id.button1);
          
          
            button.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View view)
              {
                 String result = null;
                InputStream is = null;
              
                try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://10.0.2.2/selectall.php");
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();

                        Log.e("log_tag", "connection success ");
                     //   Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
                }
                catch(Exception e)
                {
                        Log.e("log_tag", "Error in http connection "+e.toString());
                        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

                }
                //convert response to string
                try
                {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null)
                        {
                                sb.append(line + "\n");
                              //  Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
                        }
                        is.close();

                        result=sb.toString();
                }
                catch(Exception e)
                {
                       Log.e("log_tag", "Error converting result "+e.toString());
                    Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();

                }

                //parse json data
                try
                {
          
                JSONArray jArray = new JSONArray(result);
              
     
                String re=jArray.getString(jArray.length()-1);
              
  
                TableLayout tv=(TableLayout) findViewById(R.id.table);
                tv.removeAllViewsInLayout();
              
             
              
              
                   int flag=1;
                
                for(int i=-1;i<jArray.length()-1;i++)
                      
                        {
                            
                            
                            
                            
                                TableRow tr=new TableRow(DbselectActivity.this);
                              
                                tr.setLayoutParams(new LayoutParams(
                                           LayoutParams.FILL_PARENT,
                                           LayoutParams.WRAP_CONTENT));
                              
                              
                              
                           
                                if(flag==1)
                                {
                                  
                                    TextView b6=new TextView(DbselectActivity.this);
                                     b6.setText("ID");
                                     b6.setTextColor(Color.BLUE);
                                     b6.setTextSize(15);
                                     tr.addView(b6);
                              
                                  
                                    TextView b19=new TextView(DbselectActivity.this);
                                     b19.setPadding(10, 0, 0, 0);
                                     b19.setTextSize(15);
                                     b19.setText("Name");
                                     b19.setTextColor(Color.BLUE);
                                     tr.addView(b19);
                                   
                                   TextView b29=new TextView(DbselectActivity.this);
                                 b29.setPadding(10, 0, 0, 0);
                                     b29.setText("no");
                                     b29.setTextColor(Color.BLUE);
                                     b29.setTextSize(15);
                                     tr.addView(b29);
                           
                                   
                                 tv.addView(tr);
                              
                                     final View vline = new View(DbselectActivity.this);
                                          vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
                                          vline.setBackgroundColor(Color.BLUE);
                                       
                              
                              
                                tv.addView(vline);
                                flag=0;
                                  
                                  
                                }
                  
                                else
                                {
                                  
                              
                                  
                                    JSONObject json_data = jArray.getJSONObject(i);
                                  
                                    Log.i("log_tag","id: "+json_data.getInt("f1")+
                                              ", Username: "+json_data.getString("f2")+
                                              ", No: "+json_data.getInt("f3"));
                           
                              
                              
                          
                            TextView b=new TextView(DbselectActivity.this);
                                String stime=String.valueOf(json_data.getInt("f1"));
                                  b.setText(stime);
                                b.setTextColor(Color.RED);
                                b.setTextSize(15);
                                tr.addView(b);
                         
                             
                               TextView b1=new TextView(DbselectActivity.this);
                                b1.setPadding(10, 0, 0, 0);
                                b1.setTextSize(15);
                                String stime1=json_data.getString("f2");
                                 b1.setText(stime1);
                                b1.setTextColor(Color.WHITE);
                                tr.addView(b1);
                              
                              TextView b2=new TextView(DbselectActivity.this);
                             b2.setPadding(10, 0, 0, 0);
                                String stime2=String.valueOf(json_data.getInt("f3"));
                                b2.setText(stime2);
                                b2.setTextColor(Color.RED);
                                b2.setTextSize(15);
                                tr.addView(b2);
                      
                                  tv.addView(tr);
                          
                          
                        final View vline1 = new View(DbselectActivity.this);
                      vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                      vline1.setBackgroundColor(Color.WHITE);
                      tv.addView(vline1);      
                      
                         
                                }
                             
                       }
      
  
              
                }
                catch(JSONException e)
                {
                        Log.e("log_tag", "Error parsing data "+e.toString());
                        Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                }

              

                
           }
           });
          
          


     }
  

}




database name: ex1

Table name: t1

Fields Name: f1(bigint)  ,f2(text)  ,f3(bigint)



 selectall.php



    <?php

         $con = mysql_connect("localhost","root","");
         if (!$con)
               {
                         die('Could not connect: ' . mysql_error());
                }

           mysql_select_db("ex1", $con);

           
           $i=mysql_query("select * from t1",$con);

           $num_rows = mysql_num_rows($i);
        

           $check='';
          while($row = mysql_fetch_array($i))
            {
 
                  $r[]=$row;
                  $check=$row['f1']             }

         if($check==NULL)
           {           
                      $r[$num_rows]="Record is not available";
                      print(json_encode($r));
                
             }
            else
             {
                $r[$num_rows]="success";
                 print(json_encode($r));
         
              }

 mysql_close($con);
              
    ?>
 

OUTPUT:





Click button






Click here to download this program.



Display string in TextView






Program To display number, string, long number, float and double in TextView






 package name: selva.text

project name: Textview

Android version: 1.5 (support 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
   
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#00FF00"
        android:text="This is TextView" />

      <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>
    
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Large Text"
        android:textColor="#FFD700"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Medium Text"
        android:textColor="#8B0000"
        android:textAppearance="?android:attr/textAppearanceMedium" />
     <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Small Text"
        android:textColor="#ffffff"
        android:textAppearance="?android:attr/textAppearanceSmall" />
 <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>



 <Button
     android:id="@+id/button1"
     android:layout_width="106dp"
     android:layout_height="wrap_content"
     android:text="View String" />

</
LinearLayout>








TextviewActivity
.java



package selva.text;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextviewActivity extends Activity
{
    /** Called when the activity is first created. */
   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
        final TextView textview=(TextView) findViewById(R.id.textView1);
        final TextView ltextview=(TextView) findViewById(R.id.textView2);
        final TextView mtextview=(TextView) findViewById(R.id.textView3);
        final TextView stextview=(TextView) findViewById(R.id.textView4); 
        
        Button viewstring=(Button) findViewById(R.id.button1);
        
     viewstring.setOnClickListener(new View.OnClickListener()
         {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String st="Textview String";
   textview.setText(st);
   
   String st1="Large Textview String";
   ltextview.setText(st1);
   
   String st2="Medium Textview String";
   mtextview.setText(st2);
   
   String st3="Small Textview String";
   ltextview.setText(st3);  
}
});
}
}



OUTPUT:




































click View String button. 









































Program To display number, string, long number, float and double in TextView

Display Integer in TextView





Program To display number, string, long number, float and double in TextView







package name: selva.text

project name: Textview

Android version: 1.5 (support 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
   
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#00FF00"
        android:text="This is TextView" />

      <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>
    
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Large Text"
        android:textColor="#FFD700"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
     <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Medium Text"
        android:textColor="#8B0000"
        android:textAppearance="?android:attr/textAppearanceMedium" />
     <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Small Text"
        android:textColor="#ffffff"
        android:textAppearance="?android:attr/textAppearanceSmall" />
 <View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF4500"/>



 <Button
     android:id="@+id/button1"
     android:layout_width="106dp"
     android:layout_height="wrap_content"
     android:text="View number" />

</
LinearLayout>








TextviewActivity.java



package selva.text;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextviewActivity extends Activity
{
    /** Called when the activity is first created. */
   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
        final TextView textview=(TextView) findViewById(R.id.textView1);
        final TextView ltextview=(TextView) findViewById(R.id.textView2);
        final TextView mtextview=(TextView) findViewById(R.id.textView3);
        final TextView stextview=(TextView) findViewById(R.id.textView4); 
        
   Button viewno=(Button) findViewById(R.id.button2);

        
viewno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int i,i1,i2,i3;
i=111;
String st=String.valueOf(i);
   textview.setText(st);
   
   i1=222;
String st1=String.valueOf(i1);
   ltextview.setText(st1);
   
   i2=333;
String st2=String.valueOf(i2);
   mtextview.setText(st2);
   
   i3=444;
String st3=String.valueOf(i3);
   stextview.setText(st3);  
}
});
        }
}


OUTPUT:



















































Click View  number button










































Program To display number, string, long number, float and double in TextView
click here