i have apk where on start i show one layout and on the top of is radio buttonst which should on check change layout its something like on start is shown layout PC and on click it should change to Playstation or Xbox
in the evening i will post source code, but now how to make it work ? i found few solutions but nothing work
Why not create new actitivies for different layout files? However try this maybe it will help you.
CB_Event.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
setContentView(R.layout.YOURfirstLayout);
} else {
setContentView(R.layout.YOURSecondLayout);
}
}
});
In this example i used CheckBox you can use RadioButton too.
Related
I have been racking my brain and scouring many posts, but can't seem to find the answer to the dreaded state of the Expandable ListView/Checkbox problems. I can't get this to save the state. With ListView, I created a ListArray which would add and remove the position accordingly. I thought that the way around this would be to create a HashMap to do exactly the same, but it doesn't work.
Does anyone have an example of this working and the state of the checkboxes being saved?
Here is it working in ListView, but I need something similar with a HashMap -
private ListArray<Integer> checkedState = new ListArray<Integer>();
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checkedState.add(childPosition);
holder.checkbox.setSelected(true);
//Toast.makeText(_context,holder.txtListChild.getText() + " Selected",Toast.LENGTH_SHORT).show();
}
else{
holder.checkbox.setSelected(false);
checkedState.remove(childPosition).
//Toast.makeText(_context,holder.txtListChild.getText() + " UnSelected",Toast.LENGTH_SHORT).show();
}
}
});
if(checkedState.contains((Integer)childposition) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
I ended up not using setOnCheckedChangeListener and using onClick which works great.
The answer can be found here - Expandable listview with selectall checkbox : group itemclick and scrolling bug
I have 45 check boxes that you can check. What I want to do is if you try to select more than 6 check boxes it will automatically disable all the buttons, however, if you tap the even one of the checked checkbox ,it will make all the checkbox checkable. This sounds simple ,but I cannot implements this method. I would be grateful if the pros here can help a noob like me. Here is the sample code.
checkbox[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) { buttonView.setTextColor(Color.GREEN);
buttonClicked.add(buttonView.getText().toString());
buttonView.setTextSize(18);
count+=1;
if(count>=6){
for(int i = 0; i< 43;i++){
checkbox[i].setEnabled(false);
stopped = checkbox[i].isChecked();
if(stopped==true){
for(int a = 0; a < checkbox.length;a++){
checkbox[a].setEnabled(true);
}
}
}
}
}
if (!isChecked) {buttonView.setTextColor(Color.BLACK);
buttonClicked.remove(buttonView.getText().toString());
buttonView.setTextSize(15);
count-=1;
Your issue here is your reaction to finding a checked checkbox. We need to look at removing the internal loop under if(stopped==true)(Note 2).
You simply need
if(stopped){
checkbox[i].setEnabled(true);
}
Then in your if(!isChecked)(Note 3) you add your loop back in to reenable all the checkboxes so it will look like
if(!isChecked){
//your existing code
for(int i=0;i<checkbox.length;i++){
checkbox[i].setEnabled(true);
}
}
Note 1: I would advise that you swap your hardcoded "43"to checkbox.length just to keep things cleaner.
Note 2: You don't need to put ==true, it's already a boolean so this can just be if(stopped)
Note 3: This is what "else" was designed for. if(...){} if(!...){} is synonymous with if(...){}else{}.
Note 4: To void unnecessary looping (always good practice) we should maybe add another check here before the for loop to ensure that there were 6 boxes active.
if(count>=6){
for(int i=0;i<checkbox.length;i++){
checkbox[i].setEnabled(true);
}
}
count--;
Note 5: x+=1; can be replaced by x++; and similarly for x-=1; x--;(as said in the comments to your question)
friends. I'm having a really silly problem with radiogroup. Yet I'm unable to find solution.
I will try to describe how to reproduce my problem:
I have radiobutton group and two buttons inside.
I select one of them, lets say 1st one.
Then I'm clearing selection by calling radioGroup.clearCheck()
After I'm trying to select 1st button, but its not checking.
If I check 2nd, it checks normally.
If I check 1st after checking 2nd it also works normally.
This may sound crazy, yet I can't fix it. Please help me, thanks in advance.
I use
#Override
protected void init() {
View view = View
.inflate(getContext(), R.layout.wo_task_yn_result, null);
performed = (RadioButton) view.findViewById(R.id.yn_yes);
notPerformed = (RadioButton) view.findViewById(R.id.yn_no);
radioGroup = (RadioGroup) view.findViewById(R.id.yn_options);
performed.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView,
final boolean isChecked) {
Log.d(YES, "verify");
if (isChecked) {
Log.d(YES, "checked");
result = YES;
}
}
});
notPerformed.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView,
final boolean isChecked) {
Log.d(NO, "verify");
if (isChecked) {
Log.d(NO, "checked");
result = NO;
}
}
});
addView(view);
}
To create buttons and
#Override
public void clear() {
radioGroup.clearCheck();
result = "";
}
for clearing them
I had the same problem. Was unable to select the first radioButton in the group, but could select the other two. And after selecting the second radioButton, I could select the first one as well. I resolved it by doing a single radioGroup.clearCheck() instead of individual radioButtonA.setChecked(false)
Use OnCheckedChangeListener and make use of the method setSelected(true) or setChecked(true).
if u want to uncheck radio button try this method:
#Override
public void clear() {
performed.setChecked(false);
nonperformed.setChecked(false);
}
I have sucessfully made and used RadioGroup's before in xml, but each time there was a set of radio buttons in succession, all within the same LinearLayout. Now I wish to define a set of radio buttons to be part of a group, but they are not in the same layout. My start and end code for the group is:
START:
<RadioGroup android:id="#+id/radioGroup1" >
END:
</RadioGroup>
If I place this around each button individually, then it compiles, but the buttons don't act as radio buttons (i.e. activating one did not de-activate the others). If I try to place the "start" before any of the buttons and put the "end" after the last of them, then I get compilation errors.
Store the RadioButtons in an array. Instead of grouping them in a RadioGroup, you have to enable/disable them yourself. (un-tested so no copying/pasting )
declare these variables
private ArrayList<RadioButton> mGroup = new ArrayList<RadioGroup>();
private CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
for(RadioButton btn : mGroup)
btn.setChecked(false);
buttonView.setChecked(true);
}
}
Somewhere in your activity:
mGroup.add(your radiobuttons); // e.g. (RadioButton)findViewById(R.id.radio_button1);
mGroup.add(another radiobutton);
for(RadioButton btn : mGroup)
btn.setOnCheckedChangeListener(mListener)
Maybe you have to invalidate your Buttons after checking/unchecking them, to cause a redraw
Unfortunately the SDK does not directly support doing this, you would need to create a custom "group" to manager the buttons.
All the mutual exclusion logic for each RadioButton is managed by RadioGroup, so all the buttons have to be added to the same group. RadioGroup is a subclass of LinearLayout, so this inherently means they also need to all be in the same layout. RadioGroup manages each button's checked status by iterating through its children, which is why the two can't be divorced.
Here is a link to the RadioGroup source code as a starting point, you could create a custom Manager that uses the same logic here to manage the status of which button is checked in order to separate them from their layout status. RadioGroup basically just registers itself as an OnCheckedChangeListener for each child that is a RadioButton and then controls the check status of all the buttons based on user events.
HTH
Here's a refined and tested version of the first part of Entreco's code:
final ArrayList<RadioButton> mGroup = new ArrayList<RadioButton>();
CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
for(RadioButton btn : mGroup) {
if (buttonView.getId() != btn.getId() && isChecked) {
btn.setChecked(false);
}
}
}
};
It will uncheck only the other buttons and only if the state of the clicked button changed from unchecked to checked.
#Entreco answer can cause recursive call to onCheckedChanged which is fixed by #Twilite answer.
But #Twilite answer also has a problem and that's the id of views without explicit id may not be unique. If that happen, you may see either multiple selected radio buttons or none of them being checked. So, it's safer to either set a unique id or a unique tag for each one:
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
for (RadioButton btn : mGroup)
if (!btn.getTag().equals(compoundButton.getTag()))
btn.setChecked(false);
}
}
When check box inside a layout is checked the backround of layout should be white and if check box is unchecked layout should be black.is this possible in android??
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
youtLayout.setBackgroundColor(Color.WHITE);
}else{
youtLayout.setBackgroundColor(Color.BLACK);
}
}
});
U try wid `
checkBox.setOnClickListener(new OnClickListner(){
layout = finViewById(R.Layout.LinearLayout);
layout.setBackgroundColor(//color)
})
not syntactically right..`
Some example code that should get you started:
if(checkbox.isChecked())
findViewById(R.id.background).setBackgroundColor(Color.WHITE);
else
findViewById(R.id.background).setBackgroundColor(Color.BLACK);