Change color of resource - java

I have a resource png of a house linked if you want to view it. It's basically just a dark home button.
I have it included and imported as an image asset as a drawable.
I'm attempting to set it as a button, but change it to be a white color programmatically.
Here's how I'm attempting to change the color to be white:
ImageButton txtbtnAccept = new ImageButton(this);
this._surveyHomeButton = txtbtnAccept;
txtbtnAccept.setId(this.generateViewId());
txtbtnAccept.setLayoutParams(new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
layoutParams = (RelativeLayout.LayoutParams) txtbtnAccept.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.height = 200;
layoutParams.width = 200;
txtbtnAccept.setScaleType(ImageView.ScaleType.FIT_CENTER);
txtbtnAccept.setAdjustViewBounds(true);
txtbtnAccept.setLayoutParams(layoutParams);
txtbtnAccept.setBackgroundResource(0);
txtbtnAccept.setVisibility(View.GONE);
Bitmap homeImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_home);
Drawable whiteImg = new BitmapDrawable(getResources(), homeImage);
whiteImg.setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
txtbtnAccept.setImageDrawable(whiteImg);
txtbtnAccept.setImageBitmap(homeImage);
Any idea where I'm going wrong?

What you want is called tinting. Have a look at DrawableCompat.setTint(Drawable, int).

Related

Add click effect Ripple effect on Card View in Android Studio

I have programmatically added Card View. I just want to make it clickable and show animation while it is clicked. Here is my Code
CardView cardView = new CardView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cardView.setLayoutParams(layoutParams);
cardView.setRadius(15);
cardView.setPadding(25, 25, 25, 25);
cardView.setCardBackgroundColor(Color.MAGENTA);
TextView textView = new TextView(this);
textView.setLayoutParams(layoutParams);
textView.setText("Programmatically set");
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
cardView.addView(textView);
LinearLayout linearLayout = findViewById(R.id.linearLayout1);
linearLayout.addView(cardView);
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = this.obtainStyledAttributes(attrs);
int selectableItemBackground = typedArray.getResourceId(0, 0);
typedArray.recycle();
cardView.setForeground(this.getDrawable(selectableItemBackground));
cardView.setClickable(true);
With the Material Components Library just use:
cardView.setRippleColor(ContextCompat.getColorStateList(this,R.color.selector_card));
You can use this:
cardView.setClickable(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
cardView.setBackgroundResource(typedValue.resourceId);
}

Make Layout Non Clickable But Children Clickable

I've been working on a system overlay and I found that moving my imageview around the screen is a hassle using WindowManager.LayoutParams x and y, so I've decided to make a RelativeLayout with the height of the device to hold my imageview. The only problem is I want to make the Layout non clickable so that the activity below it can be clicked, but not the imageview itself as it launches an activity. Is there anyway to do this? Here is my code so far
final RelativeLayout floaterLayout = new RelativeLayout(getApplicationContext());
//this layout makes the relativelayout non clickable so that the activity below it can be clicked but it doesn't allow any children to be clicked either.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.height = 2560;
params.y = 0;
floaterLayout.setBackgroundColor(Color.RED);
floaterLayout.setAlpha(0.5f);
floaterLayout.setLayoutParams(params);
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).addView(floaterLayout, params);
ImageView view = new ImageView(getApplicationContext());
Util.setImageDrawable(view, R.drawable.floater_dots);
floaterLayout.addView(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
#Nicolas you are creating a relative layout that's not added to your parent layout.Simply create a xml with a relativeLayout and an imageView. Set that imageView's onClickListener in java code. Through this the parent layout will be nonclickable and only imageview will be clickable

How to add an ImageView over a Button

I want to add an ImageView within the onClick() of the Button. But the ImageView should be aligned to the centre of the button.
How can I do this pragmatically?
I'm creating a new instance of the ImageView to be added on top of the View that called it and it's position is equivalent of the View that called it.
How can I get the X and Y coordinates?
EDIT:
#Override
public void onClick(final View v) {
if(v.getWidth() > v.getHeight()) size = v.getHeight();
if(v.getWidth() < v.getHeight()) size = v.getWidth();
RelativeLayout.LayoutParams button = new RelativeLayout.LayoutParams(size,size);
buttonc = new ImageView(getApplicationContext());
buttonc.setBackgroundResource(R.drawable.round);
layout.addView(buttonc,button);
I'm using the above code to attempt to pragmatically set the size of the ImageView to that of the button, and it's working.
As for the coordinates, I still haven't figured it out.
To add clarity: I'm attempting to create a rounded ripple effect over the view that calls it.
I suggest a better solution create an image view before hand place it in the xml as you see fit,at first set its visibility to false .
On click switch its visibilty to true, and the image will appear
This solution is easier than what you are searching.
Feel free to ask questions
to add Imageview
#Override
public void onClick(final View v) {
//LinearLayout
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView
ImageView imageView = new ImageView(this);
//image resource
imageView.setImageResource(R.drawable.specs);
//image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
}

Dynamic ImageView Not visible Android (My code attached)

I am trying to dynamically creating imageview but it is not visible my code is:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.MultiQuesRelaLayOut);
final ImageView[] im=new ImageView[10];
for(int img=0; img<2.length; img++)
{
im[img]=new ImageView(this);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin=25;
params.topMargin=(img+1)*(35+add);
im[img].setId(img);
im[img].layout(5,5,5,5);
im[img].setVisibility(View.VISIBLE);
im[img].setBackgroundColor(color.background_dark);
im[img].setPadding(5, 15, 5, 15);
im[img].setLayoutParams(params);
layout.addView(im[img]);
im[img].setOnClickListener(ImageOnClick(im[img]));
}
How to view imageview on form by changnig it s background image or some thing like that ?
Hopes for your suggestion
Thanks
You have created the ImageView with this LayoutParams: wrap_content, wrap_content, but you have not add img to the ImageView, so, the content length is 0 for height and width.

My code receives the error "The specified child already has a parent. You must call removeView() on the child's parent first."

As the title reads, I am getting this error in my LogCat when I run my app. This is the code written after which the error occured:
public void coinAnim1() {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin1 = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
coin1 = (ImageView) findViewById(R.id.coinid);
Animation coinFall1 = AnimationUtils.loadAnimation(this,
R.anim.coinanimation);
coin1.startAnimation(coinFall1);
rl.addView(coin1, params);
}
public void coinAnim2() {
RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin2 = new ImageView(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(30, 40);
params2.leftMargin = 50;
coin2 = (ImageView) findViewById(R.id.coin2id);
Animation coinFall1 = AnimationUtils.loadAnimation(this,
R.anim.coinanimation);
coin2.startAnimation(coinFall1);
rl2.addView(coin2, params2);
}
LogCat says the line "rl.addView(coin1, params);" specifically causes the error.Does anyone know what I can do to fix this? There are similar questions, but as I am new to coding I don't know how to adapt the answers for my problem.
Any help is appreciated.
PROBLEM SOLVED:
Ashishduh solved the problem by replacing the line:
rl.addView(coin1, params);
With the line:
coin1.setLayoutParams(params);
Hope this helps someone! Thanks ashi <3
The View coin1 already has a parent (the RelativeLayout that you inflated). You can't assign it to another parent without removing it from that RelativeLayout.
I think what you're trying to do is modify the LayoutParams of coin1. Instead of doing this: rl.addView(coin1, params); you need to just set the layoutparams like this coin1.setLayoutParams(params);
From ur code snippet
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin1 = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
coin1 = (ImageView) findViewById(R.id.coinid);
u r creating coin1 & coin2 ImageView programmatically as u have already added those in xml either create those ImageViews programmatically or in xml

Categories

Resources