How to add a counter in Java? - java

The app stops. Where am I wrong? The button when clicked change the text to the number of times the item was clicked.
private Button button;
int countClicks = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
countClicks++;
button.setText(countClicks);
}
});

That's a classic mistake
You are using View.setText() on an int, and it needs to be on a String.
Try
button.setText(countClicks + "");
By concatenating the integer with an empty String, it will auto-cast it to a String

Related

How to create EditText & Button so that EditText should take new input whenever button is pressed storing prev value?

What i did exactly is that user provides input in the EditText and it gets stored in the arrray and later when the button is pressed it clears the field
and now i incremented "number" but when i click button it always takes the previous value of "number". How can i send my incremented value of "number" in OnCreate?
so that whenever i click the button it should take incremented value of "number".
Thanks in Advance!
I tried using for loop it goes endlessly i.e it crashes.pls, help.
public class thirdactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thirdactivity);
Intent intent1 = getIntent();
final int no = intent1.getIntExtra(MainActivity.EXTRA_NUMBER4, 0);
Button Next = (Button)findViewById(R.id.button2);
Next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validate(no);
}
});
}
public void validate(int number){
/**
*created an Array to store the value
*/
int DLPoints[] = new int[number];
/**
*taking inptut from user and store in userDLPV
*/
EditText editText1 = (EditText) findViewById(R.id.points);
int userDLPV = Integer.parseInt(editText1.getText().toString());
DLPoints[number] = userDLPV;
editText1.setText("");
number++
}
}
public class thirdactivity extends AppCompatActivity {
ArrayList<Integer> userInput = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thirdactivity);
Intent intent1 = getIntent();
int no = intent1.getIntExtra(MainActivity.EXTRA_NUMBER4, 0) + 1;
Button Next = (Button)findViewById(R.id.button2);
Next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validate(no);
}
});
}
public void validate(int number){
/**
*taking input from user and store in userDLPV
*/
EditText editText1 = (EditText) findViewById(R.id.points);
editText1.setText("" + number);
userInput.add(number);
}
}

How to pass edit Text value to Toast?

I've just started with android and I'm trying to make simple program to take string from user and display the result as Toast
EditText e = (EditText) findViewById(R.id.editText);
final String result = e.getText().toString();
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
Now If I type anything in editText it's supposd to print that value but instead it's printing the default text value and even if I edit that it prints the same value
As you can see in the picture on pressing the button it shows "Name" on toast
and even upon changing it shows the same thing that is "Name". I want it too show the value that I typed later.
What should I do?
You store the text when you setup the views. Thats why you get the default text.
move
final String result=e.getText().toString();
into the onClick
#Override
public void onClick(View v) {
final String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
and it should get the text when the button is pressed.
Reason is your variable String result is not taking latest value when button is clicked.
Try this:
final EditText e = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
your requirement is show EditText Text in Toast
first make EditText object as globle
class ActivityClassFileName extend AppCompactActivity
{
EditText ToastText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
//initialize EditText object
ToastText = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//showing Toast
Toast.makeText(getApplicationContext(), ToastText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Sometimes you have to display toast outside of onclick,by creating new method.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = findViewById(R.id.button);
editText=findViewById(R.id.editTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String testString=editText.getText().toString();
showToast(testString);
}
});
}
private void showToast(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}

Android Programming mistake

I create an app name of android wallpaper but the problem is that when i clicked on left and right button the next and previous wallpaper does not comes. I tried it lots but failed.
public class Wallpapers extends AppCompatActivity {
WallpaperManager managerr;
Button B;
Button b1;
Button b2;
ImageView m;
int a[]={R.drawable.w1,R.drawable.w2,R.drawable.w3};
int aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallpapers);
click();
}
public void click()
{
m = (ImageView) findViewById(R.id.imageView);
B=(Button)findViewById(R.id.button3);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
managerr = WallpaperManager.getInstance(getApplicationContext());
m.setImageResource(R.drawable.w1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if()
{
m.setImageResource(R.drawable.w2);
}
else if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent())
{
m.setImageResource(R.drawable.w3);
}
}
});
}
}
That's the code.
That's the image of app:
you have set onClickListener only for b1.
change your code follows
int currentPosition = 0;
public void click(){
m = (ImageView) findViewById(R.id.imageView);
B= (Button) findViewById(R.id.button3);
left = (Button) findViewById(R.id.button);
right = (Button) findViewById(R.id.button2);
managerr = WallpaperManager.getInstance(getApplicationContext());
m.setImageResource(R.drawable.m1);
left.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
if(currentPosition > 0){
m.setImageResource(previuosDrawable);
currentPosition--;
}
}
}
right.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
if(currentPosition < pictures.length){
m.setImageResource(nextDrawable);
currentPosition++;
}
}
}
}
First of all, if() has no conditions so it can't works add first if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent())
and then add all the else if you need and finally the else including m.setImageResource(R.drawable.w2);
Second you need is to setOnClickListener to both Button objects because you only set the listener to b1 and not to b2
For what's B?
Why you have an array called a with the drawables if you don't access to it fields? You can access to drawables using a[index] and increasing or reducing the index number from 0 to 3 or from 3 to 0 when you click b1 or b2. It will be better...

How to change a text on the click of a button in android?

I need to set text of a button by taking value from edit text ,when button is clicked it has to change its text to the text specified in Edittext.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
TextView txt=(TextView)findViewById(R.id.editText);
btn.setText((CharSequence) txt);
}
}
);
}
you just need define EditText variable instead of TextView and call getText method of editText and then toString method like this:
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
EditText txt = (EditText)findViewById(R.id.editText);
btn.setText(txt.getText().toString());
}
}
);
Declare your views globally
Button btn;
EditText txt;
set your id in onCreate method
btn=(Button)findViewById(R.id.button);
txt=(EditText)findViewById(R.id.editText);
keep your onclicklistener as follow
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
final String newText=txt.getText().toString().trim();
if(newText.length()>0){
btn.setText(newText);
}
}
}
);
final Button btn = (Button)findViewById(R.id.button);
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText et = (EditText)findViewById(R.id.editText);
if (et != null){
Button btn = (Button)view;
btn.setText(et.getText());
}
}
});
}

android Clear Button in a simple counter

I have a counter program for number. I define 3 buttons for Plus, Minus and Clear.
When I use clear button for clear TextView it's good. But after using Plus and Minus it it is Continuation last counter. that is my code.
please Help me.
public class CounterActivity extends Activity {
private Button btnPlus;
private Button btnMinus;
private Button btnClear;
private TextView txtCounter;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_menu);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnClear = (Button) findViewById(R.id.btnClear);
txtCounter = (TextView) findViewById(R.id.txtCounter);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter++;
txtCounter.setText(counter + "");
}
});
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter--;
txtCounter.setText(counter + "");
}
});
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtCounter.setText(0 + "");
}
});
}
}
Change your clear button onClick method as follows:
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter = 0;
txtCounter.setText(0 + "");
}
});

Categories

Resources