Saturday, 16 April 2016

How to Compress Drawable image using thirtparty website?.

Compress image size is very important things. because drawable image increase the image size so we have to reduce the image size.


the below website reduce the image size

https://tinypng.com/

Wednesday, 9 March 2016

Image Download using asynchronous task

hi guys image download using asynchronous very easy. The below coding very useful to all


AsyncTaskRunnerObject runner = new AsyncTaskRunnerObject(this);
runner.execute(Partnerdownloadurl,userid);//update


Partnerdownloadurl = Download url (passing parameter )
userid = userid is the login member id



class AsyncTaskRunnerObject extends AsyncTask<String,String,String> {
    private String resp,Memberid,AsyType,OBJID;
    private Context myCtx;
    public AsyncTaskRunnerObject(Context ctx){
        this.myCtx = ctx;
    }
    @Override    protected void onPreExecute() {
        Log.e("Pre Execute","Pre Execute");
    }
    @Override    protected void onPostExecute(String s) {
        Log.e("Post Execute",""+s);
        StorePreference.SinglewritePreference(myCtx, "LOCAL_FEMALE_PROFILE_IMAGE", s);
    }
    @Override    protected String doInBackground(String... params) {
        HttpURLConnection urlConnection = null;
        try {
            URL uri = new URL(params[0]);
            Memberid = params[1];
            urlConnection = (HttpURLConnection) uri.openConnection();
            urlConnection.setRequestMethod("GET");
            int statusCode = urlConnection.getResponseCode();
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }
            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                File storageDir;
                String mediatype = null;
                String audiofmt = null;
                mediatype = "Profile";
                audiofmt = ".jpg";
                if (Environment.getExternalStorageState() == null || Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
                    storageDir = new File(Environment.getDataDirectory(), mediatype);
                } else {
                    storageDir = new File(Environment.getExternalStorageDirectory() + Common.STORAGE_PATH, mediatype);
                }
                if (!storageDir.exists() && !storageDir.mkdirs()) {
                    // For HTC Device                    storageDir = new File(Common.STORAGE_PATH, mediatype);
                    if (!storageDir.exists() && !storageDir.mkdirs()) {
                    }
                }
                File image = File.createTempFile(
                        Memberid,  /* prefix */                        audiofmt,         /* suffix */                        storageDir      /* directory */                );
                FileOutputStream f = new FileOutputStream(image);
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = inputStream.read(buffer)) > 0) {
                    f.write(buffer, 0, len1);
                }
                f.close();
                resp=image.getAbsolutePath();                
               
                return resp;
            }
        } catch (Exception e) {
            Log.d("URLCONNECTIONERROR", e.toString());
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return resp;
    }
}




 

Thursday, 25 February 2016

Deleting files from a folder - android

Very simple delete the file from folder for particular format

File dir = new File(Environment.getExternalStorageDirectory() + "/Pictures/.Twogether/Videos");
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                if (new File(dir, children[i]).getAbsolutePath().endsWith("3gp2")) {
                    new File(dir, children[i]).delete();
                }
            }
        }

Thursday, 19 November 2015

How to create custom font for Text View

Hi guys i am going to explain custom font for TextView

First We have create assets directory inside the main directory and then create fonts directory inside the assets main->assets->font->  then put the font file inside the font directory

Then create the below file inside any one package. The below file created in com.crm.common package

package com.crm.Common;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

/** * Created by Ashok on 11/19/2015. */public class CustomReqularTextView extends TextView {
    public static Typeface FONT_NAME;


    public CustomReqularTextView(Context context) {
        super(context);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
    public CustomReqularTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
    public CustomReqularTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
}


Then We have to use like

<com.crm.Common.CustomBoldTextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/compid"    android:text="@string/compname"    android:layout_gravity="center_horizontal"/>

Wednesday, 18 November 2015

Download file fast and then store SDCARD

Hi guys i am going to explain download file from server and then store in sdcard. it's very simple refer the below code.


package com.readcontact;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;


public class Downloadimage extends ActionBarActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloadimage);

        new Thread(new Runnable() {
            @Override            public void run() {
                downloadFile("http://www.tivix.com/uploads/blog_pics/Android-logo.png", new File(Environment.getExternalStorageDirectory() + File.separator                        + "test.png"));
            }
        }).start();
    }

    private static void downloadFile(String url, File outputFile) {
        Log.i("ACT", "Path : " + outputFile.getAbsolutePath());
        Log.w("ASHOK MSG","Path : " + outputFile.getAbsolutePath());
        try {
            URL u = new URL(url);
            URLConnection conn = u.openConnection();
            int contentLength = conn.getContentLength();

            DataInputStream stream = new DataInputStream(u.openStream());

            byte[] buffer = new byte[contentLength];
            stream.readFully(buffer);
            stream.close();

            DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
            fos.write(buffer);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            return; // swallow a 404        } catch (IOException e) {
            return; // swallow a 404        }
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_downloadimage, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();

        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Get all phone contacts using android

hi guys i going to explain get mobile contacts from phone
 it's very simple


Get Name
cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

Get Phone number 
cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

Get Phone email

cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));


First We have to add below  permission in Adroidmanifast.xml
<uses-permission android:name="android.permission.READ_CONTACTS" />

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context="com.readcontact.Contactlist"    android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/sendmail"        android:text="Send Me"        android:layout_gravity="left"/>

<ListView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/contlist">
</ListView>

</LinearLayout>



mainactivity.java
package com.readcontact;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;


public class Contactlist extends ActionBarActivity {
    ListView Conlist;
    private ArrayList<String> arrayList = new ArrayList<>();
    private  ArrayList<String> namelist = new ArrayList<>();
    Button SendMe;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contactlist);

        Conlist = (ListView)findViewById(R.id.contlist);
        SendMe = (Button)findViewById(R.id.sendmail);


        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null, null, null);

        if (cur.getCount()>0){
            while(cur.moveToNext()){
                String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                arrayList.add(name);
            }
        }
        Conlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
       ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,arrayList);
        Conlist.setAdapter(arrayAdapter);

        SendMe.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                SparseBooleanArray checkd = Conlist.getCheckedItemPositions();

                for (int i = 0; i < Conlist.getCount(); i++) {
                    if (checkd.get(i)) {
                        namelist.add(arrayList.get(i).toString());
                    }
                }

                String mystring = namelist.toString();
                Toast.makeText(getApplicationContext(), mystring, Toast.LENGTH_LONG).show();
                namelist.clear();
            }
        });

        
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_contactlist, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();

        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


for more details
http://www.coderzheaven.com/2011/06/13/get-all-details-from-contacts-in-android/




Thursday, 5 November 2015

Call Java function from JavaScript over Android WebView

Hi guys Here I am going to explain how to call a java function from webview

          
Step 1)MainActivity .java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity MyActivity = this;
webView= (WebView) findViewById(R.id.webView);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {

MyActivity .setTitle("Loading...");
MyActivity .setProgress(progress * 100);

if (progress == 100)
MyActivity .setTitle("Android Dhina");
}
});
webView.setWebViewClient(new WebViewClient());
webView.addJavascriptInterface(new WebAppInterface(this), "Android");

webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/web.html");

}

public class WebAppInterface {

Context context;

WebAppInterface(Context c) {
context= c;
}

public void showToast(String toast) {
Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
}

}

}

Step 2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>


Step 3)assets/web.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>Android Dhina</title>

<h1>Android Dhina Webview</h1>
</head>
<body>
<input type="button" value="Call Function"
onClick="showAndroidToast('Hello Dhina!')" />

<script language="javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
 }
</script>

</body>
</html>