Android Kotlin Tutorial Day 4

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/SplashFragment">

    <fragment
        android:id="@+id/FirstFragment"
        android:name="com.example.myapplication.FirstFragment"
        android:label="@string/first_fragment_label"
        tools:layout="@layout/fragment_first">

        <action
            android:id="@+id/action_FirstFragment_to_SecondFragment"
            app:destination="@id/SecondFragment" />
    </fragment>

    <fragment
        android:id="@+id/SecondFragment"
        android:name="com.example.myapplication.SecondFragment"
        android:label="@string/second_fragment_label"
        tools:layout="@layout/fragment_second">

        <action
            android:id="@+id/action_SecondFragment_to_FirstFragment"
            app:destination="@id/FirstFragment" />
    </fragment>


    <fragment
        android:id="@+id/SplashFragment"
        android:name="com.example.myapplication.SplashFragment"
        android:label="Splash"
        tools:layout="@layout/fragment_splash">

        <action
            android:id="@+id/action_Splash_to_Register"
            app:destination="@id/RegisterFragment" />

        <action
            android:id="@+id/action_Splash_to_Home"
            app:destination="@id/HomeFragment" />
    </fragment>

    <fragment
        android:id="@+id/RegisterFragment"
        android:name="com.example.myapplication.RegisterFragment"
        android:label="Register"
        tools:layout="@layout/fragment_register">

        <action
            android:id="@+id/action_Register_to_Login"
            app:destination="@id/LoginFragment" />
    </fragment>

    <fragment
        android:id="@+id/LoginFragment"
        android:name="com.example.myapplication.LoginFragment"
        android:label="Login"
        tools:layout="@layout/fragment_login">

        <action
            android:id="@+id/action_Login_to_Home"
            app:destination="@id/HomeFragment" />
    </fragment>

    <fragment
        android:id="@+id/HomeFragment"
        android:name="com.example.myapplication.HomeFragment"
        android:label="Home"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/action_Home_to_Profile"
            app:destination="@id/ProfileFragment" />

        <action
            android:id="@+id/action_Home_to_Category"
            app:destination="@id/CategoryFragment" />


    </fragment>

    <fragment
        android:id="@+id/ProfileFragment"
        android:name="com.example.myapplication.ProfileFragment"
        android:label="My Profile"
        tools:layout="@layout/fragment_profile">

        <!--<action
            android:id="@+id/action_SecondFragment_to_FirstFragment"
            app:destination="@id/FirstFragment" />-->
    </fragment>

    <fragment
        android:id="@+id/CategoryFragment"
        android:name="com.example.myapplication.CategoryFragment"
        android:label="Category"
        tools:layout="@layout/fragment_category">

        <!--<action
            android:id="@+id/action_SecondFragment_to_FirstFragment"
            app:destination="@id/FirstFragment" />-->
    </fragment>

</navigation>

fragment_splash.xml

<?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"
    android:gravity="center"
    tools:context=".SplashFragment">

   <ImageView
       android:src="@drawable/ic_android_black_24dp"
       android:layout_width="100dp"
       android:layout_height="100dp"/>

    <TextView
        android:textColor="#4CAF50"
        android:textSize="30dp"
        android:text="Welcome to My App"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

SplashFragment.kt

package com.example.myapplication

import android.R
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import com.example.myapplication.R.layout.fragment_splash


// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [SplashFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class SplashFragment : Fragment() {

    private val MyPREFERENCES: String? = "ABCD"

    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment


        return inflater.inflate(fragment_splash, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        val sharedpreferences = requireContext().getSharedPreferences(
            MyPREFERENCES,
            Context.MODE_PRIVATE
        )


        if( ! sharedpreferences.getString("umobile","default").equals("default") )
        {
            findNavController().navigate(com.example.myapplication.R.id.action_Splash_to_Home)

        }
        else
        {
            val handler = Handler()
            handler.postDelayed(Runnable {
                findNavController().navigate(com.example.myapplication.R.id.action_Splash_to_Register)
            }, 1000)

        }



    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment SplashFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            SplashFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

fragment_register.xml

<?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"
    android:gravity="top|center_horizontal"
    tools:context=".RegisterFragment">

    <ImageView
        android:src="@drawable/ic_android_black_24dp"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <TextView
        android:textColor="#4CAF50"
        android:textSize="30dp"
        android:text="Register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/etname"
        android:hint="Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/etmobile"
        android:hint="Mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/etemail"
        android:hint="Email Address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/etpass"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnsubmit"
        android:text="Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_marginTop="30dp"
        android:text="Existing User? Click Here to Login"
        android:id="@+id/tvlogin"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

</LinearLayout>

RegisterFragment.kt

package com.example.myapplication

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.navigation.fragment.findNavController
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.myapplication.databinding.FragmentRegisterBinding
import org.json.JSONObject

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [RegisterFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class RegisterFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private var _binding: FragmentRegisterBinding? = null

    private val binding get() = _binding!!

    private lateinit var etname: EditText
    private lateinit var etmobile: EditText
    private lateinit var etemail: EditText
    private lateinit var etpassword: EditText

    private lateinit var btnsubmit: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentRegisterBinding.inflate(inflater, container, false)
        return binding.root

        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_register, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        etname = view.findViewById(R.id.etname)
        etmobile = view.findViewById(R.id.etmobile)
        etemail = view.findViewById(R.id.etemail)
        etpassword = view.findViewById(R.id.etpass)


        binding.btnsubmit.setOnClickListener {

            Log.e("cs", etname.text.toString())

            callAPI(etname.text.toString(),etmobile.text.toString(),etemail.text.toString(),etpassword.text.toString())

            //Toast.makeText(context, "Hello I am Toast :)", Toast.LENGTH_SHORT).show()
            //val hview = binding.navView.getHeaderView(0)
            //val hviewname: TextView = hview.findViewById(R.id.tvname)
           // var editTextHello = etname .text.toString()
           // findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }

        binding.tvlogin.setOnClickListener {

            //Log.e("cs", etname.text.toString())

            //callAPI(etname.text.toString(),etmobile.text.toString(),etemail.text.toString(),etpassword.text.toString())

            //Toast.makeText(context, "Hello I am Toast :)", Toast.LENGTH_SHORT).show()
            //val hview = binding.navView.getHeaderView(0)
            //val hviewname: TextView = hview.findViewById(R.id.tvname)
            // var editTextHello = etname .text.toString()
             findNavController().navigate(R.id.action_Register_to_Login)
        }


        

    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment RegisterFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            RegisterFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }


    fun callAPI(p1: String,p2: String,p3: String,p4: String) {

        // Instantiate the RequestQueue.
        val queue: RequestQueue = Volley.newRequestQueue(context)
        val url = "https://abc.com/androidapi/register.php?p1=" + p1 + "&p2=" + p2+ "&p3=" + p3 + "&p4=" + p4

        Log.e("cs", url)

        // Request a string response from the provided URL.
        val stringRequest = StringRequest(


            Request.Method.GET, url,
            Response.Listener<String> { response ->

                Log.e("cs", response)
                try {
                    val obj = JSONObject(response)

                    if ( obj.getString("p1").equals("1"))
                    {
                        findNavController().navigate(R.id.action_Register_to_Login)
                    }
                    else
                    {
                        Toast.makeText(context, obj.getString("p2"), Toast.LENGTH_SHORT).show()
                    }

                    //Log.e("cs ada", obj.getString("p1") )
                    //Log.e("cs ada", obj.getString("p2") )
                    //Log.e("cs", obj.getString("id"))
                    //Log.e("cs", obj.getString("title"))
                    //Log.e("cs", obj.getString("completed"))
                } catch (t: Throwable) {
                    Log.e("cs", "Could not parse malformed JSON: \"" + "" + "\"")
                }
            }, object : Response.ErrorListener {
                override fun onErrorResponse(error: VolleyError) {
                    Log.e("cs", error.toString())
                }
            })

        // Add the request to the RequestQueue.
        queue.add(stringRequest)
    }

}

fragment_login.xml

<?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"
    android:gravity="center_horizontal"
    tools:context=".LoginFragment">


    <ImageView
        android:src="@drawable/ic_android_black_24dp"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <TextView
        android:textColor="#4CAF50"
        android:textSize="30dp"
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/etmobile"
        android:hint="Mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/etpass"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnsubmit"
        android:text="Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>



</LinearLayout>

LoginFragment.kt

package com.example.myapplication

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.navigation.fragment.findNavController
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.myapplication.databinding.FragmentLoginBinding
import org.json.JSONObject

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [LoginFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class LoginFragment : Fragment() {

    private lateinit var etmobile: EditText
    private lateinit var etpassword: EditText
    private var MyPREFERENCES: String? = "ABCD"

    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private var _binding: FragmentLoginBinding? = null

    private val binding get() = _binding!!


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentLoginBinding.inflate(inflater, container, false)
        return binding.root

        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_login, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        etmobile = view.findViewById(R.id.etmobile)

        etpassword = view.findViewById(R.id.etpass)

        binding.btnsubmit.setOnClickListener {

            callAPI(etmobile.text.toString(),etpassword.text.toString())

            //Log.e("cs", etname.text.toString())
            //callAPI()
            //Toast.makeText(context, "Pref Saved", Toast.LENGTH_SHORT).show()
            //findNavController().navigate(com.example.myapplication.R.id.action_Login_to_Home)
           //val hview = binding.navView.getHeaderView(0)
            //val hviewname: TextView = hview.findViewById(R.id.tvname)
            // var editTextHello = etname .text.toString()
           // findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }
    }



    fun callAPI(p1: String,p2: String) {

        // Instantiate the RequestQueue.
        val queue: RequestQueue = Volley.newRequestQueue(context)
        val url = "https://abc.com/androidapi/login.php?p1=" + p1 + "&p2=" + p2

        Log.e("cs", url)

        // Request a string response from the provided URL.
        val stringRequest = StringRequest(


            Request.Method.GET, url,
            Response.Listener<String> { response ->

                Log.e("cs", response)
                try {
                    val obj = JSONObject(response)

                    if ( obj.getString("p1").equals("1"))
                    {

                        val sharedpreferences = requireContext().getSharedPreferences(
                            MyPREFERENCES,
                            Context.MODE_PRIVATE
                        )
                        val name = obj.getString("p2")
                        val email = obj.getString("p3")
                        val editor = sharedpreferences.edit()
                        editor.putString("uname", name  )
                        editor.putString("umobile", etmobile.text.toString()  )
                        editor.putString("uemail", email  )
                        editor.putString("upassword", etpassword.text.toString())
                        editor.commit()


                        findNavController().navigate(R.id.action_Login_to_Home)

                    }
                    else
                    {
                        Toast.makeText(context, obj.getString("p2"), Toast.LENGTH_SHORT).show()
                    }

                    //Log.e("cs ada", obj.getString("p1") )
                    //Log.e("cs ada", obj.getString("p2") )
                    //Log.e("cs", obj.getString("id"))
                    //Log.e("cs", obj.getString("title"))
                    //Log.e("cs", obj.getString("completed"))
                } catch (t: Throwable) {
                    Log.e("cs", "Could not parse malformed JSON: \"" + "" + "\"")
                }
            }, object : Response.ErrorListener {
                override fun onErrorResponse(error: VolleyError) {
                    Log.e("cs", error.toString())
                }
            })

        // Add the request to the RequestQueue.
        queue.add(stringRequest)
    }


    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment LoginFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            LoginFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

fragment_home.xml

<?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=".HomeFragment">


    <ImageView
        android:src="@drawable/ic_android_black_24dp"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>

    <LinearLayout
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_marginRight="10dp"

            android:background="#FF000000"
            android:id="@+id/btnmyprofile"
            android:text="My Profile"
            android:layout_width="100dp"
            android:layout_height="100dp"/>

        <Button
            android:background="#FF000000"
            android:id="@+id/btncategory"
            android:text="Category"
            android:layout_width="100dp"
            android:layout_height="100dp"/>

    </LinearLayout>

    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_marginRight="10dp"
            android:background="#FF000000"
            android:text="Change Pass"
            android:layout_width="100dp"
            android:layout_height="100dp"/>

        <Button
            android:background="#FF000000"
            android:id="@+id/btnlogout"
            android:text="Logout"
            android:layout_width="100dp"
            android:layout_height="100dp"/>

    </LinearLayout>

</LinearLayout>

HomeFragment.kt

package com.example.myapplication

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.navigation.fragment.findNavController
import com.example.myapplication.databinding.FragmentHomeBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"



/**
 * A simple [Fragment] subclass.
 * Use the [HomeFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class HomeFragment : Fragment() {
    private val MyPREFERENCES: String? = "ABCD"

    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private var _binding: FragmentHomeBinding? = null

    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val sharedpreferences = requireContext().getSharedPreferences(
            MyPREFERENCES,
            Context.MODE_PRIVATE
        )
        val umobie = sharedpreferences.getString("umobile","default")
        //val umobie = sharedpreferences.getString("umobile","default")
        //val editor = sharedpreferences.edit()

        Toast.makeText(context, "Welcome " + umobie, Toast.LENGTH_SHORT).show()

        binding.btnmyprofile.setOnClickListener {
            findNavController().navigate(R.id.action_Home_to_Profile)
            //Log.e("cs", etname.text.toString())
            //callAPI()
            //Toast.makeText(context, "Pref Saved", Toast.LENGTH_SHORT).show()
            //findNavController().navigate(com.example.myapplication.R.id.action_Login_to_Home)
            //val hview = binding.navView.getHeaderView(0)
            //val hviewname: TextView = hview.findViewById(R.id.tvname)
            // var editTextHello = etname .text.toString()
            // findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }
        binding.btncategory.setOnClickListener {
            findNavController().navigate(R.id.action_Home_to_Category)
            //Log.e("cs", etname.text.toString())
            //callAPI()
            //Toast.makeText(context, "Pref Saved", Toast.LENGTH_SHORT).show()
            //findNavController().navigate(com.example.myapplication.R.id.action_Login_to_Home)
            //val hview = binding.navView.getHeaderView(0)
            //val hviewname: TextView = hview.findViewById(R.id.tvname)
            // var editTextHello = etname .text.toString()
            // findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }

        binding.btnlogout.setOnClickListener {

            val sharedpreferences = requireContext().getSharedPreferences(
                MyPREFERENCES,
                Context.MODE_PRIVATE
            )

            val editor = sharedpreferences.edit()
            editor.remove("uname")
            editor.remove("umobile")
            editor.remove("uemail")
            editor.remove("upassword")
            editor.commit()

            //findNavController().popBackStack()

            //Log.e("cs", etname.text.toString())
            //callAPI()
            //Toast.makeText(context, "Pref Saved", Toast.LENGTH_SHORT).show()
            //findNavController().navigate(com.example.myapplication.R.id.action_Login_to_Home)
            //val hview = binding.navView.getHeaderView(0)
            //val hviewname: TextView = hview.findViewById(R.id.tvname)
            // var editTextHello = etname .text.toString()
            // findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }


    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment HomeFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            HomeFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

fragment_profile.xml

<?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"
    android:gravity="center_horizontal"
    tools:context=".ProfileFragment">



    <ImageView
        android:src="@drawable/ic_android_black_24dp"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <TextView
        android:textColor="#4CAF50"
        android:textSize="30dp"
        android:text="My Profile"
        android:layout_marginBottom="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <TextView
        android:textColor="#4CAF50"
        android:textSize="20dp"
        android:text="Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvname"
        android:textColor="#000000"
        android:textSize="20dp"
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <TextView
        android:textColor="#4CAF50"
        android:textSize="20dp"
        android:text="Mobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvmobile"
        android:textColor="#000000"
        android:textSize="20dp"
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:textColor="#4CAF50"
        android:textSize="20dp"
        android:text="Email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvemail"
        android:textColor="#000000"
        android:textSize="20dp"
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>



    <Button
        android:id="@+id/btnsubmit"
        android:text="Go Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

ProfileFragment.kt

package com.example.myapplication

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.navigation.fragment.findNavController
import com.example.myapplication.databinding.FragmentLoginBinding
import com.example.myapplication.databinding.FragmentProfileBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [ProfileFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ProfileFragment : Fragment() {
    private val MyPREFERENCES: String? = "ABCD"


    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private var _binding: FragmentProfileBinding? = null
    private val binding get() = _binding!!

    private lateinit var tvname: TextView
    private lateinit var tvmobile: TextView
    private lateinit var tvemail: TextView
    private lateinit var tvpassword: TextView



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_profile, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        tvname = view.findViewById(R.id.tvname)
        tvmobile = view.findViewById(R.id.tvmobile)
        tvemail = view.findViewById(R.id.tvemail)


        val sharedpreferences = requireContext().getSharedPreferences(
            MyPREFERENCES,
            Context.MODE_PRIVATE
        )
        tvname.setText(sharedpreferences.getString("uname","default"))
        tvmobile.setText(sharedpreferences.getString("umobile","default"))
        tvemail.setText(sharedpreferences.getString("uemail","default"))



        binding.btnsubmit.setOnClickListener {

            findNavController().popBackStack()

            //callAPI(etmobile.text.toString(),etpassword.text.toString())

            //Log.e("cs", etname.text.toString())
            //callAPI()
            //Toast.makeText(context, "Pref Saved", Toast.LENGTH_SHORT).show()
            //findNavController().navigate(com.example.myapplication.R.id.action_Login_to_Home)
            //val hview = binding.navView.getHeaderView(0)
            //val hviewname: TextView = hview.findViewById(R.id.tvname)
            // var editTextHello = etname .text.toString()
            // findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }
    }


    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment ProfileFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ProfileFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

fragment_category.xml

<?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=".CategoryFragment">


    <ListView
        android:id="@+id/list1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

CategoryFragment.kt

package com.example.myapplication

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.navigation.fragment.findNavController
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONObject

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [CategoryFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class CategoryFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    var list1: ListView? = null
    var courses = arrayOf("BCA", "PGDCA", "MCA", "MBA")

   // private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view: View = inflater.inflate(R.layout.fragment_category, container, false)

        list1 = view.findViewById<View>(R.id.list1) as ListView


       /* val arr1: ArrayAdapter<String>

        arr1 = ArrayAdapter(
            requireContext(),
            androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
            courses
        )
        list1!!.adapter = arr1*/

        // Inflate the layout for this fragment

        // Inflate the layout for this fragment
        return view


        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_category, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        callAPI()
    }


    fun callAPI() {

        var categoryarray = arrayOf("")

        // Instantiate the RequestQueue.
        val queue: RequestQueue = Volley.newRequestQueue(context)
        val url = "https://arthconsultancyservices.com/androidapi/category.php"

        Log.e("cs", url)

        // Request a string response from the provided URL.
        val stringRequest = StringRequest(


            Request.Method.GET, url,
            Response.Listener<String> { response ->

                Log.e("cs", response)
                try {

                    val arr = JSONArray(response)

                    Log.e("cs lenght", arr.length().toString())



                    for(i in 0..arr.length()-1)
                    {
                        val obj = JSONObject(arr.get(i).toString() )

                        //Log.e("cs", obj.getString("p1"))
                       // Log.e("cs", obj.getString("p2"))
                        Log.e("cs", obj.getString("p3"))

                        if(i==0)
                        {
                            categoryarray.set(0,obj.getString("p3"))
                        }
                        else
                        {
                            categoryarray += obj.getString("p3")

                        }



                    }


                        //if ( obj.getString("p1").equals("1"))
                        //{

                            //findNavController().navigate(R.id.action_Register_to_Login)
                        //}
                        //else
                        //{
                            //Toast.makeText(context, obj.getString("p2"), Toast.LENGTH_SHORT).show()
                       // }

                    //}


                    //Log.e("cs ada", obj.getString("p1") )
                    //Log.e("cs ada", obj.getString("p2") )
                    //Log.e("cs", obj.getString("id"))
                    //Log.e("cs", obj.getString("title"))
                    //Log.e("cs", obj.getString("completed"))
                } catch (t: Throwable) {
                    Log.e("cs", "Could not parse malformed JSON: \"" + "" + "\"")
                }



                //list1 = view.findViewById<View>(R.id.list1) as ListView
                val arr1: ArrayAdapter<String>

                arr1 = ArrayAdapter(
                    requireContext(),
                    androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
                    categoryarray
                )
                list1!!.adapter = arr1




            }, object : Response.ErrorListener {
                override fun onErrorResponse(error: VolleyError) {
                    Log.e("cs", error.toString())
                }
            })

        // Add the request to the RequestQueue.
        queue.add(stringRequest)
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment CategoryFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            CategoryFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}