Add AutoCompleteTextview To Your App
1)Navigate to activity_main.xml .You have to add some code here. Watch the above video for reference .
After adding the code, your activity_main.xml will look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autocompleteView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:hint="Enter Your Favourite Color" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Submit"
android:textAllCaps="false"
android:textColor="#ffffff"
android:theme="@style/MyButton" />
</RelativeLayout>
2)Modify the styles.xml as follows
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorButtonNormal">@color/colorButtonNormal</item>
</style>
<style name="MyButton" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">@color/colorButtonNormal</item>
<item name="colorControlHighlight">@color/colorPrimaryDark</item>
</style>
</resources>
3) Now Open strings.xml and make the changes as follows
<resources>
<string name="app_name">SketchLogicApp</string>
<string-array name="color_list">
<item>Red</item>
<item>Blue</item>
<item>Green</item>
<item>White</item>
<item>Black</item>
<item>Yellow</item>
<item>Orange</item>
<item>Brown</item>
</string-array>
</resources>
4) Next , Open colors.xml and make the changes as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorButtonNormal">#2889ff</color>
</resources>
5) After making all these changes ,navigate to MainActivity.java and make the some changes.Your MainActivity.java file will look like this after making the changes
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int layoutItemId = android.R.layout.simple_dropdown_item_1line;
String[] color_list = getResources().getStringArray(R.array.color_list);
List<String> color = Arrays.asList(color_list);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, layoutItemId, color_list);
AutoCompleteTextView autocompleteView =
(AutoCompleteTextView) findViewById(R.id.autocompleteView);
autocompleteView.setAdapter(adapter);
}
}
6) Now Run and test your app on your real device or emulator.
If you get errors ,comment below
How to change sketchware project to android studio.
ReplyDelete