I have to add a fragment over an Imageview, but my ImageView goes on top and covers the other views. All this is done in a RelativeLayout.
things must be done dynamically (no xml). I want that my activity has an image, which can be set DYNAMICALLY, and my fragment (added dynamically) should have transparent background, and if this is not true, i can set it to transparent at run time.
This is the code to create the image:
ImageView img=new ImageView(this);
img.setImageResource(imgID);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
((ViewGroup)findViewById(android.R.id.content)).getRootView()).addView(img);
As you can see, the image works as background, i'm using a ImageView for its scaleType.
Is there a way to do so?
SOLUTION
solved, i added the image to the wrong container.
The function below, however, has been pretty useful. It has been taken from an other answer, and lets a view to get on the back of all the others.
public static void sendViewToBack(View child) {
final ViewGroup parent = (ViewGroup)child.getParent();
if (parent!=null) {
parent.removeView(child);
parent.addView(child, 0);
}
}
Related
Can I work somehow with layouts from resources folder in java?
I have a layout in the resorces folder which contains a RelativeLayout which contains a LinearLayout, and any TextViews. And I want to create something like a class instance, and change in example the RelativeLayout background and the texts in the TextViews.
Because then I want to convert this to a Bitmap (for which I have already written the function).
And I don't want it to show up. All this in a static function.
It's possible? How do I create a something like variable/class?
Sorry for any spelling or composition errors I am not a native speaker.
fun getLayout(layoutId: Int, context: Context) : ViewGroup {
return LayoutInflater.from(context).inflate(layoutId, null, false) as ViewGroup
}
This is how you would get a Layout from the resources directory programmatically. Using it like so:
val myLayout = getLayout(R.layout.activity_main, context)
Changing the background
myLayout.setBackgroundColor(Color.RED)
Changing some text
myLayout.findViewById<TextView>(R.id.my_text_view).setText("Hello World")
I'm attempting to implement accessibility on a few custom views for an Android app.
I've condensed what is done in the Google Authenticator app with no luck:
public class CardView extends RelativeLayout {
// ...
#Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
event.setClassName(this.getClass().getName());
event.setPackageName(this.getContext().getPackageName());
event.getText().add("Card Test");
return true;
}
}
All TalkBack reports back is "Double-tap to select" when it's inside a ListView or ViewPager.
Does ViewPager override accessibility events?
What do I need to do in order to have TalkBack say "Card Test" inside ViewPagers and ListViews like I expect it to?
For current versions of Android, you need to set the content description of the view.
myView.setContentDescription("Card Test");
ListView and associated classes expect you to use the onItemSelectedListener instead of assigning an onClickListener to each View (and rightfully so).
If incorporating alanv's suggestion, try to convince android system to read out the content description
by either
If(accessibilityModeIsEnabled())//custom method that checks context.getSystemService(Context.ACCESSIBILITY_SERVICE).isEnabled()
myView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
or AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED.
or requestFocus
Above should done when myView is visible. May be during onMesasure when width and height are both positive
If list view is still unable to do so, then try doing the above tricks on the first element of list view. Accessibility in Android varies devices to device and not one strategy fits all
I'm working on an app that allows changing theme. To achieve that, I need to change background in java.
I created imageView and tried to set background as imageView.
I was using this code:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
int ID = getResources().getIdentifier("imagebackground", "drawable", getPackageName());
imgViewBackground.setImageResource(ID);
but the app crashes after 20-30 seconds of usage.
I also tried this, but the app crashes on startup:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.imageViewBackground);
layout.setBackgroundResource(R.drawable.imagebackground);
Is there an effective way to change background directly, and not through imageView in java?
Why not simply do this:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
imgViewBackground.setImageResource(R.drawable.imagebackground);
P.S: An image by the name imagebackground must be present in the drawable folder.
I recently asked about applying an animation to a marker popup window (infowindow) and was explained why that wasn't possible:
Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (e.g., after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
Researching some more, I found a project using V1 that manually creates the View on the marker's position. To do this, the other guy did something like this:
public void showPopup(View view, GeoPoint point, boolean centerPopup) {
removeAllViews();
MapView.LayoutParams lp = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
point,
Utils.dipsToPixels(0.0f, mContext),
Utils.dipsToPixels(-12.0f, mContext),
MapView.LayoutParams.BOTTOM_CENTER);
if (centerPopup) {
getController().animateTo(point);
mIgnoreNextChangeEvent = true;
}
View balloon = mInflater.inflate(R.layout.balloon, null);
balloon.setLayoutParams(lp);
((ViewGroup) balloon.findViewById(R.id.balloonBody)).addView(view);
balloon.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.bounce_in));
addView(balloon);
}
So he manually creates a balloon view and attaches it to the MapView.
I've been trying to emulate this same thing using V2 but I haven't been able and I don't even know if this is possible at all. For example, I use "GoogleMap" instead of "MapView" and I'm not sure if this has anything to do with some of the differences between V1 and V2.
I'm going to add what I have so far just as a reference. I tried to replicate the code from the other project and try to modify it so it will work in this one but I haven't been able to even compile it.
public boolean onMarkerClick(Marker marker) {
map.removeAllViews();
MapView.LayoutParams lp = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
marker.getPosition(),
Tools.dipsToPixels(0.0f, this),
Tools.dipsToPixels(-12.0f, this),
MapView.LayoutParams.BOTTOM_CENTER);
if (centerPopup) {
getController().animateTo(point);
mIgnoreNextChangeEvent = true;
}
View balloon = mInflater.inflate(R.layout.balloon, null);
balloon.setLayoutParams(lp);
((ViewGroup) balloon.findViewById(R.id.balloonBody)).addView(view);
balloon.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.bounce_in));
addView(balloon);
return false;
}
You cannot synchronize scrolling map with any view that could be over it and even if you completed this kind of solution it would look laggy.
Have you tried this workaround: https://stackoverflow.com/a/16147630/2183804 ?
I am using this project universal-image-loader in order to display in a gridview several images. But I would like to modify these images with some html (for example add ads at the bottom of each images).
I saw the image are displayed by calling imageLoader.displayImage(..), in the method getView of the ImageAdapter.
I don't know what approach is better:
add a step in displayImage(...) in order to create a webview with my image and html and transform this webview in bitmap?
modify the getView of ImageAdapter which will create the webview and add an argument webview in displayeImage()?
...
...
I guess I don't have the choice, I have to use a webview.
Moreover in the method displayImage() of ImageLoader.java, i don't understand where the images are loaded, I guess this is somewhere in this line, but when I am looking in these methods I can't find/understand
ImageSize targetSize = getImageSizeScaleTo(imageView);
String memoryCacheKey = MemoryCacheUtil.generateKey(uri, targetSize);
cacheKeysForImageViews.put(imageView.hashCode(), memoryCacheKey);
Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
(How from a string built by an uri and ImageSize we can have a bitmap? where is the image?)
Images are loaded asynchronously. So your code piece is just a top of complex logic. Main work is done in LoadAndDisplayTask class but you don't need to touch it.
For your case you can create own BitmapDisplayer:
create BitmapDisplayer implementation
implement Bitmap display(Bitmap bitmap, ImageView imageView) method
in this method you can process incoming Bitmap as you want
set result Bitmap in ImageView and return result
Set your BitmapDisplayer into configuration.