I have a big image which contains different sections. What I want is different PopUps should open when the user touches different sections of the ImageView. For instance, see this Image below:
In this image, what I want is that when the user clicks 1st square, Popup 1 should open, on square 2 popup should open. How to achieve this please.?
Moreover, I want the ImageView still be zoom and pan enabled. Please help.
Hello you can follow my this post . I think my answer will help you ::
popup window from android options menu not working
For you I have edited the code. You can follow in this manner. If button will be Image and replace button with that :
inside onCreate()
Button btn1=(Button) findViewById(R.id.btn1);
Button btn2=(Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
initiatePopupWindow1();
}
})
btn2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
initiatePopupWindow2();
}
})
outside onCreate()
private PopupWindow pwindo1,pwindo2;
private void initiatePopupWindow1() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,(ViewGroup)
findViewById(R.id.popup_element));
pwindo1 = new PopupWindow(layout, 350, 350, true);
pwindo1.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initiatePopupWindow2() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,(ViewGroup)
findViewById(R.id.popup_element));
pwindo2 = new PopupWindow(layout, 350, 350, true);
pwindo2.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
You can use (though it doesn't seem to be the best way of what actually you are trying to achieve but still you can have a single image)
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
and the event.getX() and event.getY() will give you the coordinates where you are clicking
for example
if(event.getX() == 100 and event.getY() == 100)
{ // show your popup}
But there will be a problem if you have multiple images
you have to know at what positions these boxes will be present, and you might face problem if the image is to zoomed
Related
I have a PopupWindow that has a TextView in it. I am trying to change the text in this TextView dynamically but can't seem to get it to work. The code executes, but it does nothing and gives no errors, making it hard to understand what's happening (as there is nothing that shows up in the debug logs). It's important to note that this TextView is not in the same layout as the layout given to setContentView - and that menu is called when a button is pushed in the main layout. Does anybody have any idea why this could be happening?
Here is my code:
public void popupView(View view, int viewPointer){//viewPointer is R.id.popup_layout
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(viewPointer, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupView, width, height,true);//add another parameter here (true) to make it so it goes away when clicked outside the menu itself
popupWindow.setElevation(20);
// show the popup window
// which view you pass in doesn't matter
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
and
public void menu(View view){
textView.setText("this won't change the text but will execute with zero errors...");
popupView(view, R.layout.popup_layout);
}
and in onCreate:
LayoutInflater layoutInflater = getLayoutInflater();
View text_view = layoutInflater.inflate(R.layout.popup_layout, null);
textView = text_view.findViewById(R.id.textViewID);
For the app i'm currently building i inflate a popup on the activity if there is no internet connection, the problem is that if the user presses their phone's back button, the popup disappears but u still get to see the activity without any information.
I would like to find out a way to finish the activity as well, when the back button is pressed.
Heres the code for the popup im currently inflating:
public void connection_error(final Class<?> clss){
if(!Functions.isOnline(getApplicationContext())) {
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_nointernet, null);
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.MATCH_PARENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
popupWindow.showAtLocation(findViewById(R.id.drawer_layout), Gravity.CENTER, 0, 0);
Button btn_retry = popupView.findViewById(R.id.btn_retry);
btn_retry.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Functions.isOnline(getApplicationContext())) {
finish();
popupWindow.dismiss();
launchActivity(clss);
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.label_no_internet), Toast.LENGTH_LONG).show();
}
}
});
}
}
Set a boolean that's set to true if your popup is open. Something like,
boolean popUpOpen = true;
Override the onBackPressed Method:
#Override
public void onBackPressed() {
if(popUpOpen){
super.onBackPressed(); // Closes Popup.
}
super.onBackPressed(); // Normal Behaviour - Finishes Activity if popup is open
}
I have two classes. One extends FragmentActivity (MainActivity.java) and the other extends Fragment (HomeFragment.java). What I want is to create an image button in MainActivity and put it in home_fragment.xml and then update HomeFragment.java so that my imagebutton is permanently in the layout. Right now, I have a button, and add it to home_fragment.xml, and that works fine, but then if I leave that fragment and return back to it (through a button click), the button disappears.
Here is a picture of my interface. Where home_fragment is the white space with the "DriveDroid" button inside. And it is initiated when I click "Button 1". If I click button 2, 3, or 4 a different fragment is activated and if I go back to "Button 1", which opens home_fragment.xml, then the "DriveDroid" button is gone.
MainActivity is running a separate thread and listening on a port for an incoming image. Once it gets this image it creates an imagebutton with the image as the background. It adds this image button to an xml layout (home_fragment). This is all working correctly and this is the code I have for MainActivity:
private void RepeatTask()
{
repeatTaskThread = new Thread()
{
public void run()
{
while (true)
{
try {
System.out.println("TRY_1");
Socket socket = new Socket("72.120.55.34", 5050);
// Get data sent through socket
DataInputStream DIS = new DataInputStream(socket.getInputStream());
System.out.println("DataInputStream Started");
// read data that got sent
String applicationName = DIS.readUTF();
// read array data for bitmap
int len = DIS.readInt();
byte[] data = new byte[len];
DIS.readFully(data, 0, data.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
final Drawable d = new BitmapDrawable(getResources(), bitmap);
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Log.d("tag_name", "Try_2");
ImageButton btn = new ImageButton(getApplicationContext());
//btn.setId(1);
btn.setBackgroundDrawable(d);
Log.d("tag_name", "Create Button" + btn);
ViewGroup RelativeLayout = (ViewGroup) findViewById(R.id.home_fragment);
btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
RelativeLayout.addView(btn);
Log.d("tag_name", "Button Added to View" + RelativeLayout);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
});
socket.close();
} catch (Exception e) {
System.out.println("Exception is "+e.toString());
}
try
{
// Sleep for 5 seconds
Thread.sleep(5000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
};
repeatTaskThread.start();
}
How can I get this image button to update in HomeFragment? And is this even what I should be doing? I just need my image button to now always appear whenever other parts of my code call on HomeFragment.
Here is how HomeFragment is called when Button1 is pushed (this code is in another fragment):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fr_menu, container, false);
// get home image button
ImageButton Button1 = (ImageButton) view.findViewById(R.id.btnHOME);
// button listener
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new HomeFragment()).commit();
}
});
I am using the following code to create a popup which shows as the activity loads.
I call loadingPopup(); in my oncreate method.
The popup works the problem is when ever i try to place onclick listener for the dismiss button the whole thing crashes on load. If i were to remove the DissMissButton onclick listener code it works fine. Any help would be appreciated
private void loadingPopup() {
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.popup_welcome, null);
final Button DismissButton = (Button) findViewById(R.id.button_welcomepopup_dismiss);
final PopupWindow Welcome_popupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
Welcome_popupWindow.setFocusable(false);
Welcome_popupWindow.setTouchable(true);
Welcome_popupWindow.setOutsideTouchable(true);
layout.post(new Runnable() {
public void run() {
Welcome_popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
DismissButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Welcome_popupWindow.dismiss();
}
});
}
});
You call find view on your activity view, while your button is in the popup, so of course the DismissButton is null and app force close.
Change the find view code to this:
final Button DismissButton = (Button) layout.findViewById(R.id.button_welcomepopup_dismiss);
Aftermath of this: Image for ImageButton doesn't change as intended
When ImageButton of "ibChamp" is clicked, it opens up champions.xml. In that layout, there is an Imagebutton called "ibAnnie". What I want that to do when clicked is, to change the image of "ibChamp", and to switch to that layout. What happened here is that after "ibAnnie" is clicked, it switches to "create_build_page.xml", and in that is a changed picture of "ibChamp". But that only happens for a split second before changing back to the original, unchanged picture, "create_build_page.xml" layout. When I click the back button, it goes to the "create_build_page.xml" layout with the changed picture, but the buttons do not work. I would gladly provide any additional information required.
I'm still not entirely clear on what you're trying to achieve... but if my understanding is correct, this should get you closer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.champions);
ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);
ibAnnie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View createView = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(createView);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);
img.setTag(R.drawable.ic_launcher); // so you can retrieve it later
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// this will set the imageView of ibAnnie
ibAnnie.setImageView((Integer)view.getTag());
// this will remove the selection view from your layout
((RelativeLayout) findViewById(R.id.layoutChampions)).removeChild(createView);
}
});
// this opens another Activity... you don't want that, at least not for what you seem to be trying to do.
/*try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {
}*/
}
});
}