Configure gson in Spring using GsonHttpMessageConverter [deprecated]

WebMvcConfigurerAdapter is deprecated. As of Spring 5.0 do this, so this is not working any more and we should use Force Spring Boot to use Gson instead of Jackson

Excluding jackson from classpath

@SpringBootApplication
@EnableAutoConfiguration(exclude = { JacksonAutoConfiguration.class })
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Using java config

@Configuration
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter < ? >> converters) {
        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
        converters.add(gsonHttpMessageConverter);
    }
}

References
https://www.leveluplunch.com/java/tutorials/023-configure-integrate-gson-spring-boot/

OkHttp Post Body as JSON

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

References
https://stackoverflow.com/questions/34179922/okhttp-post-body-as-json

Detect screen rotation on Android

Manifest:

<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>

Activity:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    Log.d("tag", "config changed");
    super.onConfigurationChanged(newConfig);

    int orientation = newConfig.orientation;
    if (orientation == Configuration.ORIENTATION_PORTRAIT)
        Log.d("tag", "Portrait");
    else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        Log.d("tag", "Landscape");
    else
        Log.w("tag", "other: " + orientation);

    ....
}

References
https://stackoverflow.com/questions/6896243/how-can-i-detect-screen-rotation

Show Progress bar while loading image using Glide

Glide.with(this)
            .load("https://raw.githubusercontent.com/bumptech/glide/master/static/glide_logo.png")
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false;
                }
            })
            .into(imageView);

 

References
https://stackoverflow.com/questions/35305875/progress-bar-while-loading-image-using-glide

Detect if App is in release or debug mode in android

Make sure you are referencing your project’s BuildConfig class, not from any of your dependency libraries.

if (BuildConfig.DEBUG) {
  // do something for a debug build
}
release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable false
        }
        debug {
            debuggable true
        }

References
https://stackoverflow.com/questions/23844667/detect-if-i-am-in-release-or-debug-mode-in-android
http://tekeye.uk/android/examples/android-debug-vs-release-build