:: 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)

Android Studio Example


       
7. Read a text file located in the phone's memory
       
       public void ReadBtn(){
         TextView myTextView = findViewById(R.id.myText); // Add View in import and android:id="@+id/myText" in main.xml
         try{
         FileInputStream fileIn=openFileInput("app/src/main/assets/words.txt");
         InputStreamReader InputRead=new InputStreamReader(fileIn);
         char[] inputBuffer=new char[READ_BLOCK_SIZE];
         String s="9028752349857394734987";
         int charRead = 0;
         
         while ((charRead=InputRead.read(inputBuffer))>0) {
         //char to string conversion
         String readstring=String.copyValueOf(inputBuffer,0,charRead);
         s +=readstring;
         }
         InputRead.close();
         Toast.makeText(getBaseContext(), s,Toast.LENGTH_SHORT).show();
         myTextView.setText(s);
         }
         catch (Exception e)
         {
         e.printStackTrace();
         }
       }
       
       
8. Read a text file located in the phone's CD cards
       
       private void readFile() {
         TextView myTextView = findViewById(R.id.myText); // Add View in import and android:id="@+id/myText" in main.xml
         /*
         * Similarly, the file object is created
         */
         //File myFile = new File(Environment.getExternalStorageDirectory().toString() + "/" + "words.txt");
         File myFile = new File("app/src/main/assets/words.txt");
         try {
         FileInputStream inputStream = new FileInputStream(myFile);
         /*
         * Buffer data from the input file stream
         */
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
         /*
         * Class to create strings from character sequences
         */
         StringBuilder stringBuilder = new StringBuilder();
         String line;
         try {
         /*
         * We produce by-line reading of data from a file into a string constructor
         * After the data is finished, we produce the output text in the TextView
         */
         while ((line = bufferedReader.readLine()) != null){
         stringBuilder.append(line);
         }
         myTextView.setText(stringBuilder);
         
         } catch (IOException e) {
         e.printStackTrace();
         }
         } catch (FileNotFoundException e) {
         e.printStackTrace();
         }
         //myTextView.setText(Environment.getExternalStorageDirectory().toString()); //The path to the directory on the CD-card. Working
       }
       
       
       
       
       
       
       
       
:: Czech Java source code. Free Java code for beginners programmers. Free Android Studio Java Code. Oflameron (c) by Valery Shmeleff ::
Android Studio Example