Android

To remove actionbar shadow

 

device-2016-06-08-152116

First try this

<style name="CustomAppTheme" parent="AppTheme">
    <item name="android:windowContentOverlay">@null</item>
</style>

If above solution does not work then do the following in the activity:

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setElevation(0);
}

 

By Ankit on June 8, 2016 | Android | A comment?

Android store object into shared preference

First you need to add GSON dependency (ref)

compile 'com.google.code.gson:gson:2.2.4'

Writing object to shared preference

SharedPreferences sharedPref =  getApplicationContext().getSharedPreferences("com.example.myapp.PREFERENCE_FILE_KEY",Context.MODE_PRIVATE);
	
SharedPreferences.Editor editor = sharedPref.edit();

Gson gson = new Gson();

String jsonMyObject = gson.toJson(myObject);

editor.putInt("myObject", jsonMyObject );

editor.commit()

Reading Object from shared preference

SharedPreferences sharedPref =  getApplicationContext().getSharedPreferences("com.example.myapp.PREFERENCE_FILE_KEY",Context.MODE_PRIVATE);
	
Gson gson = new Gson();

String jsonMyObject= mSharedPreferences.getString("myObject", "");

myObject =  gson.fromJson(jsonMyObject, MyObject.class);

 

Saving List of objects in to shared preference

ArrayList<userModel> userList= new ArrayList<>();

userList.add(new userModel("Jos", "Kuriya"));
userList.add(new userModel("Kiran", "Pnr"));


Gson gson = new GsonBuilder().create();
JsonArray userJsonArray = gson.toJsonTree(userList).getAsJsonArray();


//Adding data to shared preference 
SharedPreferences.Editor editor = mSharedPref.edit();
editor.putString("MyUsers", userJsonArray + "");
editor.commit();



//Retrieving data from shared preference
String jsonPreferences = mSharedPref.getString("MyUsers", "");
Type type = new TypeToken<ArrayList<userModel>>() {}.getType();
ArrayList<userModel> dataFromSharedPref = gson.fromJson(jsonPreferences, type);

 

By bm on June 7, 2016 | Android | A comment?

Android Google Drive API Deleted folder still exists in query

Google drive api does not sync immediately, That is why the deleted folders are still showing, so you have to force google drive to sync using requestSync()

A sample snippet

    private MetadataBuffer getGoogleFolders(){

        Drive.DriveApi.requestSync(mGoogleApiClient).await(); //Asynchronous request 

        CustomPropertyKey customPropertyKey = new CustomPropertyKey("App", CustomPropertyKey.PRIVATE);

        Query query = new Query.Builder().addFilter(Filters.and(
                Filters.eq(SearchableField.TITLE, "My folder"),
                Filters.eq(SearchableField.TRASHED, false),
                Filters.eq(customPropertyKey, "sampleApp"))).build();

        DriveApi.MetadataBufferResult foldersResult = Drive.DriveApi.query(mGoogleApiClient, query).await();

        if (!foldersResult.getStatus().isSuccess()) {
            Log.i(TAG,"Problem while retrieving results");
            return null;
        }

        MetadataBuffer result = foldersResult.getMetadataBuffer();

        Log.i(TAG, "total result " +  result.getCount());
        
        return result;
    }

 

OR

//Synchronous request 
Drive.DriveApi.requestSync(mClient).setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(@NonNull Status status) {

                //search query performs here 

            }
        });

 

By bm on | Android | A comment?

Android create folder inside layout

1. Open your project explorer (this folder structure will not visible in Android explorer)
2. Create a new directory inside res folder name this new directory “layouts”. (This can be whatever you want)
3. create directory tree inside layouts folder (in my example i have created gallery inside layout and values, home ->layout and values, payment ->layout)
4. move your xml files into desired directory (inside layout folder) and delete the old layout folder

layout

5. Add the following code in gradle file (app/build.gradle)

android {
    - - - - - - - - -
    - - - - - - - - -
    - - - - - - - - -

    sourceSets {
        main {
            res.srcDirs =
                    [
                            'src/main/res',
                            'src/main/res/layouts',
                            'src/main/res/layouts/gallery',
                            'src/main/res/layouts/home',
                            'src/main/res/layouts/paymet'
                    ]
        }
    }
 - - - - - - - - 

 - - - - - - - - 
}

 

By bm on | Android | A comment?

Android Intent Service

Intent Service Class sample

public class MyIntentService extends IntentService {

    public static final String Somevariables = "somevalue";

    public MyIntentService() {

        super(MyIntentService.class.getName()); // you can give any string but it is good practice to giving classname
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //--some code --
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        
        //you can retrive intent extras
        String msg = intent.getStringExtra(EXTRA_KEY);

        //--actual backgroung code gos here  --
    }
}

Add service in manifest also

<service android:name=".MyIntentService"/>

ref: https://developer.android.com/training/run-background-service/create-service.html

By bm on June 3, 2016 | Android | A comment?
Tags: ,

Android Shared Preferences

Getting application specific shared preference object

SharedPreferences sharedPref =  getApplicationContext().getSharedPreferences("com.example.myapp.PREFERENCE_FILE_KEY",Context.MODE_PRIVATE);

Getting activity specific shared preference object

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

Write to Shared Preferences

SharedPreferences.Editor editor = sharedPref.edit();
//editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.putInt("HIGHSCORE", newHighScore);
editor.commit();

Read from Shared Preferences

//int highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

int highScore = sharedPref.getInt("HIGHSCORE", defaultValue);

 

ref: https://developer.android.com/training/basics/data-storage/shared-preferences.html#WriteSharedPreference

By bm on | Android | A comment?