Wednesday, March 14, 2018

Custom Dialog Fragment in Kotlin

Hello friend today we will see how to create a custom Dialog Fragment we are all aware of Alert Dialog which has title,message, icon and two/three buttons all available by default.



AlertDialog.Builder(getActivity())
        .setCancelable(false)
        .setTitle("Alert Dialog")
        .setMessage("This is alert Dialog Message Sample text")
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setNegativeButton("Close", DialogInterface.OnClickListener { dialog, which ->  
          //Action goes here        
        }).setPositiveButton("Ok", DialogInterface.OnClickListener 
         { dialog, which ->    //action goes here}).create().show()




Now creating DialogFragment to add CustomDialogFragment to you package.


right click on package name-->New-->kotlin file/class

Enter fragment name and press ok.







Now to our custom Dialog we need xml layout. layout_dialog.xml as below copy and paste


<RelativeLayout 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="wrap_content"
    android:background="@android:drawable/alert_light_frame"
    android:padding="20dp">

    <LinearLayout
        android:id="@+id/linearLayoutContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Lorem epsumum is a dummy text"
            android:textColor="@color/label_outgoing"
            android:textSize="15dp"
            android:textStyle="normal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/label_missed" />


        <RelativeLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:gravity="center_vertical">


            <Button
                android:id="@+id/buttonCancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="cancel"
                android:textColor="@color/black"
                android:textSize="15dp"
                android:textStyle="bold"
                />


            <Button
                android:id="@+id/buttonAccept"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Accept"
                android:textColor="@color/black"
                android:textSize="15dp"
                android:textStyle="bold" />


        </RelativeLayout>


    </LinearLayout>

</RelativeLayout>



Once fragment dialog created with new kotlin class paste below code in CustomDialogFragment 


import android.app.Activity
import android.content.*
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import com.xxxxx.R

/**
 * Created by Mobidroidtech on 3/14/2018.
 */
class CustomDialogFragment : DialogFragment() {

    private var content: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        content = arguments.getString("content")

        // Pick a style based on the num.
        val style = DialogFragment.STYLE_NO_FRAME
        val theme = R.style.DialogTheme
        setStyle(style, theme)
    }

    // Override the Fragment.onAttach() method to instantiate the
    // NoticeDialogListener
    override fun onAttach(activity: Activity?) {
        super.onAttach(activity)



    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater!!.inflate(R.layout.layout_dialog, container, false)

        val btnCancel = view.findViewById<View>(R.id.buttonCancel) as Button
        val btnAccept = view.findViewById<View>(R.id.buttonAccept) as ImageButton

        val textViewContent = view.findViewById<View>(R.id.textViewContent) as TextView
        textViewContent.text = content

        //FontUtils.setTypeface(getActivity(), textViewQuestion, "fonts/mangal.ttf");
        //FontUtils.setTypeface(getActivity(), textViewAnswer, "fonts/mangal.ttf");
        btnCancel.setOnClickListener {
            Toast.makeText(activity, "action cancelled", Toast.LENGTH_SHORT).show()
            dismiss()
        }

        btnAccept.setOnClickListener {
            Toast.makeText(activity, "User Accepted Action", Toast.LENGTH_SHORT).show()
            dismiss()
        }

        return view
    }

    companion object {


        /**
         * Create a new instance of CustomDialogFragment, providing "num" as an
         * argument.
         */
        fun newInstance(content: String): CustomDialogFragment {
            val f = CustomDialogFragment()

            // Supply num input as an argument.
            val args = Bundle()
            args.putString("content", content)
            f.arguments = args

            return f
        }
    }

}



Create a style in values as below.




 <style name="DialogTheme">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>


Now we have done with creating customDialog how to show it on any action.



val ft = getSupportFragmentManager().beginTransaction()
val newFragment = ConfirmDialogFragment.newInstance("pass content here")
newFragment.show(ft, "dialog")



Tuesday, February 20, 2018

Android Working with Butter Knife library Kotlin Tutorial

Hello friends, Today we will see how to use butter knife library in kotlin with a sample project.
Butter knife in java have many tutorials but in kotlin you need to struggle.

What is butter knife

- Butter knife is View Injection library

Why we need it.

Every time finding view by findViewById, declaring them and then perform events like setText, set color, Now using butter knife we can Bind View as @BindView and use them any where.

1. Adding Butter Knife Dependency.

Add this line to build.gradle at the top below apply plugin'com.android.application'
apply plugin: 'kotlin-kapt'
Add dependancy
     dependencies {
                  ...
                  implementation 'com.jakewharton:butterknife:8.8.1'     
                  kapt 'com.jakewharton:butterknife-compiler:8.8.1'    
     }

2.  Usage of View Binding

We will see it by example code with one activity and a fragment to it added dynamically.

Lets see MainActivity.ktl

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import butterknife.ButterKnife

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ButterKnife.bind(this)

        val fragment = MainFragment.newInstance("MainFragment")
        supportFragmentManager.beginTransaction().add(R.id.container, fragment, "fragment").commit()

    }
}

Here with code you can see  ButterKnife.bind(this) statement keep it in mind and lets add xml layout for MainActivity as activity_main.xml


<?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"
    android:id="@+id/container"
    tools:context="com.fd.butterknifesample.MainActivity">
</android.support.constraint.ConstraintLayout>


Now we will consider you know how to show fragment in activity in Kotlin, For those directly starting from this post just look at our previous blog how to add fragment in activity kotlin



Now we have to add fragment and xml layout for fragment so to cover maximum points of butter knife we have added few more widget components on xml layout of fragment 





TextView :- To show use of @BindView
ImageView :- To show use of @BindDrawable and set them to imageview
Button :- To show use of @onClick listener
5 TextView inside cardview :- To show usage of action.


Now we will see code of MainFragment to get whole picture. but first layout of fragment_main.xml

<?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.fd.butterknifesample.MainActivity">

    <TextView
        android:id="@+id/textView" 
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:text="@string/label_demo"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/imageViewDemo"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button android:id="@+id/buttonDemo"
        android:layout_width="wrap_content"
        android:layout_height="51dp"
        android:layout_marginBottom="144dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayoutDemo" />

    <TextView android:id="@+id/textViewDemo"
        android:layout_width="208dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/textInputLayoutDemo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.247"
        app:layout_constraintStart_toEndOf="@+id/imageViewDemo"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.2" />

    <ImageView android:id="@+id/imageViewDemo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="28dp"
        android:layout_marginStart="28dp"
        android:contentDescription="@string/imageView_contentDesc"
        android:src="@android:drawable/dialog_frame"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView"
        app:layout_constraintVertical_bias="0.1"
        tools:layout_editor_absoluteY="74dp" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayoutDemo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:hint="Enter your name"
        app:layout_constraintBottom_toTopOf="@id/buttonDemo"
        app:layout_constraintTop_toBottomOf="@id/imageViewDemo"
        app:layout_constraintVertical_bias=".30"
        tools:layout_editor_absoluteY="169dp">
        <android.support.design.widget.TextInputEditText
            android:id="@+id/textInputEditDemo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hint" />
    </android.support.design.widget.TextInputLayout>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/buttonDemo"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="400dp">

        <android.support.v7.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView android:text="textView 1"
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp" />

            <TextView android:text="textView 2"
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp" />

            <TextView android:text="textView 3"
                android:id="@+id/textView3" 
               android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp" />
            <TextView android:text="textView 4"
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp" />

            <TextView android:text="textView 5"
                android:id="@+id/textView5" 
               android:layout_width="match_parent" 
               android:layout_height="wrap_content"
                android:layout_marginBottom="5dp" />
        </android.support.v7.widget.LinearLayoutCompat>
    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

In kotlin we can not keep any field as null like as Java to attain NullSafety so while binding views we use lateinit keyword of kotlin.

@BindView : -

    @BindView(R.id.textViewDemo)
    lateinit var mTextViewDemo: TextView
bind a single view to field "mTextViewDemo" and use it anywhere.

@BindViews :-

Similar to bindView but can be used to bind multiple view of same type to perform a comman operation like setText using butter knife ACTION
Bind 5 textview to a mutable list. If you are using normal list then use  @JvmField, when using @JvmField you can not use lateinit keyword.
    //BindViews    @BindViews(R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5)
    lateinit var labelArray: MutableList<TextView>

@onClick :-

@OnClick(R.id.buttonSample)
internal fun sayHello() {
    Toast.makeText(this, "Hello butter knife from kotlin", LENGTH_SHORT).show()
}


Bind ButterKnife library to your activity and fragment unless it is added you will get run time exceptions as lateinit field not initialized.

To bind it to fragment declare

            var mUnbinder: Unbinder? = null
Now bind it in onCreateView of Fragment as below
      //Bind this view to butterknife   
     mUnbinder = ButterKnife.bind(this, rootView!!)

Unbind it to avoid memory leaks like as below.
 override fun onDestroyView() {
        super.onDestroyView()

        //unbind view from butterknife to avoid any memory leak  
       mUnbinder!!.unbind()
    }

ACTION :-

actions are used to perform repeating task.  like we have multiple textview and they need to populated with sum text run time so instead of binding them to field and assign values them one by one
bind those multiple textviews to a mutable list and keep all the text in array which is going to assing and create one action like below.

 //Create action to set values to list of textView
val action_set_text = ButterKnife.Action<TextView?> { view, index -> view.setText(mLabel[index]) }

ButterKnife.apply(labelArray, action_set_text)


Now see at complete source code of MainFragment

MainFragment.ktl


package com.fd.butterknifesample

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.annotation.ColorIntimport android.support.design.widget.TextInputEditText
import android.support.design.widget.TextInputLayout
import android.support.v4.app.Fragment
import android.support.v4.text.TextUtilsCompat
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import butterknife.*

/** * Created by Jayesh on 2/19/2018. */class MainFragment : Fragment() {


    var mUnbinder: Unbinder? = null
    var mParam: String? = null

    // BindView    @BindView(R.id.textViewDemo)
    lateinit var mTextViewDemo: TextView

    @BindView(R.id.textInputEditDemo)
    lateinit var mTextInputLayoutDemo: TextInputEditText

    @BindView(R.id.imageViewDemo)
    lateinit var mImageViewDemo: ImageView

    //BindViews    @BindViews(R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5)
    lateinit var labelArray: MutableList<TextView>

    //Bind Color    @JvmField    @BindColor(android.R.color.white)
    @ColorInt    internal var mColor: Int = 0

    //Bind Drawable    @JvmField    @BindDrawable(R.mipmap.ic_launcher)
    var mDrawable: Drawable? = null

    @JvmField    @BindView(R.id.buttonDemo)
    var mButton: Button? = null

    var mLabel = arrayOf<String>("One", "Two", "Three", "Four", "Five")
    var mLabelColor = arrayOf<Int>(Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            mParam = arguments.getString(ARG_PARAM)
        }
    }


    override fun onDestroyView() {
        super.onDestroyView()

        //unbind view from butterknife to avoid any memory leak        mUnbinder!!.unbind()
    }


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val rootView: View? = inflater?.inflate(R.layout.fragment_main, container, false);

        //Bind this view to butterknife        mUnbinder = ButterKnife.bind(this, rootView!!)

        //Create action to set values to list of textView        val action_set_text = ButterKnife.Action<TextView?> { view, index -> view.setText(mLabel[index]) }        ButterKnife.apply(labelArray, action_set_text)


        val action_change_color = ButterKnife.Action<TextView> { view, index -> view.setTextColor(mLabelColor[index]) }
        ButterKnife.apply(labelArray, action_change_color)

        // Set Drawable to ImageView        mImageViewDemo?.setImageDrawable(mDrawable!!)

        mButton?.setOnClickListener {
            if (!TextUtils.isEmpty(mTextInputLayoutDemo?.text.toString()))
                mTextViewDemo?.text = mTextInputLayoutDemo?.text.toString()
            else                Toast.makeText(activity!!, "Type some text first", Toast.LENGTH_SHORT).show()
        }
        //Now assign ONCLICK        @OnClick(R.id.buttonDemo)
        fun onButtonClick() {
            if (!TextUtils.isEmpty(mTextInputLayoutDemo?.text.toString()))
                mTextViewDemo?.text = mTextInputLayoutDemo?.text.toString()
            else                Toast.makeText(activity!!, "Type some text first", Toast.LENGTH_SHORT).show()
        }


        return rootView!!;

    }


    override fun onAttach(context: Context?) {
        super.onAttach(context)
    }


    companion object {

        val ARG_PARAM: String = "PARAM1"

        fun newInstance(param: String): MainFragment {

            val fragment = MainFragment()
            var arg: Bundle = Bundle()
            arg.putString(ARG_PARAM, param)

            fragment.arguments = arg
            return fragment


        }

    }

    @OnClick(R.id.buttonSample)
    internal fun sayHello() {
        Toast.makeText(this, "Hello butter knife from kotlin", LENGTH_SHORT).show()
    }


}


Like @BindView and @BindViews there are more tags like
@BindInt
@BinfString
@BindDrawable
@BindFloat
@BindFont
@BindBitmap
@BindAnim
@BindDimen
 @BindColor

These are all similar to @BindView try them by yourself. and enjoy.



Monday, February 19, 2018

How to initialize array in kotlin

Hello friends today we will see how to initialize array in Kotlin.

1. Lets have a String array.


//In Java
String[] mLabel={"One","Two","Three","Four","Five"}
// In Kotlin 
var mLabel = arrayOf<String>("One", "Two", "Three", "Four", "Five")

2. Lets have int array


// In Java
int[] numbers = {1,2,3,4,5}
// In Kotlin


val numbers : IntArray = intArrayOf(1,2,3,4,5) 
3. Create array of size and initialize with value lambda

 val nu : IntArray =kotlin.IntArray(5) 
  val num : IntArray = kotlin.IntArray(5,{i->1})
     Create int array of size 5 and initializes with 1

Understand Polymorphism to solve A a =new B() problem

Hi friends today I come across that many people stumble on a common question asked in JAVA/Android interview many time.

Problem states that if there is class as A and B and C. B extends A and in class C we have something like



A a =new B();
          B b= new A();



Ok So question is whether above statements are valid or not.

To validate it remember Polymorphism in Java - like biology term species (object) can have many forms - stages.

Means SuperClasses can be initialized with SubClasses. but reverse is not true.

If class B is extending class A, so here class A is superclass and class B is subclass.

Now by this statement

          // is valid 
A a= new B();
But reverse is not true.

B b= new A(); // will not compile and gives error - incompatible types
now call method on
  a.someMethod();

if both class A and B have same method like someMethod(); which method will be called.


No doubt class B's method will be called as B's instance is assigned to a.

A a =new B();
       a.someMethod();  ///calls class B 's method.


Now complete picture is here.

***************************************************************************
import java.util.*;
class Test{
      public static void main(String args[]){
             Test test=new Test ();
              test.run();
       }
       public void run(){
               // Valid class A's object assign to  A
              A a =new A();
             // Valid as class B's object assign to B
              B b=new B();
            // Call display method of class A
            a.display();
           // Call display method of class B
           b.display();
           // Valid as object of B class is assign to a 
           a=new B();
          // Here call display method of class B???
          a.display();
         //??Why because now A a holds object of class B
         // Now call to this method?  What will Happen
         b=new A();                
         // throws runtime exception "incompatible types -                 // class A can not be converted to  B"          
        // So this method won't work.
        b.display();
        // Then why A a =new B(); is valid.
       // By the property of polymorphism 
}
             class A{
                      public void display(){
                                System.out.println("Method of A");
                      }
              }


          class B extends A{
                   public void display(){
                               System.out.println("Method of B");
                   }
          }
}
*****************************OUTPUT**************************************

Method of A
Method of B
Method of B



 That't it copy above program to save as Test.java  modify it according your doubt and clear them.