i want to create a counter with a text view - java

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");
}
}
});
}
}

Related

Media Player android

I'm struggling on designing a little audio player.
Just a list of 4 songs - each has it's own textView and when a user clicks on a textView a song starts playing but i have no idea how to make this. I though about making a onClick method for each textView but really dont like this idea.
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
/* MediaPlayer hell = MediaPlayer.create(this, R.raw.Hell);
MediaPlayer bells = MediaPlayer.create(this, R.raw.HellsBells);
MediaPlayer shoot = MediaPlayer.create(this, R.raw.Shoot);
MediaPlayer thunder = MediaPlayer.create(this, R.raw.Thunderstruck);*/
public void playMusic (View view) {
varMediaPlayer = MediaPlayer.create(this, R.raw.Hell);
varMediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the following variables:
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
will make your app crash as your assigning the variables when the MainActivity object is created.
move those to onCreate(Bundle) and assign a click listener to each of those TextViews
private static final int RAW_MUSIC_1 = R.raw.Hell;
private static final int RAW_MUSIC_2 = R.raw.HellsBells;
private static final int RAW_MUSIC_3 = R.raw.Shoot;
private static final int RAW_MUSIC_4 = R.raw.Thunderstruck;
private MediaPlayer varMediaPlayer;
private TextView song1, song2, song3, song4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assign after setContentView(...) as findViewById scan the content view layout
song1 = (TextView) findViewById(R.id.song1);
song2 = (TextView) findViewById(R.id.song2);
song3 = (TextView) findViewById(R.id.song3);
song4 = (TextView) findViewById(R.id.song4);
// add click listeners
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_1);
}
};
song2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_2);
}
};
song3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_3);
}
};
song4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_4);
}
};
}
and change playMusic(View) method to playMusic(int) and add release and reset methods to release object from memory
private void playMusic(final int musicId) {
try {
if (mediaPlayer.isPlaying()) mediaPlayer.reset();
} catch (final Exception e) {}
try {
mediaPlayer.release();
} catch (final Exception e) {}
mediaPlayer = MediaPlayer.create(this, id);
mediaPlayer.start();
}
Replace your code with this one and try running this...
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
MediaPlayer mediaPlayer = new MediaPlayer();
public void playMusic (Integer id) {
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer = MediaPlayer.create(this , id);
mediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.hell);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.HellsBells);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Shoot);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Thunderstruck);
}
});
}
}

Need to sum up the total amount and create order summary in another intent/activity

XML LAYOUT SCREENSHOT
Trying to calculate the final amount of selected items in an another activity. Need to create order summary in another activity.
Tried introducing multiplication function but also need to hard code the quantity values to auto calculate the final amount.
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int q = Integer.valueOf(pantqty.getText().toString());
float r = Integer.valueOf(pantrate.getText().toString());
float amount = q * r;
result.setText(Float.toString(amount));
}
});
}
}
Need to create a method in OnCLickListener to calculate the amount from the items selected.can if else case be used on OnclickListerButton?
Thanks for the help.
You can do something like this:
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
EditText shirtQty;
EditText shirtRate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
shirtQty = (EditText) findViewById(R.id.shirtqtyId);
shirtRate = (EditText) findViewById(R.id.shirtRateId);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculateSum();
}
});
}
private void calculateSum(){
int q=0;
String pantQty=pantqty.getText().toString().trim();
if(!pantQty.isEmpty() && pantQty!="") {
q=Integer.valueOf(pantQty);
}
float r = Integer.valueOf(pantrate.getText().toString());
int a = Integer.valueOf(shirtQty.getText().toString());
float b = Integer.valueOf(shirtRate.getText().toString());
float amount = (q * r)+(a*b);
result.setText(Float.toString(amount));
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("total",amount);
startActivity(intent);
}
}
On the secondActivity you get get the total as:
float total=getIntent().getFloatExtra("total",0.0);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.lo_pantqty);
shirtqty = (EditText) findViewById(R.id.lo_shirtqty);
shareesqty = (EditText) findViewById(R.id.lo_shareesqty);
suitqty = (EditText) findViewById(R.id.lo_suitqty);
result = (TextView) findViewById(R.id.et_result);
SUBMIT = (Button) findViewById(R.id.io_enter);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculationSum();
}
});
}
private void calculationSum() {
int q = 0;
String pantQTY = pantqty.getText().toString().trim();
if (!pantQTY.isEmpty()&&pantQTY!=""){
q = Integer.valueOf(pantQTY);
}
int r = 0;
String shirtQTY = shirtqty.getText().toString().trim();
if (!shirtQTY.isEmpty()&&shirtQTY!=""){
r = Integer.valueOf(shirtQTY);
}
int a = 0;
String suitQTY = suitqty.getText().toString().trim();
if (!suitQTY.isEmpty()&&suitQTY!=""){
a = Integer.valueOf(shirtQTY);
}
int b = 0;
String shareesQTY = shareesqty.getText().toString().trim();
if (!shareesQTY.isEmpty()&&shareesQTY!=""){
b = Integer.valueOf(shirtQTY);
}
int amount = (q*30) + (r*35) + (a*220) + (b*60);
result.setText(Integer.toString(amount));
}
}

every button click to change text view

i coded this lines to change textview1 Continuous when user clicked button but it sucks at value 1 in output why :\
public class dobeyti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Becuase you defined the num value inside the onclick method so it will be reassigned to 0 every time.
Just put int num=0 outside the method like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
int num = 0;
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Move int num = 0; outside OnClickListener. Every time you click button value on variable num is set to 0 -> increased to 1 and set as text, that is why it stuck at 1.
int num = 0;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}

Error setonclickListener undefined for the type button

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) {

Disabling edittext by using a checkbox error

I have a problem here. I want to disable a edittext by using a checkbox. If checkbox is checked, edittext isa available, if not, it is disabled. But I'm having problems. Here is my code: Can anyone check on this. Accdng to eclipse, no error. But when I run it on my phone, the enabling/disabling is not functioning.
public class Order extends Activity {
private Button button1;
private EditText txtbox1,txtbox2;
private TextView tv;
CheckBox check1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
}
public void addListenerOncheck1() {
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
txtbox1.setFocusable(true);
txtbox1.setFocusableInTouchMode(true);
} else {
txtbox1.setFocusable(false);
txtbox1.setFocusableInTouchMode(false);
}
}
});
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
}
}
you are nowhere adding your Listener by calling ur method addListenerOncheck1();
so put addListenerOncheck1(); add the end of you OnCreate method.
But I highly recommend you not to use all these additional self-made Classes for just adding listeners. Use the following code instead and add it into your OnCreate Method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
txtbox1.setEnabled(false);
} else {
txtbox1.setEnabled(true);
}
}
});
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
});
// This will make sure the user can only type numbers into the EditTexts:
txtbox1.setInputType(InputType.TYPE_CLASS_NUMBER);
txtbox2.setInputType(InputType.TYPE_CLASS_NUMBER);
// Code to disable editTexts at start once:
txtbox1.setEnabled(false);
txtbox2.setEnabled(false);
}

Categories