I am trying to put validations in my ImageButton's onClick(). It is like the tile in Boggle game. If I clicked image 1 then the nearest ImageButtons must be the only ImageButton that is clickable and the remaining buttons will be set as unclickable. How can I achieve it? Here's my code declared at onCreate().
public void tileClick() {
if (image1.isPressed()) {
image1.setClickable(false);
image1.setImageResource(R.drawable.changes);
//clickable when image1 is pressed/clicked
image2.setClickable(true);
image5.setClickable(true);
image6.setClickable(true);
//unclickable
image3.setClickable(false);
image4.setClickable(false);
image7.setClickable(false);
image8.setClickable(false);
image9.setClickable(false);
image10.setClickable(false);
image11.setClickable(false);
image12.setClickable(false);
image13.setClickable(false);
image14.setClickable(false);
image15.setClickable(false);
image16.setClickable(false);
}
}
CustomClickListener
//get ImageButton letter
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Log.i(TAG, "arg0.getId()=" + arg0.getId());
if (arg0.getId()==R.drawable.a){
Log.i(TAG,"arg0.getId()="+arg0.getId());
generatedString=generatedString+("a");
text.setText(generatedString);
//change ImageButton's background when clicked
((ImageButton) arg0).setImageResource(R.drawable.changea);
//Set ImageButton clickable = false when already clicked
arg0.setClickable(false);
}
}
};
//all 16 ImageButtons declared like this :
image1.setOnClickListener(myCommoClickListner);
Change your method to accept an ImageButton
public void tileClick(ImageButton clickedBtn) {
// validation logic
}
pass the clicked ImageButton to the function from onClick()
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
...
tileClick((ImageButton) arg0) // should change arg0 to something meaningful (v, view, etc...)
...
}
}
};
Then set the buttons clickable true/false according to the button passed.
You could put the ImageButtons in an Array and iterate over them and set the clickable according to which button was pressed.
Related
I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html
I want to have a favorite button on my app like in Gmail
So, when the user click on the star, the star became yellow, and when the user clicked it again, it turn back to normal
How can i make this happen with my custom image?
i have two images
when its not favorited (heart-grey.png)
and when its favorited (heart-red.png)
You can use visibilty to do this.You have to define the xml to have both images at the same postion(It can be done using Relative layout).
final ImageView play = (ImageView) findViewById(R.id.play);
final ImageView play2 = (ImageView) findViewById(R.id.play2);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable playImage = play.getDrawable();
if (playImage.isVisible()){
play.setVisibility(View.GONE);
play2.setVisibility(View.VISIBLE);
mediaPlayer.start(); }
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable playImage2 = play2.getDrawable();
if (playImage2.isVisible()){
play2.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
If your star is an ImageButton you can do something like this :
starSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add a condition to detect if it is already a favorite or not
if(starSelected.getDrawable() == R.drawable.theimage2) {
starSelected.setImageResource(R.drawable.thenewimage);
}else{
starSelected.setImageResource(R.drawable.thenewimage2);
}
}
});
Hope it helps
In your java code create a boolean flag:
boolean isSelected = false
Set an onClickListener inside your onCreate for the star (ImageView, Button whatever it is). Inside onClick, check for the flag like this:
if (isSelected) {
// change image src to unselected
isSelected = false;
} else {
// change image src to selected
isSelected = true;
}
and also you can save the boolean state with SharedPreferences to make sure you get the correct state every time.
I have an ImageButton and I want that onClick would replace it with another image (flip back and forth) and on a long press, would replace it to another image.
How can I do that?
I don't feel like reading long documentaries for this.
Set onClickListeners for your button then change the drawable. Since you don't have any code, the following is based on a dynamic ImageButton that only outlines how to perform the action you want. I suggest you define your ImageButton in your XML layout first and then use
iBtn = (ImageButton) findViewById(R.id.btnID);
ImageButton iBtn = new ImageButton(this);
iBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img1);
}
});
iBtn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img2);
return true;
}
});
If you're going to learn Android (or any language or platform really), you should really get comfortable reading the documentation provided, as it will give you answers to many basic questions, such as how to use various methods and classes.
That aside, you need to set both an OnClickListener and an OnLongClickListener for your button. Then inside those listeners, you'll need to set the image using the setImageResource() method. That method requires a drawable image, which you should have saved in your drawable folder (if not, put it there!)
You didn't post your existing code, so here's a generic example.
ImageButton button = (ImageButton) findViewById(R.id.img_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setImageResource(R.drawable.pic1);
}
});
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
button.setImageResource(R.drawable.pic2);
return true; // <-- This must be true.
}
});
You could read further about how to use any buttons in the button guide, you'll just be swapping for ImageButton where appropriate.
Add ImageButton to your layout :
<ImageButton
android:id="#+id/img_btn1"
android:src="#drawable/imgc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and then add this code to your Activity Oncreate() method
ImageButton imageButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.img_btn1);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.imga);
}
});
imageButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
imageButton.setImageResource(R.drawable.imgb);
return true;
}
});
}
change imga , imgb, imgc names according to your images taht are placed in drawable folder
I am trying to pass an imagebutton as a reference through an indexed array.
I thought I could set the ID and then pass that ID as such:
MyButton=(ImageButton) findViewById(R.id.imageButton1);
MyButton.setId(0);
Then in my onClick I want to pass index "0":
MyButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (MyButton.isSelected()){
Switch_Ctrl(0, OFF );
}
});
index is passed to method:
boolean Switch_Ctrl(char button_num, byte state){
button_num.setImageResource(R.drawable.switch_off);
button_num.setSelected(false);
}
I get error can't resolve method setImageResource.
So I can't use the id "button_num". Not sure how I can reference the ImageButton?
You can just store a reference to the button and change it onClick. Just make it an instance variable in your Activity or Fragment. You can declare a button seperately and then initialize it onCreate or onActivityCreated (once findViewById has become available). For example
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState){
myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
changeButtonBackground(v.getId());
});
}
}
//if doing this for multiple butons, check which one
//was pressed and change the background
private void changeButtonBackground(int id){
if(id == R.id.myButton){
myButton.setBackground(R.drawable.whatever);
}
}
Also - I wouldn't set the id of my button to anything else like 0. Your button already has an id, it's the one you defined in the XML as android:id="#+id/whateveritis". Unless you are creating view's programatically that do not exist in the XML (e.g. if you were adding TextView's to some parent layout on click of this button) you don't need to define a new id.
In my onCreate, I insert every buttons of my Activity in a ArrayList and I loop on them to bind a clickListener. Only the last element gets the bind. Why is that?
for(Button bouton: tousLesBoutons) {
bouton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
resultat.setText(((Button) v).getText());
}
});
}
i do something similar but in the layout i set the onClick value to the same function for each button and then have a function like below. All 10 buttons hit this function,maybe you can try this approach
public void onButtonClick( View v )
{
Button but = (Button) findViewById( v.getId() );
String input = but.getText().toString();