I need to use text view as CheckBox background and set different color for each state. Some how I managed to get it with Java. But I don't think it is the
proper way. Is there any other method to achieve this?
Implement onClickListener to your textview, something like:
int ispressed = 0;
TextView txtview = (TextView) findViewbyId(R.id.textview1);
txtview.setOnClickListener(new new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ispressed=0){
//CHANGE BACKGROUND COLOR
ispressed=1;
}else{
//RESTORE BACKGROUND COLOR
}
}
});
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 this FlowLayout where I have a set of TextView's which I build programatically. After getting the wanted names, I create a TextView for each name inside the layout.
What I want to do, if I click on the TextView, I want to move it into another layout. I manage to do that but I also want to move it back. I could also do that until I program it to, but I can't program it to be like a infinite loop.
This is a piece of code which will make you understand better what I'm talking about hopefully.
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tvSelected = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tvSelected.setText(tv.getText().toString());
tvSelected.setLayoutParams(params);
tv.setVisibility(View.GONE);
filteredLayout.addView(tvSelected);
}
});
unfilteredLayout.addView(tv);
Is it possible to make it work? Thanks.
LE: As you can see in the onClickListener event of the TextView, I create the other TextView I add in the other layout, to move it back I could also add an onClickListener event to this TextView but this is not the solution.
Try following:
boolean isInFilterLayout = false; //Class variable
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isInFilterLayout){
filteredLayout.remove(tv);
unfilteredLayout.addView(tv);
isInFilterLayout = false;
}else{
unfilteredLayout.remove(tv);
filteredLayout.addView(tv);
isInFilterLayout = true;
}
}
});
unfilteredLayout.addView(tv);
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.
I have a button that begins life "Unticked" with text going to a Label that says "NO". When you push the button it changes the image to "Ticked" and displays text in a Label as "YES". This all works perfectly. What I can't do or find is how to change it back to "Unticked" and "NO" if I then push it again?
Here is the code for the button:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
button9.setBackgroundResource(R.drawable.androidnearmisson);
TextView text = (TextView) findViewById(R.id.textView2);
text.setText("YES");
}
};
Any help will be greatly appreciated.
Many Thanks
You can get TextView's current text and make a comparison. If its YES, change to NO, else vice verse:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
TextView text = (TextView) findViewById(R.id.textView2);
if(text.getText().toString().equals("NO")){
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else {
button9.setBackgroundResource(R.drawable.otherimage); //Replace otherimage with proper drawable id
text.setText("NO");
}
}
};
Hope this helps.
First move this code to your onCreate :
TextView text = (TextView) findViewById(R.id.textView2);
Inside your onClick :
if(text.getText().toString().equals("NO"))
{
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else
{
//change what you want
}