OnItemClick isn't working on a ListView item - java

I have a custom layout for the list's cells where you have a checkbox and 3 text elements. I found on Stackoverflow that I should add android:descendantFocusability = "blocksDescendants" on my LinearLayout where the list is but it doesn't change anything.
Here is my XML for the page with the list :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/cl_fragment_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
tools:context=".ui.home.AperoDetailFragment"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:gravity="left"
android:textSize="18sp" />
<TextView
android:id="#+id/date_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:ems="10"
android:gravity="right"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtView_shopping_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:text="#string/shopping_list_title" />
<Button
android:id="#+id/btn_add_ingredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:clickable="true"
android:focusable="true"
android:padding="15dp"
android:src="#android:drawable/ic_menu_add"
android:text="#string/btn_add_ingredient"
android:textColor="#FFFFFF" />
</LinearLayout>
<ListView
android:id="#+id/list_ingredient"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" />
</LinearLayout>
then in my Java code I set the listener like this :
#Override
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
final TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.name);
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.date);
// some code that aren't useful right now
//-------------- List of ingredients for the detailed apero
final ListView list_ingredient = (ListView) root.findViewById(R.id.list_ingredient);
LaperoDatabase db = Room.databaseBuilder(root.getContext(),
LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();
IngredientDao dbIngredient = db.getIngredientDao();
//#TODO control if we get the right ingredients
ArrayList<Ingredient> ingredient_of_this_apero = (ArrayList<Ingredient>) dbIngredient.getIngredientsFromApero(detailApero.aid);
IngredientAdapter adapter = new IngredientAdapter(root.getContext(), R.layout.ingredient_cell_layout, ingredient_of_this_apero);
list_ingredient.setAdapter(adapter);
list_ingredient.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Ingredient selected_ingredient = (Ingredient) adapter.getItemAtPosition(position);
Log.e("test","okasodkdaso");
}
});
return root;
}
I also tried using a OnItemLongClickListener but it also does nothing. How can I put a listener for the list items ?

Related

Listview not scrolling inside custom listview

I have a custom listview, inside of which there is another one. And the one that is nested will not scroll. Perhaps you need to register something in the parent setOnTouchListener? I don't have anything like that in getView.
image
my layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="#color/gray"
tools:context=".invoice.ListPruducts">
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:stackFromBottom="true"
app:layout_constraintBottom_toTopOf="#+id/materialCardView2"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="10dp" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView2"
android:background="#color/zxing_transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/CustomCardViewStyle"
android:layout_marginTop="10dp"
app:cardElevation="10dp"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="#+id/productName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:backgroundTint="#color/yellow"
android:enabled="false"
android:hint="Наименование"
android:textColor="#color/black"
android:textColorHint="#color/black"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/productName"
android:layout_marginHorizontal="5dp"
android:layout_marginTop="5dp"
android:baselineAligned="false"
android:orientation="horizontal">
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="#+id/search_barCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#color/green"
android:ems="15"
android:focusable="true"
android:hint="Считайте ш/к"
android:textColorHint="#color/DefGray"
android:inputType="number"
android:maxLength="15"
android:textSize="18sp"
android:singleLine="true" />
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="#+id/coutOfProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1"
android:backgroundTint="#color/green"
android:hint="Кол-во: "
android:inputType="numberSigned|numberDecimal"
android:maxLength="13"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/DefGray"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
`
my getView
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
GoodsCreate_SQL myItemFromList = getItem(position);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResourse, parent, false);
TextView tvMark = convertView.findViewById(R.id.tvMark);
CheckBox cb = convertView.findViewById(R.id.checkBox2);
tvMark.setText(myItemFromList.Mark);
if (myItemFromList.myMark.equals("1")){
cb.setChecked(true);
}
return convertView;
}
Tried setOnTouchListener to write view.getParent().requestDisallowInterceptTouchEvent(true);

Why ListView is always empty in Fragment Dialog

The Dialog shows properly but the listview is always empty.
Here is what I get when I launch the Activity
enter image description here
public class MainFragment extends Fragment {
String[] cities = {"Tanger", "Meknes", "Casablanca",
"Fes", "Tetouan", "Taza", "Rabat"};
Dialog dialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
TextView currentCity = view.findViewById(R.id.city);
currentCity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = new Dialog(getContext());
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner, container, false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
EditText searchTextView = spinnerView.findViewById(R.id.editText);
ListView citiesListView = spinnerView.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
}
DialogLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="#drawable/alert_dialog"
android:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
android:textColor="#color/primaryColor"
android:fontFamily="#font/alhurra"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:fontFamily="#font/aj_bold"
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="#drawable/activitybackground"
tools:context=".MainFragment">
<TextView
android:id="#+id/city"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:fontFamily="#font/alhurra"
android:gravity="center"
android:text="مكنـاس"
android:textColor="#color/primaryColor"
android:textSize="20sp" />
</androidx.constraintlayout.widget.ConstraintLayout>
It is a problem with your layout. ConstraintLayout is somehow hiding the listview. You have to do some changes in your layout files. For example, this has worked for me using LinearLayout
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
//View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner,binding.getRoot());
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
ListView citiesListView = dialog.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
dialog.show();
==
<?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:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_below="#id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_below="#id/editText"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</RelativeLayout>

How to set Margins for View in android studio?

I have a view in an activity as below .I want to set Margin to the view.But i used Viewgroup.LayoutParams but it doesn't change the view of the row.Below i have given custom adapter which takes data from firstpagerowitems.xml.I have given the java code for adapter .I tried doing android:layout_marginbottom="10dp" for firstpagerowitems.xml and firstpage.xml but it doesnt work.
class MyAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> rTitle;
ArrayList<String> rDescription;
ArrayList<String> rImgs;
MyAdapter (Context c, ArrayList<String> title,ArrayList<String> description,ArrayList<String> imgs) {
super(c, R.layout.firstpagerowitems, R.id.textView1, title);
this.context = c;
this.rTitle = title;
this.rDescription = description;
this.rImgs = imgs;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
TextView images = row.findViewById(R.id.textView3);
TextView myTitle = row.findViewById(R.id.textView1);
TextView myDescription = row.findViewById(R.id.textView2);
Log.d("entered last","yes");
Log.d("postion", String.valueOf(position));
Log.d("rimgs", String.valueOf(rImgs));
Log.d("desc", String.valueOf(rDescription));
Log.d("title", String.valueOf(rTitle));
// now set our resources on views
images.setText(rImgs.get(position));
myTitle.setText(rTitle.get(position));
myDescription.setText(rDescription.get(position));
Random random=new Random();
int trp=random.nextInt(16);
Log.d("enteredcolor",mycolors[trp]);
// images.setBackgroundResource(R.color.lightgreen);
String fd=mycolors[trp];
// images.setBackgroundColor(Color.parseColor(fd));
// LinearLayout mylinear=row.findViewById(R.id.mylinear);
// mylinear.setBackgroundColor(Color.parseColor(fd));
row.setBackgroundColor(Color.parseColor(fd));
// ViewGroup.MarginLayoutParams margins=new ViewGroup.MarginLayoutParams(row.getLayoutParams());
// margins.setMargins(0,100,0,100);
// ViewGroup.LayoutParams layouts=new ViewGroup.LayoutParams(margins);
// row.setLayoutParams(layouts);
// ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(row);
return row;
}
}
code.java
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="one"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
firsypagerowitems.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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="#color/colorWhite"
tools:context=".Firstpage">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/mylinear">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
>
</ListView>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/fabs"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:fabSize="normal"
app:backgroundTint="#color/design_default_color_error"
app:elevation="6dp"
android:src="#android:drawable/ic_input_add"
>
</com.google.android.material.floatingactionbutton.FloatingActionButton>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
firstpage.xml
Try whether this helps...
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_marginBottom="5dp"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
Hope this helps. Feel free to ask for clarifications...

Android List onListItemClick not working

I've got a new List of things that needs to be clicked but this one isn't working. onListItemClick is never called. I have another one in my app that has been working as expected and I can't figure out what the difference is. I've seen the people saying to change focusable but I've tried that a bunch of different ways with no effect. So here's some code.
Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
v_ProjectInvestigatorSiteContact project = (v_ProjectInvestigatorSiteContact) this.getListAdapter().getItem(
position);
Intent myIntent = new Intent(this, Details.class);
myIntent.putExtra(res.getString(R.string.project), project);
startActivity(myIntent);
}// onListItemClick
Not Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
final v_SitePeople vSitePeople = (v_SitePeople) this.getListAdapter().getItem(
position);
AlertDialog.Builder builder = new AlertDialog.Builder(Share.this);
builder.setTitle(res.getString(R.string.forgot_password_check_dialog_title))
.setMessage(res.getString(R.string.share_check_dialog_text))
.setPositiveButton(res.getString(R.string.send), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
sendShareEmail(vSitePeople);
}
}).setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// cancelled, so do nothing
}
});
AlertDialog msgBox = builder.create();
msgBox.show();
}// onListItemClick
Working XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/app_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/search_gradient" >
<ImageView
android:id="#+id/searchBoxIcon"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerCrop"
android:src="#drawable/action_search" >
</ImageView>
<EditText
android:id="#+id/searchBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="#+id/searchBoxIcon"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="#+id/searchBoxIcon"
android:background="#drawable/search_box"
android:hint="#string/search_hint"
android:inputType="text"
android:maxLines="1"
android:minHeight="30sp"
android:paddingBottom="2dp"
android:paddingLeft="25sp"
android:paddingTop="2dp"
android:textColor="#ff000000" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Not Working XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/share_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Here is more on how the broken one works, just in case you want more code.
Not Working Row 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="?android:attr/listPreferredItemHeight"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical"
android:padding="6dp"
android:background="#ffffffff"
android:layout_margin="10dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/moreInfo"
android:gravity="center_vertical"
android:minHeight="20sp"
android:textColor="#ff000000"
android:textStyle="bold" />
</RelativeLayout>
Not Working View Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.share_row, null);
convertView.setTag(holder);
}// if
else
{
holder = (ViewHolder) convertView.getTag();
}
v_SitePeople i = items.get(position);
if (i != null)
{
TextView topText = (TextView) convertView.findViewById(R.id.toptext);
topText.setGravity(Gravity.CENTER_VERTICAL);
topText.setMinHeight(40);
if (topText != null)
{
if (i.SitePerson != null)
{
if (i.PersonTitle != null)
{
topText.setText(String.format(i.SitePerson + ", " + i.PersonTitle));
}
else
{
topText.setText(i.SitePerson);
}
}// if has ProtocolNumber
else
{
if (i.Nickname != null)
{
topText.setText(i.Nickname);
}
}// if does not have ProtocolNumber
}// if
}// if
return convertView;
}// getView
Thank you so much for your help.
I figured it out. I never posted the relavant code, or I'm sure you guys would have found it for me.
My not working adapter had a isEnabled method copied from my functioning adapter that disabled items of type 0. But since my new list is all just one type everything was disabled.
Sorry for the trouble and thank you for your efforts.
You haven't shown where you initialise your list and adapter, but you need to make sure the onClickListner is set. Something like:
myListView.setOnItemClickListener(this);
edit: 'this' assuming initialization is done in the same class as the click listeners appear.
When you're using a ListView, you have mainly two layouts:
1)The layout where you have your listView
Example:
<?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="wrap_content"
android:paddingTop="?attr/actionBarSize"
android:clickable="false"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:clipToPadding="false"
android:paddingLeft="#dimen/list_left_padding"
android:paddingRight="#dimen/list_right_padding"
android:paddingTop="#dimen/list_separator"
android:dividerHeight="#dimen/list_separator"
android:paddingBottom="#dimen/list_separator" />
</RelativeLayout>
2)The layout which contains each item for your ListView.
Example:
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_responsive"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:id="#+id/sampleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:clipToPadding="false">
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/textViewSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="5dp"
android:text="Sample Text"
android:textSize="17sp"
app:typeface="roboto_regular" />
</RelativeLayout>
</RelativeLayout>
If you search on StackOverFlow about this problem, you will got the following instructions to solve it:
1) Add
android:descendantFocusability="blocksDescendants"
to the root of your layout
2) Add
android:clickable="false"
android:focusable="false"
To your item.
You can use these two options separately. The first one have helped me. But, you need to pay attention on where you need to put these attributes in your XML File. Put them in your ITEM layout, not in your ListView Layout. I'm saying this, because I was using that solution, but i was putting on the wrong xml. So, it just pay attention and it will work very well.

How do I make an XML layout for a ListView row where there is a TextView left-aligned and a TextView right-aligned?

Basically what I am looking for is my rows in my ListView to look something like this:
Title: Harry Potter
Hardcover: Yes
Own: No
Country: United States
Can someone post the XML layout of one of these rows? I can't seem to get it right with my code.
Here is what I have that just is plain wrong and not working:
<?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="wrap_content">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtKey"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:textSize="18dip"
android:layout_marginLeft="8dip"
android:textStyle="bold"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:text="test"
/>
<TextView
android:id="#+id/txtValue"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:textSize="18dip"
android:layout_marginRight="8dip"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:text="test2"
/>
</TableRow>
</TableLayout>
</LinearLayout>
Got it.
<?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="wrap_content">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtKey"
android:text="Some text"
android:textSize="18dip"
android:textStyle="bold"
android:layout_marginLeft="8dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1.0"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
/>
<TextView
android:id="#+id/txtValue"
android:text="Some text"
android:textSize="18dip"
android:layout_marginRight="8dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1.0"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
/>
</TableRow>
</TableLayout>
</LinearLayout>
res/layout/listitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textLeft"
android:layout_weight="1"
android:gravity="left"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textRight"
android:layout_weight="1"
android:gravity="right"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
then in your activity:
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new BaseAdapter() {
final String[][] keysAndValues = {
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"}
};
#Override
public int getCount() {
return keysAndValues.length;
}
#Override
public String[] getItem(int position) {
return keysAndValues[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView[] foundTextFields;
if(convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem, parent, false);
foundTextFields = new TextView[] {
(TextView) convertView.findViewById(R.id.textLeft),
(TextView) convertView.findViewById(R.id.textRight)
};
convertView.setTag(foundTextFields);
}
else {
foundTextFields = (TextView[]) convertView.getTag();
}
String[] item = getItem(position);
foundTextFields[0].setText(item[0]);
foundTextFields[1].setText(item[1]);
return convertView;
}
});

Categories

Resources