I have one Linearlayout - totalincome and another TableLayout normalincometable, which should appear just below the totalincome. normalincometable will be invisible when the program runs. When the user clicks on "totalincome" the table should display. If the user clicks on "totalincome again", the table should disappear. I have tried this code, but It didnt work.
totalincome.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int x =0;
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
From this code, I can make the table visible in first click but It doesnt disappear in next click. Are there any options ?
Try this:
#Override
public void onClick(View v) {
if(normalincometable.getVisibility() == View.VISIBLE) {
normalincometable.setVisibility(View.GONE);
} else {
normalincometable.setVisibility(View.VISIBLE);
}
}
You have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
because you have define x in the button click code so whenever button click it set to 0. define x outside the button click scope.
try this way:put x variable outside the button onclick() or defined x globally
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Use like this:
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Try this
Boolean isFirstTimeClicked=true;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isFirstTimeClicked)
{
normalincometable.setVisibility(View.VISIBLE);
}
else
{
normalincometable.setVisibility(View.GONE);
}
isFirstTimeClicked=!isFirstTimeClicked;
});
}
and in your code you have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
Easiest Method is
button.setVisibility(View.VISIBLE == button.getVisibility() ? View.GONE:View.VISIBLE);
Related
how to get the position from list on click of data.
i want access button in android.on the click of button of the adapter class it should give the position.
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
you can set the position as the tag
holder.beginDate.setTag(position); // set every time getting the View
and retrieve it using
// put this code where creating a new instance of holder
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int pos = (Integer) view.getTag();
// do anything using the position
}
});
After initialization of your button in getView() method write:
holder.beginDate.setTag(position);
and in your onClick() use:
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int position=Integer.parseInt(((Button)v).getTag().toString());
}
});
i do for this code but not supported for ths issue and what i do for solution this like as if codition inside any listener.
if (btn.isEnabled()) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
But why???
any button's onClickListener will only be called if its "enabled".
You dont need to bother about onClickListeners which are assigned to disabledButtons.
suppose your button is disabled at the launch of activity then this listener wont be applied to your button.
Now after some time if you enable this button (may be after some event etc.)
"THEN ALSO THIS LISTENER WONT WORK" as you did not set the listener at the first place...
so IMO dont put that in if...
this code successfully.
if (button.isEnabled()) {
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Test", 10).show();
}
});
}
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++;
}
I am new to android development.
I have on one image in layout.I used scale animation for that image.
and i am able to stop the scaling of image at particular point. now i want to resize that image on another clicklistener.
How to do that? if any idea,help.
Here is my code.
final ImageView img_graph= (ImageView)findViewById(R.id.graph01);
final Animation AnimationScale= AnimationUtils.loadAnimation(this,R.anim.anim_scale);
final Animation AnimationScale_reverse= AnimationUtils.loadAnimation(this,R.anim.anim_scale_reverse);
if(flag ==FLAG_SCALE_IN) {
if(resp==0){
img_graph.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale);
}});
}
}
if(flag==FLAG_SCALE_OUT) {
if(resp==1){
img_graph.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale_reverse);
}});
}
}
can't we handle this case using if else in same listner ?
img_graph.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View arg0) {
if(resp==0 && flag ==FLAG_SCALE_IN) {
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale);
}else if( resp==1 &&flag ==FLAG_SCALE_OUT) {
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale_reverse);
}
}
}});
More optimization:
public void imageViewClick(View v)
{
if(flag==FLAG_SCALE_IN && resp==0)
{
}
else if(flag==FLAG_SCALE_OUT && resp==1)
{
}
}
i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.