Valerie Thomas
Valerie Thomas

Reputation: 13

Android Problems with Clickable TextView

So, an explanation of my app first: the main activity is supposed to call a second activity, which then implements a fragment in order to display details about a list item clicked. All of this seems to be going okay, but within the new activity/details window, there's a TextView that the user should be able to click in order to change from 'finished' to 'not finished' and vice versa. Right now the new fragment loads, but it doesn't seem to recognize interaction from the user. Any help would be appreciated. Thanks!

public class ListActivity extends Activity implements ListSelectionFragment.OnListFragmentInteractionListener, View.OnClickListener {


private boolean finished=FALSE;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ListSelectionFragment detailFrag = ListSelectionFragment.newInstance(displayMode, position, page);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    fragmentTransaction.add(R.id.item_frame, detailFrag);
    fragmentTransaction.commit();

    findViewById(R.id.item_frame).setVisibility(View.VISIBLE);

    TextView textView1 = (TextView) findViewById(R.id.list_item_name);
    textView1.setText(displayMode);

    if(finished==TRUE) {
        TextView textView2 = (TextView) findViewById(R.id.completebtn);
        textView2.setText("Finished");
        textView2.setTextColor(Color.parseColor("#000000"));
    }

    TextView completeBtn = (TextView) findViewById(R.id.completebtn);
    completeBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    TextView textView = (TextView) findViewById(R.id.completebtn);
    textView.setText("Finished");
    finished=TRUE;
}
}

Upvotes: 0

Views: 295

Answers (2)

Maihan Nijat
Maihan Nijat

Reputation: 9355

Set click listener on your TextView to catch the click.

For example:

First register the event listener on your TextView.

textView1.setOnClickListener(this);

And once the event (click) is happened and do the following:

@Override
public void onClick(View v) {
    finished=TRUE;
}

Update:

Replace following which you have onCreate:

if(finished==TRUE) {
  TextView textView2 = (TextView) findViewById(R.id.completebtn);
  textView2.setText("Finished");
  textView2.setTextColor(Color.parseColor("#000000"));
}

With:

textView1.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
      TextView textView = (TextView) findViewById(R.id.completebtn);
      textView.setText("Finished");
      finished=TRUE;
      TextView textView2 = (TextView) findViewById(R.id.completebtn);
      textView2.setText("Finished");
      textView2.setTextColor(Color.parseColor("#000000"));
   }
});

Note: I suggest to use setOnFocusChangeListener instead OnClick. Get the content of the TextView and if it's filled or correct then change the background colour because if user click on TextView, the background will change, even without having content.

Here is the code:

textView1.setOnFocusChangeListener(new OnFocusChangeListener() {
   @Override
   public void onFocusChange(View v, boolean hasFocus) {
       // Put your code here, what you want to execute.
   }
});

Upvotes: 1

Lucurious
Lucurious

Reputation: 174

Thats because you check if finished is true in the on create method, this, however will only get checked at the start of the program, two options you have are:

  • Check if it is finished in a loop in a separate thread (VERY BAD OPTION)

  • run a method from the onclick method and from the oncreate method (only if it is true)

Upvotes: 0

Related Questions