How to increase the text value on click of button? - android

I want to increase the value of the text on the click of abutton. However, I want to increase the value by certain amount. My initial value is 250 and I want to increase the textvalue by 250 every time i click on button.
I have written logic for it but the value increases by one.
This is the relevant code:
public class SelectCartListViewAdapter extends BaseAdapter{
private Context mcontext;
private static int counter = 250;
private String stringVal;
public SelectCartListViewAdapter(Context c){
mcontext = c;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//... some other code
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("src", "Increasing value...");
counter++;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
});
//...some other code
return myView;
}
}

That's because you have used a incremental operator here,
counter++;
Incremental operator will increase value by one only.
It should be something like this,
counter= counter+actualValue;

Try this
int counter=0, staticCounter=250;
#Override
public void onClick(View v) {
counter= counter+staticCounter;
tv1.setText(String.valueOf(counter));
}

Replace part of your code with the below code fragment.
ImageButton button = (ImageButton)myView.findViewById(R.id.addbutton);
button.setOnClickListener(new OnClickListener() {
//private int _counter;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(mcontext,"Button is clicked",Toast.LENGTH_SHORT).show();
Log.d("src", "Increasing value...");
counter+=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
// int value = (Integer.parseInt((String) tv1.getText()))+250;
// tv1.setText(value);
}
});

Related

Why can't function "if (counter >10)" work?

I'm trying to get an AlertDialog to appear if my counter is above 10.
I have tried using the TextView variable peopleCount in the if statement but it does not work too. I know using TextView will not work but I need to know if there is a workaround.
private TextView peopleCount;
private ImageView plusOne;
private ImageView minusOne;
private ImageView reset;
private int counter;
private View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.ivPlusOne :
plusCounter();
break;
case R.id.ivMinusOne :
minusCounter();
break;
case R.id.ivReset :
initCounter();
break;
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_people);
peopleCount = (TextView)findViewById(R.id.tvPeopleCount);
plusOne = (ImageView)findViewById(R.id.ivPlusOne);
plusOne.setOnClickListener(clickListener);
minusOne = (ImageView)findViewById(R.id.ivMinusOne);
minusOne.setOnClickListener(clickListener);
reset = (ImageView)findViewById(R.id.ivReset);
reset.setOnClickListener(clickListener);
initCounter();
if( counter > 10) {
AlertDialog.Builder peopleAlert = new AlertDialog.Builder(PeopleActivity.this);
peopleAlert.setCancelable(false);
peopleAlert.setTitle("People Count High");
peopleAlert.setMessage("Please check and replenish inventory");
peopleAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogPeople, int which) {
dialogPeople.cancel();
}
});
peopleAlert.show();
}
private void initCounter(){
counter = 0;
peopleCount.setText(counter + "");
}
private void plusCounter(){
counter++;
peopleCount.setText(counter + "");
}
private void minusCounter(){
counter--;
peopleCount.setText(counter + "");
}
I expected the AlertDialog to appear when counter reached 11 but nothing happens.
OnCreate only runs once, You need to move the if statement to a function and call it from your plusCounter() and minusCounter() functions.

Show hidden views of List item on click of button outside the list

I am developing an Android Application. In this app I am making a list of views using code given below. In each item of list there is delete button with visibility "Gone". Now there is another button Edit outside the list, on click of edit button I have to show delete button on each item of list. but using this code delete button shows only in the last item. Please help me to solve the problem. Thanks.
dynamicView();
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// lay.removeAllViews();
addnew.setVisibility(View.INVISIBLE);
btn_red.setVisibility(View.VISIBLE);
edit.setText("Done");
}
});
public void dynamicView() {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < templates.length; i++) {
final View customView = linflater.inflate(R.layout.order_template_item,
null);
btn_red=(ImageView)customView.findViewById(R.id.btn_negative);
btn_drag=(ImageView)customView.findViewById(R.id.button_drag);
btn_delete=(ImageView)customView.findViewById(R.id.button_delete);
final ImageView image = (ImageView)customView.findViewById(R.id.arrow);
final TextView text = (TextView)customView.findViewById(R.id.date);
final TextView sku = (TextView)customView.findViewById(R.id.time);
final TextView price = (TextView)customView.findViewById(R.id.last);
final TextView names =(TextView)customView.findViewById(R.id.name);
image.setId(i);
text.setId(i);
sku.setId(i);
price.setId(i);
names.setId(i);
btn_red.setId(i);
btn_red.setTag(i);
btn_delete.setId(i);
btn_drag.setId(i);
names.setText(templates[i]);
btn_red.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_delete.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(100,0 , 0, 0);
anim.setInterpolator(new BounceInterpolator());
anim.setDuration(1000);
btn_delete.setAnimation(anim);
}
});
btn_delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lay.removeView(customView);
}
});
lay.addView(customView);
}
You're holding reference only to the last one btn_red.
You can do something like this.
List<Button> buttons = new LinkedList<Button> ();
Then in your loop after findViewById on btn_red
buttons.add(btn_red);
And finally in your onClickListener
for (Button button: buttons) {
button.setVisibility(View.VISIBLE);
}
Do somethink like below to make visible all the button added to list items-
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addnew.setVisibility(View.INVISIBLE);
for (int i = 0; i < templates.length; i++) {
int id = btn_red.getId(i);
id.setVisibility(View.VISIBLE);
}
edit.setText("Done");
}
});

Android GridView first button not working

I am having weird problems with Android GridView. I create a 3x4 grid and insert buttons into that grid. I want the background of the button to change when the user clicks that button. And this is working just fine for all buttons except the first one (the one with index 0 - top left). OnClick event listener doesn't fire at all for that button no matter what I do.
Here is the code where I create the view:
public View getView(int position, View convertView, ViewGroup parent) {
Button imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
Log.w("NOVO", "narejena nova celica");
imageView = new Button(mContext);
imageView.setPadding(8, 8, 8, 8);
} else {
Log.w("STARO", "stara celica");
imageView = (Button) convertView;
}
imageView.setEnabled(true);
int visina = parent.getHeight();
int sirina = parent.getWidth();
float dip = mContext.getResources().getDisplayMetrics().density;
float margin = 10*dip;
int view_height = (int)(visina - 3*margin)/4;
int view_width = (int)(sirina - 2*margin)/3;
int view_dim = 0;
if (view_height <= view_width)
view_dim = view_height;
else
view_dim = view_width;
imageView.setLayoutParams(new GridView.LayoutParams(view_dim, view_dim));
imageView.setId(position);
imageView.setOnClickListener(celice.get(position));
/*imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast toast = Toast.makeText(mContext, v.getId() + "bla", Toast.LENGTH_SHORT);
//toast.show();
celice.get(v.getId()).celicaVisible(4000);
}});*/
celice.get(position).id = position;
celice.get(position).setButton(imageView);
return imageView;
}
If I replace
imageView = (Button) convertView;
with
imageView = new Button(mContext);
then the onClick() gets fired but the background still doesn't change. All the other buttons are working as expected.
And here is the custom class "Celica" that takes care of the actual work - changing the background...
public class Celica implements OnClickListener {
public boolean odkrit;
public boolean najden;
public int id;
public Drawable slikca1, slikca2;
public Celica par;
private Timer tim;
public Button but;
public Context con;
static int buttonsVisible = 0;
Celica(Drawable s1, Drawable s2) {
this.slikca1 = s1;
this.slikca2 = s2;
}
void celicaVisible(int millis) {
if (odkrit)
return;
Log.w("TEST", "prizganih " + buttonsVisible);
if (buttonsVisible >= 2)
return;
odkrit = true;
buttonsVisible++;
tim = new Timer();
tim.schedule(new timerDone(), millis);
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca2);
}
});
}
void setButton(Button b) {
but = b;
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca1);
}
});
}
class timerDone extends TimerTask {
#Override
public void run() {
if (!najden) {
odkrit = false;
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca1);
}
});
}
buttonsVisible--;
tim.cancel();
}
}
#Override
public void onClick(View v) {
celicaVisible(4000);
}
}
In Android, ID of any View must be non zero and non negative number. means View ID should be > 0. and there is problem, when you are setting ID to the Button like
imageView.setId(position)
here ID of a button will be zero when position is zero(means first item). may be due to this, First Button's OnClickListener is not getting fired...try setting a ID that is greater than zero to Button and try once...
you can write like
imageView.setId(position+1) to ensure ID > 0
I actually figured it out. Everything works if I use the view that gets provided by the onClick() method instead of saving the actual button at the creation of the Celica object.
So basically adding:
but = (Button) v;
to the onClick() method solved the problem.

Need help to change the textview content on button click

This is my code. In this code I am trying to change the content of four text on a button click on first time and on same button click I am trying to change the content of only three textview. But it's not working.
public class Home_page_Activity extends Activity {
Button next;
int textids[]={R.id.text1,R.id.text2,R.id.text3,R.id.text4};
String Question[]={"1","2","3","4"};
String three[]={"7","27","37"};
TextView t1,t2,t3,t4,t5,t6,t7;
int count=0;
int check=0;
//int button_id[]={R.id.next};
//Button btn[];
int test=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page_);
t1=(TextView)findViewById(R.id.text1);
t2=(TextView)findViewById(R.id.text2);
t3=(TextView)findViewById(R.id.text3);
t4=(TextView)findViewById(R.id.text4);
t5=(TextView)findViewById(R.id.text5);
t6=(TextView)findViewById(R.id.text6);
t7=(TextView)findViewById(R.id.text7);
next=(Button)findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(test==1)
{
String get=(String) t1.getText();
//int inter=Integer.parseInt(get);
Log.i("t1", get);
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
threetext();
}
nextquestion();
}
});
}
public void nextquestion()
{
t1.setText(Question[count]);
t2.setText(Question[count+1]);
t3.setText(Question[count+2]);
t4.setText(Question[count+3]);
Log.i("count", "value" +count);
test++;
}
public void threetext()
{
Log.i("threetext", "working");
t1.setText(three[check]);
t2.setText(three[check+1]);
t3.setText(three[check+2]);
t4.setText("");
}
Could you increment a counter in the onClick and change the views based on the counter value?

Can't figure out casting in onClick() method

I have written an application that is designed to show report. The first screen of the application is a table that shows info (name, path) of the reports.
So I want to make the row of the table clickable in order to take me to the next screen where the detailed info of the report is displayed, and to know what is the corresponding report for the row. So I wrote my own row:
TableReportRow:
public class TableReportRow extends TableRow {
Report report;
public TableReportRow(Context context, Report report) {
super(context);
this.report = report;
}
public Report getReport() {
return report;
}
public void setReport(Report report) {
this.report = report;
}
}
And then I created the table programmatically:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reports_list_activity_layout);
application = (SGRaportManagerAppObj)getApplication();
reportsRepository = application.reportsRepository.getReportsRepository();
TableLayout table = (TableLayout) findViewById(R.id.tableReportsList);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
final String tag = "tag";
for (int i= 0; i < reportsRepository.size(); i++) {
Report tempReport = reportsRepository.get(i);
TableReportRow row = new TableReportRow(this, tempReport);
// ...
row.addView(tvName);
row.addView(tvPath);
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// This is where I need help.
}
});
table.addView(row);
}
Now I need to perform casting for the view in onClick() to get the access to the GetReport() method to get the actual corresponding report for the row, but I can't seem to get it right. Can some one help me with this one, please? Any help would be appreciated.
You can cast it to a TableReportRow. When onClick is invoked, the view the listener is attached to is passed in as the parameter.
This is useful because it allows the same listener to be recycled for multiple buttons and you can identify the action on the view(based on text, contents, id, etc...)
The View parameter in the onClick event is the View that the handler is bound to. So in your case it will be an instance of TableReportRow, hence:
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableReportRow local = (TableReportRow) v;
}
});
Alternatively define your row as final in the outer block:
final TableReportRow row = new TableReportRow(this, tempReport);
...
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// row is in scope here
row.getVirtualChildCount();
}
});
TableReportRow row = new TableReportRow(this, tempReport);
row.setId(i);//set an id for this.
//...
row.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableReportRow myRow=(TableReportRow)v;
int id=myRow.getId();//your id.
myRow.getReport();//yourReport
}
}
});

Categories

Resources