Okhelp.cz

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.

long in java example


Android development

long is 64 bit signed type and used when int is not large enough to hold the value.

long je celé číslo 64 bitů -9223372036854775808 +9223372036854775807 a používá se tam, kde typ int není schopen pojmout takovou hodnotu čísla.


		// declaration and assignment of value type long
		long n = 22337203685477580L;
		// print formated value
		System.out.printf("The value of x is %d%n", n); // 22337203685477580
		System.out.format("%+,8d%n%n", n); // +22 337 203 685 477 580

		// declaring more variables in single statement
		long lo1 = 12L, lo2 = 56, lo3 = 1455555555589L;

		// long range of value
		System.out.println(Long.MAX_VALUE); // 9223372036854775807
		System.out.println(Long.MIN_VALUE); // -9223372036854775808

		// check if a string is a valid number in Java example
		// convert string to long Java example
		String sLong = "1288888888888888";
		long longParse = Long.parseLong(sLong);

		// convert strings to numbers
		long longFromString = (Long.valueOf(sLong)).longValue();

		// long to string in Java example code
		Long longObj = new Long(229999999999L);
		String str = longObj.toString();
		// else
		Long longS = 888888888888L;
		String strLong = longS.toString();

		// compare two long variables
		Long longComp1 = 5555L;
		if (longComp1.equals(55555555L))
			System.out.print("true");

		// compares the two specified long values in Java example

		int i = longS.compareTo(444444L); // -1 first < second
		// 0 first == second
		// 1 first > second
		System.out.print(i);

397LW NO topic_id



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


374

ListView set the item as checked to be highlighted | listview-set-the-item-as-checked-to-be-highlighted


Important: Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB

int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 :
android.R.layout.simple_list_item_1;


getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(position, true);


Example of usage:


public void updateList() {
		Context ctx = getActivity();
		Notes notes = new Notes(ctx);

		String[] from = { Notes.COLUMN_TITLE };
		int[] to = { android.R.id.text1 };
		int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
				android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
		ListAdapter adapter = new SimpleCursorAdapter(ctx,
				layout, notes.getNotes(), from,
				to, 0);

		setListAdapter(adapter);

		notes.close();
	}

/..................
@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        
        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setItemChecked(position, true);
     
    }

49

Emulator window was out of view and was recentred - Android emulator warning | emulator-window-was-out-of-view-and-was-recentred-android-emulator-warning


Emulator window was out of view and was recentred
Emulator] WARNING: Data partition already in use. Changes will not persist!
Emulator] WARNING: SD Card image already in use: C:\Documents and Settings\user_name\.android\avd\hvga21_up1_7.avd/sdcard.img
Emulator] WARNING: Cache partition already in use. Changes will not persist!

Basic step
Try restart Eclipse if using from menu File->Restart

Other solutions

Solution 1: try restart ADB server
//android.okhelp.cz/how-quickly-restart-adb-exe-adb-server-android-emulator-example/


Solution 2:

Delete Run Configuration


//android.okhelp.cz/android-emulator-wont-run-application-started-from-eclipse/





Solution 3:
Try delete:

c:\Documents and Settings\user_name\.android\avd\my_avd.avd\cache.img
c:\Documents and Settings\user_name\.android\avd\my_avd.avd\userdata-qemu.img



Try solution 4:
Eclipse menu select Window->Preferences->Android

Windows 32

FROM: C:\Program Files\Android\android-sdk
TO: C:\PROGRA~1\Android\android-sdk

Windows 64

FROM: C:\Program Files (x86)\Android\android-sdk
TO: C:\PROGRA~2\Android\android-sdk
281

Start Open New Activity Android Code | start-open-new-activity-android-code



      Intent i = new Intent(this, MyNextClass.class);
      startActivity(i);


150

drawBitmap, clipPath, UNION, DIFFERENCE, INTERSECT, REPLACE, XOR Android example | drawbitmap-clippath-union-difference-intersect-replace-xor-android-example



public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}

		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();

			canvas.drawColor(Color.YELLOW);

			 Bitmap b = Bitmap.createBitmap(200, 200,
			 Bitmap.Config.ARGB_8888);

			// you need to insert a image flower_blue into res/drawable folder

			paint.setFilterBitmap(true);
			Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
					R.drawable.flower_blue);
			canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			int width, height;

		    Canvas c = new Canvas(b);
		    paint.setAlpha(255); //0x80
		    c.translate(0, 30);
		    c.drawBitmap(bitmapOrg, new Matrix(), paint);
		    paint.setColor(Color.BLUE);
        Path mPath = new Path();
		    mPath.addCircle(50, 50, 50, Path.Direction.CCW);
            //c.clipPath(mPath, Region.Op.UNION);
            //c.clipPath(mPath, Region.Op.DIFFERENCE);
            c.clipPath(mPath, Region.Op.INTERSECT);
            //c.clipPath(mPath, Region.Op.REPLACE);
            //c.clipPath(mPath, Region.Op.XOR);
           paint.setColor(Color.GREEN);
           paint.setAntiAlias(true);
           c.drawCircle(30, 20, 30, paint);

		    int h = bitmapOrg.getHeight();
			//canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			canvas.drawBitmap(b, 0, 10 + h + 10, paint);

		}

	}
}



bitmap xor android


regions
367

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: 2011-09-26 20:49:35
Počet článků v kategorii: 397
Url:long-in-java-example
Share
Share
Tweet