Add items to grid view dynamically in android using EditText example
In this Article today learn to Add items to grid view dynamically in android using EditText example.
android studio using a grid view enter the value to edit textbox add to grid view value in-app. so follow the code.
package com.akash.passvalue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView gridview;
String[] item = new String[]{
"Android",
"Java",
"C#",
"Html",
"Php",
"python"
};
Button button;
List<String> ITEM_LIST;
ArrayAdapter<String> arrayadapter;
EditText edittext;
String GetItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView)findViewById(R.id.gridView1);
button = (Button)findViewById(R.id.button1);
edittext = (EditText)findViewById(R.id.editText1);
ITEM_LIST = new ArrayList<String>(Arrays.asList(item));
arrayadapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, ITEM_LIST);
gridview.setAdapter(arrayadapter);
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GetItem = edittext.getText().toString();
ITEM_LIST.add(ITEM_LIST.size(),GetItem);
arrayadapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Langauge Added SuccessFully", Toast.LENGTH_LONG).show();
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter Value Here" >
</EditText>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:numColumns="3" >
</GridView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:text="Click Here to Add Item" />
</RelativeLayout>
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
In this Article today learn to Add items to grid view dynamically in android using EditText example.
android studio using a grid view enter the value to edit textbox add to grid view value in-app. so follow the code.
1.MainActivity.java:
package com.akash.passvalue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView gridview;
String[] item = new String[]{
"Android",
"Java",
"C#",
"Html",
"Php",
"python"
};
Button button;
List<String> ITEM_LIST;
ArrayAdapter<String> arrayadapter;
EditText edittext;
String GetItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView)findViewById(R.id.gridView1);
button = (Button)findViewById(R.id.button1);
edittext = (EditText)findViewById(R.id.editText1);
ITEM_LIST = new ArrayList<String>(Arrays.asList(item));
arrayadapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, ITEM_LIST);
gridview.setAdapter(arrayadapter);
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GetItem = edittext.getText().toString();
ITEM_LIST.add(ITEM_LIST.size(),GetItem);
arrayadapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Langauge Added SuccessFully", Toast.LENGTH_LONG).show();
}
});
}
}
2.activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter Value Here" >
</EditText>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:numColumns="3" >
</GridView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:text="Click Here to Add Item" />
</RelativeLayout>
3.dimens.xml:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
4.strings.xml:
<resources>
<string name="app_name">pass value</string>
</resources>
5.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.passvalue">
<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=".TextValue"></activity>
<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