using two buttons out of twelve at once - android

I have 12 buttons in my activity..i want to use them in the following way:
Two buttons should be allowed to click at once and when those two are clicked then some action to be performed..if this action is successful, these two buttons must be "invisible" and if this action is unsuccessful, again there must be option to click any of the two buttons out of all twelve..
i have set the layout of this activity and all the twelve buttons as well.I have also set the onClick method for all of the buttons.
[ADDITION]
i mean only two out of twelve buttons be allowed to press at once..any two of them..and after that the output of both the buttons be compared..if they are equal then the buttons be invisible else they are still there and once again the user gets a chance to click two buttons..
[CODE]
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
RotateAnimation rotate = new RotateAnimation(0,90);
rotate.setFillAfter(true);
button1.startAnimation(rotate);
Random r = new Random();
int next = r.nextInt(5) + 1;
imgV1.setImageResource(images[next]); //imageView1 is given a random image
AlphaAnimation alpha = new AlphaAnimation(0,1);
alpha.setFillAfter(true);
imgV1.startAnimation(alpha);
arg0.clearAnimation();
}});
imgV1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlphaAnimation alpha = new AlphaAnimation(1,0);
alpha.setFillAfter(true);
imgV1.startAnimation(alpha);
RotateAnimation rotate = new RotateAnimation(90,0);
rotate.setFillAfter(true);
button1.startAnimation(rotate);
arg0.clearAnimation();
}});
button click gives a random image..image click gives the button back..now i want that when two buttons are clicked and if they have the same image, then they both go invisible..else they both turn back to the buttons and user can again click on any of the two buttons..
Each button has an imageView behind it in the layout..

K.. Now I got it. So, there will be 6 images in your Drawable. Here we go..
Make an Integer array of size 12 to store id's of 6 images. say, int[] images={R.drawable.img1,...};
Also Button firstClick;Drawable back; to know the first clicked button.
Now, our onClick will be as,
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(count<2){// means first click
firstClick=(Button)v;
back=v.getBackground();
action=images[0];// button1 pressed so index 0
v.setBackgroundResource(action);
v.setEnabled(false);
count++;
}
else{//second click
count=0;
if(action==images[0]){
v.setBackgroundResource(action);
v.setEnabled(false);
}else{
v.setBackgroundDrawable(back); //roll back to old background
firstClick.setBackgroundDrawable(back);
}
}
}
});

You can use setVisibility() method of view(Button) to set it's visibility on or off.
Button b = ( Button )findViewById( R.id.button1 );
b.setVisibility( b.INVISIBLE );
b.setVisibility( b.VISIBLE );

The logic that I thought is like,
You should have two variables in hand globally.
1 for counting button clicks and 2nd for storing first click action(Based upon your app).
I'm taking int action=0,count=0; as global variables.
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(count<2){// means first click
action=1;// button1 pressed
count++;
}
else{//second click
count=0;
//Here perform your action and based upon it, set visibility. Previous click is available in 'action'
}
}
});
Repeat this for all button clicks. Thats it. I'll prefer your own method to be called for perform actions and set visibility.

Related

how to implement simple like button in listview of each item

I have certain entries in my list view item. There I have a simple "like button" (not facebook like button). You can see the above mentioned SCREENSHOT; for the reference.
The moment I click on like button; i want the like button color to be changed and the like button color should remain same(changed on like) when I'll login again.
Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; that I know very well.
When I again click on the same button(like button), whose color has been changed. It must be changed back to the default color and data must get removed from database.
How can I do this...?
1. How to get value of click button.
2. How to bring back the changed color to default; once the button has been re-clicked.
Plz suggest me...
this is button code
holder.b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
}
clicked = true;
}
});
You need to add a listener to the button and using ValueAnimator you can change the button color and reverse it back when you click again.
Here is a simple and best approach to achieve your scenario. Add the onClick listener for the button in your list item like this.. I have explained each line ..
// set a default background color to the button
placeHolder.likeButton.setBackgroundColor(Color.RED);
placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null; // to hold the button animator
#Override
public void onClick(View v) {
// first time this will be null
if(buttonColorAnim != null){
// reverse the color
buttonColorAnim.reverse();
// reset for next time click
buttonColorAnim = null;
// add your code here to remove from database
}
else {
final Button button = (Button) v;
// create a color value animator
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
// add a update listener for the animator.
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
});
// you can also set a delay before start
//buttonColorAnim.setStartDelay(2000); // 2 seconds
// start the animator..
buttonColorAnim.start();
// add your code here to add to database
}
}
});
This will change the button color on your first click and then revert the color back on the next click. You can also set a delay to change the color.
Note: You have to set the default button color based on your logic.
#Override
public void onClick(View view) {
if(!check)
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup_blue);
check = true;
}
else
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup);
check = false;
}
}
you can use custom adapter for your listview(it has own layout.xml),and you can set your clicklistener in it.
You can change color or what you want. Actually I did have project like you want.I put some link if you can t do it.
Try following:
use setOnClickListener() on the button.
eg.
viewHolder.imgVwFbLike.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// TODO :
// 1. make webservice call to update like status (Assuming a web service call)
// 2. Implement a callback for webservice call, to get the status of request.
if(success)
a) change the colour of like btn. and insert the data in Db.
b) Also maintain a column in db for likestatus(by default set it false).
}
}
);
Assuming you are fetching the data from db when you login, you can check the likestatus and set the color of button accordingly.

How to check if a button is clicked or not?

I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});

TableLayout, Different Button actions depending on data in TableRow

I have the following problem:
I have a TableLayout along with several TableRows, which are dynamically created.
On the right side of every row I create a button, which should call another activity.
Now I want to pass some information with intent.putExtra(). In this case I want so pass the row number, which is also the first information in the row.
Here is a picture of the current state:
This is how I create the buttons during run-time (in a loop):
Button b1 = new Button (this, null, android.R.attr.buttonStyleSmall);
b1.setId(1000+grButtonId);
b1.setText("Request GR");
b1.setLayoutParams(params);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Some code, taken out for clarity
// See next code snippet
}
});
grButtonId++;
tr.addView(b1);
My idea so far is to use the id of the button (of course), and get the line number by the value of grButtonId.
Now comes my problem, let's have a detailed look at my onClickmethod:
#Override
public void onClick(View view) {
// finished is true, as soon as GRRequest has recieved the data
if(!finished & !dataRequested){
new GRRequest().execute(getIntent().getLongExtra("poNr", 0),(long)view.getId());
b1.setText("Show GR");
Log.d("DataList", detailList.toString());
dataRequested=true;
}
else{
if (dataRequested){
b1.setText("Show GR");
}
Intent intent = new Intent(DataTableCreater.this, GRTableCreater.class);
intent.putExtra("lineNr",view.getId());
intent.putExtra("dataList", detailList);
startActivity(intent);
}
}
When I request my data, the button I clicked on gets set to "Show GR" , as intended. The other buttons stay on "Request GR", this is also fine. But now I want these Buttons to Change to "Show GR" when tapped first and on second tap start the activity.
By now, the buttons change to "Show GR" and directly start the activity.
What would be a solution, to make this work?
Create a boolean Array clickedOnce[] = new boolean[grButtonId+1] one field for every Button.
Then have this
public void onClick(View view) {
if(!finished){
new GRRequest().execute(getIntent().getLongExtra("poNr", 0),(long)view.getId());
b1.setText("Show GR");
Log.d("DataList", detailList.toString());
clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]=true; //sets the clickedOnce for this button to true, substring(1,4) is needed to cancle the leading 1 from the id
}
else{
//Checks, if the button was clicked once
if (!clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]){
b1.setText("Show GR");
clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]=true;
}
else{
Intent intent = new Intent(DataTableCreater.this, GRTableCreater.class);
intent.putExtra("lineNr",view.getId());
intent.putExtra("dataList", detailList);
startActivity(intent);
}
}
}

Android: Knowing which button is pressed for buttons that are created programatically

I am trying to do this:
Programatically create 4 buttons on a layout. Then, create an onclick listener for each of the button. Then, based on which button is pressed, will do some logic.
I have created a LinearLayout in my XML file and called it "layout".
So, my codes go something like this:
layout = (LinearLayout)findViewById(R.id.layout);
//Create the array of buttons
Button [] subjectButtons_BT = new Button[4];
for(int i=0; i<4; i++){
subjectButtons_BT[i] = new Button(this);
// Add the button to the layout
layout.addView(subjectButtons_BT[i]);
subjectButtons_BT[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// if it is the first button, do LOGIC1
// if it is the second button, do LOGIC2 etc
}
});
}
So far, I have managed to create the 4 buttons and I can click on the buttons.
However, I do not know how to the logic in the setOnClickListener part.
I wanted to know which button is pressed. So, I tried using v.getId() but it always returns a -1.
Can someone advise if this is the right way to go? And if so, how to do the logic?
Thanks.
You have two options for identify which dynamic button is clicked.
1) Set ID to the button and compare ID
2) Set Tag to the button if you have come more information about button
and want to access it
1) You can set id to the button and in onClick() method you can get id by button.getId() method and you can compare ids and perform action according to click.
2) If you set tag then you have to get tag by calling button.getTag() method and by this way you can pass object with the button
You can set the id of the buttons after you create them, and then check for that id within the OnClickListener
layout = (LinearLayout)findViewById(R.id.layout);
//Create the array of buttons
Button [] subjectButtons_BT = new Button[4];
for(int i=0; i<4; i++){
subjectButtons_BT[i] = new Button(this);
subjectButtons_BT[i].setId(i);
// Add the button to the layout
layout.addView(subjectButtons_BT[i]);
subjectButtons_BT[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
int id = v.getId();
if (id == 0) {
} else if (id == 1) {
}
//etc.
}
});
}

Creating Buttons Dynamically, Could I select one and deselect the other buttons?

I'm creating buttons dynamically ...
for(int i=0; i<colSize;i++){
final Button btn = new Button(this);
btn.setText(SectionName[i]);
btn.setTextSize(10);
btn.setPadding(8, 3,8, 3);
btn.setTextColor(Color.WHITE);
btn.setTypeface(Typeface.SERIF, Typeface.BOLD);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//***Every time that I click my button is selected !:)
btn.setSelected(true);
}
});
}
But how could I deselect the other buttons that were selected, I just want one Button selected! :)
The brutal way (works if you have few buttons) - save your button references and create private method which loops through your buttons and deselects once you don't need
Extend your button class and make it listen for custom event which is generated when one of the buttons is clicked
Look at the RadioGroup implementation
Variation of #1. Instead of creating separate listeners for your buttons create just one and reuse it for all buttons. Extend that listener from OnClickListener and add List field. Each time you assign listener to the button add button reference to that list. Now, when onClick is triggered simply loop through the list and disable "other" buttons
Declare a variable to store the Id of the Clicked Button ::
private int EnabledButton;
set an ID on every button when are created ::
btn.setId(i);
or a tag ::
btn.setTag(i);
then in that Listener get the "EnabledButton", and call a function to deselect the other buttons::
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EnabledButton=btn.getId();
DeselectButtons();
btn.setSelected(true);
}
});
The Function to deselect the other Buttons ::
public void DeselectButtons() {
for(int i=0; i<NumberofButtons;i++){
if (EnabledButton!= i)
this.findViewById(i).setSelected(false);
}
}

Categories

Resources