Hello i'm creating an app to scan buisness cards, it has lot of views but one works very slow it has 19 buttons. Is it possible that slow working of my app is caused by this number of buttons? I've tried to put listeners outside of onCreate but it didn't help. This is my java file conected to this view:
public class Szczegoly extends Activity {
long contactID;
Bitmap contactFoto;
int imagebig=0;
int w;
Contact contact;
EditText tv1;
EditText tv2;
EditText tv3;
EditText tv4;
EditText tv5;
ImageView i;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b10;
Context to;
Button accept1;
Button accept2;
Button accept3;
Button accept4;
Button accept5;
Button cancel1;
Button cancel2;
Button cancel3;
Button cancel4;
Button cancel5;
Context context;
OnClickListener accept1listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==1)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept2listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==2)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept3listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==3)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept4listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==4)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept5listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==5)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener cancel1listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel2listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel3listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel4listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel5listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener b1lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
finish();
}
}
};
OnClickListener b2lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
contact.delete();
finish();
}
}
};
OnClickListener b3lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
}
}
};
OnClickListener b4lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
Uri call = Uri.parse("tel:"+contact.Phone1);
Intent intent = new Intent(Intent.ACTION_CALL, call);
startActivity(intent);
}
}
};
OnClickListener b5lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
}
}
};
OnClickListener b6lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
if(tv1.isEnabled())
{
tv1.setSelected(false);
tv1.setEnabled(false);
}
else
{
cancel1.setVisibility(Button.VISIBLE);
accept1.setVisibility(Button.VISIBLE);
b6.setVisibility(Button.INVISIBLE);
tv1.setEnabled(true);
tv1.setSelected(true);
tv1.setFocusableInTouchMode(true);
tv1.requestFocus();
tv1.setSelection(tv1.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 1;
}
}
};
OnClickListener b7lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
if(tv2.isEnabled())
tv2.setEnabled(false);
else
{
cancel2.setVisibility(Button.VISIBLE);
accept2.setVisibility(Button.VISIBLE);
b7.setVisibility(Button.INVISIBLE);
tv2.setEnabled(true);
tv2.setSelected(true);
tv2.setFocusableInTouchMode(true);
tv2.requestFocus();
tv2.setSelection(tv2.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 2;
}
}
};
OnClickListener b8lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr == 0)
{
if(tv3.isEnabled())
tv3.setEnabled(false);
else
{
cancel3.setVisibility(Button.VISIBLE);
accept3.setVisibility(Button.VISIBLE);
b8.setVisibility(Button.INVISIBLE);
tv3.setEnabled(true);
tv3.setEnabled(true);
tv3.setSelected(true);
tv3.setFocusableInTouchMode(true);
tv3.requestFocus();
tv3.setSelection(tv3.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr=3;
}
}
};
OnClickListener b9lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0 )
{
if(tv4.isEnabled())
tv4.setEnabled(false);
else
{
cancel4.setVisibility(Button.VISIBLE);
accept4.setVisibility(Button.VISIBLE);
b9.setVisibility(Button.INVISIBLE);
tv4.setEnabled(true);
tv4.setEnabled(true);
tv4.setSelected(true);
tv4.setFocusableInTouchMode(true);
tv4.requestFocus();
tv4.setSelection(tv4.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 4;
}
}
};
OnClickListener b10lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr==0)
{
if(tv5.isEnabled())
tv5.setEnabled(false);
else
{
cancel5.setVisibility(Button.VISIBLE);
accept5.setVisibility(Button.VISIBLE);
b10.setVisibility(Button.INVISIBLE);
tv5.setEnabled(true);
tv5.setEnabled(true);
tv5.setSelected(true);
tv5.setFocusableInTouchMode(true);
tv5.requestFocus();
tv5.setSelection(tv5.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null)
{
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 5;
}
}
};
int edit_nr=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_szczegoly);
Log.d("dupa","dupa1");
context = this;
Intent intent = getIntent();
contactID = intent.getLongExtra("contactID", 0);
contact = new Contact(contactID, this);
tv1 = (EditText) findViewById(R.id.editText1);
tv2 = (EditText) findViewById(R.id.editText2);
tv3 = (EditText) findViewById(R.id.editText3);
tv4 = (EditText) findViewById(R.id.editText4);
tv5 = (EditText) findViewById(R.id.editText5);
i = (ImageView) findViewById(R.id.imageView1);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
b10 = (Button) findViewById(R.id.button10);
tv1.setText(contact.FirstName);
tv2.setText(contact.LastName);
tv3.setText(contact.Phone1);
tv4.setText(contact.Phone2);
tv5.setText(contact.EmaiL);
accept1 = (Button) findViewById(R.id.accept1);
accept2 = (Button) findViewById(R.id.accept2);
accept3 = (Button) findViewById(R.id.accept3);
accept4 = (Button) findViewById(R.id.accept4);
accept5 = (Button) findViewById(R.id.accept5);
cancel1 =(Button) findViewById(R.id.cancel1);
cancel2 =(Button) findViewById(R.id.cancel2);
cancel3 =(Button) findViewById(R.id.cancel3);
cancel4 =(Button) findViewById(R.id.cancel4);
cancel5 =(Button) findViewById(R.id.cancel5);
}
public void closeEdit()
{
tv1.setSelected(false);
tv1.setEnabled(false);
tv2.setSelected(false);
tv2.setEnabled(false);
tv3.setSelected(false);
tv3.setEnabled(false);
tv4.setSelected(false);
tv4.setEnabled(false);
tv5.setSelected(false);
tv5.setEnabled(false);
cancel1.setVisibility(Button.INVISIBLE);
accept1.setVisibility(Button.INVISIBLE);
b6.setVisibility(Button.VISIBLE);
cancel2.setVisibility(Button.INVISIBLE);
accept2.setVisibility(Button.INVISIBLE);
b7.setVisibility(Button.VISIBLE);
cancel3.setVisibility(Button.INVISIBLE);
accept3.setVisibility(Button.INVISIBLE);
b8.setVisibility(Button.VISIBLE);
cancel4.setVisibility(Button.INVISIBLE);
accept4.setVisibility(Button.INVISIBLE);
b9.setVisibility(Button.VISIBLE);
cancel5.setVisibility(Button.INVISIBLE);
accept5.setVisibility(Button.INVISIBLE);
b10.setVisibility(Button.VISIBLE);
edit_nr=0;
}
public void onResume() {
super.onResume();
Log.d("dupa","dupa");
Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(size);
to = this;
w = size.x;
contactFoto = Bitmap.createScaledBitmap(contact.photo, w, (int) (w*contact.photo.getHeight()/contact.photo.getWidth()), true);
i.setImageBitmap(contactFoto);
accept1.setOnClickListener(accept1listener);
accept2.setOnClickListener(accept2listener);
accept3.setOnClickListener(accept3listener);
accept4.setOnClickListener(accept4listener);
accept5.setOnClickListener(accept5listener);
cancel1.setOnClickListener(cancel1listener);
cancel2.setOnClickListener(cancel2listener);
cancel3.setOnClickListener(cancel3listener);
cancel4.setOnClickListener(cancel4listener);
cancel5.setOnClickListener(cancel5listener);
b1.setOnClickListener(b1lisner);
b2.setOnClickListener(b2lisner);
b3.setOnClickListener(b3lisner);
b4.setOnClickListener(b4lisner);
b5.setOnClickListener(b5lisner);
b6.setOnClickListener(b6lisner);
b7.setOnClickListener(b7lisner);
b8.setOnClickListener(b8lisner);
b9.setOnClickListener(b9lisner);
b10.setOnClickListener(b10lisner);
}
}
The number of buttons by itself is not a likely cause of slowness. However, from the code it seems as if many actions from listeners (ie contact.savedata_nr) might be running on the main/UI thread which could cause slowness, depending on what happens when these methods are called. As mentioned in the comment, creating Bitmap might be better task to be carried out in the background as well.
To fix this, you should review your code and see what can be run in the background thread instead. Operations like saving can be done on the background thread, and then any updates to the GUI after these should be carried out on UI thread. If the background operation takes a long time, it's a good idea to up a progress indicator to show the user that something is happening and that the screen isn't just stuck.
You can read about Android threading here.
The basic example would be:
new Thread(new Runnable()
{
public void run()
{
//put background code here
contact.save(...)
//after background task is finished, update the UI
runOnUiThread(new Runnable()
{
#Override
public void run()
{
//put UI code here
textview.setText(...);
}
});
}
}).start();
Related
in if(saveInstanceState != null) I am trying to save the changes of the text in Text Views that happened by whoWon() method to save it in while user rotate the phone and get it back as it was before rotation but it shows me an error that says getText() in TextView cannot be applied to (java.lang.string) , I tried to add .toString() but it didn't work.
public class Multiplayer extends AppCompatActivity {
private TextView mTicTacTextViews [] = new TextView[9];
private Button mButtonX;
private Button mButtonO;
private Button mResult;
private int mcheckAllButtonsClicked =0;
private String TEXT_VIEW_VALUES_KEY = "VALUES OF TEXT VIEW'S";
private String mTextviewValues [] = new String[9];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null)
{
String values[]=savedInstanceState.getStringArray(TEXT_VIEW_VALUES_KEY);
for (int i=0;i<values.length;i++ ) {
mTextviewValues[i]=values[i];
mTicTacTextViews[i].getText(mTextviewValues[i]);
}
}
setContentView(R.layout.activity_tic_tac);
mTicTacTextViews[0] = (TextView) findViewById(R.id.tic_tac_one);
mTicTacTextViews[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[0]);
ifPressed_OButton(mTicTacTextViews[0]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[1] = (TextView) findViewById(R.id.tic_tac_two);
mTicTacTextViews[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[1]);
ifPressed_OButton(mTicTacTextViews[1]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[2] = (TextView) findViewById(R.id.tic_tac_three);
mTicTacTextViews[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[2]);
ifPressed_OButton(mTicTacTextViews[2]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[3] = (TextView) findViewById(R.id.tic_tac_four);
mTicTacTextViews[3].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[3]);
ifPressed_OButton(mTicTacTextViews[3]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[4] = (TextView) findViewById(R.id.tic_tac_five);
mTicTacTextViews[4].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[4]);
ifPressed_OButton(mTicTacTextViews[4]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[5] = (TextView) findViewById(R.id.tic_tac_six);
mTicTacTextViews[5].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[5]);
ifPressed_OButton(mTicTacTextViews[5]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[6] = (TextView) findViewById(R.id.tic_tac_seven);
mTicTacTextViews[6].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[6]);
ifPressed_OButton(mTicTacTextViews[6]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[7] = (TextView) findViewById(R.id.tic_tac_eight);
mTicTacTextViews[7].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[7]);
ifPressed_OButton(mTicTacTextViews[7]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[8] = (TextView) findViewById(R.id.tic_tac_nine);
mTicTacTextViews[8].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[8]);
ifPressed_OButton(mTicTacTextViews[8]);
mcheckAllButtonsClicked+=1;
}
});
mButtonX = (Button) findViewById(R.id.x_button);
mButtonX.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mUserPressed_XButton =true;
}
});
mButtonO = (Button) findViewById(R.id.o_button);
mButtonO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mUserPressed_OButton =true;
}
});
mTextviewValues [0]=mTicTacTextViews[0].getText().toString();
mTextviewValues [1]=mTicTacTextViews[1].getText().toString();
mTextviewValues [2]=mTicTacTextViews[2].getText().toString();
mTextviewValues [3]=mTicTacTextViews[3].getText().toString();
mTextviewValues [4]=mTicTacTextViews[4].getText().toString();
mTextviewValues [5]=mTicTacTextViews[5].getText().toString();
mTextviewValues [6]=mTicTacTextViews[6].getText().toString();
mTextviewValues [7]=mTicTacTextViews[7].getText().toString();
mTextviewValues [8]=mTicTacTextViews[8].getText().toString();
mResult = (Button) findViewById(R.id.result_button);
mResult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
whoWon();
}
});
}
private void whoWon ()
{
mTextviewValues [0]=mTicTacTextViews[0].getText().toString();
mTextviewValues [1]=mTicTacTextViews[1].getText().toString();
mTextviewValues [2]=mTicTacTextViews[2].getText().toString();
mTextviewValues [3]=mTicTacTextViews[3].getText().toString();
mTextviewValues [4]=mTicTacTextViews[4].getText().toString();
mTextviewValues [5]=mTicTacTextViews[5].getText().toString();
mTextviewValues [6]=mTicTacTextViews[6].getText().toString();
mTextviewValues [7]=mTicTacTextViews[7].getText().toString();
mTextviewValues [8]=mTicTacTextViews[8].getText().toString();
if(mTextviewValues[0].equals(mTextviewValues[1]) & mTextviewValues[1].equals(mTextviewValues[2]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[1].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[8]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[0].equals(mTextviewValues[3]) & mTextviewValues[3].equals(mTextviewValues[6]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[1].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[7]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[2].equals(mTextviewValues[5]) & mTextviewValues[5].equals(mTextviewValues[8]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[2].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[6]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[2].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[6]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[6].equals(mTextviewValues[7]) & mTextviewValues[7].equals(mTextviewValues[8]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else Log.i("error" , "whowon didn't work");
}
#Override
protected void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putStringArray(TEXT_VIEW_VALUES_KEY,mTextviewValues);
}
}
getText() doesn't take a parameter. mTicTacTextViews[i].getText() returns the text in the view. Are you trying to set the text in the view? If so, you want setText(CharSequence), not CharSequence getText().
i want to create a counter with a text view and want for every 15 click , to change the text in the text view and that is what i have wrote to create the counter ........ i have two text views .... one for the no. of clicks and the another for the text i want to show ,,,,,,,,,,, so i want to use "if" to set the counter to 0 (the counter is tvx) and to change the text (tvx2) to "click 15 more"
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener()
{
public int mCounter;
public Integer tx;
#Override
public void onClick(View v) {
mCounter++;
txv.setText(Integer.toString(mCounter));
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
public int mCounter=0;
public Integer tx =15;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCounter++;
if(mCounter==tx){
txv.setText(Integer.toString(mCounter));
//reset the counter after 15 click
mCounter=0
}
}
}
);
}
}
you can this way, use the ValueAnimator
public void animateText(Integer startValue, Integer endValue) {
setStartValue(startValue);
setEndValue(endValue);
ValueAnimator animator = ValueAnimator.ofInt(startValue, endValue);
animator.setInterpolator(getInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
String cleanString = animation.getAnimatedValue().toString().replaceAll("[,]", "");
BigDecimal parsed = new BigDecimal(cleanString);
String formatted = NumberFormat.getNumberInstance(Locale.US).format(parsed);
tv.setText(formatted);
}
});
animator.setEvaluator(new TypeEvaluator<Integer>() {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return Math.round(startValue + (endValue - startValue) * fraction);
}
});
animator.setDuration(getDuration());
animator.start();
}
Hello Try this if it may help
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener() {
public int mCounter=0;
//initialize mCounter with 0
public Integer tx;
#Override
public void onClick(View v) {
if(mCounter==15){
//check if mCounter equals 15
txv2.setText(Integer.toString("text to display after 15 click"));
//reset mCounter for another iteration
mCounter=0;
}
mCounter++;
//increment the mCounter
txv.setText(Integer.toString(mCounter));
}
});
}
I'm not really understand what do you want, but if you want to increase the counter every 15 click, just add if conditional in your code like this:
btn.setOnClickListener(new View.OnClickListener() {
public int mCounter;
public Integer tx;
#Override
public void onClick(View v) {
mCounter++;
if(mCounter == 15){
tx++;
txv.setText(String.valueOf(tx));
}
}
}
);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener() {
public int mCounter;
public Integer tx;
public int mCounter2;
#Override
public void onClick(View v) {
mCounter++;
mCounter2++;
txv.setText(""+mCounter);
if(mCounter2==15){
txv2.setText(""+mCounter2);
mCounter2=0;
}
}
}
);
}
I hope and you serve this
public class MainActivity extends AppCompatActivity {
private Button btn;
private TextView txv,txv2;
private int contador = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
++contador;
if(contador <= 15){
txv.setText(String.valueOf(contador));
}else{
txv2.setText("your text");
}
}
});
}
}
I have a program with 10 songs
When I click on one of the songs the seekbar start moving
But the problem is when I click on another song seekbar doesn't go back and start moving again it just stop where the first song has stopped
So how can I reset the seekbar when I click on another song
public class on extends AppCompatActivity {
MediaPlayer x;
int tx1;
mythread my = new mythread();
private SeekBar seekbar;
class mythread extends Thread {
public void run() {
while (x != null) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
seekbar.post(new Runnable() {
#Override
public void run() {
seekbar.setProgress(x.getCurrentPosition());
}
});
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on);
Button a = (Button) findViewById(R.id.button1);
Button b = (Button) findViewById(R.id.button2);
Button c = (Button) findViewById(R.id.button3);
Button d = (Button) findViewById(R.id.button4);
Button e = (Button) findViewById(R.id.button5);
Button f = (Button) findViewById(R.id.button6);
Button g = (Button) findViewById(R.id.button7);
Button h = (Button) findViewById(R.id.button8);
Button i = (Button) findViewById(R.id.button9);
Button j = (Button) findViewById(R.id.button10);
seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tx1 = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
x.seekTo(tx1);
}
});
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onone);
seekbar.setMax(x.getDuration());
x.start();
my.start();
}
});
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.ontwo);
seekbar.setMax(x.getDuration());
x.start();
my.start();
}
});
c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onthree);
seekbar.setMax(x.getDuration());
x.start();
my.start();
}
});
d.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onfour);
seekbar.setMax(x.getDuration());
my.start();
x.start();
}
});
e.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onfive);
seekbar.setMax(x.getDuration());
my.start();
x.start();
}
});
f.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onsix);
seekbar.setMax(x.getDuration());
my.start();
x.start();
}
});
g.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onseven);
seekbar.setMax(x.getDuration());
my.start();
x.start();
}
});
h.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.oneight);
seekbar.setMax(x.getDuration());
my.start();
x.start();
}
});
i.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onnine);
seekbar.setMax(x.getDuration());
my.start();
x.start();
}
});
j.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
x = MediaPlayer.create(on.this, R.raw.onten);
seekbar.setMax(x.getDuration());
my.start();
x.start();
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
if (x != null) {
x.stop();
}
}
private void stopPlaying() {
if (x != null) {
x.stop();
x.release();
x = null;
}
}}
and I tried to do this method in every onClick
public void resetseek(){
if(seekbar.getProgress() > 0)
{
seekbar.setProgress(0);
}
}
Normally one would use
seekbar.setProgress(0);
to reset a progress bar it to it's initial position.
I see you have posted your code as an answer instead of editing your question.
One big issue with your code is that you call Thread.start() more than once, which is illegal.
Also I wouldn't update the ProgressBar in a separate thread, instead you could just do it on the UI thread.
An alternate approach can be read in this question MediaPlayer, ProgressBar
Hello i'm getting the error:
the method setonclickListener(new onclickListener(){}) is undefined
for the type button
Here's my code:
public class Sliding extends Activity {
/** Called when the activity is first created. */
private LinearLayout MenuList;
private Button btnToggleMenuList;
private int screenWidth;
private boolean isExpanded;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding);
MenuList = (LinearLayout) findViewById(R.id.linearLayout2);
btnToggleMenuList = (Button) findViewById(R.id.button1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
if (isExpanded) {
isExpanded = false;
MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}else {
isExpanded = true;
MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}
}
});
}
}
can anyone tells me why?
Replace this
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
By
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Also make sure you have the below import statement
import android.view.View.OnClickListener;
Change it to
View.OnClickListener()
Write this:
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rather than:
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
Try this:
btnToggleMenuList.setonclickListener(new onclickListener() {
#Override
public void onclick(View v) {
In android app coded for common method for all buttons click event, here is the code,
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.filter);
btnOne = (Button)findViewById(R.id.btnone);
btnTwo = (Button)findViewById(R.id.btntwo);
btnThree = (Button)findViewById(R.id.btnthree);
btnFour = (Button)findViewById(R.id.btnfour);
btnFive = (Button)findViewById(R.id.btnfive);
btnSix = (Button)findViewById(R.id.btnsix);
btnSeven = (Button)findViewById(R.id.btnseven);
btnEight = (Button)findViewById(R.id.btneight);
btnNine = (Button)findViewById(R.id.btnnine);
btnTen = (Button)findViewById(R.id.btnten);
OnClickListener listener = new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
doAction(v);
}
};
}
public void doAction(View v)
{
Object tagObject = v.getTag();
int tag = (Integer) v.getTag();
String val = (String) d.get(tag);
if(val.equals("off"))
{
//select(tagObject);
//d.put(tag, "on");
Toast.makeText(getBaseContext(), "Button"+tag+"select", Toast.LENGTH_LONG).show();
}
else if(val.equals("on"))
{
//unSelect(tagObject);
//d.put(tag, "off");
Toast.makeText(getBaseContext(), "Button"+tag+"unselect", Toast.LENGTH_LONG).show();
}
}
This code is not working for me. Please give any idea....... Thanks in advance
How about declaring your listener first and then calling setOnClickListener on your views :
OnClickListener listener = new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
doAction(v);
}
};
btnOne.setOnClickListener(listener);
btnTwo.setOnClickListener(listener);
...
You have declared the listener but you forgot to set the listener for each button.
Do this for all buttons : btnOne.setOnClickListener(listener);
You need to set the listener in the button..
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.filter);
...
OnClickListener listener = new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
doAction(v);
}
};
btnOne.setOnClickListener();
btnTwo = (Button)findViewById(R.id.btntwo);
...
}
Have your class implement View.OnClickListener, like
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
...
}
}