Why does listview is not showing on my emulator? - java

I'm making simple listview of therapies and the list is not showing on the emulator. I've upgraded and downgraded my api from 22 to 30 and change relative layout to linear layout but still the list is not showing.
activty_stress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="9"
android:background="#83BCD4"
tools:context=".stress">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Find your relaxation"
android:textColor="#color/white"
android:textSize="18pt" />
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"/>
</RelativeLayout>
stress.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stress);
ListView listView = findViewById(R.id.listview);
List<String> list = new ArrayList<>();
list.add("Therapy1");
list.add("Therapy2");
list.add("Therapy3");
list.add("Therapy4");
list.add("Therapy5");
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
//clicked therapy1
startActivity(new Intent(stress.this,Therapy1.class));
} else if (position==1){
//clicked therapy2
}else{
}
}
});
}

Heyy try replacing your RelativeLayout with LinearLayout, or remove weightSum because weightSum cannot be used with RelativeLayout

Replace with this code.
Note: Whenever you give textSize, give it in sp not in Pixel.
activty_stress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#83BCD4"
android:orientation="vertical">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Find your relaxation"
android:textColor="#color/white"
android:textSize="30sp" />
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp" />
</RelativeLayout>
stress.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listview);
List<String> list = new ArrayList<>();
list.add("Therapy1");
list.add("Therapy2");
list.add("Therapy3");
list.add("Therapy4");
list.add("Therapy5");
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
//clicked therapy1
// startActivity(new Intent(stress.this,Therapy1.class));
} else if (position == 1) {
//clicked therapy2
} else {
}
}
});
}
Output:

Related

Android: ListView "setOnItemSelectedListener" not working

I have a ListView (of a predefined set of quotes in a string array) in my android program that does not seem to work when an item is selected. The ListView is populated, but the toast message in my onClickListener is never called. I am not sure what the issue is here. I have never run into this problem before.
Here is my onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
List<String> quotesList = new ArrayList<String>(Arrays.asList(quotes));
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotesList);
listView.setAdapter(arrayAdapter);
listView.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {}
});
}
Here is my activity_main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b2cfff"
tools:context="com.examples.myteststuff.listviews.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:src="#drawable/icon"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:id="#+id/listView"
android:background="#drawable/border"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="8dp"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Try setOnItemClickListener instead :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_SHORT).show();
}
});
Hope this helps

On setting OnItemLongClickListener to my listview, no action is performed when I click on the item. How should I solve this?

I am taking data from my database using and want to set that as my list item. Upon clicking and holding an item, I want a dialog to open prompting the user to select one of the given options. My code is as follows:
My MainActivity.java class
public class MainActivity extends AppCompatActivity {
ListView todoList;
DbHelper dbHelper;
ArrayAdapter<String> myAdapter;
Button rename, delete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DbHelper(this);
todoList=(ListView)findViewById(R.id.to_do);
loadTaskList();
todoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.item_dialog);
dialog.setTitle("Select");
rename = dialog.findViewById(R.id.rename_task);
delete = dialog.findViewById(R.id.delete_task);
dialog.show();
return false;
}
});
}
private void loadTaskList() {
ArrayList<String> taskList=dbHelper.getTask();
if(myAdapter==null){
myAdapter=new ArrayAdapter<String>(this,R.layout.todo_item,R.id.task_title,taskList);
todoList.setAdapter(myAdapter);
}else {
myAdapter.clear();
myAdapter.addAll(taskList);
myAdapter.notifyDataSetChanged();
}
}
My activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.example.mylist.MainActivity">
<ListView
android:id="#+id/to_do"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"/>
</LinearLayout>
My Layout for the list item:
<?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"
android:clickable="true">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Task"
android:textSize="20sp"
android:layout_centerVertical="true"
android:padding="5dp"/>
<ImageButton
android:id="#+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_delete_black_24dp" />
<ImageButton
android:id="#+id/task_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_done_black_24dp"
android:layout_toLeftOf="#id/task_delete"/>
</RelativeLayout>

How to display toolbar transparent in parallax android

I want to use Parallax Listview and Im referring this link to apply for my project:
http://stacktips.com/tutorials/android/listview-header-parallax-in-android
I have added Toolbar in Activity but it Toolbar is not display
My activity class:
public class DetailListCarActivityDemo extends AppCompatActivity {
private ListView listView;
private View heroImageView;
private View stickyViewSpacer;
private int MAX_ROWS = 20;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_list_car_demo);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
/* Initialise list view, hero image, and sticky view */
listView = (ListView) findViewById(R.id.listView);
heroImageView = findViewById(R.id.heroImageView);
/* Inflate list header layout */
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listHeader = inflater.inflate(R.layout.list_header_detail_category, null);
/* Add list view header */
listView.addHeaderView(listHeader);
/* Handle list View scroll events */
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
/* Check if the first item is already reached to top.*/
if (listView.getFirstVisiblePosition() == 0) {
View firstChild = listView.getChildAt(0);
int topY = 0;
if (firstChild != null) {
topY = firstChild.getTop();
}
// int heroTopY = stickyViewSpacer.getTop();
// stickyView.setY(Math.max(0, heroTopY + topY));
/* Set the image to scroll half of the amount that of ListView */
heroImageView.setY(topY * 0.5f);
}
}
});
/* Populate the ListView with sample data */
List<String> modelList = new ArrayList<>();
for (int i = 0; i < MAX_ROWS; i++) {
modelList.add("List item " + i);
}
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_row_detail_category, modelList);
listView.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
finish();
}
}
activity_detail_list_car_demo.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/background_toolbar_translucent" />
<ImageView
android:id="#+id/heroImageView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#drawable/mercedes"
android:scaleType="fitCenter" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#9E9E9E"
android:dividerHeight="1dp"
android:scrollbars="none">
</ListView>
#drawable/background_toolbar_translucent.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:endColor="#android:color/transparent"
android:startColor="#color/black_alpha_40"/>
</shape>
#color/black_alpha_40.xml:
<color name="black_alpha_40">#66000000</color>
UPDATED:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ListView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#9E9E9E"
android:dividerHeight="1dp"
android:scrollbars="none">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/background_toolbar_translucent" />
<ImageView
android:id="#+id/heroImageView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#drawable/mercedes"
android:scaleType="fitCenter" />
</ListView>
As you see my toolbar is transparent, and it same position with image on top, toolbar is front, image is behind.
How to display toolbar and display as I have described as above
Add layout_behaviour in your ListView.
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/listview" />
Preferably I'll tell. Use collapsible toolbar instead of doing this much of stuff.
You can use CollapsingToolbarLayout.You have to put Toolbar inside CollaspingToolbarLayout. here example

Problems with the Click event of a ListView in TabHost

I have a problem.
I'm developing an application that uses Drawer Layout with reference this example https://developer.android.com/training/implementing-navigation/nav-drawer.html
The lateral menu works correctly, but my fragment (content) load 3 tabs and each has a listview, the problem that I have is that the method onItemClick (setOnItemClickListener) does not work, ie, selecting an item did not solve anything. ¿any idea?. Thanks.!!
PS: sorry for my bad english
PrincipalActivity.java
public class PrincipalActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private String[] mLeftPanel;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
initUI();
}
private void initUI(){
mLeftPanel = getResources().getStringArray(R.array.menu_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.dwrMain);
mDrawerList = (ListView) findViewById(R.id.leftPanel);
ArrayList<DrawerOption> items = new ArrayList<DrawerOption>();
items.add(new DrawerOption(R.drawable.ic_plan_visit,mLeftPanel[0]));
items.add(new DrawerOption(R.drawable.ic_clients,mLeftPanel[1]));
items.add(new DrawerOption(R.drawable.ic_search,mLeftPanel[2]));
items.add(new DrawerOption(R.drawable.ic_sinchronize,mLeftPanel[3]));
items.add(new DrawerOption(R.drawable.ic_configure,mLeftPanel[4]));
items.add(new DrawerOption(R.drawable.ic_logout, mLeftPanel[5]));
mDrawerLayout.setDrawerShadow(R.color.listViewBackground, GravityCompat.START);
mDrawerList.setAdapter(new DrawerOptionAdapter(this, items));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (position == 0) {
fragmentPlanVisit(position);
getSupportActionBar().setTitle(mLeftPanel[0]);
}
......
mDrawerLayout.closeDrawers();
}
});
......
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem menuItem){
int id = menuItem.getItemId();
if (id == android.R.id.home){
if (mDrawerLayout.isDrawerOpen(mDrawerList)){
mDrawerLayout.closeDrawers();
}else{
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(menuItem);
}
private void fragmentPlanVisit(int position){
Fragment fragment = new PlanVisitActivity();
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_fragment, fragment)
.commit();
mDrawerList.setItemChecked(position, true);
}
}
}
activity_principal.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dwrMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ContentBackground"/>
<ListView
android:id="#+id/leftPanel"
android:layout_width="250dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:divider="#color/divideColorMenuDrawer"
android:dividerHeight="1dp"
android:background="#color/listViewBackground"
android:layout_weight="1"/>
</android.support.v4.widget.DrawerLayout>
PlanVisitActivity.java
public class PlanVisitActivity extends Fragment {
private TabHost tabHost;
private ListView lstPlanVisitCurse;
private ListView lstPlanVisitDay;
private ListView lstPlanVisitFuture;
private ArrayList<PlanVisits> outLstPlanVisits = new ArrayList<PlanVisits>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_planvisit, container, false);
lstPlanVisitCurse = (ListView)(view.findViewById(R.id.lstPlanVisit_Tab1));
lstPlanVisitCurse.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("PlanVisitActivity", String.valueOf(i));
.....
.....
}
});
......
tabHost = (TabHost)(view.findViewById(R.id.tabHost));
tabHost.setup();
TabHost.TabSpec spec = tabHost.newTabSpec(getResources().getString(R.string.tab1));
spec.setIndicator(getResources().getString(R.string.tab1));
spec.setContent(R.id.tab1);
tabHost.addTab(spec);
spec = tabHost.newTabSpec(getResources().getString(R.string.tab2));
spec.setIndicator(getResources().getString(R.string.tab2));
spec.setContent(R.id.tab2);
tabHost.addTab(spec);
spec = tabHost.newTabSpec(getResources().getString(R.string.tab3));
spec.setIndicator(getResources().getString(R.string.tab3));
spec.setContent(R.id.tab3);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
getListPlanVisit();
return view;
}
private void getListPlanVisit() {
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String s) {
int tab = tabHost.getCurrentTab();
try {
if (tab == 0) {
final PlanVisitsTask tk = new PlanVisitsTask(getActivity().getWindow().getContext(), lstPlanVisitCurse, tab);
tk.execute();
outLstPlanVisits = (ArrayList<PlanVisits>) tk.get();
} else if (tab == 1) {
final PlanVisitsTask tk = new PlanVisitsTask(getActivity().getWindow().getContext(), lstPlanVisitDay, tab);
tk.execute();
outLstPlanVisits = (ArrayList<PlanVisits>) tk.get();
} else if (tab == 2) {
final PlanVisitsTask tk = new PlanVisitsTask(getActivity().getWindow().getContext(), lstPlanVisitFuture, tab);
tk.execute();
outLstPlanVisits = (ArrayList<PlanVisits>) tk.get();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
});
}
}
activity_planvisit.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"
android:background="#color/ContentBackground"
android:focusable="false"
android:focusableInTouchMode="false">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/lstPlanVisit_Tab1"
android:divider="#color/divideColorlistView"
android:dividerHeight="1dp"
android:background="#color/listViewBackground"
android:choiceMode="singleChoice"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/lstPlanVisit_Tab2"
android:divider="#color/divideColorlistView"
android:dividerHeight="1dp"
android:background="#color/listViewBackground"
android:choiceMode="singleChoice" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/lstPlanVisit_Tab3"
android:divider="#color/divideColorlistView"
android:dividerHeight="1dp"
android:background="#color/listViewBackground"
android:choiceMode="singleChoice" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Looking for answers, i found this link onListItemClick is not working for listview?, make the adjustments and now works correctly.

setOnClickListener not firing with custom adapter and custom ListView

I've been spending a few hours on a problem and I still can't figure it out. The setOnClickListener in HotelOverviewFragment is not firing when I click an item in my ListView. However, the setOnClickListener does work from my custom adapter (NowArrayAdapter).
My question is: why the setOnClickListener not working in HotelOverviewFragment (Class where the ListView is shown)?
Here's a list of what I've tried:
Setting android:focusable="false", android:focusableInTouchMode="false", android:descendantFocusability="blocksDescendants" in the hotel_row_layout.xml and fragment_hotel_overview.xml.
Setting android:clickable on true and false. Both didn't work.
Changing from BaseAdapter implementation to arrayAdapter.
I tried different listeners in HotelOverviewFragment: setOnClickListener, setOnItemClickListener, setOnItemSelectedListener and setOnTouchListener. Unfortunately, none of those worked for me.
Here's my code:
Custom adapter
public class NowArrayAdapter extends ArrayAdapter<String> {
private Context context;
private ArrayList<String> values;
private Typeface typeface;
private static Hashtable fontCache = new Hashtable();
private LayoutInflater inflater;
private TextView item;
public NowArrayAdapter(Context context, ArrayList<String> commandsList) {
super(context, R.layout.hotel_row_layout, commandsList);
this.context = context;
values = new ArrayList<String>();
values.addAll(commandsList);
typeface = getTypeface(this.context, "fonts/Roboto-Light.ttf");
inflater = LayoutInflater.from(this.context);
}
static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}
public View getView(int position, View convertView, ViewGroup parent) {
String myText = getItem(position);
if(convertView == null) {
convertView = inflater.inflate(R.layout.hotel_row_layout, parent, false);
item = (TextView) convertView.findViewById(R.id.maps_button);
item.setTypeface(typeface);
convertView.setTag(item);
} else {
item = (TextView) convertView.getTag();
}
item.setText(myText);
//myListItem.descText.setTextSize(14);
convertView.setOnClickListener(new View.OnClickListener() {
// WORKS!
#Override
public void onClick(View view) {
Log.d("click", "Don't look at me!");
}
});
return convertView;
}
}
Fragment
public class HotelOverviewFragment extends Fragment {
private static Hashtable fontCache = new Hashtable();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hotel_overview, container, false);
ListView list = (ListView) v.findViewById(android.R.id.list);
// Set up listview and buttons
setUp(v, list);
// Inflate the layout for this fragment
return v;
}
static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}
public void setUp(View v, ListView l){
TextView address = (TextView) v.findViewById(R.id.content);
TextView header = (TextView) v.findViewById(R.id.header);
TextView hotelName = (TextView) v.findViewById(R.id.hotelName);
Typeface typeface = getTypeface(getActivity(), "fonts/Roboto-Light.ttf");
address.setText("some street \nZipCode, City \nCountry \nEmail \nphoneNumber");
address.setTypeface(typeface);
header.setText("Hotel info");
header.setTypeface(typeface);
header.setTextSize(20);
hotelName.setText("Hotel name");
hotelName.setTypeface(typeface);
// Set up button
ArrayList<String> n = new ArrayList<String>();
n.add(0, "More info");
// Show button
NowArrayAdapter adapter = new NowArrayAdapter(getActivity(), n);
l.setAdapter(adapter);
// THIS IS THE STUFF I'VE BEEN TRYING
try {
l.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("click", "Success");
}
});
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}
});
l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
l.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("click", "Success");
return false;
}
});
}catch (Exception e){
Log.d("click", e + "");
}
}
}
Layout xml of HotelOverviewFragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example"
android:background="#ffebebeb">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:src="#drawable/banner"
android:id="#+id/hotelBanner"
android:layout_gravity="top"
android:adjustViewBounds="false"
android:scaleType="fitXY" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hotelname"
android:id="#+id/hotelName"
android:gravity="center"
android:textSize="40dp"
android:layout_alignBottom="#+id/hotelBanner"
android:layout_alignParentRight="false"
android:layout_alignParentLeft="false"
android:textColor="#ffc4c4c4"
android:layout_marginBottom="5dp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:background="#drawable/header_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Header"
android:layout_toRightOf="#+id/headerImage"
android:layout_centerVertical="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/headerImage"
android:src="#drawable/ic_action_live_help"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:background="#drawable/content_card">
<TextView
android:id="#+id/content"
android:layout_gravity="left|center_vertical"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
The custom xml for a listview item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="#drawable/content_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/maps_button"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<ImageView
android:src="#drawable/ic_action_arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="false"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
Thanks in advance.
Thanks to Daniel Nugent's suggestion I got it working. Removing the convertView.setOnClickListener() is part of the answer. I think it was blocking the other listeners in the HotelOverviewFragment.
My next mistake was that I used setOnClickListener on a ListView for testing.
setOnClickListener should be used for buttons not ListViews.
So after removing setOnClickListener all the other listeners started working.
Thanks for your time.

Categories