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
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);
}
}
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.
Im trying to make a grid view with ImageButtons dynamically and i have this problem.
before starting i must say I'm new with android and java development (2.5 years of Objective-c).
Ok. so im using this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_background);
ArrayList<ImageButton> tmpStrRay = new ArrayList<ImageButton>();
Boolean load=true;
for (int i= 0;load;i++){
ImageButton iv = new ImageButton(this);
InputStream ims;
try {
ims = getAssets().open("sm_backgrounds/bgSM_"+i+".jpg");
Drawable d = Drawable.createFromStream(ims, null);
iv.setImageDrawable(d);
tmpStrRay.add(iv);
} catch (IOException e) {
load = false;
e.printStackTrace();
}
System.out.print(i);
}
GridView grid = (GridView) findViewById(R.id.gridView);
ArrayAdapter<ImageButton> adapter = new ArrayAdapter<ImageButton>(this, android.R.layout.simple_list_item_1, tmpStrRay);
grid.setAdapter(adapter);
}
and i have this XML in my layout:
<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"
tools:context=".BackgroundSelector" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:id="#+id/topLinear">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="close"
android:text="Close"
android:textSize="15sp" />
<Button
android:id="#+id/Button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showCamera"
android:text="Camera"
android:textSize="15sp" />
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/topLinear" >
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" >
</GridView>
</ScrollView>
</RelativeLayout>
and I'm getting this strange result of Grid with text the normally i will say that describe the ImageButton instead of showing the button himself.
I'm sure its an easy for the most of you - but please if you have the answer i would really like to get an explanation with it.
Thanks !!!
You use ArrayAdapter adapter which takes android.R.layout.simple_list_item_1 layout (which is TextView) and fills this textView with tmpStrRay[i].toString() text.
To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.
I have one Activity in which I try to show some simple messages, and then a list of items. So as I understand, I need two views: one for the simple layout, and one for the list of items which will be handled by the adapter.
Here is my code:
ArrayAdapter<SolutionTopic> adapter;
ArrayList<SolutionTopic> problems = new ArrayList <SolutionTopic>( );
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load your layout
setContentView(R.layout.solution);
SolutionTopic s = new SolutionTopic ();
s.setSolutionTopicName( "Hello" );
problems.add(s);
adapter = new ArrayAdapter<SolutionTopic>(this, R.layout.solution_topic_list,
R.id.label, problems);
TextView solution_title = (TextView) findViewById(R.id.solution_title);
TextView solution_description = (TextView) findViewById(R.id.solution_description);
setListAdapter (adapter);
}
and here is the solution.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" >
<TextView
android:id="#+id/solution_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label1"
/>
<TextView
android:id="#+id/solution_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label2"
/>
</LinearLayout>
and here is the solution_topic_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
What I am not sure is how to get these two to render together. Ideally what I want to do is display the TextView messages first, and then the list of items.
Any idea how I can do that?
Thanks!
You shouldn't remove the ListView from solution.xml, in that ListView your SolutionTopics will be displayed.
To get the big picture, your interface would have three Views:
-TextView: #+id/solution_title
-TextView: #+id/solution_description
-ListView: #android:id/list
The ListView contains an undefined number of entries of solution_topic_list.xml.
PD: The third parameter of ArrayAdapter (R.id.label) should be the id of the TextView in solution_topic_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
You also should implement toString method in your SolutionTopic class, to get the desired String.
How to use a layout as empty view for a listview when the adapter has zero elements?
setEmptyView is not working with this code :
public class ZeroItemListActivity extends Activity {
private ArrayList<String> listItems=new ArrayList<String>();
private ListView mMyListView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMyListView=(ListView) findViewById(R.id.MyListView);
mMyListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems));
LayoutInflater inflator=getLayoutInflater();
View emptyView=inflator.inflate(R.layout.empty_list_item, null);
//Empty view is set here
mMyListView.setEmptyView(emptyView);
}
public void addItem(View v){
listItems.add("list Item");
mMyListView.invalidate();
}
}
Layouts used : empty_list_item.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">
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty List,Click to add items"></Button>
</LinearLayout>
main.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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/MyListView"></ListView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/AddItemsButton"
android:text="Add Items" android:onClick="addItem"></Button>
</LinearLayout>
The easiest way to get this done is to put the empty view in the main.xml layout:
<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MyListView">
</ListView>
<!-- empty view -->
<LinearLayout android:id="#+id/emptyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty List,Click to add items">
</Button>
</LinearLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AddItemsButton"
android:text="Add Items"
android:onClick="addItem"></Button>
</LinearLayout>
Then, in your code, set your empty view like this:
mMyListView.setEmptyView(findViewById(R.id.emptyView));
The reason your code isn't working is that the ListView and Empty view need to be in the same view heirarchy. And your call to inflate pass null as the parent so they are in different heirarchies.
The Android Dev tutorial for List Activity covers this topic:
http://developer.android.com/reference/android/app/ListActivity.html
Basically have your class extend ListActivity. Set your list id to #id/android:listand the "empty view" id to #id/android:emptyin the same XML layout.
Hope this helps :)
When, inflating the view, you have to set the view parent to be part of the Layout hierarchy rather than pass null in some cases.
Replace
View emptyView = inflator.inflate(R.layout.empty_list_item, null);
with
View emptyView = inflator.inflate(R.layout.empty_list_item, (ViewGroup) mMyListView.getParent());
then you set the empty view as you did before with.
mMyListView.setEmptyView(findViewById(R.id.emptyView));