I am implementing a click counter variable to keep track of how many times a button is clicked and to do something each time it is clicked. I am implementing it as the following code shows.
package com.mycompany.myapp3;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import static java.lang.System.out;
import java.util.Random;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity{
public boolean onClick;
public int count=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView1=(TextView)findViewById(R.id.textView1);
final Button myButton=(Button)findViewById(R.id.btn);
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
count++;
if(onClick=true && count==1){
textView1.setText("Hello");
//Activity performed onClick
}
}
});
}
}
Whenever I try to remove the final button and replace it with a button my app crashes. How do I implement the variable into more than one button(eg not having a final button, only a button)?
(Forgive me if you are seeing this before I add the code. For some reason the toolbar does not show up until I ask the question and edit it.)