How do I populate the contents from EditText in ListView? - android

I have a layout file in which I have defined a ListView and another layout file in which I have defined the style of the individual list element. OnClick how do I populate my ListView by using the other layout? Or is it even possible to do so?
This is my ListView
<ListView
android:id="#+id/chat_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
>
</ListView>
<RelativeLayout
android:id="#+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical">
</RelativeLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/chat"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/send_button"/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/icons_chat"
android:height="20dp"
android:width="20dp"
android:id="#+id/send_button"
android:onClick="sendChat"
android:layout_alignBottom="#+id/chat"
android:layout_alignParentRight="true"
/>
This is my other layout
<EditText
android:id="#+id/user_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
OnClick I want to getText from the EditText in the first layout and update the ListView using the style in the second layout. How can I do that?

I think that one way to do it is with an ArrayAdapter which will adapt your Collection (which you want to use) to your items in your layout ListView for your case. For example
public class YourActivity extends Activity {
private ListView listView;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
listView= (ListView) findViewById(R.id.you_list_id);
//Here you can set values from your edit texts
ArrayList<String> yourArrayList = new ArrayList<>();
yourArrayList.add("someTextFromYourEditTexts");
yourArrayList.add("someTextFromYourEditTexts");
//Create your ArrayAdapter with your already populate array list
ArrayAdapter<String> yourArrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
yourArrayList );
//And when your adapter is create set it to your list view
listView.setAdapter(yourArrayAdapter);
}
}

Related

How to create EditText with menu underneath?

I want to create EditText with clickable menu underneath. User should be able to write his own custom text into EditText or choose from predefined options.
I though about adding recyclerView under EditText, but for 4-5 options its waste of recylerView. Is there any way to do it better (I dont want to write a lot of code for this feature).
AutoCompleteTextView is a component used to show suggestions while writing in an editable text field. The suggestions list is shown in a drop-down menu from which a user can select the desired item. The list of suggestions is obtained from an adapter and it appears only after a number of characters that are specified in the threshold.
Here is a simple example of AutoCompleteTextView:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="X" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
The MainActivity.java is defined below.
public class MainActivity extends Activity {
String[] animals = {"Ant", "Bbird", "Cat"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.select_dialog_item, animals);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.BLACK);
}
}
try this,
xml
<LinearLayout 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:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="State:" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
java
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
String[] arr={"used","new","repaired"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
autoCompleteTextView=findViewById(R.id.autoCompleteTextView);
ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arr);
autoCompleteTextView.setAdapter(aa);
}
}

Android AutoCompleteTextView result in List instead of default dropdown

Android AutoCompleteTextView search and returns the result in List instead of default dropdown.
Is there a way that I could redesign and put the result of an AutoCompleteTextView into a list other than to its default list design?
In my MainActivity, I have this:
String[]fruits = {"apple", "avocado", "banana", "blackberry", "blueberry", "coconut", "durian", "mango", "melon"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, fruits);
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocom);
autoCompleteTextView.setAdapter(stringArrayAdapter);
autoCompleteTextView.setTextColor(Color.RED);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(stringArrayAdapter);
}
In My xml,
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.android.autocompletenativesample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="Pick a Fruit"
android:id="#+id/tv"
/>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/autocom"
android:layout_below="#+id/tv"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:completionThreshold="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListView Implementation"
android:layout_below="#+id/autocom"
android:id="#+id/tv2"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lv"
android:layout_below="#+id/tv2"
>
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RecyclerView Implementation"
android:layout_below="#+id/lv"
/>
</RelativeLayout>
Furthermore, here is a Visual Explanation. It will search in AutoCompleteTextView field but the result will not be displayed below instead on another layout.
Should I edit the ArrayAdapter ang change the layout, or have a CustomAutoCompleteTextView that implements AutoCompleteTextView or and EditText that implements AutoCompleteTextView.
I desire for my AutoCompleteTextView's Result to be formatted like this.
RecyclerView and CardView Result design
Thanks.
i can give you the logic. check out this link.
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and this too http://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html
and filteration is done in adapter so first bind listview with your empty adapter and in textWatcher bindlistview with datasource filteration and notifie for changes.

Adding view below listview

I have created listview using ListActivity
I want to add another view below that list.So how can I do that?
here are 2 files spacelist.xml and homescreen.java using which I have created list.
spacelist.xml-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#drawable/background"
android:layout_marginBottom="10dp">
<ImageView
android:id="#+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#+id/label"
android:textSize="20dp" >
</TextView>
</LinearLayout>
homeScreen.java-
public class homeScreen extends ListActivity{
String list[]={"My Ideas","Space1","Space2","All Notes","Create New Space"};
ListView spaces;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//To display as a list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spacelist, R.id.label, list);
setListAdapter(adapter);
}
}
Now suppose i want to add another view below list.How should I do?
It really depends on what you want to do with that view: always visible at the bottom of the view over the list? or is it fine with you to show that view at the bottom of the list?
First option, you could use an Activity (not ListActivity) and do whatever you want in your layout.
Second option, you can set a footer for your listview or you can have a +1 item in your listview and that item would be your view. Of course you have to manage different type of view in your adapter so it's better to set a footer.

AutoCompleteTextView causes items in ListView to disappear

I'm doing the Extra Credit section of the Adding a List section in Android Tutorials. I tried populating the AutoCompleteTextView (ACTV) with the items contained in the ArrayAdapter, but if the addr field has characters in it above the threshold limit, the list doesn't show any items. Here is the code:
public class MyActivity extends Activity
{
List<Restaurant> model = new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView)findViewById(R.id.restaurants);
adapter = new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.addr);
textView.setAdapter(adapter);
}
private View.OnClickListener onSave= new View.OnClickListener(){
...
};
}
And the XML...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="1"
android:layout_alignParentBottom="true"
>
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<AutoCompleteTextView android:id="#+id/addr"
android:completionThreshold="5"
/>
</TableRow>
<TableRow>
<TextView android:text="Type:"/>
<RadioGroup android:id="#+id/types">
<RadioButton android:id="#+id/take_out"
android:text="Take Out"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
/>
<RadioButton android:id="#+id/sit_down"
android:text="Sit Down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="#+id/delivery"
android:text="Delivery"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RadioGroup>
</TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/save"
android:text="Save"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/details"/>
</RelativeLayout>
Changing the completionThreshold changes the number of characters I can enter before the items in the List disappear.
Thanks for the help
You are attempting to use the same adapter for both the ListView and the AutoCompleteTextView. That is not a good idea. Please use separate adapters. You might even want a separate layout for the adapter used with the AutoCompleteTextView (e.g., android.R.layout.simple_dropdown_item_1line, as I used in this sample project).

Android- empty space at top of list view

I currently have a tabview with a listview inside. I only have three items in the listview and for some reason there is a bunch of white space at top. That is, the first item doesn't start at the top but rather in the middle. Any ideas? Also, i'm thinking that I am using the wrong layout. anything better than a listview if i only need to display three items? Thanks.
Code:
// Data to put in the ListAdapter
private String[] sdrPlaylistNames = new String[]{ "More Playlists...","Dubstep", "House"};
Intent playbackServiceIntentDUB, playbackServiceIntentHOUSE;
//alert dialog
AlertDialog.Builder builder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlists_layout);
//fill the screen with the list adapter
playlistFillData();
playbackServiceIntentDUB = new Intent(this, DUBAudioService.class);
playbackServiceIntentHOUSE = new Intent(this, HOUSEAudioService.class);
}
public void playlistFillData() {
//create and set up the Array adapter for the list view
ArrayAdapter<?> sdrListAdapter = new ArrayAdapter<Object>(this, R.layout.list_item, sdrPlaylistNames);
setListAdapter(sdrListAdapter);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/selectP" android:stackFromBottom="false">
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
<TextView
android:id="#+id/selectP"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Select A Playlist Above"
android:textColor="#FF3300"
android:textSize="22dp"
android:textStyle="bold" >
</TextView>
</RelativeLayout>
If you want to display only three items then I think you have to use TableLayout.
Android - Table Layout.
Oh I missed your real problem in my previous answer.
Simply set android:layout_alignParentTop="true" to your list view as
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/selectP" android:stackFromBottom="false">
</ListView>
Hope this time I am clear..
Set your list view to AlignParentTop = true

Categories

Resources