how to remove background resource of Button? - android

How to remove button background resource i refered this, But my need is to remove button background and that should change background as per deviceDefault theme. Means just removing the resource added last time, not assigning new Resource.
Can anybody help to solve this prob ? Thank You

try assigning background:#null in xml file
for programetically try layout.setBackgroundResource(0);

You can do this by changing the background resource. In the XML file, for the button's attributes.
<Button
...
android:background="#null" />
Should do it.

Instead of setting the background to #null you can add the style:
<Button
...
style="#style/Widget.AppCompat.Button.Borderless" />

If you're only doing this a few times, a simple way is to just save the previous background before altering it. You can store it in the tag field of the Button:
//store previous background drawable
myButton.setTag(myButton.getBackground());
// ... alter background, do whatever
//restore background drawable from tag
myButton.setBackground((Drawable)myButton.getTag());

your_view.setBackground(null);
removes the pre-defined background successfully.

Button b;
b=(Button)findViewById(R.id.button1);
b.setBackgroundResource(R.drawable.ic_launcher);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b.setBackgroundResource(android.R.drawable.btn_default);
}
});
this should help you

Related

How to Set BackgroundColor on ImageView on Android?

I tried a couple things and nothing is working... I'm trying to change the BackgroundColor on a ImageView on Android, but nothing happens...
Here is my xml:
<ImageView
android:id="#+id/imageView1"
android:layout_width="350dp"
android:layout_height="550dp"
android:layout_above="#+id/btnInfo"
android:layout_alignLeft="#+id/fundo"
android:layout_alignRight="#+id/btnInfo"
android:layout_alignTop="#+id/fundo"
android:layout_centerHorizontal="true"
android:contentDescription="#string/backgroundMain" />
And the code:
public void onStart()
{
super.onStart();
Log.d("Teste", "In the onStart() event 5");
ImageView backgroundImg = (ImageView) findViewById(R.id.imageView1);
backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));
}
What am I missing?
RGB:255, 255, 255 is the color code for WHITE. Since your parent layout background color is also white you won't see the difference.
Try changing color like
backgroundImg.setBackgroundColor(Color.rgb(100, 100, 50));
Or else change the background color of parent layout.
There's nothing wrong with your code. But I would prefer doing this through xml , This will also solve your problem. Just add this in your ImageView tag.
android:background="#android:color/black"
In theory it should work...but try like this:
backgroundImg.setBackgroundColor(Color.parseColor("#FFFFFF"));
If you want to use a XML file placed in drawable folder, you might need to use:
imageView.setBackgroundResource(R.drawable.drawable);
Using PorterDuff.Mode:
imageView1.setColorFilter(colorCode,android.graphics.PorterDuff.Mode.SRC_IN);
PorterDuff.mode gives a way of composing and overlaying images in Android, see also What does PorterDuff.Mode mean in android graphics.What does it do?

android change image button background

I cannot seem to change the background image of my image button. Heres the code i'm currently trying to use:
ImageButton imgButton = (ImageButton) findViewById(R.id.showSportsButton);
imgButton.setBackgroundResource(R.drawable.tab2_selected);
However this seem to be placing the new image on top of the old image leaving me with 2 images overlapping each other.
Does anyone know why this is??
For solving this question you should implement
imgButton.setImageResource(R.drawable. tab2_selected);
Use this method:
imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon));
in xml file, <Button> write: android:backgroud="#drawable/your_file"
Make sure you are on same activity. If you are changing background of different activity first create the constructor then use object to change button.
Activity obj= new activity();
obj.imgButton.setBackgroundResource(R.drawable.tab2_selected);
and also check that oncreate() method has void return type if you are using only findviewbyid.

Change android softkeyboard sample designs, button and background images

I'm building a custom keyboard by modifying the android softkeyboard sample from the SDK. I want to change the images of the buttons and the backgrounds but I can't figure out where those values are stored. Where are they stored or how can i change the image or simply color?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
The keyboard background color can be changed in input.xml using android:background
You need to edit the file res/xml/qwerty.xml.
You can check the documentation for Keyboard and the Keys. The last one defines what is shown on the keys (does not look like you can change the background though).
You need to edit the file
res/layout/input.xml
A sample input.xml could look like this.
<com.example.keyboard.LatinKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/keyboardbackground"
android:keyBackground="#drawable/samplekeybackground"
android:keyPreviewLayout="#layout/input_key_preview"
android:keyTextcolor="#android:color/black"/>
there are few more attributes that can be found in the KeyboardView documentation.

Change button android:background to different drawable

I am new to Android and Java but have managed to teach myself and find most answers to questions on stackoverflow without needing to ask questions. Until now....
Here goes, I have many colored buttons which, when clicked, change color to a range of different colors.
There are many buttons defined for example as:
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/orange_button"
android:gravity="center"
android:onClick="onClick" />
Could someone please advise me how to change the android:background using code to change the above example to yellow, for example, when the button is clicked.
In the code below clickedButton is the ID of the button for which I need to change the background.
public void onClick(View v) {
int id=v.getId();
String clickedButton = getResources().getResourceEntryName(id);
Change button to Yellow here??
// Temporary code below to check which button was pressed
// and convert its number to an integer to eventually access an array
final TextView tvGameTimer = (TextView) findViewById(R.id.tvGameTimer);
int buttonNumber = Integer.parseInt(clickedButton.substring(6,8));
tvGameTimer.setText("" + buttonNumber);
}
I am using custom button styles to define the button colors:
res/drawable/yellow_button.xml
res/drawable/blue_button.xml
res/drawable/red_button.xml
res/drawable/orange_button.xml
res/drawable/green_button.xml
For now I just need to work out how to change the button from Orange to Yellow. I can then add the logic to change the colors as and when the app requires.
Many thanks for any help.
I am supposing that the onClick method you post is of the same Button whose background you are trying to change. Use this code.
v.setBackgroundResource(R.drawable.yellow_button);
If onClick is not the method of the same button then, use
findViewById(R.id.button17).setBackgroundResource(R.drawable.yellow_button);
Button btn = (Button) findViewById(R.id.button17);
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Try this.

i need the ON/OFF button for enable/disable by clicking the button ,this is possible by using OnClickListner or any method?

how to do the button with ON/Off by clicking it must varies and along with that it shows Green for ON else RED. this is possible without graphics or must need graphics?
any one tried like this plz help me?
ToggleButton does the same. And here is tutorial which will tell how to use this.
You can use ToggleButton
If you with to use Button instead, you can make field boolean m_isOn; in your class, and in OnClickListener check this field and set button color (e.g. with setColorFilter()) and text accordingly.
Edit
Small example, if you really wish to avoid ToggleButton and using drawables:
#Override
public void onClick(View v) {
m_isOn ^= true;
((Button)v).getBackground().setColorFilter(m_isOn ? 0xFF00FF00 : 0xFFFF0000, PorterDuff.Mode.MULTIPLY);
((Button)v).setText(m_isOn ? "ON" : "OFF");
}
use ImageButton instead of button, and selector as image.
this XML file you can put into drawable directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_prev_active" />
<item android:drawable="#drawable/button_prev_no_active" />
In layout do this:
<ImageButton android:id="#+id/btnPrev"
android:scaleType="fitXY"
android:padding="0px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="prevClickHandler"
android:src="#drawable/button_prev_selector"/>

Categories

Resources