I have two Button for increase and decrease the text size of a TextView.
They both work, but: if I increase and then decrease, the first click on the decrease button will increase the text, and the second and so on will decrease. And the other way around.
This is my code:
int txtSize = 18;
volumeUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setTextSize(txtSize++);
}
});
volumeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setTextSize(txtSize--);
}
});
Man. Try this. Avoid to increase or decrease a variable like that when you use it multiple time. Your code set first txtSize in TextView before increase it.
So in the second button, you set the increased value before decrease it.
int txtSize = 18;
volumeUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSize = txtSize+1
mTextView.setTextSize(txtSize);
}
});
volumeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSize = txtSize-1
mTextView.setTextSize(txtSize);
}
});
Try to use ++texSize instead of using texSize++
Related
I want to disable a button when the user hits the button five times.
Here's the code
if(mLatestindex<=4) {
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
mFalseButton.setEnabled(true);
mTrueButton.setEnabled(true);
mLatestindex++;
}
}
Your mLatestIndex <= 4 is placed wrongly.
You should check the index inside the Click Listener
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mLatestIndex < 5) {
// Do the stuff
// This will execute only if the index is <= 4
}
mLatestIndex++;
}
});
This will execute the code only if mLatestIndex is less than 5, and increment it.
If you need disable button on 5 times click in general just add global counter and increment it in onClick() with check
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickerCount++;
if (clickerCount >= 5) {
button.setEnabled(false);
}
}
If you need to disable view when it was clicked 5 times in certain amount of time, you could store timestamp of last clicked time and compare it with current time
I want to create an Android Activity having a power bar and a Button, such that whenever i click the Button, the power bar gets filled by say 10% of its total height.
What Android widget to use for a power bar ?
And how to update its fill?
progress=(ProgressBar)findViewById(R.id.progressBar1);
plus=(Button)findViewById(R.id.button1);
minus=(Button)findViewById(R.id.button2);
plus.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(v==plus)
{
progress.incrementProgressBy(10);
setProgress(100*progress.getProgress());
}
}
});
minus.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v1)
{
if(v1==minus)
{
progress.incrementProgressBy(-10);
setProgress(90*progress.getProgress());
}
}
});
I have created an 800 xml layout and a Fragment class, but opening a fragment with a button click takes too long.
Can anyone help me optimize the following code sample?
View lesson655 = (View) getActivity().findViewById(R.id.txtlesson655);
lesson655.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int i;
pager.setCurrentItem(0);
for (i = 1; i <= 655; i++) {
pager.setCurrentItem(pager.getCurrentItem() + 1);
}
}
});
If you don't need to go step by step through every Item you can do it this way:
View lesson655 = (View) getActivity().findViewById(R.id.txtlesson655);
lesson655.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pager.setCurrentItem(655);
}
});
I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}
How to change the android button after 2 clicks ?
the first time to change button i will use this code
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
I want to change the button view again after pressing it one more time
how can i do that ?
Perhaps do it like this:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
This will set the background after every 2nd click on your button (view).
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
Well, one way is to keep a counter.
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}