title: How do I read NFC tag just while user click the button on Android
body: <p>I need to read NFC tag when only user push a button.</p>
  <p>The following is code that I wrote but it does not work.</p>
  <p>I've tried that display NFC tag information to textview.<br />
  Nothing to display anything despite I hold up a NFC card.
  It looks like onNewIntent Event does not fire when NFC is present.</p>
  <p>Why following code is not work?</p>
  <hr />
  <p>Update:</p>
  <p>Now I tried to pass techlist argument to enableForegroundDispatch.
  It does not work yet.</p>
  <pre><code>techlist = arrayOf(
              arrayOf(android.nfc.tech.NfcA::class.java.name),
              arrayOf(android.nfc.tech.NfcB::class.java.name),
              arrayOf(android.nfc.tech.IsoDep::class.java.name),
              arrayOf(android.nfc.tech.MifareClassic::class.java.name),
              arrayOf(android.nfc.tech.NfcV::class.java.name),
              arrayOf(android.nfc.tech.NfcF::class.java.name),
              arrayOf(android.nfc.tech.NdefFormatable::class.java.name),
              arrayOf(android.nfc.tech.MifareUltralight::class.java.name)
          )
  </code></pre>
  <hr />
  <pre><code>class MainActivity : AppCompatActivity() {
      var pendintent: PendingIntent? = null
      var nfc_adapt: NfcAdapter? = null
      var iFilter: IntentFilter? = null
      var tview: TextView? = null
      var read_button:  Button? = null // read button
      var delete_button: Button? = null // delete button for content of text view that display tag information
      @SuppressLint(&quot;ClickableViewAccessibility&quot;)
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          tview = findViewById(R.id.textView) // 表示用のTextView
          nfc_adapt = NfcAdapter.getDefaultAdapter(applicationContext)
          pendintent = PendingIntent.getActivity(this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
          )
          iFilter = IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)
          val iFilters = arrayOf&lt;IntentFilter&gt;(iFilter!!)
  
          read_button = findViewById(R.id.button)
          delete_button = findViewById(R.id.button2)
          delete_button!!.isEnabled = false
  
          read_button!!.setOnTouchListener(fun(v: View, m: MotionEvent) : Boolean {
              when (m.action) {
                  MotionEvent.ACTION_DOWN -&gt; {
                      tview!!.setBackgroundColor(Color.rgb(0,0,0))
                      tview!!.setTextColor(Color.rgb(0, 255, 0))
                      tview!!.text = &quot;Now Reading A Tag&quot;
                      Log.d(null, &quot;ACTION UPPED&quot;)
                      nfc_adapt!!.enableForegroundDispatch(this, pendintent,iFilters, null)
                      return false
                  }
                  MotionEvent.ACTION_UP -&gt; {
                      nfc_adapt!!.disableForegroundDispatch(this)
                      tview!!.text = &quot;&quot;
                      return false
                  }
              }
              return false
          })
  
          // Delete contents of TextView
  
          delete_button!!.setOnClickListener {
              tview!!.text = &quot;&quot;
              tview!!.setBackgroundColor(Color.rgb(0, 0, 0))
              tview!!.setTextColor(Color.rgb(0, 255, 0))
              read_button!!.isEnabled = true
              it.isEnabled = false
          }
  
      }
  
      override fun onNewIntent(intent: Intent) {
          super.onNewIntent(intent)
          val action: String? = intent.action
          if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
              var nfc_tag_array = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_TAG)
              if (nfc_tag_array == null) {
                  tview?.setBackgroundColor(Color.YELLOW)
                  tview?.setTextColor(Color.rgb(0,0,0))
                  tview?.text =  &quot;read a tag but does not contain tag data&quot;
                  return
              }
              val t = nfc_tag_array[0] as Tag?
              if (t == null) {
                  tview!!.text = &quot;Read a tag but it a null&quot;
              } else {
                  tview!!.text = t.toString() // print tag information as a string
              }
              tview!!.setBackgroundColor(Color.YELLOW)
              tview!!.setTextColor(Color.rgb(0,0,0))
              nfc_adapt!!.disableForegroundDispatch(this)
              read_button!!.isEnabled = false // disable read button until user push the delete button
              delete_button!!.isEnabled = true
  
  
          }
          else {
              // nothing to do
          }
      }
  }
  </code></pre>