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
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 am struck at a point in my app. I have 50 buttons on my app. I want to show a text as long as a button is pressed and hide it on its release. Also, on pressing another button, the text should change and hide on its release, and so on. The position of the text remains the same. (Also, can we set a position of the text using X and Y co-ordinates?)
Below is my XML
<?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" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/NUMBERS"
android:textColor="#ecaa00"
android:textSize="28sp"
android:padding="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="3dip">
<Button
android:id="#+id/one"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/ONE" />
<Button
android:id="#+id/two"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/TWO" />
<Button
android:id="#+id/three"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/THREE" />
<Button
android:id="#+id/four"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/FOUR" />
<Button
android:id="#+id/five"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/FIVE" />
</LinearLayout>
</LinearLayout>
PS: There are many buttons and setting visible properties to each and every button and hiding them on release would take numerous lines of codes. Is there a better way to achieve the same? For example referencing the button which, on pressed shows the text and hides on release.
ok lets say that following is your textView
TextView textView = (TextView) findViewById(R.id.textView);
you can implement your requirement like this.
textView .setOnTouchListener(show_text);
and implementation for the show_text is as followed.
here you i am changing the type of text for the given textView, but if you want you can the visibility of View visible/invisible on Touch_Down/Touch_Up respectively
private View.OnTouchListener show_text = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.one) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
make_Toast("you can implement your own action here when the button is pressed");
textView.setTransformationMethod(null);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
textView.setTransformationMethod(new PasswordTransformationMethod());
}
return true;
}
return false;
}
};
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"
/>
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 two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?
public void onRadioButtonClick(View v)
{
RadioButton button = (RadioButton) v;
boolean checkBox1Selected;
boolean checkBox2Selected = false;
Toast.makeText(MainActivity.this,
button.getId()+ " was chosen."+R.id.wificheckBox,
Toast.LENGTH_SHORT).show();
if ( button.getId() ==R.id.wificheckBox) {
// Toggle status of checkbox selection
checkBox1Selected = radiowifiButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox2Selected) {
radiomobileButton.setChecked(false);
checkBox2Selected = false;
}
else if (button.getId() ==R.id.wifimobilecheckBox) {
// Toggle status of checkbox selection
checkBox2Selected = radiomobileButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox1Selected) {
radiowifiButton.setChecked(false);
checkBox1Selected = false;
}
}
}
main xml
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp">
<LinearLayout
android:id="#+id/ll_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wifimobilecheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:checked="true"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi or mobile network"
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_2"
android:paddingTop="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wificheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi "
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
</RadioGroup>
Drawabe- button_radio
<item android:state_checked="true"android:state_pressed="false" android:drawable="#drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="#drawable/checkbox"/>
If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.
As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rb_female"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:button="#drawable/selector_radio_button"
android:text="Female" />
<RadioButton
android:id="#+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/selector_radio_button"
android:text="Male" />
</RadioGroup>
Setting the android:checkedButton="#+id/rb_female" worked for me in RadioGroup.
You should remove your onRadioButtonClick method and add OnCheckedChangeListener to your radiogroup. http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
I guess by implementing your own click handler your are overwriting some functionality of the radiogroup.
Your requirement is not clear. Would you mind to make it simpler. I tried my best to understand your requirements and these are the findings given below :(.
A radio button can have text. So you don't have to add another TextView in your layout. Please remove them and add a simple android:text="blah.. blah.." to your radiobutton.
With my understanding you have two doubts.
How to customize RadioButtons with custom drawables??
Checkout the answer here.
How to deselect the previously selected radiobutton??
The truth is you don't have to do it manually as you are keeping those radiobuttons into a single radiogroup.
Hope this will help you.
To check/uncheck radio buttons not part of a RadioGroup,
add a method in the android GUI designer under the
RadioButton Properties 'onClick' (In this example radio1_onClick).
In your activity.xml under the xml for the RadioButton
RadioButton
[blah blah blah]
android:id="#+id/radio1"
android:onClick="radio1_onClick"
In your activity_main.xml you would write the following method.
private void radio1_onClick(View v)
{
CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
b.setChecked(true); /* light the button */
//b.setChecked(false); /* unlight the button */
}