Sunday, December 03, 2017

Custom Logs

Custom Logs is used to show all information's of logs (like name of class->function->line number->tags and message).




Steps to use Custom Logs in your project.

  1. Crate a project(LogsDemo) in android studio.

  2. Create a class MyLog.Java and update it-


    package pankaj.log.demo;
    
    import android.util.Log;
    
    /**
     * Created by gsolc-05 on 30/7/17.
     */
    
    public class MyLogs {
        static String TAG = "DEMO_LOG";
    
        public static void LogMessage(String message) {
            StackTraceElement l = new Exception().getStackTrace()[1];
            Log.e(TAG, l.getClassName() + "->" + l.getMethodName() + " : " + l.getLineNumber() + " : " + message);
        }
    
    
    }
    
    
    
    
    
     
  3. Create a class MainActivity.Java and update it-


    package pankaj.log.demo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            MyLogs.LogMessage("HELLO...");
        }
    
        public void helloPrint(View view) {
            MyLogs.LogMessage("HELLO...");
        }
    
        public void hiPrint(View view) {
            MyLogs.LogMessage("HI...");
        }
    }
    
    
    
     
     
  4. Open your main_activity.xml file and update it-


    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="pankaj.log.demo.MainActivity">
    
    
        <TextView
            android:layout_width="match_parent"
            android:paddingTop="20dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Press buttons to print logs" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="helloPrint"
                android:text="PRINT HELLO... LOG" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="hiPrint"
                android:text="PRINT HI... LOG" />
    
    
        </LinearLayout>
    
    
    </android.support.constraint.ConstraintLayout>
    
    
    
    
    

  5. All the application development process with CustomLogs has completed, Now run the app and look the screen. 








  6. Good bye, Thanks to read this blog.

Auto Complete Text Demo, Multi Autocomplete Text

Auto Complete Text is used to show suggestions at the time of data entry.
For the multi selection used multiselection autocomplete textview at the place of autocomplete textview.



Steps to use Multi Auto Complete Text Demo in your project.

  1. Crate a project(AutoCompleteTextDemo) in android studio.

  2. Create a class MainActivity.Java and update it-


    package com.pankaj.autocompletetextviewdemo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    import android.widget.MultiAutoCompleteTextView;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        //Create objects
        List<String> data = new ArrayList<>();
        MultiAutoCompleteTextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Create list to show suggestions
            for (int i = 1; i <= 500; i++) {
                data.add("" + i);
            }
    
            //Create adapter to add suggestions to autocomplete textview
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_dropdown_item_1line, data);
    
            //Initialize textview
            textView = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
    
            //Add adapter
            textView.setAdapter(adapter);
    
            //set tockenizer to select multiple items
            textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    
        }
    
        //show selected data on toast
        public void showData(View view) {
            Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
        }
    
    }
    
    
    
    
    
    
     
  3. Open your main_activity.xml file and update it-


    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.pankaj.autocompletetextviewdemo.MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:orientation="vertical">
    
            <MultiAutoCompleteTextView
                android:id="@+id/autoCompleteTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="type numbers" />
    
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:onClick="showData"
                android:text="Show Selected" />
    
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    

  4. All the application development process with Multi Selection Autocomplete Text  has completed, Now run the app and look the screen. 










  5. Good bye, Thanks to read this blog.

Life Cycle of Activity and Application

Life Cycle tutorial covered the all information about the activity and application life cycle .






Steps to use Life Cycle tutorial in your project.

  1. Crate a project(LifeCycleDemo) in android studio.

  2. Create a class MyApplication.Java and update it-


    package com.pankaj.lifecycledemo;
    
    import android.app.Application;
    import android.content.res.Configuration;
    import android.util.Log;
    
    /**
     * Created by gsolc on 29/11/17.
     */
    
    public class MyApplication extends Application {
        String TAG="lifecycledemoTag";
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.e(TAG, "MyApplication onCreate");
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            Log.e(TAG, "MyApplication onLowMemory");
    
        }
    
        @Override
        public void onTerminate() {
            super.onTerminate();
            Log.e(TAG, "MyApplication onTerminate");
    
        }
    
        @Override
        public void onTrimMemory(int level) {
            super.onTrimMemory(level);
            Log.e(TAG, "MyApplication onTrimMemory");
    
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            Log.e(TAG, "onConfigurationChanged");
        }
    
    }
    
    
    
    
    
      
  3. Create a class MainActivity.Java and update it-


    package com.pankaj.lifecycledemo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends AppCompatActivity {
        String TAG="lifecycledemoTag";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.e(TAG, "MainActivity onCreate");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.e(TAG, "MainActivity onDestroy");
        }
    
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            Log.e(TAG, "MainActivity onBackPressed");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.e(TAG, "MainActivity onPause");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.e(TAG, "MainActivity onResume");
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.e(TAG, "MainActivity onStart");
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.e(TAG, "MainActivity onRestart");
        }
    }
    
    
    
    
     
  4. Open your main_activity.xml file and update it-
  5. <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.pankaj.lifecycledemo.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
  6. All the application development process with Life Cycle  has completed, Now run the app and look the screen. 







  7. Good bye, Thanks to read this blog.