Monday, December 26, 2016

Square image view

Square image view is used to show a non square image into the form of square.




Steps to use SquareImageView in your project.

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

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


    package com.pankaj.square_image.demo;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    public class SquareImageView extends ImageView {
    
        //________auto created constructor
        public SquareImageView(Context context) {
            super(context);
        }
    
        //________auto created constructor
        public SquareImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        //________auto created constructor
        public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            //_______________pass same paramere value for width and geight for the SquareImageView.
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        }
    }
    
    
    
    
    
     
  3. Open your main_activity.xml file and update it-


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:background="#203F51B5"
        android:padding="@dimen/activity_horizontal_margin"
        tools:context="com.pankaj.square_image.demo.MainActivity">
    
        <!--scroll view to provide the scrollable screen if contant is large-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">
    
            <!--main child layout for scroll view-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">
    
                <!--title for default image view-->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Default Image"
                    android:textSize="20dp" />
    
                <!--default image view-->
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:scaleType="fitXY"
                    android:src="@drawable/madhya_pradesh" />
    
                <!--title for square image view-->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:text="Square Image"
                    android:textSize="20dp" />
    
                <!--square image view-->
                <com.pankaj.square_image.demo.SquareImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:scaleType="fitXY"
                    android:src="@drawable/madhya_pradesh" />
            </LinearLayout>
    
        </ScrollView>
    
    </LinearLayout>
    
    
    
     

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





  5. Good bye, Thanks to read this blog.
     

Friday, December 23, 2016

Fragment, Fragment BackStack, POP Fragment


A fragment is an independent Android component which can be used by an activity. A fragment encapsulates functionality so that it is easier to reuse within activities and layouts.

A fragment runs in the context of an activity, but has its own life cycle and typically its own user interface. It is also possible to define fragments without an user interface.

In this tutorial i will cover creation of fragment, BackStack, POP From BackStack etc.








Steps to use Fragment in your project.

  1. Crate a project(FragmentDemo) in android studio.
  2. Open your main_activity.xml and update it-
    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout 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"
        android:orientation="vertical"
        tools:context="com.pankaj.fragment.demo.MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:onClick="fragmentOne"
                android:text="fragment One" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:onClick="fragmentTwo"
                android:text="fragment Two" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:onClick="fragmentWhree"
                android:text="fragment Three" />
        </LinearLayout>
    
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>
    
    </LinearLayout>
     
  3.  Now Create a class  Utills.JAVA class and update it. "replaceFragment(fragment, fragmentManager)" is used to  check the instance of the fragment, if a instance of fragment is already added in back stack then, It will pop that fragment, else will create new instance of this given fragment.
    package com.pankaj.fragment.demo.utils;
    
    import android.content.Context;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import com.pankaj.fragment.demo.R;
    
    /**
     * Created by android_studio on 23/12/16.
     */
    public class utils {
    
    //________This method will be check, if a instance of fragment is already added in back stack then, It will pop that fragment, else will create new instance.
        public static void replaceFragment(Fragment fragment, FragmentManager manager) {
            String backStateName = fragment.getClass().getName();
            boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
    
     //fragment not in back stack, create it.     
    if (!fragmentPopped) { 
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.content_frame, fragment);
                ft.addToBackStack(backStateName);
                ft.commit();
            }
        }
    }
    



  4.  Now create three fragment FragmentOne.Java, FragmentTwo.Java and FragmentThree.Java and update to below code.

    FragmentOne.Java
    package com.pankaj.fragment.demo.fragments;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import com.pankaj.fragment.demo.R;
    import com.pankaj.fragment.demo.inner_fragments_for_fragment_one.FragmentInnerA;
    import com.pankaj.fragment.demo.utils.utils;
    /**
     * Created by android_studio on 23/12/16.
     */
    public class FragmentOne extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
         
        //________create the object of main view for FragmentOne 
            View v = inflater.inflate(R.layout.fragment_one, null);
    
            
            Button innerFragmentA = (Button) v.findViewById(R.id.innerFragmentA);
    
            //________add listener to innerFragmentA button
            innerFragmentA.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    
                    //_________replace the fragment to inner fragment
                    utils.replaceFragment(new FragmentInnerA(), getActivity().getSupportFragmentManager() );
                }
            });
     
     //________return the object of main view for FragmentOne
            return v;
        }
    }
    
    

    FragmentTwo.Java
    package com.pankaj.fragment.demo.fragments;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import com.pankaj.fragment.demo.R;
    import com.pankaj.fragment.demo.inner_fragment_for_fragment_two.FragmentTwoInnerA;
    import com.pankaj.fragment.demo.utils.utils;
    
    /**
     * Created by android_studio on 23/12/16.
     */
    
    public class FragmentTwo extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            //________create the object of main view for FragmentTwo
            View v = inflater.inflate(R.layout.fragment_two, null);
    
            //________create the object of button innerFragmentA
            Button FragmentTwoInnerFragmentA = (Button) v.findViewById(R.id.FragmentTwoInnerFragmentA);
    
            //________add listener to FragmentTwoInnerFragmentA button
            FragmentTwoInnerFragmentA.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    //_________replace the fragment to inner fragment
                    utils.replaceFragment(new FragmentTwoInnerA(), getActivity().getSupportFragmentManager());
                }
            });
    
            //________return the object of main view for FragmentTwo
            return v;
        }
    }
    

    FragmentThree.Java
     
    package com.pankaj.fragment.demo.fragments;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import com.pankaj.fragment.demo.R;
    import com.pankaj.fragment.demo.inner_fragment_for_fragment_three.FragmentThreeInnerA;
    import com.pankaj.fragment.demo.inner_fragment_for_fragment_two.FragmentTwoInnerA;
    import com.pankaj.fragment.demo.utils.utils;
    
    /**
     * Created by android_studio on 23/12/16.
     */
    
    public class FragmentThree extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            //________create the object of main view for FragmentTwo
            View v = inflater.inflate(R.layout.fragment_three, null);
    
            //________create the object of button FragmentThreeInnerFragmentA
            Button FragmentThreeInnerFragmentA = (Button) v.findViewById(R.id.FragmentThreeInnerFragmentA);
    
            //________add listener to FragmentThreeInnerFragmentA button
            FragmentThreeInnerFragmentA.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    //_________replace the fragment to inner fragment
                    utils.replaceFragment(new FragmentThreeInnerA(), getActivity().getSupportFragmentManager());
                }
            });
    
            //________return the object of main view for FragmentThree
            return v;
        }
    } 
  5.   Create XML for all three fragment.
    fragment_one.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment One" />
    
        <Button
            android:layout_width="wrap_content"
            android:text="Fragment One Inner Fragment A"
            android:id="@+id/FragmentOneInnerFragmentA"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    
    

    fragment_two.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment two" />
    
        <Button
            android:id="@+id/FragmentTwoInnerFragmentA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment Two Inner Fragment A" />
    
    </LinearLayout>
    

    fragment_three.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment three" />
    
        <Button
            android:id="@+id/FragmentThreeInnerFragmentA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment Three Inner Fragment A" />
    
    </LinearLayout>
    


  6.  Now create Fragments to open inside the fragments
     FragmentOneInnerA.Java
    package com.pankaj.fragment.demo.inner_fragments_for_fragment_one;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import com.pankaj.fragment.demo.R;
    import com.pankaj.fragment.demo.inner_fragment_for_fragment_three.FragmentThreeInnerB;
    import com.pankaj.fragment.demo.utils.utils;
    
    /**
     * Created by android_studio on 23/12/16.
     */
    
    public class FragmenOnetInnerA extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            //________create the object of main view for FragmenOnetInnerA
            View v = inflater.inflate(R.layout.fragment_inner_a, null);
    
            //________create the object of button FragmentOneInnerFragmentB
            Button FragmentOneInnerFragmentB = (Button) v.findViewById(R.id.FragmentOneInnerFragmentB);
    
            //________add listener to FragmentOneInnerFragmentB button
            FragmentOneInnerFragmentB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    //_________replace the fragment to inner fragment
                    utils.replaceFragment(new FragmentOneInnerB(), getActivity().getSupportFragmentManager());
                }
            });
    
            //________return the object of main view for FragmenOnetInnerA
            return v;
        }
    }
    



    fragment_one_inner_a.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment One Inner Fragment A" />
    
        <Button
            android:id="@+id/FragmentOneInnerFragmentB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment One Inner Fragment B" />
    </LinearLayout>
    
    



    FragmentOneInnerB.Java
    package com.pankaj.fragment.demo.inner_fragments_for_fragment_one;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import com.pankaj.fragment.demo.R;
    
    /**
     * Created by android_studio on 23/12/16.
     */
    
    public class FragmentOneInnerB extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            //________create the object of main view for FragmentOneInnerB
            View v = inflater.inflate(R.layout.fragment_one_inner_b, null);
    
            //________return the object of main view for FragmentOneInnerB
            return v;
    
        }
    }



    fragment_one_inner_b.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment One Inner Fragment B" />
    
    </LinearLayout>
    
    




    FragmentTwoInnerA.Java
    package com.pankaj.fragment.demo.inner_fragment_for_fragment_two;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import com.pankaj.fragment.demo.R;
    import com.pankaj.fragment.demo.utils.utils;
    
    /**
     * Created by android_studio on 23/12/16.
     */
    
    public class FragmentTwoInnerA extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            //________create the object of main view for FragmentTwoInnerA
            View v = inflater.inflate(R.layout.fragment_two_inner_a, null);
    
            //________return the object of main view for FragmentTwoInnerA
            return v;
        }
    }



    FragmentThree.Java
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment Two Inner Fragment A" />
    
    </LinearLayout>
    
    


    FragmentThreeInnerA.Java
    package com.pankaj.fragment.demo.inner_fragment_for_fragment_three;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import com.pankaj.fragment.demo.R;
    import com.pankaj.fragment.demo.utils.utils;
    /**
     * Created by android_studio on 23/12/16.
     */
    public class FragmentThreeInnerA extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            //________create the object of main view for FragmentThreeInnerA
            View v = inflater.inflate(R.layout.fragment_three_inner_a, null);
    
            //________create the object of button FragmentThreeInnerFragmentB
            Button FragmentThreeInnerFragmentB = (Button) v.findViewById(R.id.FragmentThreeInnerFragmentB);
    
            //________add listener to FragmentThreeInnerFragmentB button
            FragmentThreeInnerFragmentB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    //_________replace the fragment to inner fragment
                    utils.replaceFragment(new FragmentThreeInnerB(), getActivity().getSupportFragmentManager());
                }
            });
    
            //________return the object of main view for FragmentThreeInnerA
            return v;
        }
    }
    

    fragment_three_inner_a.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment three Inner Fragment A" />
    
        <Button
            android:id="@+id/FragmentThreeInnerFragmentB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment Three Inner Fragment B" />
    </LinearLayout>
    
    

    FragmentThreeInnerB.Java
    package com.pankaj.fragment.demo.inner_fragment_for_fragment_three;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import com.pankaj.fragment.demo.R;
    
    /**
     * Created by android_studio on 23/12/16.
     */
    
    public class FragmentThreeInnerB extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            //________create the object of main view for FragmentThreeInnerB
            View v = inflater.inflate(R.layout.fragment_three_inner_b, null);
    
            //________return the object of main view for FragmentThreeInnerB
            return v;
        }
    }
    



    fragment_three_inner_b.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fragment Three Inner Fragment B" />
    
    </LinearLayout>
    
    



  7.  Now Open your MainActivity.Java and update it. 
    package com.pankaj.fragment.demo;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import com.pankaj.fragment.demo.fragments.FragmentOne;
    import com.pankaj.fragment.demo.fragments.FragmentThree;
    import com.pankaj.fragment.demo.fragments.FragmentTwo;
    import com.pankaj.fragment.demo.utils.utils;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public void onBackPressed() {
    
      //_________check if stack has only one item then close the application else reopen the previous fragment 
            if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
                finish();
            } else {
                super.onBackPressed();
            }
    
        }
    
        //_________methode will be call when user click on Fragment One Button
        public void fragmentOne(View view) {
            FragmentManager manager = getSupportFragmentManager();
            utils.replaceFragment(new FragmentOne(), manager);
        }
    
        //_________methode will be call when user click on Fragment Two Button
        public void fragmentTwo(View view) {
            FragmentManager manager = getSupportFragmentManager();
            utils.replaceFragment(new FragmentTwo(), manager);}
    
        //_________methode will be call when user click on Fragment Three Button
        public void fragmentWhree(View view) {
            FragmentManager manager = getSupportFragmentManager();
            utils.replaceFragment(new FragmentThree(), manager);
        }
    }


  8. All the application development process with fragments has completed, Now run the app and click on buttons(fragment One,fragment Two,fragment Three, Fragment three Inner Fragment A, Fragment three Inner Fragment B, Fragment two Inner Fragment A, Fragment one Inner Fragment A, Fragment one Inner Fragment B).



  9. Good bye, Thanks to read this blog.