No links clickable in WebView embedded in ViewGroup if other children there - android

I have a RelativeLayout that contains a WebView and a ListView (ListViewContainer is a subclass of ListView:
public AdListViewContainer(Context context, ServiceLookup lookup, MarketList list, Registry registry, BitmapCache bitmapCache, Utilities utilities, ActionFlipper flipper) {
super(context);
mScale = getContext().getResources().getDisplayMetrics().density;
final int orientation = getResources().getConfiguration().orientation;
setClickable(true);
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mAd = new WebView(context);
mAd.setId(1234);
mAd.setWebViewClient(new AdListWebClient());
mAd.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d (TAG, "Inner Event " + event.getAction());
return false;
}
});
mAd.getSettings().setJavaScriptEnabled(true);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(ALIGN_PARENT_BOTTOM, 1);
addView(mAd, lp);
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
if (mAd != null) {
lp.addRule(ALIGN_BOTTOM, mAd.getId());
}
mListContainer = new ListViewContainer(context, lookup, list, registry, bitmapCache, utilities, flipper);
addView(mListContainer, lp);
}
The WebView is showing an ad generated by Adition, basically an img-tag in an a-tag.
The problem I have is that the ad can't be clicked. No touch or click events are received by the WebView. When I remove the ListView and the WebView is the only child of the ViewGroup (by removing the last addView call), the ad is clickable and everything is fine.
ListViewContainer is a straightforward subclass of ListView that contains a load of clickable LinearLayouts.
Any help appreciated!

Ok, I found the issue. The ListView was overlapping the WebView, caused by using ALIGN_BOTTOM instead of ABOVE. D'Oh. HierarchyViewer was a blast in finding this.

I overrode OnTouchEvent for the Webview, and put the WebView inside of a FrameLayout, filling it, then put everything else I needed on top of the webview. Working great!

Related

".addView()" for simple views

As you can see in the image, I can select the view and 3 ImageViews get added using "_root.addView(dragIcon);".
My problem is that the view is a simple view and not a ViewGroup , which means that in this picture i had to add the 3 images to the RelativeView Background "root.addView(dragIcon);" Instead of to the Green rectangle view shown in the image.
I'm pretty sure i can't change the View to a ViewGroup becuase a lot of methods (ex. onTouch) require a "View". I've tried casting the View to a ViewGroup "((ViewGroup) view).addView(dragIcon);" but that did't work.
You can make multiple Views in the app and drag them around, so I need to make the 3 images children to the specific parent View.
Any suggestions are appreciated, thanks!
Here is the part of the code that's relevant to this question...
_root = (ViewGroup)findViewById(R.id.root); //This is the background
_view = new View(this); //This is the View (Green in the Image)
private void selectView(final View view) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(final View view, final MotionEvent event) {
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageView dragIcon, sizeIconTop, sizeIconBottom;
if (view.getTag(R.string.viewSelected) != "1") {
view.setBackgroundColor(0xFF00AA00);
view.setTag(R.string.viewSelected, "1");
double Ypos = view.getTranslationY() - view.getHeight() / 2;
// Set draggable (6*3 grid on the right)
dragIcon = new ImageView(MainActivity.this);
dragIcon.setImageResource(R.drawable.drabicon);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgParams.setMargins(0,0,30,0);
dragIcon.setLayoutParams(imgParams);
dragIcon.setTranslationY((float) Ypos + 70 + view.getHeight() / 2);
_root.addView(dragIcon); //Need to change from _root.addView to view.addView
// Set size top (White line at the top)
sizeIconTop = new ImageView(MainActivity.this);
sizeIconTop.setImageResource(R.drawable.resize);
RelativeLayout.LayoutParams stImgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
stImgParams.setMargins(0,0,30,0);
stImgParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
sizeIconTop.setLayoutParams(stImgParams);
sizeIconTop.setTranslationY((float) Ypos + 99);
_root.addView(sizeIconTop); //Need to change from _root.addView to view.addView
// Set size bottom (White line at the bottom)
sizeIconBottom = new ImageView(MainActivity.this);
sizeIconBottom.setImageResource(R.drawable.resize);
RelativeLayout.LayoutParams sbImgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
sbImgParams.setMargins(0,0,30,0);
sbImgParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
sizeIconBottom.setLayoutParams(sbImgParams);
sizeIconBottom.setTranslationY((float) Ypos + 93 + view.getHeight());
_root.addView(sizeIconBottom); //Need to change from _root.addView to view.addView
} else { //Ignore this part
//((ViewGroup) view).removeView(dragIcon);
//((ViewGroup) view).removeView(sizeIconTop);
//((ViewGroup) view).removeView(sizeIconBottom);
view.setBackgroundColor(0xFF00FF00);
view.setTag(R.string.viewSelected, "0");
}
}
});
Even though the question is a bit confusing, after analysing your code I figured out what you wanted to do.
Unfortunately, you have to choose whether to use a View (and not being able to attach the ImageViews) or using a ViewGroup (and handling the events yourself).
Since your View is a View, and your 3 ImageViews are also Views (means they are at the same level of usage), you can not add the last 3 to the first.
The rule is : You can add ViewGroups and Views to a ViewGroup, but you can not add Views or ViewGroups to a View. Views are supposed to be the elementary block of Android design.
void addView (View child)
Adds a child view. If no layout parameters
are already set on the child, the default parameters for this
ViewGroup are set on the child.
In my opinion, I would make View a ViewGroup and take care of the TouchEvents by myself.
Check this documentation/code regarding TouchEvents on ViewGroups
Let me know of your progress. Will be glad to help you.
Regards,

How to add ImageView to center of RelativeLayout and let it drag on gesture?

I'm adding an ImageView to RelativeLayout via Java code, without declaring Imageview in XML. Using the following code :
final ImageView newImageView= new ImageView(context);
final RelativeLayout.LayoutParams imgvwDimens =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
newImageView.setLayoutParams(imgvwDimens);
ImageLoader imageLoader = new ImageLoader(context);
imageLoader.DisplayImage(imageURL, R.drawable.ic_launcher, newImageView);
newImageView.setScaleType(ScaleType.MATRIX);
newImageView.setLongClickable(true);
newImageView.setClickable(true);
I want to add the ImageView in center of the RelativeLayout, so that can be done by:
imgvwDimens.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
But if I do so the ImageView won't DRAG on touch, so on move I tried removing the CENTER_IN_PARENT using :
newImageView.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
{
imgvwDimens.removeRule(RelativeLayout.CENTER_IN_PARENT);
After that I'm able to move the ImageView, but on touch it gets relocated to starting of the layout.
Please provide me with helpful code or links. Thanks.
I figured it out, actually solution is pretty simple :)
Following code can be used for centring the ImageView initially, since I'm using RelativeLayout :
imgvwDimens.leftMargin = rl.getWidth()/2;
imgvwDimens.topMargin = rl.getHeight()/2;

Adding dynamic web views in android

I am going through various links on this topic. I haven't found anything useful in my case. I want to create a Web View in Android dynamically. I will add nothing onto the xml layout file.
Now I want to do two things in particular:
1. I want to set the size of this Web View to a custom width and height and set it at the bottom of the Layout.
2. Once the page loads in the Web view. On click on the Web view should resize and go full screen(which means fill_parent on width and height for web view in its parent layout).
My current code looks something like this below which does not work particularly well as the full screen does not fit the content to screen:
public class MainActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=(RelativeLayout)findViewById(R.id.test);
final WebView web = new WebView(this);
//web.setId(1);
web.getSettings().setJavaScriptEnabled(true);
web.loadData("<script type=\"text/javascript\" src=\"http://www..myurl.com/myfolder/banner.php\"></script>", null, null);
layout.addView(web, new LayoutParams(300,100));
web.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
web.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I understand your problem correctly, you may want to consider the following approach.
First, declare and initialize the (parent) relative layout, together with its parameters and set it as the content view (delete setContentView(R.layout.activity_main);).
final RelativeLayout layout = new RelativeLayout(this);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight());
layout.setLayoutParams(params);
setContentView(layout);
Then, initialize the web view and its parameters, and add the "bottom rule" to the parameters.
final WebView web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
web.loadData(
"<script type=\"text/javascript\" src=\"http://www..myurl.com/myfolder/banner.php\"></script>",
null, null);
RelativeLayout.LayoutParams webViewParams = new RelativeLayout.LayoutParams(
300, 100);
webViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Finally, add the web view to your (parent) relative layout and add its listener.
layout.addView(web, webViewParams);
web.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
web.setLayoutParams(params);
return false;
}
});
Hope this is what you needed.
Cheers!

I am developing one application in which i want to add image on another image where user touch.please help me

public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
int x = (int) event.getX() ;
int y = (int) event.getY();
System.out.println("xxx");
release=(ImageView)findViewById(R.id.imageView2);
RelativeLayout.LayoutParams lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //Assuming you use a RelativeLayout
ImageView iv=new ImageView(getApplicationContext());
System.out.println("YYYY");
lp.setMargins(x,y,0,0);
System.out.println("aaaa");
iv.setLayoutParams(lp);
System.out.println("bbbbb");
iv.setImageDrawable(getResources().getDrawable(R.drawable.pain_icon));
System.out.println("ccccc");
((ViewGroup)v).addView(release);
}
return true;
}
You haven't added the dynamicallly created view to ur parent. first declare ur parent and add this iv (imageview in ur code) to the parent using relativeLayout.addView(iv);
i'm guessing here since you din't post the error.
R.id.imageView2 most likely has a parent and you are trying to add it to another viewgroup. thats whats causing the error. you need to create a new instance of ImageView then add it to your view group
ImageView im = new ImageView(this);
im.setImageResource(R.drawable.nameofimage);
((ViewGroup)v).addView(im);

How do I position ZoomButtonsController in custom ImageView?

Layouting in Android is getting me rather perplexed.
I'm slowly implementing a custom ImageView where I'd like to make use of the ZoomButtonsController.
However, I would like to decide where the zoom buttons go in the layout and I can't figure out how to move them from the default bottom center position.
I have been experimenting with layouting simple views such as buttons in the main activity and this seems to be working as I would guess and expect.
In the case of the ZoomButtonsController I would however like to reposition them. I'm using a RelativeLayout as the mail layout and add the ZoomButtonsController within the custom ImageView.
The Activity code
public class ImageViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CustomImageView imageView = new CustomImageView(this);
relativeLayout.addView(imageView);
}
}
The CustomImageView code
public class CustomImageView extends ImageView {
private ZoomButtonsController mZoomButtons;
public CustomImageView(Context context) {
super(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
mZoomButtons = new ZoomButtonsController(this);
mZoomButtons.getZoomControls();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
setLayoutParams(layoutParams);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
Log.d("TAG", "touch");
mZoomButtons.setVisible(true);
return true;
}
}
I've tested with WRAP_CONTENT in the parameters, but this only makes the zoom buttons disappear.
As a matter of fact, I couldn't position the ZoomButtonsController in any way and in the end had to accept the default placement.

Categories

Resources