EditText margin lost on changing background color - android

I've created 5 Edit-text in a Linear layout with horizontal orientation and they have no padding or margins set. When I change the background color of these Edit-text's the borders are lost. Kindly help how to make sure my edit text do not loose their borders.Following is the code snippet.
Saving the padding and reassigning it back hasn't helped!
private void setToNonEditMode(EditText textBox, boolean nonEditMode){
if(nonEditMode){
textBox.setClickable(false);
textBox.setBackgroundColor(getResources().getColor(R.color.transparent));
textBox.setFocusable(false);
textBox.setFocusableInTouchMode(false);
}else{
textBox.setClickable(true);
textBox.setBackgroundColor(getResources().getColor(R.color.white));
textBox.setFocusable(true);
textBox.setFocusableInTouchMode(true);
}
}

Have you tried defining an edit text as a shape and then assign it?
I see you create your EditText programatically, but here is something you could try:
Create a drawable as an xml in your res/drawable folder:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#000000"/>
</shape>
Use this:
textBox.setBackgroundResource(R.drawable.name_of_your_xml_shape_file);

Related

How to pass color to custom xml item in Android?

I have a custom shape for button's dashed border. With hardcoded color everything works as expected, but I need to pass color from outside. How can I do it?
Here is my xml.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid/>
<corners android:radius="16dip" />
<stroke
android:width="1dp"
android:color="#color/blue"
android:dashWidth="3dp"
android:dashGap="3dp"
/>
</shape>
</item>
</selector>
and this is usage
android:background="#drawable/dashed_border_button"
I need to change border color from hardcoded to dynamic
I achieved the same by using another Drawable resource file wit the attribute Stroke - Color = "your color"
and then setting the background Drawable to new Drawable file
yourview.setBackgroundResource(R.drawable.another);
This is because the method :
DrawableCompat.setTint(as.getBackground(),Color.BLUE);
Set even the solid fill color to the blue (here in this case) , which you don't want.
Hope it helps!!`
You need to get background() from your view and make
DrawableCompat.setTint(color, DrawableCompat.wrap(view.bacground())

Changing color of a portion in picture

I am doing such type of project, in my project, i want to change the color of only a portion of my picture, for example when I click on a button I want the blue become green, i want to know if there is any way to do that or it's impossible
To achieve the above feature, you could define a shape and place it above the button and then change the color onClick. A simple example is:
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp">
</corners>
<solid
android:color="#4848ea">
</solid>
</shape>
Now change the color of the shape using the following code.
GradientDrawable Shape = (GradientDrawable)shape.getBackground();
Shape.setColor(Color.GREEN);
Place the above code within the button onClick.

Textview Background does not work

I’m one of Chinese Engineer and My English Speaking is not so good.
There is a ListView having some(three or two) GridView Items, and each GridView have some TextView Items.
Now I want to Set the this TextView Item's border-color like the picture refer screenchot then I made some drawable like this code-pic
I set the Item's backgroud property with this drawable. When I was running the app, all of things were working well, but when I click those items found it was not working. There was no border and color change.
How to achive this?
try this create drawble file like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/colorPrimary" />
<corners android:radius="10dp" />
</shape>
</item>
apply to your textview
Make use of android:state_pressed in your dataset_setting_button_selector.xml instead of android:state_activated
https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

setting a Drawable background programmatically for a programmatically created button

I'm trying to set a drawable background to a button which is created programmatically.
Below is the code where I create the button and set the background
Button increaseQuantity = new Button(this);
increaseQuantity.setText("+");
//increaseQuantity.setBackgroundResource(R.drawable.quantity_button);
increaseQuantity.setBackgroundDrawable(getResources().getDrawable(R.drawable.quantity_button));
Below is the xml which has the code for the drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<stroke android:width="1dp" android:color="#ee404040" />
<size android:width="2dp" android:height="2dp"/>
<gradient android:startColor="#11809100" android:centerColor="#11910000" android:endColor="#55FFB900" android:angle="45" />
</shape>
As per the xml my button should be in round shape (because width and height are the same), but I'm not able to get a round shaped button, instead I'm getting an oval shaped button. Could anyone correct this code to get a circular button?
just two things here:
Replace the deprecated methods: setBackgroundDrawable and getDrawable are deprecated.
Set the dimensions programmatically
This should do the work:
increaseQuantity.setBackground(ContextCompat.getDrawable(this, R.drawable.round));
increaseQuantity.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
//replace 100 with your desired diameter of the button.
Also, you will have to replace LinearLayout to the parent layout you're associating your button with.

how to set the color of the TextView structure?

is there any possible way to set the border around the TextView such like the pics shown below?
the first pic is what i currently got and the second one is what kind of border i want.
all element that contain in the LinearLayout are just indiviual textViews
thx so much for the help~~
You can do that by setting your TextView background a xml shape that has background and border like this:
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#191919" />
<stroke android:width="1dp" android:color="#191919"/>
</shape>
You can red more about this here
I think you can do that please check the following link: Android - Way to appear bordered text on the TextView? this might help to solve u r problem.

Categories

Resources