Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Drawable Bitmap get width height Android


		Drawable dr = getResources().getDrawable(R.drawable.algebra);
		int h = dr.getIntrinsicHeight();
		int w = dr.getIntrinsicWidth();

		Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.algebra);
		int _nWidth = ball.getWidth();
		int _nHeight = ball.getHeight();

397LW NO topic_id


Další témata ....(Topics)


Eclipse update failed folder in use | eclipse-update-failed-folder-in-use

Failed to rename directory ....
If using for update Android SDK manager,
try close Eclipse if is open.
Or change security settings of the folder which have problem.
Or disable antivirus for a moment.

Download file from URL | download-file-from-url

Download image file from URL to ImageView Java Android source example code.

Context context = thisClass.this;
Drawable image = ImageOperations(context,
ImageView imgView;
imgView = (ImageView)findViewById(R.id.idImageView);

	private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			return null;
		} catch (IOException e) {
			return null;

	public Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;

Map TreeMap key value pair sort by value Java Android example | map-treemap-key-value-pair-sort-by-value-java-android-example

Map TreeMap sorted by value Java Android example.


import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class MainClass {
	public static void main(String[] arg) {

		// english;germany dictionary
		String[] arrayOfString = { "one;eine", "two;zwei", "three;drei",
				"four;vier" };

		Map<String, String> map = new TreeMap<String, String>();

		for (String s : arrayOfString) {
			String[] array = s.split(";");
			String sKey = "", sValue = "";
			if (array.length > 1) {
				sKey = array[0];
				sValue = array[1];
				map.put(sKey, sValue);
		for (Entry<String, String> entry: mapSortedByValues(map)) {
		    System.out.println(entry.getKey() + " = " + entry.getValue());

	static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> mapSortedByValues(
			Map<K, V> map) {
		SortedSet<Map.Entry<K, V>> sortedSetOfEntries = new TreeSet<Map.Entry<K, V>>(
				new Comparator<Map.Entry<K, V>>() {
					public int compare(Map.Entry<K, V> entry_1, Map.Entry<K, V> entry_2) {
						int res = entry_1.getValue().compareTo(entry_2.getValue());
						return res != 0 ? res : 1; 
						// return entry_1.getValue().compareTo(entry_2.getValue());							
		return sortedSetOfEntries;
three = drei
one = eine
four = vier
two = zwei


Disable enable internet connection in Android Emulator | disable-enable-internet-connection-in-android-emulator

If you try function for checking internet connection you can disable internet on the emulator:
Settings - Wireless and networks - Mobile networks - Data enabled (checked - unchecked )

 public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) 
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        return false;


layout-sw600dp values-sw600dp Android example of use | layout-sw600dp-values-sw600dp-android-example-of-use

Why the app selects data from basic layout folder if smallest width is higher then the number in folder name?
Example 1
layout-sw600dp values-sw600dp (smallest width sw for data usage from this folder is 600dp density independent pixel!!!!!)
Device screen resolution is 1200 x 900 px (pixel) Wow, app to be select data from sw600dp folder! Realy?
DPI of device screen - dot per inch (pixel per inch) is 480 pixel it is wery important number!

  1. App selects smallest dimension of screen. In our case 900 px
    Medium screen have 160 dpi (The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen.).

  2. App calculate ratio 480 / 160 = 3 (The conversion of dp units to screen pixels: px = dp * (dpi / 160))

  3. App calculate smallest dimesnion of screen in dp 900 / 3 = 300 dip or dp (density independed pixel).

  4. App selects data from basic values and layout folder because sw600dp is greater than 300dp.

In our case smallest dimension of screen must be at least 1800 real - physical pixels (1800 px / 3 ratio(dpi/160) = 600 dp (dip density independend pixels) to be used data from folders values-sw600dp and layout-sw600dp.

Example 2 see Example 1 abouve
Device: Nexus 7 (2012) selected from Android Studio tool layout editor
Resolution: 800x1280 px
DPI: tvdpi (approximately 213dpi)
Ratio: 1.33 (213 / 160)
Smallest width in px: 800
Convert px to dp: 601.5 (800 / 1.33)
Result:Smallest width is 601.5dp The App to be used data from folders values-sw600dp and layout-sw600dp.

Editace: 2013-12-09 13:01:23
Počet článků v kategorii: 397

11 Share