In my MainActivity class, I have this button:
<Button
android:id="#+id/target"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/speak"
android:layout_marginTop="20dp"
android:background="#drawable/button_red"
android:text="#string/target" />
I now want to add a badge with the number 1, using Material Design (see the documentation).
This is how I tried:
public void setBadge() {
BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
badgeDrawable.setNumber(1);
BadgeUtils.attachBadgeDrawable(badgeDrawable, findViewById(R.id.target));
}
but what I get is the error
'attachBadgeDrawable(com.google.android.material.badge.BadgeDrawable, android.view.View, android.widget.FrameLayout)' in 'com.google.android.material.badge.BadgeUtils' cannot be applied to '(com.google.android.material.badge.BadgeDrawable, android.view.View)'
If you tried this on a device with <18 API this might work;
In your XML wrap the button with a FrameLayout;
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="HELLO"/>
</FrameLayout>
In your Activity;
//I've used post to eliminate race condition between the button and its badge.
button.post(() -> {
BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
badgeDrawable.setNumber(1);
//Note that there is a third argument which is our FrameLayout
BadgeUtils.attachBadgeDrawable(badgeDrawable, findViewById(R.id.button), findViewById(R.id.frame));
});
Related
I am new in Android programming. Could any tell me way to solve the following issue?
I make a XML file with following codes:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/activity_splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context="com.royalrandhawa.xemp.SplashScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true>
<EditText
android:id="#+id/mainSearch"
android:layout_width="300dp"
android:textColor="#373737"
android:textColorHint="#bfbfbf"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="This is your field...."
android:background="#drawable/mainsearch"
/>
<Button
android:text="Surf Now!"
android:id="#+id/surfNowBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:text="Filter Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/filtersearch"
android:layout_weight="1"
android:checked="false"/>
<RadioButton
android:text="Trace my activities"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/traceme"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
Layout details are:
user enter his query in EditText and there is some other option like Filter Search and Trace Activities in RadioButtons.
I want these:
After Click on Search button, get value of EditText and check if either or both radiobuttons are selected then make two variable with fsVr and tmVr and set there values to true. and start new activity name Result
After that send these values to a URL with HTTP POST method.
and show the server response in TextView in Result (Activity)
Thank You!
First your xml is wrong. It should be like that
You need to create two different RadioGroup and both have only one RadioButton because of your requirement is either or both radiobuttons are selected
And java code is here
private void initview1() {
RadioButton filtersearch = (RadioButton) findViewById(R.id.filtersearch);
RadioButton traceme = (RadioButton) findViewById(R.id.traceme);
Button Search_Buttom = (Button) findViewById(R.id.Search_Buttom);
EditText mainSearch = (EditText) findViewById(R.id.mainSearch);
Search_Buttom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Boolean fsVr = false, tmVr = false;
String search_data = mainSearch.getText().toString();
fsVr = filtersearch.isChecked();
tmVr = traceme.isChecked();
// go to your new activity with required data..
}
});
}
my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below.
The three choices are:
1. [] Male
2. [] Female
3. [] Custom: (self-described identity)
However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.
So the following code is from my XML page, with some elements blocked out by "####".
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="#+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_male"
android:checked="true" />
<RadioButton android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
As you can see, I have tried to use a LinearLayout to isolate the custom option.
However, there are unintended and undesired side effects.
1. The custom option can be selected in addition to the other 2 predefined genders.
2. The custom option cannot be selected on its own.
In the actual Java file for the activity, I have the following code:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.
Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.
EDIT: Here's a picture of what I want it to look like:
(http://i68.tinypic.com/ao2oow.png)
Unfortunately I need 10 reputation to post images. :(
Mohit's answer gives the EditText on a different line than the custom input.
(http://i63.tinypic.com/68u88x.png)
Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.
Because selectedId will not be 1,2 or 3....debug it you will get value..
The custom option cannot be selected on its own.
Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..
On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...
I dont no what is your task but here is how can get all three RadioButton working and get custom text too....
xml...
UPDATE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ex.MainActivity" >
<RadioGroup
android:id="#+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/male_female_custom_choice"
android:layout_toRightOf="#+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="#+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="#+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java file..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
you can also set visibility of LinearLayout so that it only visible when custom in checked....
Hope it help..
put radio group in RelativeLayout
set third radiobutton text as empty/null
add EditText as layout_alignParentBottom="true"
now programmatically call EditText's onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b)
thirdRadio.setCheched(true);
}
});
I have two layouts. I want to keep one layout gone when the activity is loaded and it should be visible onClick of another layout. so I have added this code OnClickListener of the layout.
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutFrom.getVisibility() == View.GONE){
linearLayoutFrom.setVisibility(View.VISIBLE);
}else{
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
And have set visibility of the layout gone in xml file..
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayoutAdditionalContactFrom">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView13"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_person_black_48dp"
android:layout_marginTop="05dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_marginRight="10dp"
android:drawableRight="#drawable/ic_expand_more_black_24dp"
android:text="Additional contact (optional)"
android:cursorVisible="false"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:layout_gravity="center"
android:visibility="gone"
android:id="#+id/LinearLayoutFrom">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_weight="1"
android:hint="Name"
android:layout_gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText3"
android:layout_weight="1"
android:hint="Phone"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="OR"
android:id="#+id/textView19"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView13"
android:layout_marginLeft="48dp"
android:hint="Input if you're not receiver" />
</LinearLayout>
Unable to understand whats going wrong.. The listener is not getting called at all.. Please help..
Your problem is that your EditText is capturing the click event when you click on it. If you click somewhere else in the LinearLayout it should work.
You can replace the EditText with a TextView if you don't need the user to edit the content.
Change
linearLayoutFrom.setVisibility(View.VISIBLE);
To
findViewById(R.id.YOURLAYOUTID).setVisibility(View.VISIBLE);
I think you can't access to local vars in sub methods
It Seems That Layout Visibility is Already set to GONE, so Onlick listener is not working on hidden View and it should not work too.
INVISBLE means you are trying to add a listener to a view which is not there. You can add the listener to a visible view only.
WORKAROUND
1) Try to make a dummy view which is visible but having the same color as background.
2) Try to set the listener for parent and check the position (whether the position does
belongs to INVISIBLE view).
Please try to set the onClickListener to the EditText and to the ImageView and no to the LinearLayout
The problem is that the Handler of the EditText is most important that the LinearLayout handler.
Almost you can try to make a break point to the OnClick and see what is happend
This is an example to explain you how to do that:
in MainActivity Class:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private LinearLayout linearLayoutFrom;
private LinearLayout additionalContactFrom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
additionalContactFrom = (LinearLayout) findViewById(R.id.LinearLayoutAdditionalContactFrom);
linearLayoutFrom = (LinearLayout) findViewById(R.id.LinearLayoutFrom);
linearLayoutFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"linearLayoutFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (additionalContactFrom.getVisibility() == View.GONE) {
additionalContactFrom.setVisibility(View.VISIBLE);
} else {
additionalContactFrom.setVisibility(View.GONE);
}
}
});
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"additionalContactFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (linearLayoutFrom.getVisibility() == View.GONE) {
linearLayoutFrom.setVisibility(View.VISIBLE);
} else {
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
}
}
in xml file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayoutAdditionalContactFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayoutFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:background="#android:color/holo_green_light"
android:orientation="vertical"
android:visibility="gone" >
</LinearLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:hint="Input if you're not receiver"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
This is very important that when you want add some view (for example
add a linearlayout to another linerlayout). you should use framelayout or
relativelayout(do not use linearlayout) for do that.
I have a page that returns a list of items backs from a database. I want to add each item to my android fragment as a checkbox dynamically with an onClick, that can tell if an item is being checked or un-checked.
How can I add checkboxes dynamically with on-clicks and different titles for each?
Below is the xml I am inserting the checkboxes into:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#e5e5e5"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="#drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="#+id/styleDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox" />
</LinearLayout >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="horizontal"
android:background="#drawable/bg_card">
<!-- Card Contents go here -->
<Button
android:id="#+id/buttonAddList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create List"
style="?android:attr/borderlessButtonStyle"
android:textColor="#color/orange"
android:textStyle="bold"
/>
</LinearLayout >
</FrameLayout>
</LinearLayout>
</ScrollView>
I currently have one checkbox in the above code. I plan on removing this. That checkbox is just to show where I want my check boxes to show up.
What you need to do first is add an id to your LinearLayout (in that XML file), the one which is going to hold the CheckBoxes. Then, in the code you need to get that LinearLayout by its id and use addView() to add CheckBoxes that you create dynamically. I imagine in pseudocode it'd look like this:
for (int i = 0; i < numberOfCheckBoxes; i++) {
CheckBox checkBox = new CheckBox();
checkBox.setTitle("Your title");
checkBox.setOnClickListener(new OnClickListener() {
// Your code to be executed on click
});
linearLayout.addView(checkBox);
}
Does this help?
PS: It'd be nice if you kept your code clean - ADT (and I believe Eclipse too) gives you the Shift+Ctrl+F shortcut to indent your code automatically - use it as often as possible ;)
Since you are processing database items, I suggest using a CursorAdapter to do the heavy work for you. A CursorAdapter, like any of the Adapter classes can process the database items and custom-fit them into a layout of your choice, to use in a ListView.
You have to make adjustments to your code:
Create a layout file that contains whatever you want to put in the dynamic list. This is an example, say it's named list_contents.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="#drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="#+id/styleDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox" />
</LinearLayout >
</FrameLayout>
Then, instead of returning a List from your AsyncTask, return the Cursor itself
from your database. This Cursor will be processed by CursorAdapter. I recommend this guide:
http://www.gustekdev.com/2013/05/custom-cursoradapter-and-why-not-use.html
Implement the CursorAdapter methods:
In your implementation of newView(), inflate list_contents.xml (Note that if you use ResourceCursorAdapter you wouldn't need to do this)
In your implementation of CursorAdapter.bindView() do this:
CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox);
checkbox.setText(cursor.getString(cursor.getColumnIndex(YOUR_DATABASE_COLUMN_NAME_FOR_CHECKBOX_VALUES)));
checkbox.setOnCheckedChangedListener(listenerInitializedSomewhereFromFragmentCode);
Change your ScrollView to a ListView (it can be inside any Layout), and give it an id, say R.id.listview.
Finally, in the part where you process the List from the database, where we now have a Cursor instead, just do this:
CustomCursorAdapter cca = new CustomCursorAdapter(getActivity(), resultFromDatabase, 0);
listView.setAdapter(cca);
Note: getActivity() is for when you are working inside a Fragment. It should be a context, so inside an Activity it can just be "this".
Note2: listView should have been initialized at this point via findViewById.
Note3: If listView already has an Adapter and Cursor set, you should consider calling listView.getAdapter().changeCursor() instead.
Simple Code In Kotlin
fun createCheckbox() {
var arr_cb = arrayOfNulls<CheckBox>(checkbox_size)
val layout = findViewById<View>(R.id.layout_checkbox) as ViewGroup
val ll = LinearLayout(this)
ll.orientation = LinearLayout.VERTICAL
for (i in 0 until arr_cb.size) {
arr_cb[i] = CheckBox(this)
arr_cb[i]?.text = health_status.get(i).toString()
arr_cb[i]?.setPadding(25, 0, 0, 0)
arr_cb[i]?.id = i
arr_cb[i]?.tag = health_status[i]
arr_cb[i]?.setTextColor(resources.getColor(R.color.title_color))
arr_cb[i]?.setOnCheckedChangeListener(
arr_cb[i]?.let {
handleCheck(it)
})
arr_cb[i]?.buttonTintList =
ColorStateList.valueOf(resources.getColor(R.color.theme_color))
ll.addView(arr_cb[i])
}
layout.addView(ll)
}
handleCheck method
private fun handleCheck(chk: CheckBox): CompoundButton.OnCheckedChangeListener? {
return object : CompoundButton.OnCheckedChangeListener {
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
if (!isChecked) {
//uncheck
} else {
//check
}
}
}
}
and you want to do something use direct checkboxList object like as
val layout = findViewById<View>(R.id.layout_checkbox) as ViewGroup
val ll = LinearLayout(this#MedicalHistoryActivity)
ll.orientation = LinearLayout.VERTICAL
for (i in 0 until health_status.size) {
arr_cb[i]?.isEnabled = true
// do something like change color or more
}
layout.addView(ll)
for enable checkbox or more.
Thank you
How to disable radio buttons group after clicking on some radio button in Android activity ?
This is my XML code
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tPitanje1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1. Neposredno regulisanje saobracaja na putevima vrse:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tPitanje1"
android:layout_marginLeft="20dp"
android:layout_marginTop="62dp" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="uniformisani komunalni policajci" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unoformisani policijski sluzbenici" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inspektori za drumski saobracaj" />
</RadioGroup>
</RelativeLayout>
And this is my Java Code
package com.example.autoskola;
import android.app.Activity;
import android.os.Bundle;
public class obs1 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.obs1);
}
}
I'm interested in how to disable to change to click on another radiobutton?
If I'm understanding correctly, you want to disable the RadioGroup once a certain RadioButton is clicked. Just add an OnCheckedChangedListener and if the id of the RadioButton clicked is the right one, then disable the group by using setEnabled(false). Of course, you can't reenable the RadioGroup unless you have some other logic in your code that will do the re-enabling.
RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1);
radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if (checkedId == R.id.radio0)
group.setEnabled(false);
}
});
Edit: It seems that setEnabled() doesn't work, so you should try changing your code so it loops through each RadioButton upon checking radio0:
if (checkedId == R.id.radio0){
for(int i = 0; i < group.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(false);
}
}