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.
Related
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);
}
}
When i click an already existing element in my ListView, im changing its color, but i also want to place it at the bottom of the ListView
Is there a way to place a given View from a ListView to the bottom of the ListView?
Are you talking about footer view this how it is implemented.
Your footer xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="#string/footer_text_1"
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
</LinearLayout>
</LinearLayout>
Your activity class
public class MyListActivty extends ListActivity {
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
}
I've got a TextView and it's getting a list in it.
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
//Own row layout
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));
//listView.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
Now I want a small grey description of the list item, like here:
I read these tutorials: 1 and 2 but I have to admit, I don't understand what they say.
Can someone explain how to use them? Do I have to remove my TextView/List I have now? Is there another way to make a little description, without using a custom ListView?
Here's 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" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="2dp"/>
I don't really use the listview "list", just for testing purposes.
You need to understand that the following line
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));
means that you want to use layout.activity_home.xml for each item's view of your list.
So I don't see why there is a ListView inside it...
By default the ArrayAdapter uses one single text field per item. If you want a more complex item layout, you need to override the getView() method of your adapter.
Here is the simplest solution I can think of:
In Home.java:
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, Liste) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String item = getItem(position);
TextView subTitleView = (TextView) view.findViewById(R.id.subtitle);
subTitleView.setText("Subtitle of " + item);
return view;
}
});
}
}
And layout/list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
First use the setContentView and call the layout.. From the layout call the LISTVIEW. then using the adapter put the LISTE inside the list view use your data.
Let's say I need to take names of movies the person likes. I don't know how many he likes so I want to present him with just one text field. He can click a button and add more fields to fill. I can use ScrollView of course but should I think about using a ListView? If so, I can't seem to understand how. Please help
Better, add edittext dynamically inside scrollview, onclick of a button.
try this:
public class TestingActivity extends ListActivity {
/** Called when the activity is first created. */
EditText value;
Button insert;
ArrayList<String> data=new ArrayList<String>();
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
insert = (Button) findViewById(R.id.button2);
value=(EditText)findViewById(R.id.value);
insert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
data.add(value.getText().toString());
getListView().setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, data));
}
});
getListView().setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, data));
}
}
xml is:
<?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="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="insert" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollingCache="false" >
</ListView>
</LinearLayout>
You could implement an own BaseAdapter that uses EditText-Views to let people enter data.
Here's how to implement a good Adapter.
If the Button is getting clicked you can just add another entry in a List that is used to add another View to your Adapter.
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