Checkbox setOnclicListner causes crash in android - android

I am implementing an application, in my application I am trying to show a Popwindow. Whenever a menu item was tapped I am displaying a pop window with title and check box. In that I code I am trying to setonclick listener to the Checkbox, unfortunately this is causes to crash the application.
The following is my code.
In xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="#+id/popup"
android:layout_height="wrap_content"
android:background="#drawable/music_bg"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView android:id="#+id/txtTopTitleForSetInfoInput" android:text="Voice Coaching"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#FFFFFF" android:textSize="21sp" android:gravity="center"
android:textStyle="bold" android:layout_centerVertical="true" />
<Button android:id="#+id/btnCloseControl"
android:background="#drawable/music_close"
android:layout_width="40dip" android:layout_height="40dip"
android:layout_alignParentRight="true" android:layout_centerVertical="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" >
<TextView
android:id="#+id/txtVoiceCoaching"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cbVoiceCoachingOn"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/cbVoiceCoachingOn"
android:gravity="center"
android:text="Voice Coaching"
android:textColor="#FFFFFF"
android:textSize="21sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/cbVoiceCoachingOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/txtVoiceCoaching" />
</RelativeLayout>
Code in on menu item selected:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
super.onMenuItemSelected(featureId, item);
int[] location = new int[2];
ll.getLocationOnScreen(location);
int popupWidth = 300;
int popupHeight = 160;
Point p = new Point();
p.x = location[0];
p.y = location[1];
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) this.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.volume_pop_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(this);
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 = 10;
int OFFSET_Y = -40;
// 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);
Button close = (Button) layout.findViewById(R.id.btnCloseControl);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
CheckBox cbVoiceCoahcingOn = (CheckBox) findViewById(R.id.cbVoiceCoachingOn);
if(cbVoiceCoahcingOn==null)
{
System.out.println("cbVoiceCoahcingOn==null");
}
cbVoiceCoahcingOn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("onclick");
}
});
return true;
}

You made a mistake here:
CheckBox cbVoiceCoahcingOn = (CheckBox) findViewById(R.id.cbVoiceCoachingOn);
Mistake: You are trying to find out View from the main XML layout instead of finding it from the inflate layout for Dialog.
Correct:
CheckBox cbVoiceCoahcingOn = (CheckBox) layout.findViewById(R.id.cbVoiceCoachingOn);

Related

PopMenus with icons

I want to achieve exactly like PopupMenu with icons On layout click which is in Custom Toolbar . So , i have taken the help of an answer but issue i am facing is : Nothing is poping out on click .
POPLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/poplayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/home" />
<TextView
android:id="#+id/tvDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME"
android:layout_marginLeft="20dp"
android:paddingTop="5dp"
android:gravity="center_vertical"
android:textColor="#color/backgroud_user" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_cloud_upload" />
<TextView
android:id="#+id/cloud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHECKIN"
android:layout_marginLeft="20dp"
android:paddingTop="5dp"
android:gravity="center_vertical"
android:textColor="#color/backgroud_user" />
</LinearLayout>
</LinearLayout>
Toolbar.xml(consist of an layout to be clicked)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
android:background="#color/toolbar_color"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview"
android:text="My Assignments"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="90dp"
android:layout_weight="0.5"
android:textSize="20sp"
/>
<LinearLayout(**layout to be clicked**)
android:layout_width="35dp"
android:layout_height="35dp"
android:background="#75aadb"
android:id="#+id/accountinfo_layout"
android:layout_gravity="center"
android:layout_marginLeft="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:src="#drawable/account"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
Code.class
accountInfoLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(point!=null){
int[] location = new int[2];
v.getLocationOnScreen(location);
point = new Point();
point.x = location[0];
point.y = location[1];
showPopup(getActivity(),point);
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.poplayout);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.pop_window, 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 = 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.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
Nothing in showing on click . Please help me in finding Bug .
I think just remove if(point!=null) condition it will work
accountInfoLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
point = new Point();
point.x = location[0];
point.y = location[1];
showPopup(getActivity(),point);
}
}
}

Unable to do Pop menu

I have been trying to do such a pop up menu. However, I am not able to do it since I can not put pictures and give design in pop-up menu. Does any one know to do such a pop menu?
Go to this link. I hope this will help you achieving what you want.
An example for pop up menu is here.
EDIT
Create an PopUpWindow layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llSortChangePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/sort_popup_background"
android:orientation="vertical" >
<TextView
android:id="#+id/tvDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/distance"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:paddingTop="5dp"
android:gravity="center_vertical"
android:textColor="#color/my_darker_gray" />
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sort_popup_devider"
android:contentDescription="#drawable/sort_popup_devider"/>
<TextView
android:id="#+id/tvPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/priority"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupSortOnClick"
android:textColor="#color/my_black" />
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sort_popup_devider"
android:contentDescription="#drawable/sort_popup_devider"/>
<TextView
android:id="#+id/tvTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/time"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupSortOnClick"
android:textColor="#color/my_black" />
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sort_popup_devider"
android:contentDescription="#drawable/sort_popup_devider"/>
<TextView
android:id="#+id/tvStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/status"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:textColor="#color/my_black"
android:clickable="true"
android:onClick="popupSortOnClick"
android:paddingBottom="10dp"/>
</LinearLayout>
and then create the PopUpWindow in your Activity:
// 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);
}
finally pop it it up onClick of a button or anything else:
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);
}
});

Disabling background layouts when clicking a button in android

I have two List-views and a button in my XML.On clicking the button a popup box will appear.So i want to disable the underlying layouts when button is clicked.How can i do that ?
Here is my xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:background="#25232c"
android:layout_height="fill_parent">
<Button
android:id="#+id/show_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
Here is my code for popup box
// Get the x and y position after the button is draw on screen
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.show_popup);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
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)
{
int popupWidth = 550;
int popupHeight = 350;
// 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.datepicker_popup, 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 = 7;
int OFFSET_Y = 65;
// 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.
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener()
{
/* disable(content_view);*/
#Override
public void onClick(View v)
{
popup.dismiss();
}
});
Here is my custom layout for popup window
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/popup"
android:layout_width="308dp"
android:layout_height="224dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="#drawable/popup_bg"
android:orientation="vertical" >
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Close" />
<Button
android:id="#+id/now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Now" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
you can try
listView.setVisibility(View.INVISIBLE); or listView.setVisibility(View.GONE);
when button is clicked
inside onClick of button
LinearLayout linearLayout = = (LinearLayout ) findViewById(R.id.main_layout);
linearLayout.setEnabled(false);
Kick-off example:
final LinearLayout ll = (LinearLayout) findViewById(R.id.main_layout);
Button btn = (Button) findViewById(R.id.show_popup);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ll.setVisibility(View.GONE);
}
});

How to auto scale Popup Window Android

Can someone help me. I have a popup window and how can i autoscale it for every resolution of android devices it must be in the center of my application?
This is my main.java
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.show_popup);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
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) {
int popupWidth = 230;
int popupHeight = 300;
// 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.
// Clear the default translucent background
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.CENTER, 0,0);
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) layout.findViewById(R.id.filter);
close.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
popup.getContentView();
}
});
}
This is my popup_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/popup_bg"
android:orientation="vertical" >
<Button
android:id="#+id/filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/CheckBox04"
android:layout_marginTop="103dp"
android:text="Filter" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/CheckBox03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/CheckBox01"
/>
</RelativeLayout>
If you do android:gravity="center" for the layout of your pop-out window, it should center for all devices

1px line at left side of popup [duplicate]

I have a problem when creating a popup for my app. I'm trying to show a popup that fills the total size of my screen. The problem is that i'm getting 1px line at the left side that isn't filled.
my popup xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/popup"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/popup_round_corners"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="#string/zm_main_uvod" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="Ok" />
</LinearLayout>
</LinearLAyout>
and my function to show popup:
private void showPopup(final Activity context) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout)findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.startpopup, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
popup.setWidth(width+5);
popup.setHeight(height+5);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.FILL, 0, 0);
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
I tried with gravity.center and try setting offsets to -30,-30 but nothing happend. Any ideas?

Categories

Resources