I'm trying to add several items within a single Drawable which would be used in a GridView:
- text aligned at the center of the drawable
- image at the background
- a programmatically variable number (0-3) of images (stars) ALIGNED horizontally at the bottom of the drawable.
How could I accomplish this please?
At the moment I'm trying to use a custom ImageView and set the background image using setImageResource. An alternative would be to use a TextView, however I still have no clue about the stars at the bottom.
Any help would be appreciated!
George.
You don't mean Drawable, you mean to say a View. You simply have to find a decent tutorial about making custom views for AdapterView (ListView, GridView, ...).
One such tutorial to get you started is http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
Quick dump of the code:
main.xml
<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=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}
Related
I want to display a single row of 5 images which then I can click on, this is what I hope to get:
[1][2][3][4][5]
This is what I actually get:"____" is a large, long blank area
[1]________________________
[2]________________________
[3]________________________
[4]________________________
[5]________________________
At first I thought there was something up with the orientation so I tried both "horizontal" and "vertical" but nothing worked.
Here is the adaptor code:
public class CustomTopGridMenuAdaptor extends BaseAdapter {
private Context mContext;
private final int[] gridViewImageId;
private ImageView imageViewAndroid;
public CustomTopGridMenuAdaptor(Context context, int[] gridViewImageId) {
mContext = context;
this.gridViewImageId = gridViewImageId;}
#Override
public int getCount() {
return gridViewImageId.length;
}
#Override
public Object getItem(int i) {
return null;
}
public void setImageSource (int i,ImageView v){
imageViewAndroid.setImageResource(gridViewImageId[i]);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
View gridViewAndroid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
gridViewAndroid = new View(mContext);
gridViewAndroid = inflater.inflate(R.layout.android_custom_gridview_layout_top, null);
imageViewAndroid = (ImageView)
gridViewAndroid.findViewById(R.id.android_gridview_image_top);
setImageSource(i,imageViewAndroid);
} else {
gridViewAndroid = (View) convertView;
}
return gridViewAndroid;
}
}
As far as it goes the adaptor is working just fine.
This is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/android_custom_gridview_layout_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/android_gridview_image_top"
android:layout_width="50dp"
android:layout_height="50dp"/>
</LinearLayout>
This is the xml segment in the activity layout, where the menu will be displayed:
<GridView
android:id="#+id/android_gridview_menu_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="196dp">
Can you tell me why it's not working?
Use the android:numColumns="2" in your GridView if you want 5 then change the android:numColumns="5"
<GridView
android:id="#+id/android_gridview_menu_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:numColumns="2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="196dp">
I don't think that gridviews can be made horizontally scrollable. Android has provided HorizontalScrollView it will scroll your view horizontaly. try this.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
</LinearLayout>
</HorizontalScrollView>
Happy coding!!
My Problem is --> I put a white line as a view in the Fragment item xml but it doesn't appear when i debug it. How can i get the changes i make in the fragment item xml file. Because i tried some little changes like alpha property change but it didnt change is it because of this is a listview and has itself design? Please help me about that problem.
The xml code and adapter code are below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/menulogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/infoicon"
android:layout_gravity="center"
android:paddingTop="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/txtMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Company Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/txtMenu"
android:background="#FFFFFF"
/>
</LinearLayout>
My listview adapter codes right down below.
public class MenuListAdapter extends ArrayAdapter<Menu_Item> {
private final ArrayList<Menu_Item> list;
private final Activity context;
private int[] colors = new int[]{0x34a4a4a4, 0x34fafafa};
public MenuListAdapter(Activity context, ArrayList<Menu_Item> list) {
super(context, R.layout.menu_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
public ImageView menulogo;
public TextView txtMenu;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.menu_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.menulogo = (ImageView) view.findViewById(R.id.menulogo);
viewHolder.txtMenu = (TextView) view.findViewById(R.id.txtMenu);
view.setTag(viewHolder);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtMenu.setText(list.get(position).getMenuName());
//holder.menulogo.setImageBitmap(list.get(position).getMenuImage());
String menuName=list.get(position).getMenuName();
if (menuName.equals("Info")){
holder.menulogo.setImageResource(R.drawable.infoicon);
}
if (menuName.equals("Odalar")){
holder.menulogo.setImageResource(R.drawable.accomodation);
}
if (menuName.equals("Galeri")){
holder.menulogo.setImageResource(R.drawable.galeryicon);
}
if (menuName.equals("Aktiviteler")){
holder.menulogo.setImageResource(R.drawable.activitiesicon);
}
if (menuName.equals("Robin's Kids Club")){
holder.menulogo.setImageResource(R.drawable.kidsclub);
}
if (menuName.equals("Restaurants")){
holder.menulogo.setImageResource(R.drawable.restaurantsicon);
}
view.setId(list.get(position).getMenu_id());
return view;
}
#Override
public int getViewTypeCount() {
if (getCount() != 0)
return getCount();
return 1;
}
}
I found an answer to my question and wanted to share it with you.
I only used these 2 properties below of ListView
android:divider="#FFFFFF"
android:dividerHeight="1dip"
Like this...
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="#FFFFFF"
android:dividerHeight="1dip"
/>
In ListView XML add these two lines
<ListView
android:divider="#android:color/white"
android:dividerHeight="5dip"
.
.
</ListView>
Try to change the View with id line for a Space component. It is an extension of View, and works better for what you want.
Here is a description of the widget:
https://developer.android.com/reference/android/widget/Space.html
I'm a beginner to Android development, but not to software development. I've been looking for tutorials on how to create dynamic ListViews with images and have had some trouble.
The tutorial I followed most closely was #2 on this link (the Custom ArrayAdapter example): http://www.mkyong.com/android/android-listview-example/
This works fine if I remove the header and footer from my layout xml file, but I really need those to be included. Below I'll post my source from my Activity java file, my custom array adapter java file, and my layout xml file as well as the output. I think there may be something I'm missing or not understanding about how the layout inflater works.
Thanks for your time.
MainActivity.java
public class MainActivity extends ListActivity {
static final String[] MAIN_MENU =
new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ImageAdapter(this, MAIN_MENU));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
// to contact
if(selectedValue.equals(MAIN_MENU[4])) {
contactPressed(v);
}
// to icd-10
else if(selectedValue.equals(MAIN_MENU[0])) {
startActivity(new Intent(MainActivity.this, ICDActivity.class));
}
// to services
else if(selectedValue.equals(MAIN_MENU[3])) {
startActivity(new Intent(MainActivity.this, ServicesActivity.class));
}
}
public void contactPressed(View v) {
startActivity(new Intent(MainActivity.this, ContactActivity.class));
overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
}
}
ImageAdapter.java
public class ImageAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;
public ImageAdapter(Context context, String[] values) {
super(context, R.layout.activity_main, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.lvText);
ImageView imageView = (ImageView) rowView.findViewById(R.id.lvImage);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
if (s.equals("ICD-10"))
{
imageView.setImageResource(R.drawable.icon_icd10);
}
else if(s.equals("EDUCATION"))
{
imageView.setImageResource(R.drawable.icon_education);
}
else if(s.equals("NEWS"))
{
imageView.setImageResource(R.drawable.icon_news);
}
else if(s.equals("SERVICES"))
{
imageView.setImageResource(R.drawable.icon_services);
}
else if(s.equals("CONTACT"))
{
imageView.setImageResource(R.drawable.icon_contact);
}
return rowView;
}
}
activity_main.xml
<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" >
<ImageView
android:id="#+id/custom_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/gray">
</ImageView>
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:scaleType="fitXY"
android:src="#drawable/logo_header" />
</RelativeLayout>
<!-- Footer aligned to bottom -->
<RelativeLayout
android:id="#+id/footerLayout"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="30dp" >
<ImageView
android:id="#+id/footer"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="30dp"
android:scaleType="center"
android:src="#drawable/footer" />
<TextView
android:id="#+id/footerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="14dp"
android:onClick="contactPressed"
android:layout_marginBottom="5dp"
android:text="#string/footerText" />
</RelativeLayout>
<ScrollView
android:id="#+id/scrollableContents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout" >
<LinearLayout
android:id="#+id/linearList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:padding="5dp"
tools:context=".MainActivity" >
<!-- Making a dynamic ListView with images with the two attributes below -->
<ImageView
android:id="#+id/lvImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/icon_icd10" >
</ImageView>
<TextView
android:id="#+id/lvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/lvText"
android:textSize="25sp" >
</TextView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
The output I get for this is:
If I comment out the header and footer code in the xml I get:
The goal is for it to look similar to this but with images on the right side of each line:
Thanks for your time.
First looks like you have
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
on your #+id/scrollableContents, which is good. But you also have it on the view nested inside it, #+id/linearList which is not right. There may be other layout problems but it is hard to tell. In general this layout looks very complex for what you are trying to achieve.
But I'm not sure really what you mean by "dynamic ListView" and it looks like you are trying to re-create the functionality of a listview using a ScrollView. I don't think you should take that approach. The ListView is very flexible and you can use a custom layout for your items.
So I found the solution to my problem while searching for another tutorial. I finally ended up using this tutorial: http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html
My problem was that I tried to replace my ListView with a TextView and ImageView in the same layout.xml file when I should have created my TextView and ImageView in a different layout.xml file. Below is the final code and the correct output I was looking for. Thanks!
list_single.xml (below)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="5dp"
android:layout_marginTop="9dp"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="17dp"
android:layout_marginLeft="5dp"
android:textSize="23sp" />
</RelativeLayout>
activity_main.xml (below)
<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" >
<ImageView
android:id="#+id/custom_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/gray">
</ImageView>
<ImageView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:scaleType="fitXY"
android:src="#drawable/logo_header" />
<!-- Footer aligned to bottom -->
<ImageView
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:scaleType="center"
android:src="#drawable/footer" />
<TextView
android:id="#+id/footerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="14dp"
android:onClick="contactPressed"
android:text="#string/footerText" />
<ListView
android:id="#+id/mainMenuList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:divider="#000000"
android:dividerHeight="0.1dp" />
</RelativeLayout>
ImageAdapter.java (below)
public class ImageAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;
public ImageAdapter(Context context, String[] values) {
super(context, R.layout.list_single, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView textView = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
if (s.equals("ICD-10"))
{
imageView.setImageResource(R.drawable.icon_icd10);
}
else if(s.equals("EDUCATION"))
{
imageView.setImageResource(R.drawable.icon_education);
}
else if(s.equals("NEWS"))
{
imageView.setImageResource(R.drawable.icon_news);
}
else if(s.equals("SERVICES"))
{
imageView.setImageResource(R.drawable.icon_services);
}
else if(s.equals("CONTACT"))
{
imageView.setImageResource(R.drawable.icon_contact);
}
return rowView;
}
}
MainActivity.java (below)
public class MainActivity extends Activity {
ListView list;
static final String[] MAIN_MENU =
new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.mainMenuList);
list.setAdapter(new ImageAdapter(MainActivity.this, MAIN_MENU));
// register onClickListener to handle click events on each item
list.setOnItemClickListener(new OnItemClickListener() {
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
String selectedValue = MAIN_MENU[position];
// to contact
if(selectedValue.equals(MAIN_MENU[4])) {
contactPressed(v);
}
// to icd-10
else if(selectedValue.equals(MAIN_MENU[0])) {
startActivity(new Intent(MainActivity.this, ICDActivity.class));
}
// to services
else if(selectedValue.equals(MAIN_MENU[3])) {
startActivity(new Intent(MainActivity.this, ServicesActivity.class));
}
}
});
}
public void contactPressed(View v) {
startActivity(new Intent(MainActivity.this, ContactActivity.class));
overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
}
}
And the final output (below)
I'm working on project and main layout contains ListView. I want to dynamically add subItems to ListViewItem View. My main layout is as follows :
main_layout.xml
<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="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
list_item.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="50dip"
android:background="#android:color/transparent" >
</LinearLayout>
And I want to add TextView to returned view in getView with following properties :
Height : fill_parent
Width : wrap_content
Text : Text View Item
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.M_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
return convertView;
}
Please note that, I want to dynamically add subItems to ListViewItem. You can suppose that, I have array of TextView and want to fill my listView using this Items. How can I do this scenario ? I can not find any result with searching on other threads and aslo Google. So, Could any one please help me to solve my problem ?
Thanks in Advance :)
This was my getView() method.
main.xml
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/button1"
android:cacheColorHint="#android:color/transparent"
android:divider="#00ff00"
android:fastScrollEnabled="true"
android:focusable="false"
android:listSelector="#000000"
android:paddingTop="5dp"
android:saveEnabled="true" >
</ListView>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Adapter
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
/*convertView = mInflater.inflate(R.layout.item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
holder = (ViewHolder) convertView.getTag();
holder.getAppName().setText(str.get(position));*/
if(position==0)
{
ImageView iv = new ImageView(getApplicationContext());
iv.setBackgroundResource(R.drawable.ic_launcher);
convertView = iv;
}
else if(position==1)
{
TextView tv = new TextView(getApplicationContext());
tv.setText("text view");
convertView = tv;
}
else
{
EditText et = new EditText(getApplicationContext());
et.setText("ET go home");
convertView = et;
}
return convertView;
}
private class ViewHolder {
private View pathRow;
private TextView appNameView = null;
public ViewHolder(View row) {
pathRow = row;
}
public TextView getAppName() {
if (null == appNameView) {
appNameView = (TextView) pathRow.findViewById(R.id.tv);
}
return appNameView;
}
}
}
The commented out portion is the one used to be. The code below it is the one which I used to dynamically generate the view and return them. my listitem.xml still has a textView in it. But the adapter sets the view which we are returning from getView(). This works for me.
I really don;t see the point of the LinearLayout in your item.xml. You can do that in your own code.
I have a list of element in a fragment.
I want to disable the title of list
http://bugs.haploid.fr/file_download.php?file_id=856&type=bug
here are the code the cell of my list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/stations_listView_layout">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/header_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingBottom="5dip"
android:layout_marginTop="29dip"
android:text="A"
style="#style/FragmentTitleSecondary">
</com.infrabel.railtime.views.RobotoMediumTextView>
<View
android:id="#+id/header_bar"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#color/fragment_title_color">
</View>
<RelativeLayout
style="#style/stations_listView_inner_layout"
android:layout_width="fill_parent"
android:layout_height="60dip">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:layout_gravity="center_vertical"
android:textSize="#dimen/menu_list_item_primary_text_size"
android:textColor="#color/grey_normal">
</com.infrabel.railtime.views.RobotoMediumTextView>
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="25dip"
android:textSize="#dimen/fragment_title_text_size"
android:textColor="#color/grey_light">
</com.infrabel.railtime.views.RobotoMediumTextView>
</RelativeLayout>
</LinearLayout>
i tried to set android:clickable = false or android:focusable="false" and it doesn't work.
One way to make certain ListView item not clickable is:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
// FIRST ITEM - DO NOTHING
} else {
// DO SOMETHING IF YOU WISH...
}
}
});
another way would be to modify your custom adapter:
list.setAdapter(new ArrayAdapter<String>(this, R.layout.item,
listItems) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final TextView v = (TextView) super.getView(position,
convertView, parent);
if(position == 0) {
v.setClickable(false);
// havent tested this thou...
}
}
});
got it , i have in my cell.xml
so the text will be drawn every time when i add cell.
we must declare this in the main xml, than change the text from our adapter:
String firstLetter = station.getTitle().substring(0, 1);
if (position == _alphaIndexer.get(firstLetter)) {
TextView header = (TextView) activity.findViewById(R.id.header_textview);
header.setText(firstLetter);
}