Android button with multiple click simple - java

i am new in android programming, and i am working on my first application, so i just want to know how every time when i click same button it does some action, for example if i have a button called ( next ) and i want to click on it and an image will appear, this one i did it, but i want to click on the same button and show another image view in the same activity.
i have tried some code but with no results
so please if anyone can post a code that explain how i can do it.

Here is a quick example of code...
Rather than show a random image, it will show a random String. All you need to do is just modify it to show images instead.
public class MainActivity extends Activity {
private String[] names = {"Joe", "Mark", "Amanda", "Kelly", "Michael", "Jenny"};
private Button button;
private TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get our views
name = (TextView) findViewById(R.id.name);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
next(); // show random name
}
});
}
// Display random name on button click
private void next() {
name.setText(names[rand()]);
}
// Pick a random number from 0 to names.length
private int rand() {
return new Random().nextInt(names.length);
}
}

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.

I Would Like To Have A Single Button Display A Different Line Of Text Each Consecutive Time It's Pressed in Android Studio

I'm extremely new to coding, so apologies if this question is trivial or the answer is easily found somewhere. I have searched, but I cannot find anything that helps.
Basically, I'm trying to code a simple, 2 button app in Android Studio.
Button1 is meant to simply display a series of commands to the user via text box.
Button2 merely resets.
My problem is, I would like Button1 to change what's displayed in the text view each time it is pressed, but I cannot figure out how to do so. I don't want to make 6 or 7 buttons.
Basically I would like it to run as follows;
Text = "Pick a number"
user presses Button1
Text = "Add 15" (This is as far as I've gotten)
user presses Button1
Text = "Multiply times 5"
user presses Button1 etc. etc. etc.
If anybody could please explain or usher me in the right direction, I would be greatly appreciative.
You can use button.setOnClickListener
public class MyActivity extends Activity {
EditText et;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Perform your Logic here.
et.setText("New text");
}
});
}
}
you can use a globle and a swithchcase
public class MyActivity extends Activity {
EditText et;
int CLICKS=0;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CLICKS++;
switch(clicks)
{
case 1:
et.setText("Pick a number");
break;
case 2:
et.setText("Add 15");
break;
case 3:
et.setText("Multiply times 5");
break;
}
}
});
}
}

how to use java onclick shuffle array

I am learning how to use strings and onlclick in java. I have written a programme below which shuffle three names and then outputs them into three buttons.
When I click on Paul, I want the message to be displayed in message box. Since Paul will be in a button each time. I am puzzled on how to attach my message to Paul.
Paul moves around due to the use of array. I understand this is a tough question, but I also know, there are some very clever ppl out there who love a challenge.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void generate(View view) {
String [] names = new String[3];
names[0] = "Bob";
names[1] = "Paul";
names[2] = "Mike";
Button btn_a = (Button) findViewById(R.id.a);
Button btn_b = (Button) findViewById(R.id.b);
Button btn_c = (Button) findViewById(R.id.c);
TextView message = (TextView)findViewById(R.id.message);
Arrays.asList(names);
Collections.shuffle(Arrays.asList(names));
btn_a.setText(names[0]);
btn_b.setText(names[1]);
btn_c.setText(names[2]);
}
public void a1(View view) {
}
public void b1(View view) {
}
public void c1(View view) {
}
}
This is a trick practical implementation in Java where a single listener is used for multiple buttons, rather than one listener for each button, so that each button's content determines what happens, not each button's listener. Helps for dynamic button grids (i.e. an 8x8 chessboard) to not define 64 listeners and code them all.
I don't have an Android IDE on hand, so this is pseudo-code, but you should be able to get the gist from this.
//Create a Universal Listener for all our buttons
OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText().toString(); //get the button's name
if(text.equals("Paul")) {
//do anything for Paul ONLY in here
}
}
});
btn_a.setOnClickListener(listener); //give all the buttons the same listener, but only Paul's listener will do anything when you click on it
btn_b.setOnClickListener(listener);
btn_c.setOnClickListener(listener);
Using info from: http://developer.android.com/reference/android/widget/Button.html and https://stackoverflow.com/a/5620816/2958086

How to assign a numerical value to a check RadioButton and pass this onto the next activity (similar to a questionnaire app)

I am new to Android development and I am trying to design a simple questionnaire-type app. Each question has set answers within a RadioGroup. I would like to assign a numerical value to the checked RadioButton so I can then send this to the next activity where eventually it will be used to total the scores and produce a message.
I have been able to create a toast; for testing, to show which RadioButton is checked. But I am having trouble with assigning a value to each RadioButton once checked. I know that I will need to use a IF statement but not sure how. Can anyone help or send me a link to a good tutorial?
Here's some sample code within one of my .java file;
public class Diabetes_Question_1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diabetes_question_1);
Button btnBack = (Button) findViewById(R.id.btnBack1);
btnBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Diabetes_Question_1.this, Diabetes_Question_2.class));
}
});
final RadioGroup radioDQ1Group = (RadioGroup) findViewById(R.id.radioDQ1Group1);
Button btnNext = (Button) findViewById(R.id.btnNext1);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Diabetes_Question_1.this, Diabetes_Question_3.class));
int selectedId = radioDQ1Group.getCheckedRadioButtonId();
RadioButton rb1 = (RadioButton) findViewById(selectedId);
Toast.makeText(Diabetes_Question_1.this, rb1.getText(), Toast.LENGTH_SHORT).show();
}
});
}
};
Every view has a tag- an object that can hold whatever data you want. Use it to hold the integer value- you can do that via setTag() and getTag(). Then when you start the new activity, add it to the extras bundle of the intent you launch. The new activity should then read that value from the incoming intent.

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

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++;
}
};

Categories

Resources