Wednesday 29 May 2013

ProgressDialogExample

1)ProcessDialog.java
===============
package com.ovte.appsolution;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.Window;
import android.widget.TextView;

public class ProcessDialog extends Dialog{
     private TextView title;           
     public ProcessDialog(Context context,String strtitle,String strbody)
     {
         super(context);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.customdialog);
         setCancelable(false);
         setCanceledOnTouchOutside(false);
         //setTitle(strtitle.toString());
         title = (TextView)findViewById(R.id.txtdialogtext);
         title.setText(Html.fromHtml(strbody.toString()));
         show();
     }
}
2)customdialog.xml
============
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:gravity="center"
  android:layout_height="wrap_content">
  <ProgressBar android:id="@+id/progressBar1"
   android:layout_gravity="center"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   </ProgressBar>
  <TextView
    android:id="@+id/txtdialogtext"
    android:text="Loging ..."
    android:layout_marginLeft="15dip"
    android:layout_gravity="center"
    android:textSize="18dip"
    android:textColor="#FFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>
3)GetInvestData.java
===========
package com.ovte.appsolution;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Toast;

public class InvestActivity extends Activity{
    private WebView mWebViewInvest = null;
    private ImageView imgInvest = null;
    private String strDiscription = null,StrImageLink=null,strImage=null;
    private MemoryCache mMemoryCache=null;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xinvest);
      
        mMemoryCache=new MemoryCache();      
        imgInvest = (ImageView)findViewById(R.id.imgInvest);      
        mWebViewInvest = (WebView)findViewById(R.id.webview_Invest);
        mWebViewInvest.getSettings().setJavaScriptEnabled(true);
        mWebViewInvest.getSettings().setSupportZoom(true);
        mWebViewInvest.setBackgroundColor(Color.TRANSPARENT);
      
        if(GlobalClass.CheckNetwork(this)){
            new GetInvestData().execute();
        }
    }
   
    private final class GetInvestData extends AsyncTask<String,JSONObject,JSONObject>{
        private ProcessDialog mProcessDialog = null;
        @Override
        protected void onPreExecute() {
            mProcessDialog = new ProcessDialog(InvestActivity.this, "Invest", Constant.LOADING);
            super.onPreExecute();
          
        }
        @Override
        protected JSONObject doInBackground(String... params) {

            if(GlobalClass.CheckNetworkNoMessage(InvestActivity.this)){
                String strURL = getResources().getString(R.string.Invest_URL);
                try {
                    return new JSONObject(GlobalClass.GETHTTPConnection(strURL));
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
            else{
                return null;
            }
        }  
        @Override
        protected void onPostExecute(JSONObject mJsonObject) {
            mProcessDialog.dismiss();
            super.onPostExecute(mJsonObject);
            if(mJsonObject!=null){
                try {
                    System.out.println("Invest Response == " +mJsonObject);
                    StrImageLink=mJsonObject.getString("download_link");
                    JSONArray subJArray = mJsonObject.getJSONArray("Data_array");  
                    for(int i=0; i<subJArray.length();i++){
                        JSONObject JInvestDescription = subJArray.getJSONObject(i);
                        strDiscription = JInvestDescription.getString("description");
                        strImage = JInvestDescription.getString("image_url");
                        System.out.println("Description Invest === "+strDiscription);
                    }
                    WebSettings settings = mWebViewInvest.getSettings();
                    settings.setDefaultTextEncodingName("utf-8");
                    strDiscription = "<font color = '#FFFFFF'>"+strDiscription+"</font>";
                    String Discription = webViewLoadData(strDiscription);
                    mWebViewInvest.loadData(Discription, "text/html", "utf-8");
                    System.out.println("ImageLink Invest=== "+(StrImageLink+strImage));                
                    URL=StrImageLink + strImage;
                    mMemoryCache.put(String.valueOf(URL.hashCode()),GlobalClass.ImageShrink(GlobalClass.GETHTTPConnectionBitmap((StrImageLink + strImage),getApplicationContext()), Constant.SCREEN_WIDTH, Constant.SCREEN_WIDTH));
//                    mMemoryCache.put(String.valueOf(URL.hashCode()),BitmapImageUtil.loadFrom((StrImageLink + strImage), InvestActivity.this, R.drawable.imgdefaultbigimage));
                    imgInvest.setImageBitmap(mMemoryCache.get(String.valueOf(URL.hashCode())));
          
                } catch (JSONException e) {                          
                        e.printStackTrace();
                }  
            }    
            else{
                Toast.makeText(InvestActivity.this,Constant.NETWORK_NOT_AVAILABLE,Toast.LENGTH_SHORT).show();
            }
        }      
    }
   
    private String URL=null;
    @Override
    protected void onDestroy() {
      
        if(mMemoryCache!=null && URL!=null){
            if(mMemoryCache.get(String.valueOf(URL.hashCode()))!=null){
                if(!mMemoryCache.get(String.valueOf(URL.hashCode())).isRecycled()){
                    mMemoryCache.get(String.valueOf(URL.hashCode())).recycle();
                }
            }
        }
        mMemoryCache.clear();
        System.gc();
        Log.i("App Solution","Invest Destroy");
        super.onDestroy();
    }
   
//*******************************************************************************************************************************
//This function is used for get text
   
    public static String webViewLoadData(String strDiscription) {
        StringBuilder buf = new StringBuilder(strDiscription.length());
        for (char c : strDiscription.toCharArray()) {
                switch (c) {
                case 'ä':
                        buf.append("&#228;");
                        break;
                case 'ö':
                        buf.append("&ouml;");
                        break;
                case 'À':
                        buf.append(" ");
                        break;
                case 'Á':
                        buf.append(" ");
                        break;
                case 'Ü':
                        buf.append("&#220;");
                        break;
  
                case '´':
                        buf.append("&#180;");
                        break;
                case 'è':
                        buf.append("&#232;");
                        break;
                case 'é':
                        buf.append("&#233;");
                        break;
                case 'Ö':
                        buf.append("&#214;");
                        break;
                case '#':
                        buf.append("%23");
                        break;
                case '%':
                        buf.append("%25");
                        break;
                case '\'':
                        buf.append("%27");
                        break;
                case '?':
                        buf.append("%3f");
                        break;
                case 'ü':
                        buf.append("&#252;");
                        break;
                case '¤':
                        buf.append("&#226;");
                        break;
                case '€':
                        buf.append("&#128;");
                        break;
                case '–':
                        buf.append("&#150;");
                        break;                   
                default:
                        buf.append(c);
                        break;
                }
            }
            String strTemp = buf.toString();
            System.out.println(" Decode :"+strTemp);
            return strTemp;
    }
}
4)string.xml
    <string name = "Invest_URL">http://app-solution.ch/webservices/appsolution/getmedia.php?action=invest</string>


No comments:

Post a Comment