Hi I have written simple custom dialog . which has few check boxs and one submitt button .
whenever I tried to read the checkbox apllication throws Nullpointer exception .. can somebody helps to solve this , below is my custom dailog code
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
CheckBox chk1= (CheckBox) findViewById(R.id.chkbox1);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(chk1.isChecked())
dialog.dismiss();
}
});
dialog.show();
}
NullPointerException because you didn't instantiated with dialog.findViewById() and set OnClickListener for the CheckBox. Place it as below:
CheckBox chk1= (CheckBox)dialog.findViewById(R.id.chkbox1);
chk1.setOnClickListener(new OnClickListener() {
//do something here
});
Change:
CheckBox chk1= (CheckBox) findViewById(R.id.chkbox1);
to:
CheckBox chk1= (CheckBox) dialog.findViewById(R.id.chkbox1);
Remember that if you're simply using findViewById(), you're calling it for Activity in which you currently are, but as far as I see, you want to find this CheckBox in R.layout.custom which is set for dialog.
I see that you're properly loading dialogButton, so you probably just forgot to do the same with chk1.
When you have populated a layout for dialog then you need to access it through dialog. But you are accessing it through parent view. Anyway just call it through dialog.findViewById(R.id.chkbox1)
I am trying add radio group to the custom dialog .. It comes when dialog is loaded , but how to add the action lister to that radio group in the dialog .. below is my custom layout xml .
<?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"
android:theme="#android:style/Theme.Light">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
/>
<CheckBox
android:id="#+id/chksmart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMARTAPPLIANCE "
android:layout_below="#+id/editText"
/>
<CheckBox
android:id="#+id/meter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Device"
android:layout_below="#+id/chksmart"
/>
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/android:list"
android:textColor="#android:color/black" >
<RadioButton
android:id="#+id/radioGet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="GET"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioPut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="PUT"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioPost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="POST"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioDelete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="DELETE"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioevent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="ADDEVENT"
android:textColor="#android:color/background_dark" />
</RadioGroup>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" SUBMIT "
android:textColor="#00000f"
android:textSize="25px"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/radiogroup"
/>
Related
I am trying to create a ripple when my button is clicked by using the RippleEffect project, however every time I run the code the app crashes. How do I fix this? Alternatively, is there an easier way to get a ripple when a button is clicked? Please help if possible. Thank you!
<RelativeLayout
android:id="#+id/topPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.andexert.library.RippleView
android:layout_width="match_parent"
android:layout_height="95dp">
// Button 1
<Button
android:id="#+id/name1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="95dp"
android:gravity="center"
android:background="#drawable/button_border"
android:textSize="17dp"
android:textAllCaps="false"
android:textStyle="normal|bold" />
// TIME 1
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/name1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="4dp"
android:textSize="18dp"
android:textColor="#android:color/darker_gray"
android:elevation="2dp"
android:id="#+id/editText1" />
// NAME 1
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text=" "
android:elevation="2dp"
android:layout_alignBaseline="#+id/name1"
android:layout_alignBottom="#+id/name1"
android:layout_centerHorizontal="true"
android:cursorVisible="false" />
</com.andexert.library.RippleView>
</RelativeLayout> // End Left Side
Just to check the sample codes
View that imitates Ripple Effect on click which was introduced in Android L.
For a working implementation, Have a look at the Sample Project - RippleViewExample
Include the library as local library project.
Include the RippleView widget in your layout.
In your onCreate method refer to the View and add 'OnClickListener' for the same.
mButton = (RippleView) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code
}
});
I'm sorry for this question but i'm confused about it.
I have two buttons like this:
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
And in the Android code :
a = (Button) findViewById(R.id.b1);
b = (Button) findViewById(R.id.b2);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//my code
}
});
When I change the arrangement of the buttons to look like this:
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
then the a button doesn't listen. The listener is associated to the b button!
You might have changed the ids while rearranging.
<Button
android:id="#+id/b2"
/>
<Button
android:id="#+id/b1"
/>
Check that, and clean + build project. Hope it helps.
When you rearrange the buttons the listener will move to the second button,
So when you will click the button with text = 2 you wouldn't get anything,
and when you click the button with text = 1 you will have the action assigned.
Otherwise in the fist case, the 2 button will respond and not the 1
I have a program which computes perimeter, circumference, an area of different shapes like, square, circle, rectangle and soon. Any help with enabling the radiobutton to display an edittext to write the measurements.
check below code don't use as it is, its just reference
inyourxml.xml
<RadioGroup
android:id="#+id/groupRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_big_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:gravity="center"
android:minHeight="30dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/circumferenceRadio"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/tab_selector_background"
android:button="#null"
android:checked="true"
android:gravity="center"
android:text="#string/choice_yes"
android:textColor="#drawable/tab_selector_text" />
<RadioButton
android:id="#+id/perimeterRadio"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/tab_selector_background"
android:button="#null"
android:checked="false"
android:gravity="center"
android:text="#string/choice_no"
android:textColor="#drawable/tab_selector_text" />
</RadioGroup>
<EditText
android:id="#+id/perimeterEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_normal_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:background="#drawable/action_bar_background"
android:inputType="textPassword" />
<EditText
android:id="#+id/circumferenceEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_normal_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:background="#drawable/action_bar_background"
android:inputType="textEmailAddress" />
inyourjava.java
RadioGroup groupRadio=(RadioGroup)rootView.findViewById(R.id.groupRadio);
EditText perimeterEditText=(EditText)rootView.findViewById(R.id.perimeterEditText);
EditText circumferenceEditText=(EditText)rootView.findViewById(R.id.circumferenceEditText);
groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.circumferenceRadio)
{
circumferenceEditText.setVisibility(View.VISIBLE);
perimeterEditText.setVisibility(View.INVISIBLE);
}
else if(checkedId==R.id.perimeterRadio)
{
perimeterEditText.setVisibility(View.VISIBLE);
circumferenceEditText.setVisibility(View.INVISIBLE);
}
}
});
Hide a EditText & make it visible by clicking a menu
in case of menu ... you can declare setvisibility in xml and make it hide... then programatically make it visible...hope this helps
I have a row of buttons that I want to populate a listview. That looks like this:
Button1 | Button2 | Button3 | Button4
List Item 1
List Item 2
List Item 3
If I press Button1, I will show one view and if I select Button2, I will show another view. I have the listview working with no button selected. However, I want to select a button and populate a listveiw based on the button selected. This is only my 2nd app so I may be going about it the wrong way.
Here is my layout file:
<LinearLayout
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_width="match_parent"
>
<VideoView
android:id="#+id/video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal" >
<Button
android:id="#+id/chapters"
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/chapters" />
<Button
android:id="#+id/scales"
android:layout_width="100dp"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/scales" />
<Button
android:id="#+id/b_flat"
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/b_flat" />
<Button
android:id="#+id/e_flat"
android:layout_width="75dp"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/e_flat" />
<Button
android:id="#+id/concert"
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/concert" />
<Button
android:id="#+id/bass"
android:layout_width="75dp"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/bass" />
<Button
android:id="#+id/video"
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/video" />
<Button
android:id="#+id/practice"
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_layout_height"
android:textSize="#dimen/text_size"
android:background="#drawable/button_custom"
android:text="#string/practice" />
</LinearLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
I want to select the "Chapters" button and populate the list button or select "concert" and populate the listview.
I don't know where or how to set the button pressed. Here is what I tried unsuccessfully:
public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";
private Button mChaptersButton;
#Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_improvisation);
mChaptersButton = (Button)v.findViewById(R.id.chapters);
mChaptersButton.setPressed(true);
getActivity().setTitle(R.string.chapters_title);
mChapters = ChapterList.get(getActivity()).getChapters();
ChapterAdapter adapter = new ChapterAdapter(mChapters);
setListAdapter(adapter);
}
This didn't work because I can't use SetContentView in ListFragment. Also, the buttons didn't have focus. Any suggestions would be greatly appreciated
create common listview adapter and populate the listview with different data set on each button click. Make sure to call your adapter.clear() so that the old data from listview will disappear. I hope you got the idea. For setContentView, non of ListFragment and Fragment parent have thesetContentView()` method. You should have an Activity with a Layout and set your ListFragment there. rest check this link Fragment setcontentView Error
I have an Activity that does the following
Button distanceSort = (Button) findViewById(R.id.distanceSortButton);
Button waitTimeSort = (Button) findViewById(R.id.waitTimeSortButton);
Button nameSort = (Button) findViewById(R.id.nameSortButton);
Button mapButton = (Button) findViewById(R.id.locationsMapButton);
Button dashboardButton = (Button)findViewById(R.id.dashboardButton);
RadioGroup sortingGroup = (RadioGroup)findViewById(R.id.sortingRadioGroup);
and the xml is this
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sortingRadioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/headerBar"
android:layout_centerHorizontal="true"
android:checkedButton="#+id/distanceSortButton"
android:orientation="horizontal" >
<RadioButton
android:id="#id/distanceSortButton"
style="#style/tabText"
android:layout_weight="1"
android:background="#drawable/radio_group_selector"
android:button="#null"
android:checked="true"
android:gravity="center"
android:text="#string/distance_sort_button" />
<View
android:layout_width="1dp"
android:layout_height="48dp"
android:background="#drawable/tab_separator" />
<RadioButton
android:id="#+id/waitTimeSortButton"
style="#style/tabText"
android:layout_weight="1"
android:background="#drawable/radio_group_selector"
android:button="#null"
android:gravity="center"
android:text="#string/wait_time_sort_button" />
<View
android:layout_width="1dp"
android:layout_height="48dp"
android:background="#464646" />
<RadioButton
android:id="#+id/nameSortButton"
style="#style/tabText"
android:layout_weight="1"
android:background="#drawable/radio_group_selector"
android:button="#null"
android:gravity="center"
android:text="#string/name_sort_button" />
</RadioGroup>
What my problem is that all the buttons are found, but the radio group always returns null. I want to use that RadioGroup to find out which button has been checked. Doesn't make sense to me that the buttons inside radio group can be found but the RadioGroup itself cant' be?