I asked a question before on how I can change the color of the default android drawable star into yellow, rather than white, and I got an answer to do this:
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_star);
drawable.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
Here is my star:
android:onClick = "star"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:background="#android:drawable/btn_star"
/>
But, the star is still white (Image below) Why wasn't the setColorFilter working? The star's edges turn yellow when I click on it... How can I keep the star yellow using the android's provided star drawable?
Change your onclick listener to this
public void star (View object){
object.getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
}
I believe android creates a specific drawable object for each widget that uses a drawable.
If you ask why do get the edge glow yellow, simply it's the default theme for this drawable, if you removed your code it will still glow yellow at edges.
If you want to change it on loading, you can write the code in onCreate() if you are using activity
View object = findViewById(R.id.object_id);
object.getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
You are doing it all wrong.
You have set the background in xml.
Than you are creating a Drawable object in java and apply color filter on it.
Those two are totally different with no connection at all.
Do this :
(I am assuming that you are using a ImageButton in layout xml)
<ImageButton
android:id="#+id/imageButton"
android:onClick="star"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true" />
Now in your activity class's onCreate() method do :
ImageButton btnStar = (ImageButton) findViewById(R.id.imageButton);
Drawable drawable = getResources().getDrawable(android.R.drawable.btn_star);
drawable.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
btnStar.setBackground(drawable);
This will set the modified drawable as your ImageButton's Background.
You can apply same logic for Button, ImageView, etc.
Related
I making a drawing pad for teachers, i want to include geometrical shapes in it, there is a button on click of which a shape is displayed.
What i am doing now is i created an image view already and set its visibility to gone, on button i am making it visible.
<ImageView
android:id="#+id/ivReact"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/rectangle"
android:visibility="gone"
android:layout_centerInParent="true"
/>
i want the previous image to be there, when a button is clicked the same image should be added twice in the layout and if the button clicked again it should be added again and go on.
is it possible to do that?
Use this code inside onclick
ImageView imageview = new ImageView(MainActivity.this);
RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativeLayout);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// Add image path from drawable folder.
imageview.setImageResource(R.drawable.demo_new_image);
imageview.setLayoutParams(params);
relativelayout.addView(imageview);
create a recycler view and put a image view there. Create a arraylist. Onclick add same image to the arraylist and apply adapter.notifyDataSetChanged().
You have to update image programmatically when button is clicked:
imageView.setImageResource(R.drawable.your_image);
EDIT:
If you have ImageViews in LinearLayout, you can simply create new and add to layout:
ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.image_view_layout_name, null);
linearLayout.addView(imageView);
I have a ImageView with the following layout
<ImageView
android:id="#+id/view_row_circular_icon_with_text_icon"
style="#style/ImageView"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_gravity="right|center_vertical"
android:padding="5dp"
android:src="#drawable/ic_broadband_router_white"/>
The android:src property is useful for previewing this component.
And I set the image I want do display with the following
ImageView mIcon = (ImageView) findViewById(R.id.view_row_circular_icon_with_text_icon_bg);
mIcon.setImageDrawable(rowEntry.getIcon());
rowEntry.getIcon() returns a Drawable.
The end result is both the image from android:src property and the image set from mIcon.setImageDrawable() in the ImageView.
How can I programmatically override the image so that only the latter is shown?
As i can see that your setting image of a another view which
R.id.view_row_circular_icon_with_text_icon_bg
Check the proper id and set the image
I have a LinearLayout in which a TextView's visibility and background (GradientDrawable) changes according to a function myFunction():
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="vertical" >
(...Some views...)
<TextView
android:id="#+id/currentPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="XX.X%"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/White" />
</LinearLayout>
The currentPercentage TextView has GONE visibility by default and, according to some events in the fragment that contains it, may have it changed to Visible (or back to GONE) and its background to a different GradientDrawable like so:
myFunction(...){
TextView currentPercentage= (TextView) getView().findViewById(R.id.currentPercentage);
if(condition ... ) {
Color color = // Color based on the condition...
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TL_BR,
new int[] {color,color});
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int dp = Math.round(10f / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
gd.setCornerRadius(dp);
currentChartPercentage.setBackground(gd);
} else if( otherCondition...)
{
// ... similar stuff ...
} else {
// Hide the currentPercentage TextView
currentPercentage.setVisibility(View.GONE);
}
}
This works fine as the TextView's background and visibility change smoothly whenever myFunction() is called and the other elements in the LinearLayout move (with animation) to make room for the TextView.
My issue is that whenever the visibility goes from GONE to VISIBLE, the View fades in nicely (as expected because of android:animateLayoutChanges="true") but right in the end shows up with an annoying black background where the GradientDrawable round corners are (see image).
Right after that, if I touch something else or drag the fragment (it's inside a ViewPager), the black background disappears. If myFunction() is called again, with the View already VISIBLE, then the background color changes as desired with no black background.
Also, if I disable the animation, I don't get the black background too. Thus, the problem seems to be related to the visibility going from GONE to VISIBLE with an animation.
This behavior is obtained on an emulator with Android 4.3 and I haven't tested it on my real device yet.
Does anybody have a clue on why this may be happening?
EDIT:
Just tested this on a real device with ICS without any issue.
EDIT 2:
I once had a problem with an undesirable background showing up in a ListView similar to Background ListView becomes black when scrolling but in this case, setting a cacheColorHint does nothing.
I've tried to put the intern icons of Android (anrdoid.R.drawable.bla) into an ImageButton
and I wanted to change the color of Icon (not the Background!), but it doesn't work like I want to.
Here is what I've tried:
my ImageButton from the Layout:
<ImageButton
android:id="#+id/imageButton1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:src="#android:drawable/ic_lock_silent_mode" />
what I've tried in my Activity:
Drawable myIcon = getResources().getDrawable( android.R.drawable.ic_lock_silent_mode);
ColorFilter filter = new LightingColorFilter( R.color.blue, R.color.blue);
myIcon.setColorFilter(filter);
No matter what Values I've tried for the LightingColorFilter it always gives the same result. The icon inside the Imagebutton gets dark. But thats not what I wnated. I just wanted to apply a color from my colors.xml, it somehow doesnt work out.
Is this even the right direction I'm going? Or is there another opertunity (And I don't mean coloring myself in Photoshop and putting them into the drawables folder)
this worked for me, for example it will paint in dark gray:
ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton11); // image button
imgBtn.setColorFilter(getResources().getColor(android.R.color.darker_gray), Mode.SRC_ATOP);
I am working on a Android Project in which I need to show a Button or ImageView on header of my activity(Screen). Below is my XML layout of my activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/app"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_margin="2px"
android:orientation="vertical"
android:padding="2px" >
<LinearLayout
android:id="#+id/tabBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/BtnSlide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:padding="0px"
android:src="#drawable/button" />
</LinearLayout>
</LinearLayout>
With the above layout, I can see my black button image just below the Proximity title. Is it possible to show my button image to left of Proximity instead of getting shown one line below Proximity?
Or
Is there any way, I can make the image which is left to Proximity title clickable? if I can do that, then I don't need to show Black Button image at the top.
That image is coming from AndroidManifest.xml file. I am not sure how to make that clickable.
With the above layout, I can see my black button image just below the
Proximity title. Is it possible to show my button image to left of
Proximity instead of getting shown one line below Proximity?
Since Proximity is in titlebar you need to create your own custom titlebar to be able to place button to titlebar. Here is example how to achieve it.
Also try to think an usage of ActionBar.
Try this
Button b=new Button(context);
View v = findViewById (android.R.id.title); // Getting the title bar view
v.addView(b); // setting Button
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Toast.makeText(context, "You have clicked on Title", Toast.LENGTH_SHORT).show();
}
});
How ever I'll suggest you to use ActionBar
Note: I haven't tried that. Let me know if it work for you