Reputation: 53
I currently have 2 variables, iR
and iL
, which are defined as the number of times that the user touches the screen. I define them as shown below:
public class Touchscreen extends Activity {
int iL;
int iR;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touchscreen);
final View touchLayoutL = findViewById(R.id.touchLayoutL);
final View touchLayoutR = findViewById(R.id.touchLayoutR);
final Button redo = (Button)findViewById(R.id.redo);
final Button next = (Button)findViewById(R.id.next);
iL=1;
iR=1;
touchLayoutL.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if (event.getAction()==(MotionEvent.ACTION_DOWN)){
iL++;
}
});
touchLayoutR.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if (event.getAction()==(MotionEvent.ACTION_DOWN)){
iR++;
}
});
if (iL>1 || iR>1) redo.setTypeface(null, Typeface.BOLD);
if (iL>3 && iR>3) next.setTypeface(null, Typeface.BOLD);
}
What I'm having trouble with is the last two lines of code,
if (iL>1 || iR>1) redo.setTypeface(null, Typeface.BOLD);
if (iL>3 && iR>3) next.setTypeface(null, Typeface.BOLD);
for some reason, the Typeface
of redo
and next
never change. I have tried moving these two lines in different places, but they still do not work. Does anyone know why this is occurring?
Thanks in advance
Upvotes: 0
Views: 71
Reputation: 1155
The setTypeface
requires a Typeface
, you cannot use null
.
If you want to use whatever the current Typeface
is of the Button
you can use the TextView#getTypeface() method.
if (iL>1 || iR>1) redo.setTypeface(redo.getTypeface(), Typeface.BOLD);
if (iL>3 && iR>3) next.setTypeface(next.getTypeface(), Typeface.BOLD);
Or, if you are using the normal
font then you can use the setTypeface
single-paramter method. In which case you would provide Typeface.DEFAULT_BOLD
if (iL>1 || iR>1) redo.setTypeface(Typeface.DEFAULT_BOLD);
if (iL>3 && iR>3) next.setTypeface(Typeface.DEFAULT_BOLD);
/*
* Same as above
if (iL>1 || iR>1) redo.setTypeface(Typeface.NORMAL, Typeface.BOLD);
if (iL>3 && iR>3) next.setTypeface(Typeface.NORMAL, Typeface.BOLD);
*/
Upvotes: 0
Reputation: 2641
That's because
if (iL>1 || iR>1) redo.setTypeface(null, Typeface.BOLD);
if (iL>3 && iR>3) next.setTypeface(null, Typeface.BOLD);
is only executed in the onCreateMethd, but not attached to the listeners.
Try moving into the onTouch methods
Upvotes: 3