how to calculate the number of touches on a button in android - java

hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.

Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable

A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};

Related

How can I use a double between two classes?

I want to build a currency calculator.
There is a plain text and a button.
if somebody scribe a number in the plain text and press the button a dialog will be shown.
BUT the number of the plaintext is everytime 2.1311!
here is my code
//this is the Main Activity
public class MainActivity extends AppCompatActivity {
public void PesoInEuro (View view){
EditText Peso = findViewById(R.id.EuroBetrag);
String amountPeso = Peso.getText().toString();
double amountPesodouble = Double.parseDouble(amountPeso);
double amountEurodouble = amountPesodouble * 46.85;
String amountEuro = String.valueOf(amountEurodouble);
Button buttonOne = (Button) findViewById(R.id.PesoEuro);
buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
}
public void openDialog(){
DiaPesoEuro exampleDialog = new DiaPesoEuro();
exampleDialog.show(getSupportFragmentManager(), "example Dialog");
}
First of all, it's a good practice to find your views in the OnCreate method.
Secondly, you have read and calculate your values when the user hit the button.
In the code above, you got the data in the PesoInEuro, and when the user presses the button, it shows the retrieved data and your calculation is based on them.

Android - How to make buttons listen more than one time

I have an app with a home screen and a bunch of buttons on it, and therefore listeners for each. After the user clicks on one of the buttons, a new layout is brought up and that layout has a back button with a listener.
The problem is that whenever the user presses the back button, the home screen layout is brought back up but none of the listeners work for the buttons anymore.
Here is some sample code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
setContentView(R.layout.main); // return to home screen
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
You should not change a screen with setContentView(). Screens are changed in Android by starting a new Activity with startActivity(new Intent(...)) or with Fragments like recommended by Malimo (which is a bit more difficult to do but much nicer). You call two times setContentView() where one is destroying the other one.
in my opinion you should use fragments for your contentviews. so every fragment will be responsible for its contentview and can add listeners each time it is displayed...
http://developer.android.com/guide/components/fragments.html
I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.
The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener,
when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.
To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners
from the home screen again.
I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...),
as well as when the back button is clicked:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listeners(); // set up all the listeners for the buttons
}
public void listeners()
{
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
listeners(); // recursively call the listeners again to 'start over'
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.

Android Notification Area Customization

I don't know whether this question get minus points, but I searched every where and my last resort is stackoverflow.
I need to add five buttons to notification area in horizontally. And each button I need to add even listener. I know it is possible to do with RemoteViews. But I never seen anyone adding event listener to each element.
These are the references if anyone need to refer.
Notifications Documentation
How to create a custom notification on android
SlidingDrawer API
You can add 5 anonymous listeners, or a single named listener.
Anonymous:
Button b1 = new Button(...);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// first listener's code goes here
}
});
Button b2 = new Button(...);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// second listener's code goes here
}
});
...
named is much the same, but contains a switch statement to differentiate what happens:
View.OnClickListener myListener = new View.OnClickListener() {
public void onClick(View v) {
String buttonTitle = ((Button)v).getText();
if ("title1".equals(buttonTitle)) {
// do things for the first button's click
} else if ("title2".equals(buttonTitle)) {
// do things for the second button's click
}
...
}
});
...

How to change button text color programmatically on press?

I create button like this:
Button button = new Button(this);
button.setText("2012");
button.setBackgroundColor(Color.TRANSPARENT);
button.setOnClickListener(mCorkyListener);
layout.addView(dateButton);
On click listiner i have this method. Here i want to change button text color. Bu View don't have this method
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
//v.setBackgroundColor(Color.RED);
//so how to change pressed button text color ?
//v.setTextColor(colors);
}
};
There wouldn't be just one button. There would be many of those and i need to chnage text color when button pressed.
I know you asked about changing text color, and everyone else has pretty well covered that, but you could also change the button color itself (which I find much more visible than a text color change :p)...
import android.graphics.PorterDuff;
To set it to green (assuming you start with a standard gray button):
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Button aButton = (Button) view.findViewById(R.id.abutton);
aButton.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
}
}
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
button.setTextColor(Color.RED);
}
};
button.setTextColor(Color.WHITE);
This will change the textcolor of the button.Do u want to give a press effet to button when click on it?
the best way to do this is not programmatically (using selector), but if you want to do it programmatically you can cast it to Button and then change the color.
public void onClick(View v) {
Button b = (Button) findViewById(v.getId());
b.setBackgroundColor(Color.RED)l
}
If you are interested to use the View of onClick(View v) then just cast it to Button
public void onClick(View v) {
if(v instanceof Button){
((Button)v).setTextColor(Color.WHITE);
}
}

Which context do i need?

I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.
Here is the code:
case R.id.delappt:
//rmvall();
final Dialog dialogdelsel = new Dialog(this);
dialogdelsel.setContentView(R.layout.delsel);
dialogdelsel.setTitle("What would you like to do?");
dialogdelsel.setCancelable(true);
Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
btndelsel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete selected code here.
}
});
Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
btndelall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete all code here.
final Dialog dialogdelcon = new Dialog();
dialogdelcon.setContentView(R.layout.delcon);
dialogdelcon.setTitle("Deletion Confirmation");
dialogdelcon.setCancelable(true);
Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
buttoncnclok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialogdelcon.dismiss();
}
});
dialogdelcon.show();
}
});
dialogdelsel.show();
break;
getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object
If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.
To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.

Categories

Resources