DonNadie
DonNadie

Reputation: 33

How to change between fragments with buttons inside the fragments in Kotlin?

I hope I am not asking a repeated question, but only found this answer in java and I am using Kotlin. I am trying to exchange between fragments using buttons. Here is my code. First of all, Thanks.

For main

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

And its xml file

           <fragment
            android:id="@+id/main"
            android:name="com.example.stackoverflow.fragment1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_fragment1"/>
</LinearLayout>

For Fragment1

      class fragment1 : Fragment(){
       override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?): View? {
        return Layoutinflater.inflate(R.layout.fragment_fragment1, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        button1.setBackgroundColor(Color.parseColor("#faaf45"))
     button1.setOnClickListener(){
           //Not sure what to put here
        }
}

And fragment2

       class fragment2 : Fragment(){
        override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?): View? {
        return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
    }
         override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
         button2.setBackgroundColor(Color.parseColor("#aaaf45"))
       button2.setOnClickListener(){
         //Not sure what to put here
        }
}

Upvotes: 2

Views: 3177

Answers (2)

Majid Sadeghi
Majid Sadeghi

Reputation: 188

In MainActivity:

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

    fun changeFragment(fragmnet:Fragment){
      getFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }
}

In MainActivity laout :

     <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 </LinearLayout>

In fragment1:

class fragment1 : Fragment(){
   override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View?  {
    return Layoutinflater.inflate(R.layout.fragment_fragment1, container,false)
    }
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    button1.setBackgroundColor(Color.parseColor("#faaf45"))
    button1.setOnClickListener(){
       (context as MainActivity).changeFragment(fragmnet2.newInstance())
    }
  companion object {
    fun newInstance(): fragment1 {
        return fragment1()
     }
    }
 }

In fragment2:

class fragment2 : Fragment(){
    override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
   }
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     super.onViewCreated(view, savedInstanceState)
     button2.setBackgroundColor(Color.parseColor("#aaaf45"))
   button2.setOnClickListener(){
     (context as MainActivity).changeFragment(fragmnet1.newInstance())
    }

  companion object {
      fun newInstance(): fragment2 {
        return fragment2()
       }
     }
 }

Upvotes: 1

haresh
haresh

Reputation: 1420

 class fragment1 : Fragment(){
   override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_fragment1, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    button1.setBackgroundColor(Color.parseColor("#faaf45"))
 button1.setOnClickListener(){
    supportFragmentManager.beginTransaction()
    .replace(R.id.container, fragment2()).commit()        }
 }

And fragment2

class fragment2 : Fragment(){
    override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
}
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     super.onViewCreated(view, savedInstanceState)
     button2.setBackgroundColor(Color.parseColor("#aaaf45"))
   button2.setOnClickListener(){
        supportFragmentManager.beginTransaction()
    .replace(R.id.container, fragment1()).commit() 
    }
 }

Upvotes: 0

Related Questions