I'm creating an app which should list some snowboard tricks and when one clicks on a trick, a popup should appear with information on the trick and how to do it (with text and maybe a youtube vid).
How does one realise this?
Here is a code for Creating a custom list adapter with a popup window that pop up when you press a certain image in the ListView row:
Here is the Adapter:
public class tasksRepositoryAdapter extends ArrayAdapter<Task>
{
private ArrayList<Task> list;
public tasksRepositoryAdapter(Context context, int textViewResourceId, List<Task> tasksRepository)
{
super(context, textViewResourceId, tasksRepository);
this.list = new ArrayList<Task>();
for (Task task : tasksRepository)
{
this.list.add(task);
}
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row;
final ViewHolder holder = new ViewHolder();
tfRobotoRegular = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.new_row, null);
holder.tvTitle = (TextView) row.findViewById(R.id.text_title);
String title = tasksRepository.get(position).getTitle();
if (title.length()>25)
{
title = title.substring(0, 24);
title = title + "...";
}
holder.tvTitle.setText(title);
holder.tvTitle.setTypeface(tfRobotoRegular);
holder.tvDate = (TextView) row.findViewById(R.id.text_date);
holder.tvDate.setText(tasksRepository.get(position).getDate());
holder.tvDate.setTypeface(tfRobotoRegular);
holder.tvTime = (TextView) row.findViewById(R.id.text_time);
holder.tvTime.setText(tasksRepository.get(position).getTime());
holder.tvTime.setTypeface(tfRobotoRegular);
holder.tvDescription = (TextView) row.findViewById(R.id.text_description);
String description = tasksRepository.get(position).getDescription();
if (description.length()>46)
{
description = description.substring(0, 45);
description = description + "...";
}
holder.tvDescription.setText(description);
holder.tvDescription.setTypeface(tfRobotoRegular);
holder.tvId = (TextView) row.findViewById(R.id.text_id);
holder.tvId.setText(String.valueOf(tasksRepository.get(position).getId()));
holder.tvId.setTypeface(tfRobotoRegular);
holder.tvLocation = (TextView) row.findViewById(R.id.text_location);
holder.tvLocation.setText(tasksRepository.get(position).getCity());
holder.llRowLayout = (LinearLayout) row.findViewById(R.id.llRowLayout);
holder.imCalendar = (ImageView) row.findViewById(R.id.iCalendar);
holder.imClock = (ImageView) row.findViewById(R.id.iClock);
holder.imLocation = (ImageView) row.findViewById(R.id.iLocation);
holder.imTaskStatusButton = (ImageView) row.findViewById(R.id.iTaskStatusButton);
holder.imTaskStatusButton.setTag(position);
holder.imTaskStatusButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
int[] location = new int[2];
currentRowId = position;
currentRow = v;
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
v.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
point = new Point();
point.x = location[0];
point.y = location[1];
showStatusPopup(TasksListActivity.this, point);
}
});
String status = tasksRepository.get(position).getStatus();
Log.d(TAG, "The status of the current row: "+ status );
setStatusColorImages(status, holder.imClock, holder.imCalendar, holder.imLocation, holder.llRowLayout);
return row;
}
}
Here is the ViewHolder:
static class ViewHolder
{
RelativeLayout rlTitle;
LinearLayout llRowLayout;
TextView tvId;
TextView tvTitle;
TextView tvDate;
TextView tvTime;
TextView tvDescription;
TextView tvLocation;
ImageView imClock;
ImageView imCalendar;
ImageView imLocation;
ImageView imTaskStatusButton;
}
And the PopUp Window:
// The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);
// Creating the PopupWindow
changeStatusPopUp = new PopupWindow(context);
changeStatusPopUp.setContentView(layout);
changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setFocusable(true);
// Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
int OFFSET_X = -20;
int OFFSET_Y = 50;
//Clear the default translucent background
changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
Related
I have a Recyclerview that is managed by an adapter. For each item in the recycler I inflate a complex view that contains also a horizontal progressbar which takes the whole width of screen.
I have to position a baloon TextView with the percentage value (20% , 60% etc) that points to the progressbar like an indicator to the amount of progress. I tried using this code
int[] startPosition = new int[2];
Integer progresswidth;
WindowManager wm = (WindowManager) contextthis.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
progresswidth = size.x;
holder.horiz_progress.getLocationOnScreen(startPosition);
float exactvalue = (progresswidth * currentitem.getMatchPercent()) / 100;
startPosition[0] = startPosition[0] + Math.round(exactvalue);
startPosition[0] = startPosition[0] - holder.baloon_txt.getWidth() / 3 ;
startPosition[1] = startPosition[1] + 10;
holder.baloon_txt.setX(startPosition[0]);
holder.baloon_txt.setY(startPosition[1]);
But the problem is that holder.horiz_progress.getLocationOnScreen always returns 0 so I cannot position the balloon_txt.
I had a similar issue inside an activity and there i resolved it overriding OnWindowFocusChanged but this is inside the adapter so I don't know how to get it done.
EDIT
My current adapter code:
public class ResultsAdapter extends RecyclerView.Adapter<ResultsAdapter.ViewHolder>{
private List<ResultsItem> mResults;
private View mSelectedView;
private int mSelectedPosition;
Context contextthis;
android.os.Handler handler;
int[] startPosition = new int[2];
public ResultsAdapter(Context context,List<ResultsItem> resultsItemList) {
this.mResults = resultsItemList;
contextthis = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.results_item, null);
final ViewHolder viewHolder = new ViewHolder(v);
viewHolder.results_likeimg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return viewHolder;
}
#Override
public void onBindViewHolder( final ViewHolder holder, int position) {
final ResultsItem currentitem = mResults.get(position);
//set Image
if(currentitem.getImageslist().get(0).getPicture() != null)
ImageLoader.getInstance().displayImage(currentitem.getImageslist().get(0).getPicture(), holder.results_img);
//baloon set
holder.baloon_txt.setText(currentitem.getMatchPercent() + "% " + "Match");
holder.horiz_progress.setProgress(currentitem.getMatchPercent());
final View view = holder.horiz_progress;
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
view.getLocationOnScreen(startPosition);
Integer progresswidth;
WindowManager wm = (WindowManager) contextthis.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
progresswidth = size.x;
float exactvalue = (progresswidth * currentitem.getMatchPercent()) / 100;
startPosition[0] = startPosition[0] + Math.round(exactvalue);
startPosition[0] = startPosition[0] - holder.baloon_txt.getWidth() / 3 ;
startPosition[1] = startPosition[1] + 10;
holder.baloon_txt.setX(startPosition[0]);
holder.baloon_txt.setY(startPosition[1]);
}
});
//logo
if(currentitem.getPriceslist().get(0).getSource() != null)
ImageLoader.getInstance().displayImage(currentitem.getPriceslist().get(0).getSource(), holder.results_logo_img);
//description
holder.description_txt.setText(currentitem.getDescription());
//price
holder.price_curr.setText(currentitem.getPriceslist().get(0).getCurrency());
holder.price_txt.setText(String.valueOf(currentitem.getPriceslist().get(0).getPrice()));
}
#Override
public int getItemCount() {
return mResults.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView results_img, results_dislikeimg, results_likeimg, results_logo_img;
ProgressBar horiz_progress;
TextView baloon_txt, price_txt, description_txt, buybtn, sharebtn, price_curr;
public ViewHolder(View view) {
super(view);
this.results_img = (ImageView) view.findViewById(R.id.resultsitem_img);
this.results_dislikeimg = (ImageView) view.findViewById(R.id.results_item_dislike);
this.results_likeimg = (ImageView) view.findViewById(R.id.resultsitem_like);
this.results_logo_img = (ImageView) view.findViewById(R.id.logoimg);
this.horiz_progress = (ProgressBar) view.findViewById(R.id.progressBar_horizontal);
this.baloon_txt = (TextView) view.findViewById(R.id.baloonMatch_txt);
this.price_txt = (TextView) view.findViewById(R.id.price_txt);
this.description_txt = (TextView) view.findViewById(R.id.description_txt);
this.buybtn = (TextView) view.findViewById(R.id.buybtn);
this.sharebtn = (TextView) view.findViewById(R.id.sharebtn);
this.price_curr = (TextView) view.findViewById(R.id.price_curr);
}
}
}
getLocation() returns 0 because the view has not been laid out yet. You need to set a layout listener:
final View view = holder.horiz_progress;
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Do what you need to do here.
// Then remove the listener:
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
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());
}
}
});
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;
}
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
I'm trying to inflate a PopupWindow from a button that sits within list items (from a custom list adapter) but am getting NullPointerExceptions in multiple places.
Here's the onClick for the button where I'm trying to make the PopupWindow happen:
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = null;
if (scores[0] == null) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) v.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(v.findViewById(R.id.fragment_content), Gravity.CENTER, 0, 0);
}
I'm getting the NullPointerException at tag.setText(tags[i]);. If I comment that and just try to do scores, I get a NullPointerException at score.setText(Integer.toString(tag1score));. Both tags[] and scores[] are initialized and filled before the onClick, and I've tested to ensure that the results in both are Strings and NOT null.
Any thoughts as to why this NPE is happening?
You need to use the right thing wherein to look for the view. You are inflating into pw, but you are looking in v for your views. I think this might work better;
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = null;
PopupWindow pw = null;
if (scores[0] == null) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) popupView.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) popupView.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
it keeps track of the view you have inflated into, so you can search it for your elements.