Wednesday, 22 March 2017

Utilmethod to convert SP to PX

private static float sDensity = Float.MAX_VALUE;
private static float sDensity4Fonts = Float.MAX_VALUE;


public static int convertSpToPx(int spSize) {
    return Math.round((float) spSize / getDisplayDensity4Fonts());
}

private static DisplayMetrics getDisplayMetrics() {
    Display display = ((WindowManager) API.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics;
}

private static float getDisplayDensity4Fonts() {
    synchronized (Helper.class) {
        if (sDensity4Fonts == Float.MAX_VALUE) {
            sDensity4Fonts = getDisplayMetrics().density * getFontScale();
        }
        return sDensity4Fonts;
    }
}

Thursday, 16 March 2017

Android Volley

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url, null,
                new Response.Listener<JSONObject>() {

                    @Override                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                        listener.notifyResponse(response.toString(), 200);

                    }
                }, new Response.ErrorListener() {

            @Override            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());

            }
        }) {

            /**             * Passing some request headers             * */            @Override            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json");
                headers.put("apiKey", "xxxxxxxxxxxxxxx");
                headers.put("Cookie", sam);
                return headers;
            }

        };

// Adding request to request queue        App.getInstance().addToRequestQueue(jsonObjReq);


    }


UseFul Links



Wednesday, 15 March 2017

Android Espresso

Build.gradle


//In default config
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

//In dependencies
androidTestCompile 'com.android.support.test:runner:0.5'androidTestCompile 'com.android.support.test:rules:0.5'
//Espresso coreandroidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude group: 'com.android.support', module: 'appcompat' exclude group: 'com.android.support', module: 'support-v4' exclude module: 'support-annotations' exclude module: 'recyclerview-v7'}

Sample Example of UI test using espresso


import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;



import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withHint;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

public class ActivityTest {
Context appContext;
@Rule
public ActivityTestRule<Activity> mActivityTestRule =
new ActivityTestRule<Activity>(Activity.class);
@Before
public void setUP() {
appContext = InstrumentationRegistry.getTargetContext();
}

@Test
public void firstTest() {
// Context of the app under test.

String toEmail = "sonuash@gmail.com";
String toTextView = appContext.getResources().getString(R.string.sample);
String toSubView = appContext.getResources().getString(R.string.samplestr);


//Type text in edittext through espresso
onView(ViewMatchers.withId(R.id.toAddrFieldView)).perform(typeText(toEmail));
//match text displayed in textview
onView(withId(R.id.toAddrTextView)).check(matches(allOf(withText(toTextView), isDisplayed())));
//match hint displayed in edittes
onView(withId(R.id.subjectFieldView)).check(matches(withHint(toSubView)));
//click on any button / edittext
onView(ViewMatchers.withId(R.id.toolbar)).perform(click());

}
}

Wednesday, 22 February 2017

libcurl.so duplicate spicified JNI

Put this in build.xml

packagingOptions {

        pickFirst 'lib/armeabi-v7a/libcurl.so'
        pickFirst 'lib/armeabi-v7a/libssl.so'
        pickFirst 'lib/x86/libcurl.so'

    }

Caused by java.lang.unsupportedoperationexception failed to resolve attribute at index 13

I faced this issue while implementing custom image button , here in xml following attribute is not supported



android:background="?attr/xxx"


Friday, 19 August 2016

FireBase Data Storage and Retrival .Real Time Update of Recycler View

Declare FireBase dependencies in Gradle Files and do necessary steps for FireBase Setup.

I'm gonna write only the coding part.

//Declare this in oncreate of your activity
Firebase.setAndroidContext(this);

//Get a FireBase Reference Change URL a/c to your Firebase project(Check it in Firebase console)
Firebase ref = new Firebase("https://birthday-2599.firebaseio.com/");



//Onclick of the button save data to FireBase

Person person = new Person();

String name = nameEd.getText().toString().trim();
String phNo = number.getText().toString().trim();
String dob = tv.getText().toString().trim();
//Adding valuesif(!(name.equals("") || phNo.equals("") || dob.equals(""))) {
    person.setName(name);
    person.setPhNo(Integer.parseInt(phNo));
    person.setDob(dob);
    FirebaseHelper.save(person);
    finish();
}




//Save method

//SAVEpublic static Boolean save(Person person) {
    if (person == null) {
        saved = false;
    } else {

        ref.push().setValue(person);

        saved = true;

    }
    return saved;
}




//Retrive
//Set valuechange event listener and update data in RecyclerView


ref.addValueEventListener(new ValueEventListener() {

    @Override    public void onDataChange(DataSnapshot snapshot) {
//Clear array which your are passing in the adapter
        displayArray.clear();
        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
            //Getting the data from snapshot            Person person = postSnapshot.getValue(Person.class);

            //Adding it to a string            String string = person.toString();


            displayArray.add(string);
            RecyclerView.Adapter adapter = new DataAdapter(displayArray);
            recyclerView.setAdapter(adapter);


        }
    }

    @Override    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }


});


ProjectFiles


Wednesday, 13 July 2016

Cannot load modern controls UI. Upgrade to the latest version of the Android YouTube API (FIX)


YouTube Video you have uploaded not playing in app using YouTubeAPI android

One of the reason I found is while uploading video to youtube do not share it private..

If you do you get the warning as cannot load...and video will not be played

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...