Dynamic Buttons with two functionalities ANDROID - android

I have dynamic buttons that are created by a variable that can change.I want that these buttons have two functions. I did one option but I don't know how to implement the other option.
the first time I click the button I call a function that do something and the second time that I click the same button I would like to do another action. And I want to repeat this running with all the dynamic buttons created.
My code is:
LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
final int index = i;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
buttonsLayout.addView(buttonMalla);
}
}

EDIT - Very important:
I readed your code again, you could use the getTag/setTag to remember the last state of the button (i missed the for part, sorry!)
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
buttonMaila.setTag(Boolean.FALSE);
Then in setOnClickListener
if (((Boolean)v.getTag()) == Boolean.TRUE)
{
// Do first action
v.setTag(Boolean.FALSE);
}
else
{
// Do second action
v.setTag(Boolean.TRUE);
}
An idea could be to use a variable to know which was the last action.
A boolean variable
private boolean action = false;
If action is false do the first thing and set it to true. If it's true do the second action and set it to false.
It should go out any method (global of the class)
Something like
private boolean action;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (action == true)
{
// Do first action
}
else
{
// Do second action
}
action = !action;
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
Anyway if it does something of important you should manage better the button (example button action is not based on a boolean variable but in a specific state.) time ago i builded a "select all" and "unselect all" function in an app and i checked if the user have unselect manually something to let the button act again like a "select".. I hope i gave to you an idea.
Anyway the boolean variable is the most immediate way to do it.

Related

How to check if button has been clicked

I have a bunch of dynamic buttons which I am setting an onClickListeners as they are produced, as well as tagging them with IDs.
Not sure if this a simple one which I have just spent too much time staring at but this is the problem.
If a user clicks a button, it changes colour this is simple and has been achieved by:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter == 0) {
button.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
Toast.makeText(getActivity(), "User Has Been Marked As Present",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = true;
counter++;
} else {
button.setBackgroundColor(Color.parseColor("#FFFFFF"));
Toast.makeText(getActivity(), "User Has Been Marked As Absent",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = false;
counter = 0;
}
}
});
If the user clicks it again, it will change back to the previous colour - but...
If the user clicks one of the other dynamic buttons that hasn't been previously clicked, the counter is thrown out.
I need to know if the button has been clicked and if not, should mark the user as present.
Currently, If on one button I click it and mark the user as present, and then move onto the next button, I will have to click it once (which marks the user as absent due to the counter) then press it again to mark the user as present.
I need the counter to treat each button individually, any ideas how this could be achieved?
Once the user has been marked present,maybe disable the onClick listener for that button since you wouldn't need it anymore?
I don't mean to sound condescending but I'm having trouble understanding what you're trying to achieve, but if each button is supposed to hold different information about a user, why not make a custom button that does just that? Make a class called customButton in your package and paste the following code there:
import android.content.Context;
import android.widget.Button;
public class customButton extends Button {
boolean haveIBeenClicked; //false by default
public customButton(Context context) {
super(context);
}
public void toggleHaveIBeenClicked(){
haveIBeenClicked=!haveIBeenClicked;
updateBackgroundColor();
}
void updateBackgroundColor(){
if (haveIBeenClicked){
this.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
}
else{
this.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
}
then, inside the onClick method (in the activity whose snippet you've shown earlier) you can just call
((customButton)v).toggleHaveIBeenClicked();
...after having created a customButton object and setting an on click listener on it.
Please let me know if this achieves what you desired. If you have trouble running this code, make sure to let me know if the comments and we'll work it out

How to design android layout for this snapshot?

I am a new in android programming, I made a layout with this figure:
Now I want to know when one of these buttons clicked I should run an new activity or change visibility to false and show new layout without run a new activity, what is the best solution?
You consider that count of these buttons are more than ten.
I want show a text with image,..(when clicked) because that is a educational book and these buttons are chapters list of that book
for an example if you want to change only the layout then you could do something like this
FirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
FirstView();
}
});
/
void FirstView(){
setContentView(R.layout.yourOtherLayout);
// then declare the layout views here.
firstView=false;
}
you can do this in all the buttons just create different methods for each
to handle the Back Button you can declare Boolean variables and use If else Statement to loop through them for example
boolean firstView = true, secondView = true;
#Override
public void onBackPressed(){
if (firstView == false ){
then firstView Is Showing.
// show the view you want and set
firstView = true;
}else if (SO ON)...
else { super.OnBackPressed(); // exit }
}

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.

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

Modifiy text in a button - if statement

I am currently creating an android application with different options. One of the option would be to have a button that would show "Activate" as default. When the application would be running, clicking on it would change it to "Disable" and then to "activate" if clicked again. I believe that all I have to do is to .getText with a string variable then use this variable in a if statement but it seems like it is not reacting to any of my conditions...
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText == "#string/Disable") {
button.setText(R.string.Enable);
}
else if (buttonText == "#string/Enable"){
button.setText(R.string.Disable);
}
}
});
Thanks for help
Phyzikk
You shouldn't use the == operator when comparing strings in Java. Source
You should either use the .equals() method of the string, or alternatively you could keep a global boolean state flag to determine which value is set. This way you won't need to do a string compare every time you need to figure out if it's active or disabled.
Use .equals to compare strings. You wont need the #String/ prefix as this is not part of what the button displays.
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText.equals(getResources().getText(R.string.Disable)) {
button.setText(R.string.Enable);
}
else if (buttonText.equals(getResources().getText(R.string.Enable)){
button.setText(R.string.Disable);
}
}
});

Categories

Resources