How to make edittext underline transparent white programmatically - java

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.

Related

How to give curved edges for a popup window and a noticeable shadow to the popup window

I have to give curved edges to a popup window with a noticeable shadow to the popup window is working fine but without the curved edges and a noticeable shadow the background window and the popup window doesn't show a noticeable space in b/w them
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
getWindow().setLayout((int)(width*1.0),(int)(height*.8));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
getWindow().setElevation(40);
}
The answer is very simple, try this:-
First create a custom popup and assign a custom layout to it.
private fun channelPopup(){
// addDummyProfile()
channelDialog = Dialog(context)
channelDialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
val lp = channelDialog!!.getWindow()!!.getAttributes()
val window = channelDialog!!.getWindow()
window!!.setGravity(Gravity.CENTER)
channelDialog!!.getWindow()!!.setAttributes(lp)
channelDialog!!.getWindow()!!.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
channelDialog!!.setCanceledOnTouchOutside(true)
channelDialog!!.setCancelable(true)
channelDialog!!.setContentView(R.layout.exitsing_channel_layout)
val tv_videos_no: TextView = channelDialog!!.findViewById(R.id.tv_videos_no) as TextView
val recycler_channel = channelDialog!!.findViewById<RecyclerView>(R.id.recycler_channel)
recycler_channel!!.layoutManager= LinearLayoutManager(context)
channelAdapter = ChannelAdapter(tv_videos_no)
recycler_channel!!.adapter = channelAdapter
channelDialog!!.show()
}
and then in your layout give the parent layout a custom background,create a new drawable xml and paste this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/white"/>
<corners
android:radius="30dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
and then set this drawable XML as background to the parent layout of custom popup layout XML.
Add this line in custom popup Java file.
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

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"
...
/>

Use drawable multiple times but with different colours

I have a selector for a toggle button checked and unchecked - is there a way that I can use a custom layer list with a shape and use different colours? I am adding them in at runtime but using XML as the design.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/toggle_custom"/>
<item android:state_checked="false" android:drawable="#drawable/toggle_custom_off"/>
</selector>
And my toggle_custom and toggle_custom_off
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mainColourOn">
<shape android:shape="rectangle" />
</item>
<item android:bottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#E6FFFFFF"/>
</shape>
</item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mainColourOff">
<shape android:shape="rectangle"/>
</item>
<item android:bottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#FFF"/>
</shape>
</item>
LayerDrawable layerOn = (LayerDrawable) getResources().getDrawable(R.drawable.toggle_custom, getTheme());
LayerDrawable layerOff = (LayerDrawable) getResources().getDrawable(R.drawable.toggle_custom_off, getTheme());
GradientDrawable toggleOn = (GradientDrawable) layerOn.findDrawableByLayerId(R.id.mainColourOn);
GradientDrawable toggleOff = (GradientDrawable) layerOff.findDrawableByLayerId(R.id.mainColourOff);
int colour = persons.get(i).getColour();
toggleOn.mutate();
toggleOff.mutate();
toggleOn.setColor(colour);
toggleOff.setColor(colour);
So for example have one toggle that is using the colour red and another using blue using the same XML. Thanks
You can reuse the xml for the layer-list Drawable and create the StateListDrawable for each ToggleButton programmatically like this:
void setToggleButtonColor(ToggleButton tButton, int colour)
{
LayerDrawable layerOn = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.toggle_custom);
layerOn.mutate();
LayerDrawable layerOff = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.toggle_custom_off);
layerOff.mutate();
Drawable toggleOn = layerOn.findDrawableByLayerId(R.id.mainColourOn);
Drawable toggleOff = layerOff.findDrawableByLayerId(R.id.mainColourOff);
toggleOn.setColorFilter(colour, PorterDuff.Mode.MULTIPLY);
toggleOff.setColorFilter(colour, PorterDuff.Mode.MULTIPLY);
StateListDrawable tbBackground = new StateListDrawable();
tbBackground.addState(new int[]{android.R.attr.state_checked}, layerOn );
tbBackground.addState(StateSet.WILD_CARD, layerOff);
tButton.setBackgroundDrawable(tbBackground);
}
Let's test it with two ToggleButtons:
ToggleButton toggleButton1 = (ToggleButton) findViewById(R.id.togglebutton1);
toggleButton1.setChecked(true);
ToggleButton toggleButton2 = (ToggleButton) findViewById(R.id.togglebutton2);
toggleButton2.setChecked(true);
setToggleButtonColor(toggleButton1, ContextCompat.getColor(this, R.color.blue));
setToggleButtonColor(toggleButton2, ContextCompat.getColor(this, R.color.magenta));

Selector Drawable programmatically [duplicate]

This question already has answers here:
Android: How to create a StateListDrawable programmatically
(3 answers)
Closed 8 years ago.
I want to create selector drawables programmatically. The shape has to be in the following form:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid android:color="#4aa5d4" />
</shape></item>
<item><shape>
<stroke android:width="1dp" android:color="#4aa5d4" />
</shape></item>
</selector>
Why? Because i want those 2 colors the be changeable. I know i have to create some kind of Drawable for this. I already managed to create my own GradientDrawables like this:
public GradientDrawable getBackgroundGradient() {
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] { BACKGROUND_GRADIENT_TOP_COLOR, BACKGROUND_GRADIENT_BOTTOM_COLOR });
return gd;
}
But now I need a SelectorDrawable.
Ok, here is, what i came up with using the linked topic.
public StateListDrawable getSelectorDrawable(int color) {
StateListDrawable out = new StateListDrawable();
out.addState(new int[] { android.R.attr.state_pressed }, createNormalDrawable(color));
out.addState(StateSet.WILD_CARD, createStrokeDrawable(color));
return out;
}
public GradientDrawable createNormalDrawable(int color) {
GradientDrawable out = new GradientDrawable();
out.setColor(color);
return out;
}
public GradientDrawable createStrokeDrawable(int color) {
GradientDrawable out = new GradientDrawable();
out.setStroke(1, color);
return out;
}
You can just supply a drawable as the resource in your Selector XML. The drawable can be anything (PNG, XML, etc)
Like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/shape_highlighted"
android:state_pressed="true"/>
<item
android:drawable="#drawable/shape_disabled"
android:state_enabled="false"/>
<item
android:drawable="#drawable/shape_normal"/>
</selector>

Change Layout background on press and revert back to original

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.

Categories

Resources