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 8051051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:13:17+00:00 2026-06-05T07:13:17+00:00

first post please be gentle… This code creates a table 10 x 10 full

  • 0

first post please be gentle…

This code creates a table 10 x 10 full of hyphens in textviews simples…

With the onClickListner I need it to change the colour if the text of the one clicked.

it fails on tv.setTextColor(Color.RED);

can be sorted or is there a better way??

Thanks.

_Temp class:

public class _Temp extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_temp);


    TableLayout treeTable = (TableLayout) findViewById(R.id.main_table);

    // Add 10 Rows / 10 Cols ~~~~~~~~~~~~~~~~~~~~~~~~~~
    int idxCount = 0;

    for (int tree = 1; tree <= 10; tree++) {
        idxCount = 0;

        TableRow TableRows = new TableRow(this);
        TableRows.setId(tree * 100 + idxCount);
        TableRows.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        TableRows.setBackgroundColor(Color.LTGRAY);
        idxCount++;

        for (int bird = 1; bird <= 10; bird++) {
            TextView label_TableCols2 = new TextView(this);
            label_TableCols2.setId(tree * 100 + idxCount);
            label_TableCols2.setBackgroundColor(Color.DKGRAY);
            label_TableCols2.setClickable(true);
            label_TableCols2.setOnClickListener(this);

            label_TableCols2.setText("-");

            TableRows.addView(label_TableCols2); // add the column to the table row
            idxCount++;
        }
        treeTable.addView(TableRows, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    }
}

public void onClick(View v) {
    Log.i("scoreTable", "Something Clicked - ID = " + Integer.toString(v.getId()));

    int resId = getResources().getIdentifier(Integer.toString(v.getId()), "id", getPackageName());

    Log.i("scoreTable", "Colour Update 1 - resId = " + Integer.toString(resId));

    TextView tv = (TextView) findViewById(resId);

    Log.i("scoreTable", "Colour Update 2 - Just to see if it gets this far");

    tv.setTextColor(Color.RED);

    Log.i("scoreTable", "Colour Update 3 - Just to see if it gets this far");
}
}

and a_temp.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

                <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/main_table"
                android:layout_width="match_parent"
                android:layout_height="fill_parent" >

            </TableLayout>

        </LinearLayout>

    </HorizontalScrollView>

</ScrollView>

Edit:
OK that was too easy… how about from an inflator!!

so, click an item in the table inflator popup, pick colour and click “Choose Colour” button, update colour of the hyphen clicked.

public void onClick(View v) {
    ((TextView) v).setTextColor(Color.RED); // <--- SWEET, WORKING

    AlertDialog.Builder PopUpBuilder = new AlertDialog.Builder(this);

    //...

    PopUpBuilder.setPositiveButton("Choose Colour", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            // Do stuff and store global variables

            updateTable();
        }
    });
    AlertDialog PopupDialog = PopUpBuilder.create();
    PopupDialog.show();
} 

public void updateTable() {

    TextView scoreTableTitle = (TextView) findViewById(R.id.textTitle1);
    scoreTableTitle.setText("woo woo"); // <--- WORKING

    ((TextView) v).setTextColor(Color.BLUE); // <--- ????           
}
  • 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-06-05T07:13:18+00:00Added an answer on June 5, 2026 at 7:13 am

    The clicked View is your TextView, so you just have to set his color on your listener :

    public void onClick(View v) {
        ((TextView) v).setTextColor(Color.RED);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post, so please be gentle. I've been playing around with
This is my first post on this forum (so please be gentle ) I'm
This is my first post on StackOverflow, so please be gentle... I have some
This is my first post, so please be gentle. I've recentley started using Powershell
This is my first post on Stack, so please bear with me if I
This is my first post here. I have a problem. I need to take
This is my first post, so please be nice :) I have a question
This is my first post, so please forgive me if this question has been
This is my first post, and first please forgive me for my poor english.
First question on SOF, please be gentle if this may be a stupid question.

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.