how to handle onclick event of button inside popup window in android - android

In my application, I have a button initially on the screen, and in onclick of the button, a popup window should open. In the popup window, I have an imagebutton, and onclick of this button, I want to start an activity. The popup window opens, but I don't understand how to handle the onclick of the imagebutton inside the popup window.
In main.xml, I have a button, and in popup_example.xml, I have an imagebutton.
My Java code is as follows:
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Button b=(Button)findViewById(R.id.btn);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main)));
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});
//if onclick is written here it gives runtime exception.
});
and I have two xml layouts.........
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ghj" />
</LinearLayout>
popup_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#8E2323">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:background="#000000">
<ImageButton android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:src="#drawable/vitalss"
android:layout_weight="1"
android:background="#8E2323"/>
</TableLayout>
</TableLayout>
</LinearLayout>

You have to find the button into the Popup view:
View pview = inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main));
PopupWindow pw = new PopupWindow(pview);
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)pview.findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});

In main activity button onClick you can use:
popupWindow.getContentView().findViewById(R.id.buttonInPopup).setOnClickListener(new OnClickListener(...)

This will not give error in Inflater and work properly:
LayoutInflater layoutInflater = getLayoutInflater();
View pview = layoutInflater.inflate(R.layout.popup_example, (ViewGroup)findViewById(R.layout.main));
PopupWindow pw = new PopupWindow(pview);
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)pview.findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});

best solution :)
Pass onCclickMethod from xml
<ImageButton
android:id="#+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
**android:onClick="onClick"**
android:contentDescription="#string/save"
android:src="#drawable/save" />
it wroked from me..

Related

Android Custom Alert with XML - Button Click Action

I have 2 layout files, one for main layout showing (activity_main.xml) and another is for custom dialog showing (dialog_custom.xml). The 2 files are as follows-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/btn_1_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with 1 Button"
android:padding="10dip"
android:layout_marginTop="40dip">
</Button>
<Button android:id="#+id/btn_2_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert With 2 Buttons"
android:padding="10dip">
</Button>
<Button android:id="#+id/btn_3_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with 3 Buttons"
android:padding="10dip">
</Button>
<Button android:id="#+id/btn_custom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with Custom Layout - From XML"
android:padding="10dip">
</Button>
</LinearLayout>
dialog_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dialog_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/dialog_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#id/dialog_info">
<Button
android:id="#+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#3333"
android:text="Cancel"/>
<Button
android:id="#+id/dialog_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#888888"
android:text="Agree"/>
</LinearLayout>
</RelativeLayout>
And what I have done in the Main class is -
public class Activity_Main extends Activity
{
Context context = Activity_Main.this;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_main);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
}
So, I get a dialog like this-
Now I want to add on click option for this CANCEL and AGREE button. But if I want, I am getting forced close. Can anyone please help me, how can I do it?
Thanks for helping.
Do it as follows :
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.layout.dialog_main);
Button btCancel = (Button) dialog.findViewById(R.id.dialog_cancel);
Button btOk = (Button) dialog.findViewById(R.id.dialog_ok);
btCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Cancel Click
}
});
btOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//OK CLICK
}
});
dialog.show();
If you want to add Buttons or any Widget (inside of the Dialog) make sure that you are finding the view with dialog.findViewById(R.id.ID_WIDGET) otherwise it will crash.
I couldn't find your click code cancel and agree, but try this:
Button cancel=(Button)dialog.findViewById(R.id.dialog_cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do your work
}
});
Thanks if problem persists then post your error and click code.
You may need to add View.OnClickListener to buttons of dialog.
Button btnCancel = (Button) dialog.findViewById(R.id.dialog_cancel);
Button btnOk = (Button) dialog.findViewById(R.id.dialog_ok);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Called on click of Cancel button
}
});
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Called on click of Cancel button
}
});
dialog.show();

How to create three dot button on the top right of an item on an android listview?

Like this
If you click the button a menu will drop down. I use ListView to implement, but I don't know how to attach the three-dot-button and add a pop-up menu.
Here is my list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:background="#drawable/card_background" >
<TextView
android:id="#+id/tvMain"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light" />
</LinearLayout>
</FrameLayout>
And the screenshot of my app:
https://www.dropbox.com/s/h2ko2ctfn5ohmf9/Screenshot_2013-09-10-15-43-57.png
It's just a ImageView with the correct PNG (you'll have to draw the image yourself on photoshop, paint, GIMP or any other graphical editor) and use a PopupWindow to make the popup.
here is a simple example on how to do a PopupWindow (got it from here http://android-er.blogspot.de/2012/03/example-of-using-popupwindow.html)
#Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(view, 0, -0);
}

NullPointerException on OnclickListner of a button in the alertDialogue

The layout which i am inflating into the alertbuilder is having two button. But i am not able to set the onClickListner for that. this exception is occurring. Please see my code.
XML of the custom alertDialogue.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_common"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/user_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:singleLine="true" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
AlertDialog.Builder alert = new AlertDialog.Builder(Myclass.this);
alert.setTitle("title");
alert.setIcon(iconImage);
LayoutInflater inflater = (LayoutInflater)MyClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_dialogue, null, false);
user_input= (EditText)view.findViewById(R.id.user_text);
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
if(content.equals("")) {
user_input.setHint(hint);
} else {
user_input.setText(content);
}
alert.setView(view);
searchAlert = alert.create();
searchAlert.show();
You are missing view before findViewById, by excluding view you are refering to the activity..:
Button cancel = (Button) view.findViewById(R.id.cancel);
Button cancel = (Button) view.findViewById(R.id.cancel);
check here
user_input= (EditText)view.findViewById(R.id.user_text);//defined view for user_input
Button cancel = (Button) findViewById(R.id.cancel);//not defined view for button
change to
Button cancel = (Button)view. findViewById(R.id.cancel);

android Dialog alert

i am using a layout file for showing a dialog. My xml file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<EditText android:id="#+id/dialogEditText" android:text="Enter Your Text Here" android:layout_height="wrap_content" android:layout_width="match_parent"></EditText>
<DatePicker android:id="#+id/datPicker" android:layout_height="wrap_content" android:layout_width="match_parent" ></DatePicker>
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:text="Add" android:id="#+id/dialogAddBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
<Button android:text="Cancel" android:id="#+id/dialogCancelBtn" android:layout_width="200px" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
</LinearLayout>
</LinearLayout>
I want to do something when user clicks on "OK" or Cancel button . So i have created anonymous handlers for those button. My Coding is
LayoutInflater inflater = LayoutInflater.from(obj);
View inflatedView1= inflater.inflate(R.layout.dialog_view,null);
final Dialog d= new Dialog(obj);
final Window window= d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
window.setTitle("Add Item");
window.setContentView(R.layout.dialog_view);
final EditText input= (EditText) inflatedView1.findViewById(R.id.dialogEditText);
Button okBtn= (Button)inflatedView1.findViewById(R.id.dialogAddBtn);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast msg= Toast.makeText(obj.getApplicationContext(),"Hello",Toast.LENGTH_LONG);
msg.show();
}
});
Button cancelBtn= (Button) inflatedView1.findViewById(R.id.dialogCancelBtn);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});
d.show();
When i am clicking on "Ok" or "Cancel button" nothing is happening. I am not able to understand why this is happening.
Please suggest me some solution.
you are setting the event handlers on your inflated view but at the same setting the content view to your plain layout:
window.setContentView(R.layout.dialog_view);
Just change this line to
window.setContentView(inflatedView1);
Or, perhaps a better approach without using the layout inflater would be to use
d.setContentView(R.layout.dialog_view);
And then setting the event handlers via
Button okBtn = (Button) d.findViewById(R.id.dialogAddBtn);
okBtn.setOnClickListener(...
Michael

making buttons appear and disappear on an image view

I have an image view, i want to to create two buttons which will enable users to go to next or previous image. When a user taps the image , the buttons should appear and on the second tap , the button should disappear. How to go about doing this ?
Fortunately currently I am working on the same thing and here is my code modify it to suit your needs
xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:visibility="gone">
<Button
android:id="#+id/buy"
android:text="Buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/cancel"
android:text="Cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Main code
Button mBuy = (Button) findViewById(R.id.buy);
mBuy.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
Button mCancel = (Button) findViewById(R.id.cancel);
mCancel.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
ImageView mView = (ImageView) findViewById(R.id.image);
mView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout mLayout = (LinearLayout) findViewById(R.id.button_holder);
if(!isVisible)
{
isVisible = true;
mLayout.setVisibility(View.VISIBLE);
}
else
{
isVisible = false;
mLayout.setVisibility(View.GONE);
}
}
});

Categories

Resources