OnClick Event not detected on parentView in AChartEngine - android

I use the following code to draw GraphView on LinearLayout,whose parent is a RelativeLayout
the code for drawing GraphView on LinearLayout
mLinearView = (LinearLayout) findViewById(R.id.LinearView);//ChildLayout
mRelativeView = (RelativeLayout) findViewById(R.id.RelativeLay);//ParentLayout
YearXYGraph mYearGraph = new YearXYGraph();
final GraphicalView mGraphView = mYearGraph.execution(
MainActivity.this, mGirlHeightYearFirstGrade,
mGirlHeightYearSecondGrade,
mGirlHeightYearThirdGrade,
mGirlHeightYearFourthGrade,
mGirlHeightYearFifthGrade, mGirlWeightYearFirstGrade,
mGirlWeightYearSecondGrade, mGirlWeightYearThirdGrade,
mGirlWeightYearFourthGrade, mGirlWeightYearFifthGrade);
mLinearView.addView(mGraphView);
onclicking LinearLayout I'm Showing a popup,code follows
mGraphView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
double[] xy = mGraphView.toRealPoint(0);
p = new Point();
p.x = (int) xy[0];
p.y = (int) xy[1];
if (p != null){
showPopup(MainActivity.this, p);
}
}
});
Im also using onClick event on ParentLayout to get some coordinate
mRelativeView.setOnTouchListener(new RelativeLayout.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("in--");
p1 = new Point();
p1.x = (int) event.getX();
p1.y = (int) event.getY();
System.out.println("x--"+p1.x);
return false;
}
});
code for Showing popup
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 75;
int popupHeight = 40;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
Rect location = new Rect();
location.left =p1.x;
location.top = p1.y;
location.right = location.left + popup.getWidth();
location.bottom = location.top + popup.getHeight();
/* // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;*/
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
// popup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, location.left , location.bottom );
popup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, location.left , location.bottom );
// Getting a reference to Close button, and close the popup when clicked.
TextView firstView = (TextView) layout.findViewById(R.id.textView1);
firstView.setText(p.x + " , " +p.y);
firstView.setTextColor(Color.WHITE);
}
problem is onClickEvent not detected on ParentLayout,,Child Layout detects OnClick,please provide some suggestions

Related

How can I load WebView in custom popup window in android?

I have Create popup.xml and I want to load my webpage in WebView from assets folder.
Here is my popup.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pp"
android:orientation="vertical" >
<WebView
android:id="#+id/webview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is my activity file
How to load my webpage in webview with popup window?
Is there any solution?
public class MainActivity extends ActionBarActivity {
Point p;
public int a, b, c, d;
ImageButton au, gc, cert, busa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
au = (ImageButton) findViewById(R.id.au);
gc = (ImageButton) findViewById(R.id.gc);
cert = (ImageButton) findViewById(R.id.cert);
busa = (ImageButton) findViewById(R.id.busa);
au.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null) {
showPopup(MainActivity.this, p);
}
}
});
}
// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will
// return (0, 0))
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 720;
int popupHeight = 380;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 130;
int OFFSET_Y = 100;
// Clear the default translucent background
// popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
// Getting a reference to Close button, and close the popup when
// clicked.
}
}
Webview
webview1.loadURL("the url goes here");

PopWindow always show at last in ListView...!

I working on android PopupWindow. In this application I create 1 custom ListView. Now I want to show PopupWindow when user click on TextView of custom ListView.
My problem is :
PopupWindow always show at last TextView even if I click on first or other TextView . How can I resolve this.??
TextView of ListView.
holder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
holder.end.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
if (p != null){
showPopup(context,p);
holder.popupText.setText(holder.end.getText().toString());
}
}
});
My popupWindow function.
private void showPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout)context.findViewById(R.id.layoutPopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
holder.popupText = (TextView) layout.findViewById(R.id.showPopUp);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popup.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
int OFFSET_X = 30;
int OFFSET_Y = 30;
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(layout.findViewById(R.id.showPopUp), Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
Tray changing your click listener with this code. It should work.
holder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
if (p != null){
showPopup(context,p);
holder.popupText.setText(holder.end.getText().toString());
}
}
});

How to center PopupWindow?

I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:
private void showPopup() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
Button btnAgree = (Button) layout.findViewById(R.id.ok);
btnAgree.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}
I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?
you can use setHorizontalOffset:
ListPopupWindow popupWindow = new ListPopupWindow(this);
// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);
this code will show perfect popup in center of you specific view
// click event of specific view
#Override
public void onClick(View v) {
super.onClick(v);
if (v == lin_filterActionbar) {
PopupWindow popupwindow_obj; // create object
popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
popupwindow_obj.showAsDropDown(lin_filterActionbar);
}
}
private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {
final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);
View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
dialog_AllergiesSelect.setContentView(v);
dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setFocusable(true);
// TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
// LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
// lis_testSelection.setLayoutParams(lin_laLayoutParams);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
//dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);
Rect location = locateView(lin);
dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);
// Getting a reference to Close button, and close the popup when clicked.
return dialog_AllergiesSelect;
}
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}

Android: Set size and location of quick action at Listview

I want to use QuickAction dialog in my learning App, but I don't know how to change the size of the dialog. Now, the dialog is close to phone border, but I want to left blank between borders or reduce the width.
Thanks~
call setRootViewId(R.layout.notify_quick_action); at constructor.
public void setRootViewId(int id) {
mRootView = (ViewGroup) inflater.inflate(id, null);
mTrack = (ViewGroup) mRootView.findViewById(R.id.tracks);
mArrowDown = (ImageView) mRootView.findViewById(R.id.arrow_down);
mArrowUp = (ImageView) mRootView.findViewById(R.id.arrow_up);
mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(mRootView);
}
call show() at setOnItemClickListener()
public void show (View anchor) {
preShow();
int[] location = new int[2];
mDidAction = false;
anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1]
+ anchor.getHeight());
mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = mRootView.getMeasuredWidth();
int rootHeight = mRootView.getMeasuredHeight();
int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
int xPos = (screenWidth - rootWidth) / 2;
int yPos = anchorRect.top - rootHeight;
boolean onTop = true;
if (rootHeight > anchor.getTop()) {
yPos = anchorRect.bottom;
onTop = false;
}
showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), anchorRect.centerX()+180);
setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);
Log.d("-------", "xPos="+xPos+";yPos="+yPos+";rootWidth="+rootWidth+";rootHeight="+rootHeight+";screenWidth="+screenWidth);
mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 100, yPos);
if (mAnimateTrack) mTrack.startAnimation(mTrackAnim);
}
Just use custom PopupWindow. Yeah, here link:
https://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/

Add Views programmatically to LinearLayout after it got inflated

I want to add some LinearLayouts to an existing Linearlayout.
The xml of the Activity looks as followed:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popupLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPopUp"
android:gravity="center"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/ll_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
The code of the Activity:
public class MainActivity extends Activity {
private Point p;
private PopupWindow popup;
private LinearLayout myLInearLayout;
private TextView valueTV;
private Button valueB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button popUpButton = (Button) findViewById(R.id.open);
popUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null)
showPopup(MainActivity.this, p);
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.open);
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popupLinearLayout);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
// add LInearLayout
myLInearLayout = (LinearLayout) findViewById(R.id.ll_horizontal);
// add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
valueTV.setLayoutParams(params);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
myLInearLayout.addView(valueTV);
myLInearLayout.addView(valueB);
}
#Override
public void onBackPressed() {
if (popup != null && popup.isShowing()) {
popup.dismiss();
popup = null;
} else {
super.onBackPressed();
}
}
As you can see I want to add a Button and a TextView after I inflate a View and show it with an Animation in a PopupWindow. I would like to add some Views to the LinearLayout inside the ScrollView. This is for testing purposes, later I want to Add full LinearLayouts to the LinearLayout inside the ScrollView.
The Animation works perfectly. I just can't add some Views programmatically. Everything I get is a NullPointerException in the Line where I try to add the views.
I appreciate your help.
Try this way,hope this will help you to solve your problem.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button popUpButton = (Button) findViewById(R.id.open);
int[] location = new int[2];
popUpButton.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
popUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (p != null)
showPopup(p);
}
});
}
// The method that displays the popup_layout.
private void showPopup(Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popupLinearLayout);
View layout = LayoutInflater.from(this).inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(this);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
// popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup_layout a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup_layout at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// add LInearLayout
myLInearLayout = (LinearLayout) layout.findViewById(R.id.ll_horizontal);
// add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
valueTV.setLayoutParams(params);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
myLInearLayout.addView(valueTV);
myLInearLayout.addView(valueB);
}
I figured out how to do it. I just had to inflate the popup.xml to be able to get a reference to the LinearLayout I wanted to customize.
Here is the code if someone is facing the same problem:
public class MainActivity extends Activity {
private Point p;
private PopupWindow popup;
private Button popUpButton;
private TextView valueTV;
private Button valueB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popUpButton = (Button) findViewById(R.id.open);
popUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null)
showPopup(MainActivity.this, p);
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.open);
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.popup_layout, null);
ViewGroup dlgView = (ViewGroup) contentView
.findViewById(R.id.ll_horizontal);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(contentView);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
dlgView.addView(valueTV);
dlgView.addView(valueB);
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(popUpButton, Gravity.NO_GRAVITY, p.x + OFFSET_X,
p.y + OFFSET_Y);
}
}

Categories

Resources