Pages

Tuesday, 14 April 2015

android list view row animation reference links

This is nice library.

just did minor changes in adapter like set duration etc and animation will work .
There are many animations like fade in ,fade out , wave, shake and many more.

https://github.com/karnshah8890/tech_andy/tree/master/ListviewAnimationDemo

Wednesday, 11 February 2015

Android animation must know points

This is specially for objection Animator.

- if you have such requirement that in animation one view visibility change to visible and same time other view's gone (For example view flipper) then set default (before animation start) visibility after animation start.

-sometimes I phase following problem . In animator xml if there is rotation and alpha. In xml if I wrote code for rotation and then alpha then alpha is not affecting even if  I have set no offset in alpha.

For ex :

Alpha Not working

 <!-- Rotate. -->
    <objectAnimator
        android:duration="@integer/card_flip_time_half"
        android:interpolator="@android:interpolator/linear"
        android:propertyName="rotationY"
        android:valueFrom="0"
        android:valueTo="90"
        android:startOffset="@integer/resize_time_delay" />

<objectAnimator
        android:duration="0"
        android:propertyName="alpha"
        android:valueFrom="0.0"
        android:valueTo="1.0"
         />


Alpha working


<objectAnimator
        android:duration="0"
        android:propertyName="alpha"
        android:valueFrom="0.0"
        android:valueTo="1.0"
         />

 <!-- Rotate. -->
    <objectAnimator
        android:duration="@integer/card_flip_time_half"
        android:interpolator="@android:interpolator/linear"
        android:propertyName="rotationY"
        android:valueFrom="0"
        android:valueTo="90"
        android:startOffset="@integer/resize_time_delay" />

Wednesday, 10 December 2014

android change view language of particular app programmatically

Use following code to change language

Locale myLocale = new Locale(lang);
Resources res = getBaseContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;

res.updateConfiguration(conf, dm);
getBaseContext().getResources().updateConfiguration(
    getBaseContext().getResources().getConfiguration(),
    getBaseContext().getResources().getDisplayMetrics());

Saturday, 18 October 2014

Android open keyboard on button click

You can open keyboard using following code

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
txt_credit.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);


replace txt_credit with your edit text's view 

place above code in button's on click

Wednesday, 17 September 2014

android call how to call api with file upload ?

use following sample code to call api.

Also check <uses-permission android:name="android.permission.INTERNET" /> permission in manifist file.


public static String getSoapResponseWithBaseURL(final Context context,
String baseUrl, ArrayList<NameValuePair> nameValuePairs)
throws ClientProtocolException, IOException {
try {
if (context != null && Utils.isOnline(context)) {

HttpPost httppost = new HttpPost(baseUrl);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
"UTF-8"));

HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient
.execute(httppost);
HttpEntity r_entity = httpResponse.getEntity();

String string = EntityUtils.toString(r_entity);
try {
// return string.toString();
return URLDecoder.decode(string.toString(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return string.toString();
}

} else {
if (context != null) {
Utils.showNoInternetError(context);
}
return "";
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

===============================
For file upload : use httpmime.jar and apache mime.jar

public static String getSoapResponseForFile(final Context context,
String postFixOfUrl, List<NameValuePair> nameValuePairs,
List<NameValuePair> filenameValuePairs,
final FileUploadListener fileUploadListener) {
String respString = "";

try {
if (context != null && Utils.isOnline(context)) {
String baseUrl = Utils.getBaseUrl(context);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(baseUrl + postFixOfUrl);

// MultipartEntity entity = new MultipartEntity();

totalSize = 0;
CustomMultiPartEntity entity = new CustomMultiPartEntity(
new MultipartProgressListener() {
@Override
public void transferred(long num) {

fileUploadListener
.onFileUpload((int) ((num / (float) totalSize) * 100));
if (num == totalSize) {
fileUploadListener.onFileUploadFinish();
}
}
});

for (int index = 0; index < filenameValuePairs.size(); index++) {
File myFile = new File(filenameValuePairs.get(index)
.getValue());
if (myFile.exists()) {
FileBody fileBody = new FileBody(myFile);
entity.addPart(filenameValuePairs.get(index).getName(),
fileBody);
}
}

totalSize = entity.getContentLength();

for (int index = 0; index < nameValuePairs.size(); index++) {

entity.addPart(nameValuePairs.get(index).getName(),
new StringBody(
nameValuePairs.get(index).getValue(),
Charset.forName("UTF-8")));

}

httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost,
localContext);
HttpEntity r_entity = response.getEntity();
return respString = EntityUtils.toString(r_entity);
} else {
fileUploadListener.onError(context
.getString(R.string.error_connection_network_message));
if (context != null) {
Utils.showNoInternetError(context);
}
}
} catch (IOException e) {
e.printStackTrace();
fileUploadListener.onError(context
.getString(R.string.error_upload_recordings));
}
return respString.toString();
}



Also custom multipart entity class code

package com.vishler.app.utils;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

public class CustomMultiPartEntity extends MultipartEntity {

private final MultipartProgressListener listener;

public CustomMultiPartEntity(final MultipartProgressListener listener) {
super();
this.listener = listener;
}

public CustomMultiPartEntity(final HttpMultipartMode mode,
final MultipartProgressListener listener) {
super(mode);
this.listener = listener;
}

public CustomMultiPartEntity(HttpMultipartMode mode, final String boundary,
final Charset charset, final MultipartProgressListener listener) {
super(mode, boundary, charset);
this.listener = listener;
}

@Override
public void writeTo(final OutputStream outstream) throws IOException {
super.writeTo(new CountingOutputStream(outstream, this.listener));
}

// public static interface ProgressListener {
// void transferred(long num);
// }

public static class CountingOutputStream extends FilterOutputStream {

private final MultipartProgressListener listener;
private long transferred;

public CountingOutputStream(final OutputStream out,
final MultipartProgressListener listener) {
super(out);
this.listener = listener;
this.transferred = 0;
}

// public void write(byte[] b, int off, int len) throws IOException {
// out.write(b, off, len);
// this.transferred += len;
// this.listener.transferred(this.transferred);
// }

@Override
public void write(byte[] b, int off, int len) throws IOException {
int BUFFER_SIZE = 10000;
int chunkSize;
int currentOffset = 0;

while (len > currentOffset) {
chunkSize = len - currentOffset;
if (chunkSize > BUFFER_SIZE) {
chunkSize = BUFFER_SIZE;
}
out.write(b, currentOffset, chunkSize);
currentOffset += chunkSize;
this.transferred += chunkSize;
// Log.i("CustomOutputStream WRITE","" + off + "|" + len + "|" +
// len + "|" + currentOffset + "|" + chunkSize + "|" +
// this.transferred);
this.listener.transferred(this.transferred);
}
}

@Override
public void write(int b) throws IOException {
out.write(b);
this.transferred++;
this.listener.transferred(this.transferred);
}
}
}