I am using this code to create buttons and added this buttons in layout. When I click one of this button selected changed successfully. But what should I do here for clicking in button the previous button's selected state will be unselected.
In one word only one button has to be selected.
private void createButtons(String categoryTitle){
final Button myButton = new Button(mActivity);
myButton.setBackgroundResource(R.drawable.btn_default_holo_dark_trans1);
myButton.setText(categoryTitle);
if (mFirstButtonSeleted){ //this is for first run only first button will be selected
myButton.setSelected(true);
mFirstButtonSeleted = false;
}
mButtonsLayout.addView(myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myButton.setSelected(true);
}
});
}
Create a reference to the previously selected button. Then in your on click method you could do something like this :
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(previousButton !=null)
previousButton.setSelected(false);
myButton.setSelected(true);
previousButton = myButton;
}
});
Related
in my android project ,as show in picture, how to event click on every added button.
on click button add new view, on same as new view add same.
this is how i add new views..
if(myLayout.getParent() != null) {
((ViewGroup)myLayout.getParent()).removeView(myLayout);
}
final EditText editTextWayPoints = (EditText) myLayout.findViewById(R.id.add_item_name);
final ImageButton btnone = (ImageButton) myLayout.findViewById(R.id.add_btn);
btnone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(AddRecipeActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
binding.linlay1.addView(myLayout);
now i want button click on every new views.
What I want is when I click the "x" button of the second item, it will remove the second item and then make the "+" of the first item visible. Here is what I try but not work, the "+" button is not showed.
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildAt(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
Try this code,
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildItemId(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
I hope that will help you for solving your problem.
I need to be able to add buttons to a layout using an "add" button. The problem is that I need each button to have an OnClickListener()/onClick method. I was thinking every time the "add" button is pressed then i would add a new button to an array but im not sure add the listener and implement an onClick method for each button I create.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SmartChronometer chrono = (SmartChronometer) findViewById(R.id.chrono);
final Button start = (Button) findViewById(R.id.button2);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (chrono.isRunning())
chrono.pause();
else {
chrono.chronoStart();
}
}
});
}
I need to add chronomoter,button and listeners every time I click an "Add" button.
set all the clicklisteners as you wich!
call findViewById(R.id.btnSecond).setVisibility(View.GONE); on creat, then when clickin the first button
Button btnSecond;
...
public void onClick(View v) {
findViewById(R.id.btnSecond).setVisibility(View.VISIBLE);
if (btnSecond.getVisibility() == View.VISIBLE); {
findViewById(R.id.btnThird).setVisibility(View.VISIBLE);}
}
this way you can put all your information in the java file and all buttons in xml, but they will be hidden until click.
This is one way, other answer my come. Good Luck :)
implements OnClickListener
Button add = (Button) findViewById (R.id.addButton);
add.setOnClickListener (this);
List<Button> buttons = new ArrayList <Button>();
for (int i = 0; i < buttons.size (); i++){
buttons.get (i).setOnClickListener (this);
}
#Override
public void onClick (View v){
for (int i = 0; i < buttons.size (); i++){
if (v.getId () == buttons.get (i).getId ()){
// do stuff you want
}else if (v.getId() == R.id.addButton){
//add button
}
}
}
Hope this will work, didnt test it.
This is the first time I'm ever dabbling in Android development so please bear with me.
My requirement is this:
I have two buttons on screen, A and B. If the user presses both buttons (order doesn't matter), I need another page to be displayed. Pressing either A or B should do nothing.
Is this possible? If so, how would I achieve this?
Thank you.
This is possible if you take a flag. (boolean)
You should set a flag in your button listeners.
public class Mtest extends Activity {
Button b1;
Button b2;
boolean flag_1 = false;
boolean flag_2 = false;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
flag_1 = true;
doSomething();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
flag_2 = true;
doSomething();
}
};
}
public void doSomething(){
if(flag_1 && flag_2)
{
//DO SOMETHING
}
}
}
Create two boolean's like button1isClickedand button2isClicked,then set an onClickListener for each Button. When the the Button is clicked set the value of these two boolean's to true, then simply create an if() statement that will chekc to see if both buttons have been clicked, like this:
if(button1isClicked == true && button2isClicked == true){
//display your new page
}
Can any one tell me? how can i use the same button for two Action?
Like em using one button in my activity that calculate some values and after calculating the when i again press same button then reset the all fields. Like in this Application
http://www.craziness.com/games/play-love-tester/ when i test the love by pressing the button
then i again press the same button then all fields reset.
what should i use in my activity for the above problem?
You can create a global variable which indicates the state of the program and then change this value when needed. In the OnClickListener of your Button you create an if statement which checks this variable and does the needed things for the associated value.
Example:
public class MainActivity extends Activity {
private int state = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (state == 0) {
// State 1
}
else if (state == 1) {
// State 2
}
else {
// Default state
}
}
});
// Rest of your code including state changing part
}
}
You can do it by changing the button text.
If you do not want that someone sees the text change. Test (example "click" and the other state "click " (1 blank at the end) or another solution.
....
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// todo: check for expected instance (Button)
Button btc = (Button)v;
String bText = btc.getText().toString();
if (bText == "open") {
btc.setText("close");
}
else if (bText == "close") {
btc.setText("open");
}
}
[...]
You can do so by using a toggle button.
In your xml file add a toggle button
<ToggleButton
android:id="#+id/tbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textOff="Reset"
android:textOn="Calculation"
android:background="#drawable/icon"/>
Java file
public class Reviews extends Activity implements OnClickListener {
private ToggleButton tbtn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tbtn = (ToggleButton) findViewById(R.id.tbtn);
tbtn.setOnClickListener(this);
public void onClick(View view) {
if (tbtn.isChecked()) {
//calculate the result
}
else {
//Reset your global calculation variable;
}
}
}
}