Listview in layout Android - android

I have a string-array, that i need to display in a layout.
The code currently shows the list, but the list isn't in the layout, its make a new "screen" where the only thing you can see is the list.
Here's the code that i just can't get to show the list in the layouts listview.
import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class idchart extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.idchart);
Resources res = getResources();
// To get the string array associated with particular resource ID.
String[] ids = res.getStringArray(R.array.idchart_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.test_list_item,ids);
setListAdapter(adapter);
}
}
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_main"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/logo" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<RelativeLayout
android:id="#+id/relative"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
</LinearLayout>
I haven't been able to find anything that helped :( so i hope you will be able to.

ListView listView=(ListView)findViewById(R.id.lv);
//rest of code
listView.setAdapter(adapter);
And you can avoid extending ListActivity.
If you are using ListActivity then
ListView listView=getListView();

Your idchart class should be a subclass of a regular Activity instead of a ListActivity which are for only displaying lists.
Update
Replace
public class idchart extends ListActivity
with
public class idchart extends Activity

If you extend ListActivity, your ListView must have a specific id:
android:id="#id/android:list"
From the doc:
ListActivity has a default layout that consists of a single, full-screen list in the
center of the screen. However, if you desire, you can customize the screen layout by
setting your own view layout with setContentView() in onCreate(). To do this, your own
view MUST contain a ListView object with the id "#android:id/list"

Related

How to know the View Elements after loading a layout?

I am trying to write code , where after loading the Layout , i want to know which are the view elements are related to the layout like TextView, EditText , Checkbox etc.
MyCode :
MainActivity.java
package com.example.myview;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
// How to get to know about the UI Elements related to this layout //
}
}
demo_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lnr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddccee"
android:orientation="vertical">
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
</LinearLayout>
This layout has TextView , EditText , so , i want to know how to get UI elements are related to this layout programmatically.
So , please suggest me some solution.
You can use getChildCount REF to get the count of views added toViewGroup and then use getChildAt REF to get the View at given index.
You are using LinearLayout as base layout which already extends the ViewGroup Just change below line.
LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
After you've inflated your demo_layout just call findViewById on your view instance like this:
View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
TextView txt1 = (TextView) view.findViewById(R.id.txt1);
Elements of view must be retrieved by view.findViewById().
Except in activity you use findViewById() directly to retrieve view elements of the contentView you set in setContentView().

Throwing error Layout is not defined. Even though present in R.java

I have created a simple ListActivity and a custom List item called people_list_row.xml for the same ListView.
I am using a custom Array adqapter peopleCustomArrayAdapter.java. Inside this custom array adapter Eclipse is throwing the error "people_list_row cannot be resolved or is not a field". Though layout people_list_row present inside R.java
There are no errors. I tried to Clean, Build the project still no luck.
//Inside Custom Array Adapter
public class peopleCustomArrayAdapter extends ArrayAdapter<String> implements OnClickListener{
String[] values;
public peopleCustomArrayAdapter(Context context,String[] values) {
super(context,R.layout.people_list_row1,values);
this.values=values;
}
}
<!-- people_list_row.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:background="#035633"
android:orientation="vertical">
<TextView android:id="#+id/textName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView" />
</LinearLayout>
</RelativeLayout>
Since an adapter (ArrayAdapter) class doesn't implicitly import the resources, you have import them manually:
import com.example.package.R;
You wouldn't need to do that in e.g. a class that extends Activity.

Set default ListView

Is there any way to change the ListView in a SherlockListFragment from defaulting to R.id.list? e.g. I have a custom list that overrides the default ListView in xml (list) that is defined as being below a linear layout. How do I make it so that setListAdapter will use this ListView instead of the default android ListView?
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_view_panel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/toggleLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<ToggleButton
android:id="#+id/toggle_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ToggleButton
android:id="#+id/toggle_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ToggleButton
android:id="#+id/toggle_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toggleLL" />
</RelativeLayout>
Fragment:
public class ProblemFragment extends SherlockListFragment
{
private SeparatedListAdapter list;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getSherlockActivity().setContentView(R.layout.apps_layout);
list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));
setListAdapter(list); //Sets the list to the default list, not the overwritten list specified in the xml
}
}
Just change your layout XML to use android:id="#android:id/list".
ListActivity and ListFragment rely on the ListView having the id android.R.id.list, which is publicly exposed in the SDK (and accessible in XML via #android:id/list). If you look at the docs for either of those classes, both mention that using your own layout requires that you give your ListView this id for it to work properly.

Replication of Apple's Search in Android

I want to create a UI similar to as shown here http://appsreviews.com/wp-content/uploads/2010/08/Cures-A-Z-App-for-iPhone.jpg
I started out with trying to put two custom lists side by side like in this code
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Emp extends Activity {
/** Called when the activity is first created. */
private String tableName = DBHelper.tableName;
private SQLiteDatabase newDB;
public static WeakHashMap<String, Empbook> temp = new WeakHashMap<String, Empbook>();
final List<Empbook> listOfEmpbook = new ArrayList<Empbook>();
final List<String> listOfAlphabets = new ArrayList<String>();
TextView txt;
EmpbookAdapter adapter = new EmpbookAdapter(this, listOfEmpbook);
Integer pos;
Integer count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt=(TextView)findViewById(R.id.textView1);
ListView list = (ListView) findViewById(R.id.ListView01);
ListView alist = (ListView) findViewById(R.id.ListView02);
list.setClickable(true);
alist.setClickable(true);
AlphabetListAdapter alphabetadapter = new AlphabetListAdapter(this,
listOfAlphabets);
list.setAdapter(adapter);
alist.setAdapter(alphabetadapter);
the alphabetadapter is for the list displaying alphabets on the right in the screen.
My XML is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="TextView" android:id="#+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<ListView android:id="#+id/ListView01" android:layout_width="280dp"
android:orientation="vertical" android:layout_height="wrap_content"></ListView>
<ListView android:id="#+id/ListView02" android:layout_width="wrap_content"
android:orientation="vertical" android:paddingLeft="282dp"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
The problem that's occurring is that only one view at a time(the one which is put earlier in the above xml is displayed while the other just doesn't appear).
Can someone please push me in the right direction?
EDIT: I tried to set the weights of the lists setting one to zero and setting the other to 1,it works partially now i can see both lists however one of the list isn't getting populated....will update if i work it out.
Posted an answer below (One listview dropped though.) Check it out.
If the index on the side is what you're looking for, you should try this: http://hello-android.blogspot.com/2010/11/sideindex-for-android.html
If you want to add to elements side by side which together fill their parent:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListView01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="#FFFF0000"/>
<ListView
android:id="#+id/ListView02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10"
android:background="#FF00FF00"/>
</LinearLayout>
You had "wrap_parent" as the height of the second element. If it wasn't being filled properly it would have the height of 0 - I've changed it to match parent. I've also added a system for using "percentage" filling.
Also, all other "fill_parent" tags I've changed to "match_parent" - not because it changes the functionality of the code but because "fill_parent" is deprecated because as a label it is misleading.
Also, I've added a background to the elements which will more helpfully debug where your problem is.
I would also suggest that what you should be aiming for is infact one View (NOT a ListView even though I have kept it for this example) which would be placed above the other (Just as the Apple search has their alphabet):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#+id/ListView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF0000"/>
<!-- Since the contents of the view don't change it seems wasteful to create this as a listview -->
<ListView android:id="#+id/ListView02"
android:layout_width="20dp"
android:layout_height="match_parent"
android:background="#FF00FF00"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"/>
</RelativeLayout>
Found a work around now use a textview and a listview nested in a framelayout like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:cacheColorHint="#00000000" />
<TextView android:id="#android:id/empty"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:text="textview" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:background="#android:color/transparent" android:id="#+id/sideIndex"
android:paddingLeft="280dip"
android:layout_width="300dip" android:layout_height="fill_parent"
android:gravity="center_horizontal">
<TextView android:text="T" android:id="#+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</FrameLayout>
More can be found out here http://dotslasha.in/wp/143/creating-floating-views-in-android .
Ty and Cheers !! :)
You don't need to implement this yourself, Google has helpfully given you API to use their search functionality.
The documentation on the subject should be enough to get you from start to finish. It's available here: http://developer.android.com/guide/topics/search/search-dialog.html
In the second ListView you have got one big padding: android:paddingLeft="282dp". I assume you are not coding for tablets in landscape only, so just remove the padding-attribute.
Remove the text view which is the first element (you can replace this by using addHeaderView() or wrapping this linearlayout onside a vertical one).
Look carefully at how the width and height are set in the following code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListView01"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
></ListView>
<ListView
android:id="#+id/ListView02"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
></ListView>
</LinearLayout>
In my experience, the weight will only work properly if the width is set to wrap_content.

Set emptyView of android listActivity

i have created a My custom listActivty by extending ListActivity like this.
public class MainList extends ListActivity {
ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
setContentView(R.layout.list_main);
}
}
And declaring its xml like this
<?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">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="My Text"
/>
</LinearLayout>
i am using CursorAdapter as listActivity adapter.Its showing perfectly with MainList is empty.
My problem is that i want to show more than a textview when MainList is empty. Basically i need custom view(imageview,button etc) to be shown when list is empty.How should i do this.
Thanks
For the listview there is one default method to set empty view.
setEmptyView(view)
Try This.... In the place of view pass your custom view.
If you are not getting then tell me. I will post an example for this.
You may want to count the items in your CursorAdapter. If 0 is returned, hide/show parts from your XML? That's how I done similar, but they may be better ways.
Try using the android:id/empty for your custom view. I think you can apply the id to any view, not just a TextView

Categories

Resources