Display Back Arrow on Toolbar

If you are using an ActionBarActivity then you can tell Android to use the Toolbar as the ActionBar like so: Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar); And then calls to getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); will work. You can also use that in Fragments that are attached to ActionBarActivities you can use it like this: ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); ((ActionBarActivity) … Read more

How to scale an Image in ImageView to keep the aspect ratio

Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you’re setting the image to the ImageView using android:src=”…” rather than android:background=”…”. src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly … Read more

How to create a release signed apk file using Gradle?

Easier way than previous answers: Put this into ~/.gradle/gradle.properties RELEASE_STORE_FILE={path to your keystore} RELEASE_STORE_PASSWORD=***** RELEASE_KEY_ALIAS=***** RELEASE_KEY_PASSWORD=***** Modify your app/build.gradle, and add this inside the android { code block: … signingConfigs { release { storeFile file(RELEASE_STORE_FILE) storePassword RELEASE_STORE_PASSWORD keyAlias RELEASE_KEY_ALIAS keyPassword RELEASE_KEY_PASSWORD // Optional, specify signing versions used v1SigningEnabled true v2SigningEnabled true } } buildTypes { … Read more

Android: Go back to previous activity

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it’ll take you back to the previous activity. Keep track of the activity … Read more

Get screen width and height in Android

Using this code, you can get the runtime display’s width & height: DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int height = displayMetrics.heightPixels; int width = displayMetrics.widthPixels; In a view you need to do something like this: ((Activity) getContext()).getWindowManager() .getDefaultDisplay() .getMetrics(displayMetrics); In some scenarios, where devices have a navigation bar, you have to check at runtime: … Read more

Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley [closed]

I’m hoping someone can provide some concrete examples of best use cases for each. Use Retrofit if you are communicating with a Web service. Use the peer library Picasso if you are downloading images. Use OkHTTP if you need to do HTTP operations that lie outside of Retrofit/Picasso. Volley roughly competes with Retrofit + Picasso. … Read more

View’s getWidth() and getHeight() returns 0

The basic problem is, that you have to wait for the drawing phase for the actual measurements (especially with dynamic values like wrap_content or match_parent), but usually this phase hasn’t been finished up to onResume(). So you need a workaround for waiting for this phase. There a are different possible solutions to this: 1. Listen … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)