Android: How to add button below the listview - android

I bind the data from array-list to Listview as below:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListView lstView = getListView();
lstView.setChoiceMode(2);
ArrayList<String> listTODO = PrepareList();
lv_arr = (String[]) listTODO.toArray(new String[0]);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, lv_arr));
lstView.setTextFilterEnabled(true);
}
private ArrayList<String> PrepareList() {
ArrayList<String> todoItems = new ArrayList<String>();
todoItems.add("AAA");
todoItems.add("BBB");
todoItems.add("CCC");
todoItems.add("DDD");
return todoItems;
}
And the ListView have set multi-selected. I want to get the text have been selected on that Listview after button hv been clicked.
Anybody can help me to put the button below that ListView. I can't figure it out how to put the button coz it's didn't have the xml layout file.
.

add after super.onCreate
listView.setContentView(R.layout.your_custom_layout_for_listview)
take a look at this for more info

You can use a xml layout and out a button under the listview element. The only thing to make sure is to have a listview element with it
android:id="#android:id/list"

the line android:layout_weight="0.5" in ListView is very important.
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:soundEffectsEnabled="true" >
</ListView>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/myBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textSize="18dip" >
</Button>
</LinearLayout>
if you do not have xml layout, add these controls to a LinearLayout and set layout_weight in code like the following lines:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 0.5f);
myBtn.setLayoutParams(param);

Related

How do I populate the contents from EditText in ListView?

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);
}
}

Adding multiple TextViews into LinearLayout programmatically

There is long string. I am dividing long string into words and setting TextView for each word.
(You can ask why I need this function? When user clicks on textview(word), application shows the meaning of the word)
...
TextView tv = new TextView(this);
tv.setTextSize(24);
tv.setText(word);
ll.addView(tv);
...
My LinearLayout:
<LinearLayout
android:id="#+id/llReader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical" >
</LinearLayout>
If I put LinearLayout's orientation vertical, it is putting each TextView in new line.
For example:
word1
word2
word3
If I put it in horizontal orientation, it is putting all words only in one line:
word 1, word 2, word 3
In result word4,word5, word6 is not visible.
How to add TextViews programmatically in this way?
You should add Horizontal LinearLayout in your Vertical LinearLayout:
<LinearLayout
android:id="#+id/verticalLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/horizontalLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
In your activity,
private LinearLayout mReaderLayout;
protected void onCreate(Bundle savedInstanceState) {
mReaderLayout = (LinearLayout) findViewById(R.layout.llReader);
}
private void addText(String text) {
TextView textView = new TextView(this);
textView.setText(text);
mReaderLayout.addView(textView);
}
You can use gridview with adapter.
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
and in onCreate:
String[] strings= "Long string".split(" ");
gridView = (GridView) findViewById(R.id.gridview1);
// Create adapter to set value for grid view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strings);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText() , Toast.LENGTH_SHORT).show();
}
});
You can add use a RelativeLayout right away or you can add a two buttons in a linear, vertical Layout and then add that into a horizontal layout. Then add the next vertical linear layout with the other two buttons into the horizontal layout.

Android Mono List view

Hi Hope some one can help me i'm new to Mono and Android
i can do the ListActivity with code
eg. activity.cs code looks like this
[Activity(Label = "My Activity")]
public class TestScreen : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
string[] values = new String[] {"One", "Two", "Three"};
var list = new List<string>();
for (int i = 0; i < values.Length; ++i)
{
list.Add(values[i]);
}
ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, list);
and my result is the ListActivity in a single screen
like this
Cant post images :(
Single Screen With the list
What i would like to achieve is
i would like to create a Layout
and then pull the list into the layout
like this lest call this layout custom.axml
not sure how to link the data to my layout
Cant post images :(
this layout consist of 3 objects
Button
ListView --- want to populate the list view with the list
TextView
This is your layout at the bottom, minus the XML header.
To make it work, you have to modify your OnCreate method to load your listview this way.. since you have more items in your view than just the List for your activity.
SetContentView (Resource.Layout.YourLayoutFileName);
ourlist = FindViewById<ListView> (Android.Resource.Id.List);
ourlist.Adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, list);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:id="#+id/yourbutton"
android:layout_width="300dip"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Your Text"/>
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
<TextView
android:id="#+id/txtview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:textSize="20dip" />

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

Android setcolour listview row

I have a listview and two textveiws in it for two columns. My goal is to set the colour of e.g. the 5th row of the listview to blue. Details:
config.xml has the layout for the acitivty: buttons and the listview. Part of it:
<ListView android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView01">
</ListView>
row.xml defines the two coloumns for the listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linlay0">
<LinearLayout android:orientation="horizontal"
android:id="#+id/linlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/left"
android:layout_width="160px"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:paddingLeft="5px"/>
<TextView android:id="#+id/right"
android:layout_width="140px"
android:layout_height="wrap_content"
android:maxWidth="140px"
android:singleLine="false"/>
</LinearLayout>
</LinearLayout>
MainActivity.java is for the management for the activity, as well as the listview. So contentview is set to config.xml:setContentView(R.layout.config);
This is how i upload the listview with data:
lv1=(ListView)findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i=0; i<31; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("left1", date[i]);
map.put("right1", name[i]);
mylist.add(map);
}
simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);
date[i] and name[i] are arrays declared and uploaded at the beginning of the class.
I am running some queries, comparing arrays and now i want to the set colour of a specific row in the ListView to blue. Like i said contentview is set to config.xml, while the TextViews of the ListView is in row.xml.
So TextView txt1 = (TextView) findViewById(R.id.left); is uninterpretable for Eclipse.
I would have solved this by implemented my own adapter, and used the position parameter in the getView() method of the adapter to set the background of the LinearLayout by code.
In your CustomAdapter in getView method use this
if(position==5)
{
convertView.setBackgroundColor(Color.BLUE);
}
simpleAdapter is a system adpater,you can not control it.In my opioion,if you want set color at 5th row,you should implementation any adapter and override the getView or bindView method
,accord the position(row number) you can do anything you want

Categories

Resources