Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change a color for dynamically created buttons in a gridview
    primarykey
    data
    text
    <p>Hello I was using this tutorial in order to create an arraylist of button description that would allow me to create the buttons. <a href="http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/" rel="nofollow">http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/</a></p> <pre><code>public class CategoryButtonAdapter extends BaseAdapter{ private Context mContext; private ArrayList&lt;DishCategory&gt; dishCategories; //button to be created private Button button; //will take in an array list created in the orderlayout that will be the //dish category. This will be the from where we will the count for the adapter public CategoryButtonAdapter(ArrayList&lt;DishCategory&gt; dishCategories) { this.dishCategories = dishCategories; } public CategoryButtonAdapter(Context context, ArrayList&lt;DishCategory&gt; dishCategories) { this.mContext = context; this.dishCategories = dishCategories; } public int getCount() { return dishCategories.size(); } //to be implementated later so it can b3e used to find menu categories public Object getItem(int position) { return null; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { //button to be created if(convertView == null ) { //if it is not recycled, initialize some new attributes button = new Button(this.mContext); button.setLayoutParams(new GridView.LayoutParams(100,100)); button.setPadding(2,2,2,2); } else { button = (Button) convertView; } //setButton to the description of the category button.setText(dishCategories.get(position).getDescription()); //this can be changed later to change the sex appeal of the app //for now it will be plain button.setTextColor(Color.WHITE); button.setBackgroundColor(Color.BLACK); button.setHighlightColor (Color.GREEN); button.setId(position); button.setOnClickListener(new DishCategoryButtonListener(button.getId())); //new loadDishItems(categoryButtons.get(position).getDescription())); return button; } </code></pre> <p>Now it is able to create the buttons as wished when called upon in the main activity. Here is my custom onclicklistener class </p> <pre><code> DishCategoryButtonListener implements OnClickListener { private final int position; private DishCategoryButtonListener(int position) { this.position = position; } public void onClick(View v) { System.out.println("The position is " + position); //button.setTextColor(Color.BLACK); //button.setBackgroundColor(Color.GREEN); } </code></pre> <p>The issue I would run into is that whenever I selected a button on the screen. It would show me the correct position, but for some reason it does not change color to green and black if it was selected. I want it to be green if selected and black if it is not. Is there a way I could do this? Would I have to implement the onclick listener in the main class or would I have to use this custom listener?</p> <p>*<em>EDIT</em>*I just tried that and instead what I have seen is a pattern. Lets say the list contains about 20 objects in the array list. This then creates 20 buttons in a 1 column gridview. What seems to be happening is that whenever on of them is clicked, it actually changes the color of the button at the bottom of screen. If I scroll down, it changes the color of that button at the bottom of the screen.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload