I would like to set the alpha of this:
<ImageView
android:id="#+id/DashboardView_upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:src="#drawable/ab_logo"
android:paddingLeft="#dimen/layoutPaddingExtra"
android:paddingTop="#dimen/layoutPaddingExtra"
android:paddingRight="#dimen/layoutPaddingExtra"
android:paddingBottom="#dimen/layoutPaddingLess"
android:contentDescription="Up"
android:layout_gravity="center_horizontal" />
I have correctly got the resource in code and called it _upButton. This:
_upBtn.setImageAlpha(20);
Gives this:
android.widget.imageview does not contain a definition for setImageAlpha
Why?
The ImageView needs to be casted to an ImageView to allow the imageview to access the functions within the ImageView.
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
img.setImageAlpha(20);
If you are using an API below API-16, then please use:
img.setAlpha(20);
instead of:
img.setImageAlpha(20);
Good luck!
Related
I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:
My TextView at the beginning:
When I click a button:
If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
When you want to change the color behind the TextView then change the ImageView background as below...
ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);
If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.
Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).
You should use ImageView instead of TextView, the:
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}
ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this
android:drawableTop="#drawable/any_drawable"
android:text="#string/any_text"
Allow me to explain. I have :
a button with picture (located at #drawable/pic),
linear layout (id=linear1)
the button xml is below :
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:src="#drawable/pic"
android:maxWidth="80dp"
android:maxHeight="80dp"
tools:ignore="ContentDescription" />
the linear layout xml is as follow :
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:orientation="horizontal"
tools:ignore="UselessLeaf" >
what i want is, when i click the button i want to create/generate imageview programatically inside the linearlayout and i want to fill it with the same picture for the button (pic). The code is below :
//initiate imageview
ImageView img=new ImageView(this);
//get drawable from button
Drawable blabla=btn1.getDrawable();
//set drawable to imageview
img.setImageDrawable(blabla);
//set height and width of imageview to 50dp
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(50,50);
img.setLayoutParams(parms);
img.setScaleType(ScaleType.FIT_XY);
//place imageview to linearlayout
layoutTempat.addView(img);
The code works fine with displaying the imageview with the same image as the button have.
however the problem is : when i set the imageview to 50dp programatically, the image inside button changed too.. how can it happened ? i am so confused as iam a newbie..
Thanks before.
The two views are sharing the same drawable.
It's plausible your manipulations on one view are being sent to the underlying drawable, effecting how its displayed in the other view -- frankly I don't know. But assuming the case is as you described, this problem is easily fixed by cloning the drawable as follows:
Drawable dr = btn1.getDrawable().getConstantState().newDrawable();
img.setImageDrawable(dr);
HI am trying to get an image view using the approach
int mId = getResources().getIdentifier(s, "id",getPackageName());
ImageView img = (ImageView) findViewById( mId);
img.setImageResource(kBonusImage);
but I am getting 0 in mId.
Here is my xml layout:-
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="35dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:id="#+id/trFirstRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imgFirst"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#drawable/cell_border"
android:gravity="center"
/>....
The problem is that you are trying to load an ImageView with an id with the name"id". However, the XML layout file does not have an ImageView with android:id=#+id/id". This means that findViewById() cannot find the ImageView you are requesting. The specified id must exactly match one from your XML view file.
Using getResources().getIdentifier() is completely unnecessary. The typical way to get a View object in Java code is to use the R.id class which is generated from your XML layout files when you build your app. In this case, you have defined in your XML layout file a ImageView resource with id imgFirst. So you can call findViewById() like this:
ImageView img = (ImageView) findViewById(R.id.imgFirst);
This assumes that you have previously called setContentView() to load the XML layout file which contains this ImageView resource.
How can i make a ImageView button change the background image?
Like you hit the imageview button and then the background changes to what i set it to
You've got lots of methods for it
ImageView i;
i.setImageBitmap(bm);
i.setImageDrawable(drawable);
i.setImageResource(resId);
All you need to do is to check documentation and you'll find an answer. No need to post such questions on StackOverFlow. Good luck!
<ImageView
android:id="#+id/pic1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/before_pic" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here"
android:onClick="switchPic" />
/**
* Called when button is clicked.
*/
public void switchPic(View view) {
ImageView after = (ImageView) findViewById(R.id.pic1);
after.setImageResource(R.drawable.after_pic);
}
In XML, assuming the ImageView tag is in your parent view so it would fill the background, set it to your first image, "before_pic". When the Button is clicked it calls the switchPic function.
In the switchPic function, first we create an ImageView object called 'after' and pass it the same ImageView "pic1". Then re-inflate it with the new image "after_pic" with this line of code: after.setImageResource(R.drawable.after_pic);
button.setBackgroundDrawable(drawable);
button.setBackgroundResource(R.drawable.YOUR_DRAWABLE);
These are your options for a button.
I have a code made by Fedor, it can be found "here".
The first image is what I have now,
and the second image is what I want to accomplish.
Can someone guide me with this. I have been struggling for days trying to solve this problem.
Please help me, Thanks in advance!
Here is an example of something similar. You have to create a custom adapter for your ListView.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
You can probably use most of that example. Just change the row.xml to create tha layout you want and the getView() in the adapter.
You just need to modify the list item layout (in item.xml) to have another ImageView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="#+id/image1" android:layout_width="50dip"
android:layout_height="50dip" android:src="#drawable/stub"
android:scaleType="centerCrop" />
<ImageView android:id="#+id/image2" android:layout_width="50dip"
android:layout_height="50dip" android:src="#drawable/stub"
android:scaleType="centerCrop" />
<TextView android:id="#+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_gravity="left|center_vertical" android:textSize="20dip"
android:layout_marginLeft="10dip" />
</LinearLayout>
and modify LazyAdapter's getView() method to add support for the second ImageView.
A tutorial for creating a Custom ListView can be found here:
http://justcallmebrian.com/?p=139
Just need to change the XML layout for each item to have 2 ImageViews like Robby said. Then in your getView of your Adapter (LazyAdapter if you followed along with the other people's answers) you should have something like this:
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setResource(R.drawable.icon1);
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setResource(R.drawable.icon2);
TextView text = (TextView)findViewById(R.id.text);
text.setText("I have 2 images");
The tutorial I pasted earlier depicts a way to make the generation of the list dynamic (i.e. not having the resource of R.drawable.icon1/2 and not having the text for your Text images). Something like this may work (assuming you have a Model class that will hold all 3 pieces of information):
int resid1 = context.getResources().getIdentifier("com.domain.sub:drawable/" + myList.get(position).getImage1Name, null, null);
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setResource(resid1);
int resid2 = context.getResources().getIdentifier("com.domain.sub:drawable/" + myList.get(position).getImage2Name, null, null);
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setResource(resid2);
TextView text = (TextView)findViewById(R.id.text);
text.setText(myList.get(position).getText());
Of course the snippet above assumes you have an ArrayList called myList that also getters to get the image names and the text to display.