Reputation: 31
I'm working on an app (part of a project in school), and I have an activity, that consists of an ImageView, a SearchView, a ListView, and a Button.
I'm trying to put the button in front of the ListView, which was working gucci until I got to the actual making the button do something part. After setting the onClick method, I noticed that the button didn't do anything. I tried just printing something using the button, but nothing worked. Also tried creating a new button, outside the ListView, but that also failed (also didn't cause the onClick event).
I am kinda clueless, and cannot think of anything that could've caused this problem...
xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_color"
android:orientation="vertical"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:layout_gravity="center"
tools:context=".SetupActivity">
<ImageView
android:layout_width="300dp"
android:layout_gravity="center"
android:layout_height="130dp"
android:layout_marginTop="13dp"
android:background="@drawable/setup_text"
android:id="@+id/setupText"
></ImageView>
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="65dp"
android:id="@+id/searchViewSetup"
android:theme="@style/AppTheme.Toolbar"
app:iconifiedByDefault="false"
></androidx.appcompat.widget.SearchView>
<RelativeLayout
android:layout_width="wrap_content"
android:paddingHorizontal="8dp"
android:layout_height="wrap_content">
<ListView
android:id="@+id/list_item"
android:layout_gravity="center"
android:layout_width="395dp"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:layout_height="wrap_content"
tools:listitem="@layout/activity_ingredients_layout"
android:paddingBottom="10.0sp"
android:clipToPadding="false"
android:scrollbars="none"
android:paddingTop="10.0sp">
</ListView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/setup_btn_shadow"
android:layout_marginLeft="287dp"
android:layout_marginTop="450dp"
android:alpha="0.5"
></ImageView>
<android.widget.Button
android:layout_width="73dp"
android:id="@+id/setup_floating_btn"
android:layout_height="73dp"
android:layout_marginLeft="300dp"
android:layout_marginTop="460dp"
android:foregroundGravity="bottom"
android:src="@drawable/ic_baseline_arrow_forward_ios_24"
android:background="@drawable/setup_btn"
></android.widget.Button>
</RelativeLayout>
</LinearLayout>
java:
package com.example.yummilyproject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import com.example.yummilyproject.databinding.ActivitySetupFridgeBinding;
import java.util.ArrayList;
public class SetupActivity extends AppCompatActivity implements View.OnClickListener {
ActivitySetupFridgeBinding binding;
SearchView searchView;
ListView listView;
Button btn;
String[] ingredients = {"Avocado", "Tomato", "Pasta", "Cauliflower", "Egg", "Salmon", "Chicken", "Beef", "Broccoli", "Cheese", "Zucchini", "Lemon", "Sweet Potato", "Kale", "Carrot", "Black Beans", "Asparagus",
"Spinach", "Rice", "Potato", "Yoyo Beans"};
Boolean[] checkboxes = {false, false, false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
int[] images = {R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_setup_fridge);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
listView = findViewById(R.id.list_item);
searchView = findViewById(R.id.searchViewSetup);
btn = (Button) findViewById(R.id.setup_floating_btn);
btn.bringToFront();
btn.setOnClickListener(this);
binding = ActivitySetupFridgeBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ArrayList<Ingredient> ingredientArrayList = new ArrayList<>();
for (int i = 0; i < images.length; i++) {
Ingredient ingredient = new Ingredient(ingredients[i], images[i], checkboxes[i]);
ingredientArrayList.add(ingredient);
}
ListAdapter listAdapter = new ListAdapter(SetupActivity.this, ingredientArrayList);
binding.listItem.setTextFilterEnabled(true);
binding.listItem.setAdapter(listAdapter);
binding.listItem.setClickable(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
listAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
listAdapter.getFilter().filter(newText);
return false;
}
});
binding.listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Ingredient ig = (Ingredient) parent.getItemAtPosition(position);
ig.setIngredientCheckBox(!ig.isIngredientCheckBox());
CheckBox cb = view.findViewById(R.id.ingredientChekcBox);
cb.setChecked(!cb.isChecked());
//cb.setChecked(!cb.isChecked());
}
});
}
@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
if (v == findViewById(R.id.setup_floating_btn))
{
System.out.println("press btn");
String[] ingredientsList = new String[ingredients.length];
int cnt = 0;
for (int i = 0; i<ingredients.length; i++)
{
if (checkboxes[i] == true)
{
ingredientsList[cnt] = ingredients[i];
cnt++;
}
}
System.out.println(ingredientsList);
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("ingredientsList", ingredientsList);
}
}
private AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.8F);
public class Ingredient
{
private String ingredientTitle;
private int ingredientPhoto;
private boolean ingredientCheckBox;
public Ingredient(String ingredientTitle, int ingredientPhoto, boolean ingredientCheckBox) {
this.ingredientTitle = ingredientTitle;
this.ingredientPhoto = ingredientPhoto;
this.ingredientCheckBox = ingredientCheckBox;
}
public String getIngredientTitle() {
return ingredientTitle;
}
public void setIngredientTitle(String ingredientTitle) {
this.ingredientTitle = ingredientTitle;
}
public int getIngredientPhoto() {
return ingredientPhoto;
}
public void setIngredientPhoto(int ingredientPhoto) {
this.ingredientPhoto = ingredientPhoto;
}
public boolean isIngredientCheckBox() {
return ingredientCheckBox;
}
public void setIngredientCheckBox(boolean ingredientCheckBox) {
this.ingredientCheckBox = ingredientCheckBox;
}
}
public class ListAdapter extends ArrayAdapter<Ingredient>
{
public ListAdapter (Context context, ArrayList<Ingredient> ingredientList)
{
super(context, R.layout.activity_ingredients_layout, ingredientList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Ingredient ingredient = getItem(position);
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_ingredients_layout, parent, false);
}
ImageView imageView = convertView.findViewById(R.id.ingredientPhoto);
TextView title = convertView.findViewById(R.id.ingredientTitle);
CheckBox cb = convertView.findViewById(R.id.ingredientChekcBox);
imageView.setImageResource(ingredient.ingredientPhoto);
title.setText(ingredient.ingredientTitle);
cb.setActivated(ingredient.ingredientCheckBox);
return convertView;
}
}
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
Upvotes: 3
Views: 316
Reputation: 7371
Your way of using equality is wrong. You cannot use ==
to see if two objects are equal to each other.
If you really want to use equals
to verify it's the same object, you should do this instead:
if (v.equals(findViewById(R.id.setup_floating_btn)) {
// Do whatever you need to do when the button is clicked here.
}
While this can work, it's not the recommended way of verifying whether this was the View
clicked. Instead you should check the clicked View
's ID
like this:
if (v.getId() == R.id.setup_floating_btn) {
// Do whatever you need to do when the button is clicked.
}
Have a look at the difference between equals
and ==
here.
On a side-note: you're mixing things together here - ViewBindings with normal findViewById
which is really bad and makes things very difficult to read. ViewBindings are meant to make it easier to inflate all views in a screen as well as making all references to views type-safe and making sure no views are null. You should probably read up on this too.
Also in your XML you use sp
as units for dividerHeight
and padding
, which is completely wrong. sp
units are used only for text and not for heights or paddings of views.
Also take a look at what bringToFront()
does on a View
and see if this really should apply/be used on your Button
.
Upvotes: 1