Change Layout background on press and revert back to original - java

I am trying to change the background color of a layout on button press and but change back to the original drawable on release.
The following code changes the background resource onclick button which changes the background but on release it stays with the new drawable resource:
btnOne = (Button) findViewById(R.id.btnIcon1);
btnOne.setOnClickListener(oneClick);
btnOne.setOnTouchListener(oneC);
View.OnClickListener oneClick = new View.OnClickListener() {
#SuppressLint("NewApi")
public void onClick(View v) {
editor.putString("AndroidInfo", "1");
editor.commit();
Intent myIntent = new Intent(getApplicationContext(), VersionDetail.class);
startActivityForResult(myIntent, 0);
overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
}
};
View.OnTouchListener oneC = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
rlOne.setBackgroundResource(R.drawable.dateborderclick);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
rlOne.setBackgroundResource(R.drawable.dateborder);
return true; // if you want to handle the touch event
}
return false;
}
};
dateborder xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<solid android:color="#CCE5E5E5" />
</shape>
dateborderclick xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<solid android:color="#CCD1FFFE" />
</shape>
dateborder is the default background. When the user interacts with the button I want the dateborder to be dateborderclick on press and back to dateborder on release. Based on the code above it should work but now the pressing works but the click doesn't.

You're making harder on yourself than it needs to be. Android has a State List Drawable that takes care of this for you. Here's what one looks like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
android:dither="true"
android:variablePadding="false">
<item
android:state_selected="true"
android:state_enabled="true"
android:drawable="#drawable/tab_on" />
<item android:drawable="#android:color/transparent" />
</selector>
Basically you define items that are in various states and give them a drawable. The last one if your default state. If you follow the link above you will see all of the different flags you can set to tune it to be exactly what you want. Once you define this drawable just set it as the background of your button.

Related

Android: Programming custom made button background color, while fitting inside button

I'm trying to change the background color of a button I made to a certain color when toggled and back when untoggled, but when I do the color changes the shape of the button.
Here is my code:
public void onClick(View v){
if(index < 9)
GameEngine.getInstance().setNumber(number);
else if(index == 9)
GameEngine.getInstance().setNumber(number);
else if (index == 12) {
GameEngine.getInstance().draftModeSetter();
v.setBackgroundColor(Color.parseColor("#ffb6c1"));
}
}
And here is the result before color change and the result after color change. You can clearly see the "draft" button gets becomes bigger and a sharper square.
If the background color was set using
btn.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
it can be reset using:
btn.getBackground().clearColorFilter();
I think you should use xml : shape, selector like :
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#color/blue_400" />
<stroke
android:width="1dp"
android:color="#color/blue_400" />
</shape>
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_comm_bt_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/shape_comm_bt_normal"
android:state_pressed="false" />
</selector>
use
<Button
...
android:background="#drawable/selector_common_bt"
...
/>

How to make edittext underline transparent white programmatically

View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
editText.getBackground().setColorFilter(ContextCompat.getColor(getActivity().getApplicationContext(),R.color.ed_background), PorterDuff.Mode.SRC_ATOP);
} else {
int color = Color.parseColor("#FFFFFF");
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
};
<color name="ed_background">#33FFFFFF</color>
This makes the edit text underline black, but I want it to be a transparent white. Setting a custom style with xml also sets it to black. How can I make the underline a transparent white with java?
You can try smth like this:
creates drawable xml for ex edittext_lined.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:color="#color/ed_background" android:width="1dp"/>
</shape>
</item>
then in code:
editText.setBackgroundResource(R.drawable.edittext_lined);
Just set the edittext background color to white/transparent in xml layout / in code.

Compare two drawable in a onClickListener

this is my question:
At the first click on button I set the background of view
public void clickOnButton(View v){
if(compareDrawable(getResources().getDrawable(R.drawable.green_button), v.getBackground())) {
v.setBackgroundResource(R.drawable.red_button);
}else{
v.setBackgroundResource(R.drawable.green_button);
}
}
Where the compare function has this code:
public boolean compareDrawable(Drawable d1, Drawable d2){
try{
Bitmap bitmap1 = ((BitmapDrawable)d1).getBitmap();
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
stream1.flush();
byte[] bitmapdata1 = stream1.toByteArray();
stream1.close();
Bitmap bitmap2 = ((BitmapDrawable)d2).getBitmap();
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream2);
stream2.flush();
byte[] bitmapdata2 = stream2.toByteArray();
stream2.close();
return bitmapdata1.equals(bitmapdata2);
}
catch (Exception e) {
// TODO: handle exception
}
return false;
}
I've already try to use some of these comparations:
if(v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.green_button).getConstantState()))
or
if(getResources().getDrawable(R.drawable.green_button).hashCode() == v.getBackground().hashCode())
and the code of xml files is like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#70c656" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
but they doesn't work, they return false every time. How can I compare these object?
Why would you need to compare Drawables just to change the image back and forth?
Instead use CheckBox or other CompoundButton and use selector xml with 4 states:
checked and pressed
checked
pressed
default

How to set random colors and button rounded at the same time?

I'm trying to make buttons rounded, random background color? When I tried the code below, the button isn't rounded.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtestament);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayoutnew);
String[] values = { "Matthai", "Marka", "Luka", "Johan",
"Sawltak Tangthu", "Rom Laikhak", "Korin Masa", "Korin Nihna",
"Galati", "Efesa", "Filippi", "Kolose", "Thesalonika Masa",
"Thesalonika Nihna", "Timoti Masa", "Timoti Nihna", "Titus",
"Filemon", "Hebru", "James", "Peter Masa", "Peter Nihna",
"Johan Masa", "Johan Nihna", "Johan Thumna", "Jude",
"Maangmuhna" };
Button[] b = new Button[values.length];
for (int i = 0; i < b.length; i++) {
b[i] = new Button(this);
}
int[] btnColor = { 0xAAe60038, 0xAA9142d6, 0xAAf07b04, 0xAA1515ff,
0xAA23699e, 0xAA0a71ff, 0xAA3e3d39, 0xAA00b323, 0xAA754e45,
0xAAfa061e, 0xAAe66d2d, 0xAAff00ff };
// calling random
Random random = new Random();
// ramdomizing a color
int c = btnColor[random.nextInt(btnColor.length)];
for (int i = 0; i < values.length; i++) {
// applying button rounded xml style
b[i].setBackgroundDrawable(getResources().getDrawable(
R.drawable.round));
// setting randomized color
b[i].setBackgroundColor(c);
// layout
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
// margin
((MarginLayoutParams) params).setMargins(5, 5, 5, 5);
// padding
b[i].setPadding(10, 10, 10, 10);
// text color
b[i].setTextColor(0xFFFFFFFF);
// text
b[i].setText(values[i]);
// text size
b[i].setTextSize(18);
Typeface face = Typeface.createFromAsset(getBaseContext()
.getAssets(), "UBUNTU-R.TTF");
b[i].setTypeface(face);
layout.addView(b[i], params);
}
}
Here is round.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- you can use any color you want I used here gray color -->
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
I think the problems are setBackgroundDrawable, setBackgroundColor and romdomizing. What additional codes are needed? ??? Update:How to edit round.xml to make the button rounded?
You are setting button background as drawable and then the color also. So statements are executed sequentially. So in the end your button should have a background color set.
So try commenting one of them and run the code again.
b[i].setBackgroundDrawable(getResources().getDrawable( R.drawable.round));
// set background drawable
// first your background drawable will be set
b[i].setBackgroundColor(c);
//set background color.
bkg.xml in drawable folder
<?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/pressed" />
<item android:state_focused="false"
android:drawable="#drawable/normal" />
</selector>
normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp"
android:color="#0FECFF" /><!-- #330000FF #ffffffff -->
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF1A47"/>
<stroke android:width="3dp"
android:color="#0FECFF"/>
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
b[i].setBackgroundResource(R.drawable.bkg);
Snap Shot
Edit:
int colors []= {Color.RED, Color.BLACK, Color.BLUE,Color.GREEN};
Random r = new Random();
for (int i = 0; i < colors.length; i++) {
b[i].setBackgroundColor(colors[r.nextInt(3)]);
}
look at Document . view.setBackgroundDrawable(drawable) and b[i].setBackgroundColor(c); you can't use both at a time. it will take to effect last one only.

Button Shown as pressed on start of Activity in Android

How can i show a button as it is pressed whenever the activity is created and UI is shown to user in Android?
This should be okay:
#Override
public void onStart(){
super.onStart();
Button button = (Button) findViewById(R.id.test_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, "Clicked");
}
});
// This will NOT trigger the onClickListener!
button.setPressed(true);
}
You should use the button selector state like the following code i.e(res/drawable/button.xml)
<?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/button_add_to_favorites_active">
</item>
<item android:state_focused="true" android:drawable="#drawable/button_add_to_favorites_active">
</item>
<item android:drawable="#drawable/button_add_to_favorites_active"> //Here set the drawable looks like pressed one.
</item>
</selector>

Categories

Resources