I have a textView in which i have set the color as transparent in xml
android:background="#ffffff"
Now i have written the code to change the image of the textView onClick
t1.setBackgroundResource(R.drawable.fslash);
but it does not seem to do anything onClick of the textView.
Please help
You have to implement click method like this
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
// Your Code
} catch (Exception e) {
}
}
});
try this,
t1.setOnClickListener(new OnClickListener(){
private void onClick(View v){
TextView txt=(TextView)v.findViewById(R.id.txtid);
txt.setBackgroundResource(R.drawable.fslash);
}
});
try setting:
android:clickable="true"
or
you can also do in XML:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<android:background="#drawable/yourimg" />
</item>
<item android:background="#000000" /> <!-- default -->
</selector>
If you want to click on the TextView, you have to set the clickable attribute to true. Otherwise it will not listen to any click! You can do that in code or in the xml file:
Code:
t1.setClickable(true);
XML:
android:clickable="true"
Related
How I can change the background color to a View this under a button when I click the button? I've tried a selector does not work because the View not change the color. What is the problem?
This is my code:
XML
...
<View
android:id="#+id/viewPlanos2"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_gravity="center" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#color/transparente"
android:drawableTop="#drawable/buttonimage"
android:gravity="center"
android:paddingTop="50dp" />
<View
android:id="#+id/viewPlanos1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_gravity="center" />
...
JAVA
View linea2 = (View)findViewById(R.id.viewPlanos2);
linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));
linea_verde
<item android:state_pressed="true"><shape>
<gradient android:angle="90" android:endColor="#color/azul" android:startColor="#color/azulOscuro" />
</shape></item>
<item android:state_focused="true"><shape>
<gradient android:angle="90" android:endColor="#color/azul" android:startColor="#color/azulOscuro" />
</shape></item>
<item><shape>
<solid android:color="#color/rojo" />
</shape></item>
EDIT:
I have tried:
public void onClick(View v) {
if(v == botonMetro) {
linea2.setBackgroundResource(R.drawable.linea_verde);
and
linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));
}
}
But the code not work
You are wrongly using drawable use this one one
view.setBackgroundResource(R.drawable.linea_verde)
as you say change the background color to a View this under a button when I click the button
do like this
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// change color of your view here
linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));
}
});
setBackgroundColor() is for colors only, but it seems your using a state list drawable. If you are sure you want to use a different color depending on the Button's state, set the state list drawable using this code:
view.setBackgroundDrawable(R.drawable.linea_verde);
Otherwise, just set the background color using
view.setBackgroundColor(getResources().getColor(R.drawable.yourcolor);
But in this case, use a click listener, and also make sure to actually use a color resource, e.g.:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
You can also do like this.
android:background="#drawable/linea_verde"
If you would like to apply default background of selected item you could use background attribute with android ?attr.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground" />
selectableItemBackground attribute will use proper drawable according to Android version. On pre-lollipop it will use solid background, but on lollipop devices the ripple effect will be used.
Code from: com.android.support/appcompat-v7/23.0.0/res/values/values.xml
<item name="selectableItemBackground">#drawable/abc_item_background_holo_dark</item>
public void onClick(View v) {
int id = v.getId();
if(id == R.id.button) {
linea2.setBackgroundResource(R.drawable.linea_verde);
}
}
I think you were comparing the view the wrong way. I just happened to stumble to this question and since it is not marked as answered, this might help others.
Create two separate files for your shapes and change the background of your view after button click:
private boolean isPressed = false;
public void onClick(View v) {
if(v == botonMetro) {
if(isPressed){
linea2.setBackgroundResource(R.drawable.pressed_shape);
} else{
linea2.setBackgroundResource(R.drawable.un_pressed_shape);
}
isPressed = !isPressed;
}
}
I have a Checkbox that will be the button Follow/Following of my app.
How can I change the text of this button in the XML file without have to create a image for that?
Is this possible?
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/follow_card" android:state_checked="false">
</item>
<item android:drawable="#drawable/following_card" android:state_checked="true"/>
<item android:drawable="#drawable/follow_card"/>
</selector>
Follow_card/Following_card are two diferent XML drawable files defining the shape of the button
I'd do it programmatically. As the other comment says, you'll need to create an onClickListener, this way:
CheckBox your_checkbox = (CheckBox) findViewById(R.id.your_checkbox_id);
your_checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (view.isChecked())
your_checkbox.setText("Hey I'm checked!");
else
your_checkbox.setText("I'm not checked!");
}
});
I want to custom button ,
If user pressed it will show red color and still show red until user pressed other button
how to do this? thanks.
Try this code:
final Button b1 = (Button)findViewById(R.id.btn_1);
final Button b2 = (Button)findViewById(R.id.btn_2);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setBackgroundColor(Color.RED);
b2.setBackgroundColor(Color.WHITE);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setBackgroundColor(Color.RED);
b1.setBackgroundColor(Color.WHITE);
}
});
Use OnClickListeners to change the background of the Button using setBackgroundColor() or setBackgroundDrawable()
You can use a CheckBox for your button and set the background to a state list drawable that tests for the android:state_checked attribute.
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.RED);
}
});
button2.setOnClickListeners(new OnClickListener() {
public void onClick(View v) {
button1.setBackgroundColor(Color.WHITE);
}
});
What you want is a state list. A quick Google found this article: http://blog.androgames.net/40/custom-button-style-and-theme/ that explains them step by step. That way you don't need any code :)
Here's a code that you need to save as an .xml file and place into your drawable folder.
The android:drawable tags point to the drawable resources for each button state. You can shorten this list if you want to.
Then you can use it as a drawable when creating your layout.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/menu_button_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/menu_button_normal" />
<item android:state_pressed="true" android:drawable="#drawable/menu_button_pressed" />
<item android:state_enabled="true" android:drawable="#drawable/menu_button_normal" />
<item android:state_enabled="false" android:drawable="#drawable/menu_button_normal"/>
</selector>
I'm not sure this is even possible. I have this button:
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="45px"
android:drawableTop="#drawable/buttontv"
android:layout_weight="1"
android:background="#null"
android:textColor="#ffffff"
android:textSize="9px"
android:text="TV"/>
And this button has this item xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/tv" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/tv_pressed" />
<item
android:drawable="#drawable/tv" />
</selector>
And in my application I use this code for when clicking my button:
OnClickListener b1Listener = new OnClickListener(){
#Override
public void onClick(View v) { loadUrl("http://example.org/"); v.setPressed(true); }
};
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(b1Listener);
What I would like that when I have pressed the button, the drawableTop sets to one of the items with the #drawable/tv_pressed attribute value - and stays there (as in 'this is the currently active button').
I tried adding v.setPressed(true) in the onClick function (as this was all I could find with Google) but that didn't work.
Can this be done? Or is there a better alternative to what I'm trying to accomplish?
If you need a button that gets pressed and stays active, use a ToggleButton
https://stackoverflow.com/a/9750125/1257369
button.setFocusable(true);
button.setFocusableInTouchMode(true);///add this line
or with styles.xml
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
as mentioned above by the answer, it is ideal to use a togglebutton but if you are using a listview in this scenario, then the togglebutton will block out your listview's setOnItemClickListener. You can fix that issue as well by adding the descendants as blocked in the listview layout. However some people have reported that even then, their listview items won't click after using toggle buttons.
Are you working with dialogs/dialogfragments?
This code might help you if you are, because i had a simmilar problem and this worked for me. :)
1- extend your class with DialogFragment
,Override Onstart() method.
#Override
public void onStart() {
super.onStart();
final AlertDialog D = (AlertDialog) getDialog();
if (D != null) {
Button positive = (Button) D.getButton(Dialog.BUTTON_POSITIVE);
positive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (edittext.equals("")) {
Toast.makeText(getActivity(), "EditText empty don't close",Toast.LENGTH_SHORT).show();
} else {
D.dismiss(); //dissmiss dialog
}
}
});
}}
Is there a way to specify an alternative background image/color for a Button in the XML file that is going to be applied onClick, or do I have to do a Button.setBackground() in the onClickListener?
To change the image by using code:
public void onClick(View v) {
if(v.id == R.id.button_id) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}
Or, using an XML file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="#drawable/login" /> <!-- default -->
</selector>
In OnClick, just add this code:
ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));
In the latest version of the SDK, you would use the setBackgroundResource method.
public void onClick(View v) {
if(v == ButtonName) {
ButtonName.setBackgroundResource(R.drawable.ImageResource);
}
}
public void methodOnClick(View view){
Button.setBackgroundResource(R.drawable.nameImage);
}
i recommend use button inside LinearLayout for adjust to size of Linear.
Try:
public void onclick(View v){
ImageView activity= (ImageView) findViewById(R.id.imageview1);
button1.setImageResource(R.drawable.buttonpressed);}
I used this to change the background for my button
button.setBackground(getResources().getDrawable(R.drawable.primary_button));
"button" is the variable holding my Button, and the image am setting in the background is primary_button