Custom View in android/ - android

I need to make a custom view as shown in diagram, which needs to functional like radio button, any of the options needs to be selected. How can I achieve this? I don`t want to use series of buttons.

You can use RadioGroup and RadioButtons to achieve this. For example,
<RadioButton
android:id="#+id/custom_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:button="#null"
android:background="#drawable/button_custom"/>
Where button_custom is the background image of the button shown. This image should have all the variations (like, normal, pressed, enabled, disabled, selected).

Related

Is there a way to adjust the size of a drawable next to a radio button?

I like to give users a choice between three options. I am using a radio group and three radio buttons. I also like to have an image next to each radio button. I have figured out I can use a drawable like the xml code below:
<RadioButton
android:id="#+id/radioButtonMom"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableStart="#drawable/mom"
android:onClick="onRadioButtonClicked"
android:text="#string/mom"
android:textSize="32sp"
android:textStyle="italic" />
However, I can't adjust the size of the drawable. So, for smaller screens, things don't look right. Is there a way I can add an image to a radio button and still be able to set the size of the image(in dp)? Can I do this while I still have the radio buttons in a radio group, so that only one can be selected at any give time?
Thanks

android: different textvies and edittexts depending on radiobuttons

I'm starting to learn a bit about android programming and watch plenty of guides/tutorials but there's one question none of the guides answer. I have a radio group of two buttons, simply like this(in the xml):
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="button1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
</RadioGroup>
The thing is that i want to show different textviews and different edittexts depending on what radio button is clicked, does anybody know where i can find a good example about this or might even wanna make an example yourself?
Thanks
You can simply wrap your textview's of type1 with some parent View like LinearLayout and so on for the other textview's types.
Than simply onCheckedChanged(RadioGroup arg0, int checkedId) show and hide the wrappers views.
The easiest way to do it is to just add everything to your main view. Then, depending on which is clicked, set the visibility of all the items you don't want to see to View.GONE, and those you do to View.INVISIBLE. Or if you have something more complex and the two views don't share a lot of variables, implementing them as fragments may make sense.

implement android:button="#drawable/checkbox" programmatically

I am trying to create custom Check Box button image. After some research, I came across this code sample:
<CheckBox android:id="#+id/chkFav" android:layout_width="wrap_content"
android:layout_marginRight="0dp" android:button="#drawable/checkbox"
android:layout_height="wrap_content" android:clickable="true"/>
My query is how to actually implement android:button in code.
setButtonDrawable(Drawable d) is the way to go for. Make sure it is state-list drawable to respond to user interaction.
Step By step instruction
Have at least 2 images. (one for checked state and another for normal state)
Create xml drawable. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Use setButtonDrawable(R.drawable.your_xml_drawable).
**Notes-- there are many ways to achieve. This is just one simple way to do it.

Removing Android ToggleButton's green indicator light

I would like my app to have a day of week selector with multiple day selection capability. I'm using ToggleButtons for this right now, but because of the indicator lights a ToggleButton takes too much space on the screen. Without the lights, my ToggleButtons would look like normal (toggleable) Buttons and they could fit in one row. How can I hide the lights?
The answer is this part:
android:background="#android:drawable/btn_default"
For example, following will make the light disappear and toggle button would look like a default button, but with the toggle functionality:
<ToggleButton android:id="#+id/your_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOn="On" android:textOff="Off"
android:background="#android:drawable/btn_default" />
You can use a custom android:background drawable for them, that will override the graphics including the indicator light. If you look at Android: using framework drawables in custom button selector, there's instructions for copying resources from the SDK to your own project. You presumably could copy the platform normal button drawable and use that as your background.

Radio Button does not toggle its state

I add a RadioButton in my layout.
It is unchecked to begin with. And when I click on it , it becomes checked (as shown in emulator). But when when i click on it again, it does not become unchecked again?
<RadioButton android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"/>
If you're looking for checkbox behavior with radio button appearance, you could pass in the xml style to a checkbox.
<CheckBox android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"
style="#android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton/>
This can be useful in some cases (ex. using radio buttons in a RecyclerView) but you should be careful because the user expects radio buttons to behave a certain way. If you're allowing the user to make multiple selections you should probably use a normal checkbox, as mentioned in the comments above.
Hope this helps!
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
When using radios you usually have more than one and choose between them. Like easy, medium, hard.
If you are having more than 1 radio buttons to work with then add "RadioGroups" as follows:
<RadioGroup android:id="#+id/group1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="#+id/radio1" android:text="madras"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="#+id/radio2" android:text="bombay"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>
Have a look at this example , i am sure this will make your idea clear regarding Radio Buttons.
Also refer this Android Developer - Form Stuff page .
After searching a lot on SO, I came up with a not-so-good but decent workaround.
Declare a boolean variable for each RadioButton you have, initialize it with false, and change the state of the variable and the RadioButton at every click.
boolean isToggledRadio1 = false;
RadioButton radio1 = (RadioButton) findViewById(R.id.radiobutton1);
radio1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isToggledRadio1 = !isToggledRadio1; //Switch boolean value
RadioButton rb = (RadioButton)v;
rb.setChecked( isToggledRadio1 );
}
});
I know it's ideal to use a Checkbox, but if someone needs the radiobutton, then they need the radiobutton.
This is not an optimal solution, as it will basically toggle the button twice every time the user clicks the button (one is the default behaviour, the second time is inside your onclick function), so if you're using an OnCheckedChangeListener you will probably get two calls for the same click.
There's another workaround, which is to change the android:button in a checkbox to another drawable with an xml template, but it's a bit more complex, requires at least 2 more files for the states.
That is a conceptual question: Radio buttons allow you to choose between several options (represented by the other radio buttons). Typically, one radio button out of a group is always checked, even if in an initial state no buttons may be checked if you do not declare one as a default value. That means also that a single button does not allow to toggle its state unless other radio buttons are present in the same group - if there is only one option, you will have to choose it.
If you want a binary toggle, you will want to use a checkbox instead.
Solution is set checked[true/false] intermediate in Java code
ToggleButton t = (ToggleButton) findViewById(R.id.toggle_button);
t.setChecked(true);
// t.setChecked(false);

Categories

Resources