Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement two onlicklisteners for two groups of buttons in two layouts?
    primarykey
    data
    text
    <p>Is there a way to set two button onclicklisteners for two different columns of buttons (set in two layouts, A layout and B layout, with buttons from A1-A8 and B1-B8) in order for the next screnario to work: I want buttons from their own layouts not to have any action between them (for example when I click A2 and then A7 nothing to happen, and after A7 if I click A3 again nothing to happen, and still if I click A8....and so ON) but when I click for example A1 and B7 after that, I have some code that compares text of buttons and that part is working fine. But i'm strugling with the first part, the part where buttons from the same layout should tako no action when clicked. I was adviced to use two onlicklisteners (now I use only one for all buttons) but I didn't find the way to finish that. I settext to buttons randomly from imported prepopulated sqlite database. Here's my code so far:</p> <pre><code> protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spojnice); a1 = (Button) findViewById(R.id.bA1); a2 = (Button) findViewById(R.id.bA2); a3 = (Button) findViewById(R.id.bA3); b1 = (Button) findViewById(R.id.bB1); b2 = (Button) findViewById(R.id.bB2); b3 = (Button) findViewById(R.id.bB3); nextQuestion(); } public void nextQuestion() { TestAdapter mDbHelper = new TestAdapter(this); mDbHelper.createDatabase(); try{ mDbHelper.open(); Cursor c = mDbHelper.getTestData(generateWhereClause()); mAnsweredQuestions.add(c.getLong(0)); ArrayList&lt;MyStruct&gt; labelsA = new ArrayList&lt;MyStruct&gt;(); ArrayList&lt;MyStruct&gt; labelsB = new ArrayList&lt;MyStruct&gt;(); labelsA.add(new MyStruct(c.getString(2), "1")); labelsB.add(new MyStruct(c.getString(3), "1")); labelsA.add(new MyStruct(c.getString(4), "2")); labelsB.add(new MyStruct(c.getString(5), "2")); labelsA.add(new MyStruct(c.getString(6), "3")); labelsB.add(new MyStruct(c.getString(7), "3")); Collections.shuffle(labelsA); Collections.shuffle(labelsB); question.setText(c.getString(1)); a1.setText(labelsA.get(0).label); a1.setTag(labelsA.get(0).tag); a1.setOnClickListener(clickListener); a1.getBackground().clearColorFilter(); a1.setEnabled(true); b1.setText(labelsB.get(0).label); b1.setTag(labelsB.get(0).tag); b1.setOnClickListener(clickListener); b1.getBackground().clearColorFilter(); b1.setEnabled(true); a2.setText(labelsA.get(1).label); a2.setTag(labelsA.get(1).tag); a2.setOnClickListener(clickListener); a2.getBackground().clearColorFilter(); a2.setEnabled(true); b2.setText(labelsB.get(1).label); b2.setTag(labelsB.get(1).tag); b2.setOnClickListener(clickListener); b2.getBackground().clearColorFilter(); b2.setEnabled(true); a3.setText(labelsA.get(2).label); a3.setTag(labelsA.get(2).tag); a3.setOnClickListener(clickListener); a3.getBackground().clearColorFilter(); a3.setEnabled(true); b3.setText(labelsB.get(2).label); b3.setTag(labelsB.get(2).tag); b3.setOnClickListener(clickListener); b3.getBackground().clearColorFilter(); b3.setEnabled(true); } </code></pre> <p>I placed this line of code in my xml for every button, tagging it for left and right layout, maybe it will help. I didn't find how to make it helpful:</p> <pre><code>android:tag="L" android:tag="R" </code></pre> <p>For comparing purposes this code works fine, but it also compares buttons from the same layout, and I can't have that. How to implement two onclicklisteners to manage this issue of mine?</p> <p>EDIT:</p> <p>I tried with an array but it did not work.</p> <pre><code>private static final int[] idArrayA = {R.id.bA1, R.id.bA2, R.id.bA3, R.id.bA4, R.id.bA5, R.id.bA6, R.id.bA7, R.id.bA8}; private static final int[] idArrayB = {R.id.bB1, R.id.bB2, R.id.bB3, R.id.bB4, R.id.bB5, R.id.bB6, R.id.bB7, R.id.bB8}; final OnClickListener clickListenerA = new OnClickListener(){ public void onClick(View v) { if (Arrays.asList(idArrayA).contains(v.getId())) { return; }else{ //do some code here if pressed buttons are not from the same array } } }; </code></pre> <hr> <p>This is what I've done so far, and I'm not getting anywhere. Can someone help me out with some actual code, to wrap this thing, cause this keeps me in place. I have tried with two onclicklisteners and it's not working at all, it actually working less well than the first code I started with.</p> <p>I placed this in my activity scope:</p> <pre><code>private Button buttonClicked; </code></pre> <p>And two onClickListeners:</p> <pre><code>final OnClickListener clickListenerA = new OnClickListener(){ public void onClick(View v) { Button button = (Button) v; button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333)); if (buttonClicked == null) { // first button is clicked buttonClicked = button; } else { // second button is clicked if (buttonClicked.getTag().equals(button.getTag())) { button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33)); buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33)); buttonClicked.setEnabled(false); button.setEnabled(false); counter = counter + 5; score.setText("Poeni: " + counter); } } } }; final OnClickListener clickListenerB = new OnClickListener(){ public void onClick(View v) { Button button = (Button) v; button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333)); if (buttonClicked == null) { // first button is clicked buttonClicked = button; } else { // second button is clicked if (buttonClicked.getTag().equals(button.getTag())) { buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33)); button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33)); buttonClicked.setEnabled(false); button.setEnabled(false); counter = counter + 5; score.setText("Poeni: " + counter); } } } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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