Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3310548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:46:23+00:00 2026-05-17T21:46:23+00:00

I’ve to do a memo card game in android for my project. As for

  • 0

I’ve to do a memo card game in android for my project. As for now i’m at the stage where i try to figure out the basic shape of my app.

I’ve read some tutorials, but i can;t figure out how to modify a lot of dynamically created buttons (ie. in ‘for’ loop), from xml, or even from the code.
To make my question clear here’s the code i made

package piotrek.test1;



import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.ArrayList;
import java.util.*;


public class test1 extends Activity implements View.OnClickListener {

private ArrayList<Integer> listaKart = new ArrayList<Integer>();    
private int rozmiar = 0;    
private int licznik = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TworzKarty();
    //tu tworzymy definicje nowego wygladu (layout) czyli w formie tabeli

    TableLayout layout = new TableLayout (this);
    layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );

    //ustawienie odstepow do 'scian'

    layout.setPadding(10,10,10,10);

    //petle 'populujace' miejsca w tabeli przyciskami

    for (int f=0; f<=5; f++) {
        TableRow tr = new TableRow(this);
        for (int c=0; c<=4; c++) {
            ToggleButton b = new ToggleButton (this);
            //b.setText(""+f+c);
            b.setText(Integer.toString(listaKart.get(licznik)));
            b.setTextSize(10.0f);
            b.setTextColor(Color.rgb( 100, 200, 200));
            b.setOnClickListener(this);
            tr.addView(b, 60,60);
            licznik++;
        } // for
        layout.addView(tr);
    } // for

    super.setContentView(layout);
} // ()


public void TworzKarty(){

for (int i=0;i<=14;i++){
    this.listaKart.add(i);
    this.listaKart.add(i);
}
//pomieszanie kolejnosci numerków
Collections.shuffle(listaKart);
rozmiar=listaKart.size();


}



public void onClick(View view) {
//((Button) view).setText("*");
//((Button) view).setEnabled(false);
if (((ToggleButton) view).isChecked()) {
    Toast.makeText(test1.this, Integer.toString(rozmiar), Toast.LENGTH_SHORT).show();
} 
else {
    Toast.makeText(test1.this, "Not checked", Toast.LENGTH_SHORT).show();
}
}




} // class

What happens when i run the app is, that i have 30 togglebuttons on the screen, each of them has a number form 0 to 14 (15 pairs). That’s what i wanted to have, but when i press any of them buttons besides showing the toast button text changes form what i want it to be to ON and then after another press to OFF.
I can’t figure out how to control this behaviour – i would really appreciate some tips on how to achieve this strictly in code or using xml. Another case is how to use xml to define such layout and randomness of the text set on buttons.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:46:23+00:00Added an answer on May 17, 2026 at 9:46 pm

    If you add

    b.setTextOff(Integer.toString(listaKart.get(licznik)));
    b.setTextOn(Integer.toString(listaKart.get(licznik)));
    

    to your inner for() loop, the buttons will always display their position in the list, whether toggled on or off.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.