skip to Main Content

I am working on a sample code and I want a Button showing a message but it didn’t work. App will open but buttonn didn’t work

This app should show a Toast but it does not. I tried lateinit var firstComend : Button but didn’t work too

I should sat will converting Activity to Fragment

Please help me

package com.mysfk.android.frogments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.mysfk.android.R
import android.graphics.Color
import android.telephony.SmsManager
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.EditText
import android.widget.Toast


private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"



class OrderFragment : Fragment() {
    private var param1: String? = null
    private var param2: String? = null

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

     var firstComend = view?.findViewById<Button>(R.id.firstButton)
        firstComend?.setOnClickListener {
  
            Toast.makeText(context, "ارسال شد", Toast.LENGTH_SHORT).show()
        }

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_order, container, false)
    }

    companion object {
    
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            MessageFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

2

Answers


  1. Try to set your click listener inside the onCreateView, like this:

       override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
    
            return inflater.inflate(R.layout.fragment_order, container, false)
            var firstComend = view?.findViewById<Button>(R.id.firstButton)
            firstComend?.setOnClickListener {
      
                Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show()
            }
        }
    
    Login or Signup to reply.
  2. All view related decision must be done in onCreateView or onViewCreated

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
    
            return inflater.inflate(R.layout.fragment_order, container, false)
        }
    
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?){
            var firstComend = view?.findViewById<Button>(R.id.firstButton)
            firstComend?.setOnClickListener {
               Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show()
            }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search