Using Horizontal Custom ListView in Android

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=”MyHorizontalCustomeListView” />

<Gallery      android:id=”@+id/horizontallistview”      android:layout_width=”fill_parent”      android:layout_height=”wrap_content” />

</LinearLayout>

 

row.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=”fill_parent”  android:orientation=”vertical” >

<TextView      android:id=”@+id/itemtext”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_margin=”20dp”

android:textColor=”@android:color/white”

android:textStyle=”bold”

android:textSize=”30sp”/>

</LinearLayout>

 

Main.java

public class Main extends Activity {

Gallery myHorizontalListView;

MyAdapter myAdapter;

@Override

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myHorizontalListView =(Gallery)findViewById(R.id.horizontallistview);

myAdapter = new MyAdapter(this);        myHorizontalListView.setAdapter(myAdapter);

myHorizontalListView.setOnItemClickListener(newOnItemClickListener(){

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {

Toast.makeText(Main.this, arg0.getItemAtPosition(position).toString() + ” Clicked”,Toast.LENGTH_LONG).show();

}

});

}

public class MyAdapter extends BaseAdapter {

Context context;

String[] itemsArray = { “SUN”,”MON”, “TUS”, “WED”, “THU”, “FRI”, “SAT”};

MyAdapter(Context c){       context = c;      }

@Override

public int getCount() {

return itemsArray.length;     }

@Override

public Object getItem(int position) {

return itemsArray[position];     }

@Override

public long getItemId(int position) { return position;     }

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,null);

TextView listTextView = (TextView)rowView.findViewById(R.id.itemtext);    listTextView.setText(itemsArray[position]);

return rowView; }

}

}


Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box