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" />
Related
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.
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);
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
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
I'm just starting to develop on Android so this may be a very basic thing, but. . .
I'm having a really hard time setting up a ListView within a LinearLayout. Maybe it's a problem in my XML layout, or maybe it's a problem with my databinding code.
I'm confused by the many tutorials that say that my Activity should extend ListActivity. Why would this one widget require that my entire UI be changed?
The thing is, I only want the scrolling box of items to occupy a part of this one screen. Is ListView the wrong widget to use? Should I be using a ScrollView instead?
Thanks -- here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/enter_text"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="#+id/txtTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:width="200px"/>
</LinearLayout>
<ListView android:id="#+id/listRecent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And here's my code:
public class MyApp extends Activity {
private String TAG = "MyApp";
//UI ELEMENTS
EditText txtTo;
ListView listRecent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//GET REFERENCES TO UI ELEMENTS
listRecent = (ListView) this.findViewById(R.id.listRecent);
try {
//----------------------------------------
// create the grid item mapping
String[] from = new String[] {"zero", "one", "two", "three"};
int[] to = new int[] {0, 1, 2, 3};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.recently_dingdonged, from, to);
listRecent.setAdapter(adapter);
//----------------------------------------
}
catch(Exception e) {
Log.e(TAG, "Error here (3116019)", e);
}
}
ListView should be nested inside of scrollview. By extending listActivity you gain the ability to setAdapter and populate the data into your list view through the main object and not through instantiating the list. This is by no means a requirement though.
You don't need to make your Activity extend ListActivity. You can, but it's not necessary.
In relation with your xml, try this out:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/enter_text"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/txtTo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="0.5"
android:width="200px"/>
</LinearLayout>
<ListView android:id="#+id/listRecent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>