Blog purpose for android basic example for android app developer. any query please my contact

Wednesday, 15 February 2023

kotlin autocomplete textview example

 kotlin autocomplete textview example


Sure, I can help you with that! Here's an example of how you can implement an autocomplete TextView in Android using Kotlin:


activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/linear_layout"

android:gravity="center">


<AutoCompleteTextView

android:id="@+id/autoTextView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:hint="@string/hint"/>


<Button

android:id="@+id/btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/submit"/>


</LinearLayout>




2.String.xml:


<resources>

<string name="app_name">AutoCompleteTextViewInKotlin</string>

<string name="hint">Please type language...</string>

<string name="submit">Submit</string>

<string name="submitted_lang">Submitted language:</string>


<string-array name="Languages">

<item>Java</item>

<item>Kotlin</item>

<item>Swift</item>

<item>Python</item>

<item>Scala</item>

<item>Perl</item>

<item>Javascript</item>

<item>Jquery</item>

</string-array>


</resources>



3. MainActivity.kt:

     import android.os.Bundle

 import android.view.View

 import androidx.appcompat.app.AppCompatActivity

 import android.widget.ArrayAdapter

 import android.widget.AutoCompleteTextView 

import android.widget.Button

 import android.widget.Toast


class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?)

{

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)


val autotextView

= findViewById<AutoCompleteTextView>(R.id.autoTextView)

// Get the array of languages

val languages

= resources.getStringArray(R.array.Languages)

// Create adapter and add in AutoCompleteTextView

val adapter

= ArrayAdapter(this,

android.R.layout.simple_list_item_1, languages)

autotextView.setAdapter(adapter)


val button

= findViewById<Button>(R.id.btn) if (button != null)

{

button ?.setOnClickListener(View.OnClickListener {

val enteredText = getString(R.string.submitted_lang) + " " + autotextView.getText() Toast.makeText(this @MainActivity, enteredText, Toast.LENGTH_SHORT).show()

})

}

}

}




4.MainFeast.xml:


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http:// schemas.android.com/apk/res/android"

package="com.Autocomplete.kotlinapp">


<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>


</manifest>


No comments:

Post a Comment