Search bar in android studio - java

I have a soundboard app, and I want to make a search bar so users can find the sounds by search. I have multiple tabs for the buttons. The buttons have different images, which are stored in an array. Each buton has textview below them, as a title. My questions is how to make a search bar so the users can find sounds from all of the 10 Tabs(around 300 sounds overall) at the same time? I have 10 Tabs so that means I have 30 arrays with different informations like the sound of the button, the image of the button, and the text for the textview below the buttons. Here are the codes:
single_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button"
android:longClickable="true"
android:onClick="werbung"
android:padding="0dp"
android:text=""
android:textColor="#ffffff"
android:textSize="17dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textview"
android:layout_width="0dp"
android:layout_height="42dp"
android:layout_below="#id/button"
android:layout_alignLeft="#id/button"
android:layout_centerInParent="true"
android:gravity="center"
android:text=""
android:textColor="#color/black"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
</android.support.constraint.ConstraintLayout>
The arrays containing the texts for the textview, and the sounds and images for the buttons in Tab1.java(all of the tabs' code is the same, just with different arrays):
public String[] items = {
"text1", "text2", "text3"
};
public static int[] soundfiles ={
R.raw.sound1, R.raw.sound2, R.raw.sound3
};
public static int[] images ={
R.drawable.image1, R.drawable.image2, R.drawable.image3
};
The buttons with the text below them are created from the single_item.xml with this code(this is also in the Tab.java):
// CustomGrid Adapter
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private String[] items;
LayoutInflater inflater;
public CustomGridAdapter(Context c, String[] items) {
this.context = c;
this.items = items;
inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int position) {
return items[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_item, null);
}
Button button = (Button) convertView.findViewById(R.id.button);
TextView textView = (TextView) convertView.findViewById(R.id.textview);
textView.setText(items[position]);
button.setBackgroundResource(images[position]);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (context instanceof MainActivity) {
((MainActivity) context).TabEightItemClicked(position);
}
}
});
return convertView;
}
}
Tab layout:
<?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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/tab1">
<GridView
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tabOneGridView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:numColumns="auto_fit"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:columnWidth="155dp"
android:stretchMode="spacingWidthUniform"/>
</RelativeLayout>
Tab design: https://i.stack.imgur.com/NlC09.png

Related

custom listview on fragment - can't use getActivity()

I am trying to add delete button next to my item details basing on this answer.
I tried it on a new project, it works perefect:
and when I click delete, I delete the item.
sadly, I tried to use it in my own project when my listView is in calendar_tab.xml. calendar_tab uses CompactCalendarTab.java - fragment class.
so Android Studio errored:
E:\Downloads\MyCustomAdapter.java
Error:(49, 63) error: cannot find symbol method getSystemService(String)
I tried to change
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
to
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
but with no luck.
custom_listview.xml:
<?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" >
<TextView
android:id="#+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />
calendar_tab.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:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/calendar_tab"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toolbar_calendar"
android:background="#color/teal_300"
android:layout_alignParentTop="true"
android:padding="10sp"
android:layout_alignParentStart="true">
<ImageButton
android:id="#+id/back_button"
android:src="#mipmap/ic_arrow_back_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:onClick="goBackmain"
/>
<ImageButton
android:id="#+id/next_button"
android:src="#mipmap/ic_keyboard_arrow_left_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/showdate"
android:layout_toStartOf="#+id/showdate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#color/black"
android:id="#+id/showdate"
android:layout_alignBaseline="#+id/prev_button"
android:layout_alignBottom="#+id/prev_button"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="#+id/prev_button"
android:src="#mipmap/ic_keyboard_arrow_right_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/showdate"
android:layout_toEndOf="#+id/showdate" />
</RelativeLayout>
<com.github.sundeepk.compactcalendarview.CompactCalendarView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/compactcalendar_view"
android:layout_width="fill_parent"
android:layout_height="250dp"
app:compactCalendarTargetHeight="250dp"
app:compactCalendarTextSize="12sp"
app:compactCalendarBackgroundColor="#null"
app:compactCalendarTextColor="#color/blue_grey_700"
app:compactCalendarCurrentSelectedDayBackgroundColor="#color/teal_300"
app:compactCalendarCurrentDayBackgroundColor="#color/teal_600"
app:compactCalendarCurrentDayIndicatorStyle="fill_large_indicator"
app:compactCalendarEventIndicatorStyle="small_indicator"
app:compactCalendarOtherMonthDaysTextColor="#534c4c"
app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true"
android:layout_below="#+id/toolbar_calendar"
/>
<ListView
android:id="#+id/bookings_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/compactcalendar_view"
>
</ListView>
my fragment:
public class CompactCalendarTab extends Fragment {
final ListView bookingsListView = (ListView) v.findViewById(R.id.bookings_listview);
adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mutableBookings);
final ArrayList<String> list = new ArrayList<>();
final MyCustomAdapter adapter = new MyCustomAdapter(list, this);
bookingsListView.setAdapter(adapter);
compactCalendarView = (CompactCalendarView)
v.findViewById(R.id.compactcalendar_view);
}
my custom adapter:
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private CompactCalendarTab context;
public MyCustomAdapter(ArrayList<String> list, CompactCalendarTab context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_listview, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
return view;
}
}
Change this line
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
To this
LayoutInflater inflater = LayoutInflater.from(parent.getContext());

Android - Custom ListView For Only One Single Row

I Want Something Like Shown In Image Below... As Item 3, Item 4 And Item 7 Has A Toggle Switch But Item 1, Item 2, Item 5, Item 6 Doesn't Have. Can Anyone Help Me To Make This Layout And Make Toggle Switch Work Too
I Want This (Made In Photoshop)
My Java File
import android.content.*;
import android.view.*;
import android.widget.*;
class CustomSettingsAdapter extends ArrayAdapter<String> {
String[] settingItems = {
"Themes",
"Entry Tune",
"Remember Last Location",
"About Us",
"Exit"
};
public CustomSettingsAdapter(Context context, String[] Items) {
super(context, R.layout.main_settings_listview, Items);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.main_settings_listview, parent, false);
String itemName = getItem(position);
TextView textView =(TextView) customView.findViewById(R.id.itemName);
Switch mButton = (Switch) customView.findViewById(R.id.Switch);
if (position == 1 || position == 2) {
mButton.setVisibility(View.VISIBLE);
}
textView.setText(settingItems[position]);
return customView;
}
}
** XML **
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:minHeight="48dp"
android:id="#+id/mainActivityListBackground"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Number"
android:id="#+id/itemName"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:layout_centerVertical="true"
/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Switch"
android:visibility="invisible"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
Use this code it help you.
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:textSize="16dp" />
<Switch
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:gravity="center"
android:text="Off"
android:visibility="invisible" />
</LinearLayout>
and use this adapter
public class PhoneAdapter extends BaseAdapter {
private Context context;
public PhoneAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 7;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
convertView = View.inflate(context, R.layout.item, null);
TextView mCode = (TextView) convertView.findViewById(R.id.code);
Switch mButton = (Switch) convertView.findViewById(R.id.toggleButton);
mCode.setText("item"+i+1);
if (i == 2 || i == 3 || i == 6)
mButton.setVisibility(View.VISIBLE);
return convertView;
}}
and this is output:-
feel free to ask if you stuck anywhere in between.
EDIT:- getView() is use for identify which button is clicked so you don't want to care about it .In the getView() the i variable is used for identify which item is clicked.
Just set your OnchangeListner inside getView() and your problem solve.
You can use recycler view.You can do this by creating two xml for two different designs,and on basis of condition you can set view in layout inflater.Use these methods for extra views.
#Override
public int getViewTypeCount() {
return VIEW_TYPE_COUNT;
}
#Override
public int getItemViewType(int position) {
return position;
}
You can also refer to this link.

Android ListView OnClickListener

I need to add a code responsible for action when one row from my list is clicked. I don't know if it should be OnItemClickListener or OnClickListener and how&where to write it. My app is with view holder. Here is my code:
public class JobListAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
public static class WorkViewHolder {
public TextView mJob;
public ImageView mImageAndroKorpo;
}
public JobListAdapter(Context mContext, List<String> mDane) {
super(mContext, R.layout.list_element_job, mDane);
this.mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
WorkViewHolder mHolder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.list_element_job, parent, false);
mHolder = new WorkViewHolder();
TextView mJobsName = (TextView) convertView.findViewById(R.id.nazwa_oferty);
ImageView mImageAndroKorpo = (ImageView)convertView.findViewById(R.id.list_image);
mHolder.mJob = mJobsName;
mHolder.mImageAndroKorpo = mImageAndroKorpo;
convertView.setTag(mHolder);
} else {
mHolder = (WorkViewHolder)convertView.getTag();
}
final String mWorkPosition = getItem(position);
mHolder.mJob.setText(mWorkPosition);
mHolder.mJob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here?
}
});
return convertView;
}
}
I added code where I think it should be placed. Is it ok? OnItem or OnClick? And how to use item position?
My list_element_job.xml:
<?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="wrap_content"
android:background="#android:color/white"
android:padding="10dp"
<CheckBox
android:id="#+id/list_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/list_image"
android:src="#drawable/android_white_piece"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="1dp"
android:layout_height="1dp"/>
<TextView
android:text=""
android:background="#drawable/android_korpo_transparent3"
android:textStyle="bold"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="left"
android:layout_toLeftOf="#id/list_image"
android:layout_width="0dp"
android:textSize="7pt"
android:layout_height="wrap_content"
android:id="#+id/nazwa_oferty"/>
<TextView
android:text="Details..."
android:background="#android:color/white"
android:clickable="true"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/nazwa_oferty"
android:textSize="6pt"
android:textColor="#android:color/darker_gray"/>
</RelativeLayout>
Please help me somehow :)
Just remove the focus for the checkbox in xml like below:
<CheckBox
android:id="#+id/list_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
because of check box focus list item click listeners wont work.
Just remove onClick from your adapter class. And add OnItemClickListener to your ListView
example:
JobListAdapter jobListAdapter = new JobListAdapter (...);
listView.setAdapter(jobListAdapter);
listView..setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});

ListFragment OnItemClickListener

I have a list fragment displaying my custom list items which consist of a relative layout containing images and text etc.
<?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="match_parent"
android:background="#color/main_background"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/source_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#color/black"
android:layout_alignParentLeft="true"
android:contentDescription="#string/info" />
<TextView
android:id="#+id/item_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:textStyle="bold"
android:layout_toRightOf="#+id/source_image"
android:textColor="#color/black"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textAlignment="center"
android:text="#string/item_desc" />
<ImageView
android:id="#+id/arrow_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
android:contentDescription="#string/info"
android:src="#drawable/arrow_right" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_centerInParent="true"
android:background="#color/main_background"
android:layout_below="#+id/item_description">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="20dp"
android:padding="5dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:src="#drawable/chameleon" />
<ImageView
android:id="#+id/item_image2"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:gravity="center"
android:src="#drawable/chameleon" />
<ImageView
android:id="#+id/item_image3"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:gravity="center"
android:src="#drawable/chameleon" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
I am trying to set up an OnItemClickListener so when the user clicks anywhere on the list item, a new Intent is started.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Populate list view with array objects
adapter = new HomeListItemAdapter(items, getActivity());
listView = getListView();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(listView.getContext(), "CLICKED", Toast.LENGTH_SHORT);
toast.show();
}
});
}
I am attempting this as shown above but can't seem to get the OnItemClickListener to work (just testing with Toast instead of Intent for now). Where am I going wrong?
Thanks
EDIT: Added custom list adapter code
public class HomeListItemAdapter extends BaseAdapter {
private List<HomeListItem> items;
private Context context;
private int numItems = 0;
public HomeListItemAdapter(final List<HomeListItem> items, Context context) {
this.items = items;
this.context = context;
this.numItems = items.size();
}
public int getCount() {
return numItems;
}
public HomeListItem getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Get the current list item
final HomeListItem item = items.get(position);
// Get the layout for the list item
final RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
// Set the the item picture(s)
ImageView itemImage = (ImageView) itemLayout.findViewById(R.id.item_image);
itemImage.setImageDrawable(item.getItemPic());
//Set the profile picture / source picture
ImageView sourceImage = (ImageView) itemLayout.findViewById(R.id.source_image);
sourceImage.setImageDrawable(item.getSourcePic());
// Set the text label as defined in our list item
TextView itemDesc = (TextView) itemLayout.findViewById(R.id.item_description);
AssetManager am = context.getAssets();
Typeface font = Typeface.createFromAsset(am, "Raleway-Thin.ttf");
itemDesc.setTypeface(font);
itemDesc.setText(item.getText());
return itemLayout;
}
}
Try extending ListFragment (instead of just a normal Fragment). Then you can override onListItemClicked:
public void onListItemClick(ListView l, View v, int pos, long id)
This gives you the list, view, position, and id, so you should be able to do whatever you need.
Could you try setting the HozizontalScrollView to not get focus with the XML attribute
android:focusable="false"
If you have a TextView inside the row then that will take focus when the row is clicked. This will mean that onListItemClick is not called. You either need to have an OnClickListener on the TextView or remove focus from the TextView.
See ListFragment OnListItemClick not being called
Try this sample project for costum ListFragment. It is a very good example with a costum ListFragment showing 2 types of views for phone and tablets.
http://s000.tinyupload.com/index.php?file_id=09444774931653041243
Courtsey
http://jcrazyandroider.blogspot.in/2015/01/download-links.html

ListView items not responding to tap

I'm just getting my feet wet with Android and have built a UI that contains a TabHost with three tabs. Each tab is powered by its own Activity. The first Tab contains a listview with a prepopulated set of rows and is built from a custom ArrayAdapter.
The problem I'm running into is that none of the ListView rows are tappable. In other words, when I tap on them there is no orange selection. If I use the scroll ball on my Nexus One it will select, but any touch gestures don't seem to be responding.
All the UI is being handled using XML files with a main.xml housing the TabHost -> LinearLayout -> TabWidget/FrameLayout and a nearby_activity.xml file containing my ListView UI
<?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:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/nearby_no_events"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:choiceMode="multipleChoice"
android:divider="#d9d9d9"
android:dividerHeight="1px"
android:cacheColorHint="#eee"
/>
</LinearLayout>
And the relevant code from my Activity that is set to show in the selected tab.
public class NearbyActivity extends ListActivity
{
private ArrayList<Event> m_events = null;
private EventAdapter m_adapter = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.nearby_activity);
getEvents();
this.m_adapter = new EventAdapter(this, R.layout.eventrow, m_events);
setListAdapter(this.m_adapter);
}
private void getEvents()
{
m_events = new ArrayList<Event>();
for (int i = 0; i < 100 ; i++)
{
Event e = new Event();
e.setEventName("Event " + i);
e.setVenueName("Staples Center");
e.setStartDate(new Date());
m_events.add(e);
}
}
private class EventAdapter extends ArrayAdapter<Event>
{
private ArrayList<Event> items;
public EventAdapter(Context context, int textViewResourceId, ArrayList<Event> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.eventrow, null);
}
Event e = items.get(position);
if (e != null)
{
TextView nameText = (TextView)v.findViewById(R.id.eventName);
TextView venueNameText = (TextView)v.findViewById(R.id.venueName);
if (nameText != null)
{
nameText.setText(e.getEventName());
}
if(venueNameText != null)
{
venueNameText.setText(e.getVenueName());
}
}
return v;
}
}
}
My listview row's are populated by an XML file as well.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:padding="4dip">
<TextView
android:id="#+id/eventName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:inputType="text"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="18sp"
android:textColor="#000"
/>
<TextView
android:id="#+id/venueName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/eventName"
android:layout_alignParentBottom="true"
android:layout_marginRight="55dip"
android:singleLine="true"
android:ellipsize="end"
android:scrollHorizontally="true"
android:textSize="13sp"
android:textColor="#313131"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
/>
</RelativeLayout>
Thanks for any help you can offer.
You want a Listview tappable, well you need implement the method onListItemClick()
#Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "This is my row number " + position,Toast.LENGTH_LONG).show();
}
and to ensure the orange colour you must set the property
android:focusable="false"
in your Listview Row.xml

Categories

Resources