:: Java Code. Source code examples for Android Studio Java. English and Czech edition ::


Android Studio Java


Free Java Mobile Source Code Samples and translate to Czech language
 

       
Translate This Site to Czech >>>>
       
       A complete beginner's code collection to Android Studio, which will allow you to start writing programs faster.
       
       
1. Display a line of text on the screen in myTextView
       
       String aboutCat = "Sample text will be displayed.";
       myTextView.setText(aboutCat); //Display string aboutCat on the screen
       
       
2. Programmatic access to a string resource is done like this
       
       String catName = getResources().getString(R.string.text_title);
       myTextView.setText(catName); //Display string catName
       
       
3. Show a letter from a word or string
       
       //Select a character from a string
       //How to count characters - 0,1,2,3,4,...
       String testString = "Oflameron";
       char myChar = testString.charAt(4);
       myTextView.setText(Character.toString(myChar)); // Displays the fifth character - m

Android Studio Example


       
       
4. Show a letter from a word or string
       
       //Searches for the substring str and returns the index (position) of the found substring. If the substring is not found, then -1 is returned
       String FullString = "Android java examples"; //Full string
       myTextView.setText(String.valueOf(FullString.indexOf("java")));
       
       
5. If the NextString is empty, then execute the code
       
       //Checks if a string is empty
       if(NextString.isEmpty()) {
       // Here you write the code to be executed
       }
       
       
6. Calculate string length
       
       //Write text string length in myTextView
       String BigString = "Oflameron";
       myTextView.setText(String.valueOf(BigString.length())); //Returns 9 (nine characters)
       
       
Shmeleff - Valery Shmeleff
       
       
7. Open web page
       
       Intent browseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.oflameron.ru"));
       startActivity(browseIntent);
       
       
RSS
       
       
Shmeleff
         
Shmeleff
           
Shmeleff
       
       
8. Open map by specific coordinates or request
       
       String uri = "geo:"+ latitude + "," + longitude;
       Intent mapIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
       startActivity(mapIntent);
       
       
9. Take a picture with the camera
       
       Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       startActivityForResult(cameraIntent, CAMERA_RESULT);
       
       Uses a standard camera application in the system
       The image received from the camera can be processed in the onActivityResult method:
       

Android Studio Example


       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == CAMERA_RESULT) {
           Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
           ImageView ivCamera = (ImageView) findViewById(R.id.iv_camera);
           ivCamera.setImageBitmap(thumbnail);
         }
       }
       
       The image from the camera can be saved in a specific directory by adding a parameter to the intent
       
       File photo = new File(Environment.getExternalStorageDirectory(), "oflameron.jpg");
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
       
       For this intent, you do not need the permission android.permission.CAMERA, because The camera is not used directly, but the built-in camera application is just called. However, it is recommended that you add the following line to AndroidManifest:
       
       
       
       
Deutsche >>
       
       
Shmeleff - Valery Shmeleff
       
       
10. Take a picture with the camera
       
       You must specify this in the manifest file. This is done using the <uses-function> tags.
       
       <manifest ... >
       <uses-feature android:name="android.hardware.camera"
           android:required="true" />
       ...
       </manifest>
       
       Android allows you to transfer the execution of various actions to other applications using intent objects. This way we can take photos using an external application. This process includes three stages: creating a specific intent object, launching a new Activity, processing the results in the current Activity
       static final int REQUEST_IMAGE_CAPTURE = 1;
       
       private void dispatchTakePictureIntent() {
           Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
               startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
           }
       }
       
       Using the resolveActivity () method, we check for the presence of an application for working with the camera. If intent cannot be handled by any external application, then calling the startActivityForResult () method will crash your application.
       
       Photos that the user takes must be saved in some public directory on an external medium, because in this case the photos will be available to other applications. A suitable directory for storing photographs can be obtained using the getExternalStoragePublicDirectory () method by passing it the DIRECTORY_PICTURES argument. To work with this directory, you need to set the appropriate permission in the manifest of your application.
       
       <manifest ...>
           <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
           ...
       </manifest>
       
       Writing permission allows both reading and reading.
       
       If you want photos to be accessible only using your application, then use the getExternalFilesDir () method to get the directory. On Android 4.3 and below, writing to this directory also requires WRITE_EXTERNAL_STORAGE permission. Starting with Android 4.4, permission is no longer required, since the directory is no longer available to other applications. Based on this, you can declare permission like this :
       
       <manifest ...>
           <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
           android:maxSdkVersion="18" />
       ...
       </manifest>
       

Android Studio Example


       Please note that when you delete your application, the directory returned by the getExternalFilesDir () method will also be deleted
       
       When you define a directory for the photo, you need to create a file with a unique name and possibly save the path to it. The following is an example of a method that performs this procedure using the current date and time.
       
       
       
       This method creates a file for the photo and launches the camera application using the intent.
       
       
       
       The created photo can be made publicly available using the system Media Provider.
       
       
Scenar filmu Wiki >>
       
       
       

Android Studio Example

:: Czech Java source code. Free Java code for beginners programmers. Free Android Studio Java Code. Oflameron (c) by Valery Shmeleff ::
Android Studio Example