Use GetCheckedRadioButtonId() with an external radio group
up vote
1
down vote
favorite
I am a beginner in android development and am blocked on my project. I searched a solution all this morning and didn't find so here it is.
I have made a layout with a radiogroup and several radiobutton. I use it for different views of my project.
I didn't know that we can't find and element by its id from a view if this element is in an external view. So I searched a solution to "include" the layout with my radiogroup.
No problem with that, I get it with a LayoutInflater and can create the view of my layout.
Then I can get the radiogroup of this view.
The problem is that I can't manipulate it. The radiogroup is recognized but I can't get the checked radiobutton, it always return -1.
Here is the onCreate method, where the view is created.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View rank_W = inflater.inflate(R.layout.dialog_layout_w, (ViewGroup) findViewById(R.id.rank_w));
View rank_M = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.rank_m));
// I also tried the commented way to make the view, it didn't change anything.
//viewRank = getLayoutInflater().inflate(R.layout.dialog_layout, null);
//viewRankW = getLayoutInflater().inflate(R.layout.dialog_layout_w, null);
tvT = findViewById(R.id.tv_T);
tvR = findViewById(R.id.tv_R);
dp = findViewById(R.id.dp);
rankM = findViewById(R.id.rankM);
rankW = findViewById(R.id.rankW);
rankM.setEnabled(false);
rankW.setEnabled(false);
gamesDB = new GamesDB(this);
rgT = findViewById(R.id.rg_t);
rgRM = rank_M.findViewById(R.id.rg_ranks);
rgRW = rank_W.findViewById(R.id.rg_ranksW);
rankM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rgT.getCheckedRadioButtonId() == R.id.rd_mm) {
rM = true;
dialogRank(view);
} else if (rgT.getCheckedRadioButtonId() == R.id.rd_wm) {
rM = true;
dialogRankW(view);
}
}
});
}
I try to get the checked radio button in the dialogRank(view) method, here it is :
public void dialogRank(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//rgRM = rank_M.findViewById(R.id.rg_ranks);
int radioButtonID = rgRM.getCheckedRadioButtonId();
View radioButton = rgRM.findViewById(radioButtonID);
int idx = rgRM.indexOfChild(radioButton);
if (rM) {
newRankM = idx;
rM = false;
} else if (rW) {
newRankW = idx;
rW = false;
}
}
});
builder.setView(dialogLayout);
builder.show();
}
So in this method a dialog is opened with the list of the radio buttons and the goal for me is to get the index of the checked radiobutton when the user clicks on OK.
But idx returns -1.
I find nothing about external radio buttons, is there a solution to this ?
Thank you for reading (sorry for english).
java android android-layout radio-button radio-group
add a comment |
up vote
1
down vote
favorite
I am a beginner in android development and am blocked on my project. I searched a solution all this morning and didn't find so here it is.
I have made a layout with a radiogroup and several radiobutton. I use it for different views of my project.
I didn't know that we can't find and element by its id from a view if this element is in an external view. So I searched a solution to "include" the layout with my radiogroup.
No problem with that, I get it with a LayoutInflater and can create the view of my layout.
Then I can get the radiogroup of this view.
The problem is that I can't manipulate it. The radiogroup is recognized but I can't get the checked radiobutton, it always return -1.
Here is the onCreate method, where the view is created.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View rank_W = inflater.inflate(R.layout.dialog_layout_w, (ViewGroup) findViewById(R.id.rank_w));
View rank_M = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.rank_m));
// I also tried the commented way to make the view, it didn't change anything.
//viewRank = getLayoutInflater().inflate(R.layout.dialog_layout, null);
//viewRankW = getLayoutInflater().inflate(R.layout.dialog_layout_w, null);
tvT = findViewById(R.id.tv_T);
tvR = findViewById(R.id.tv_R);
dp = findViewById(R.id.dp);
rankM = findViewById(R.id.rankM);
rankW = findViewById(R.id.rankW);
rankM.setEnabled(false);
rankW.setEnabled(false);
gamesDB = new GamesDB(this);
rgT = findViewById(R.id.rg_t);
rgRM = rank_M.findViewById(R.id.rg_ranks);
rgRW = rank_W.findViewById(R.id.rg_ranksW);
rankM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rgT.getCheckedRadioButtonId() == R.id.rd_mm) {
rM = true;
dialogRank(view);
} else if (rgT.getCheckedRadioButtonId() == R.id.rd_wm) {
rM = true;
dialogRankW(view);
}
}
});
}
I try to get the checked radio button in the dialogRank(view) method, here it is :
public void dialogRank(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//rgRM = rank_M.findViewById(R.id.rg_ranks);
int radioButtonID = rgRM.getCheckedRadioButtonId();
View radioButton = rgRM.findViewById(radioButtonID);
int idx = rgRM.indexOfChild(radioButton);
if (rM) {
newRankM = idx;
rM = false;
} else if (rW) {
newRankW = idx;
rW = false;
}
}
});
builder.setView(dialogLayout);
builder.show();
}
So in this method a dialog is opened with the list of the radio buttons and the goal for me is to get the index of the checked radiobutton when the user clicks on OK.
But idx returns -1.
I find nothing about external radio buttons, is there a solution to this ?
Thank you for reading (sorry for english).
java android android-layout radio-button radio-group
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am a beginner in android development and am blocked on my project. I searched a solution all this morning and didn't find so here it is.
I have made a layout with a radiogroup and several radiobutton. I use it for different views of my project.
I didn't know that we can't find and element by its id from a view if this element is in an external view. So I searched a solution to "include" the layout with my radiogroup.
No problem with that, I get it with a LayoutInflater and can create the view of my layout.
Then I can get the radiogroup of this view.
The problem is that I can't manipulate it. The radiogroup is recognized but I can't get the checked radiobutton, it always return -1.
Here is the onCreate method, where the view is created.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View rank_W = inflater.inflate(R.layout.dialog_layout_w, (ViewGroup) findViewById(R.id.rank_w));
View rank_M = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.rank_m));
// I also tried the commented way to make the view, it didn't change anything.
//viewRank = getLayoutInflater().inflate(R.layout.dialog_layout, null);
//viewRankW = getLayoutInflater().inflate(R.layout.dialog_layout_w, null);
tvT = findViewById(R.id.tv_T);
tvR = findViewById(R.id.tv_R);
dp = findViewById(R.id.dp);
rankM = findViewById(R.id.rankM);
rankW = findViewById(R.id.rankW);
rankM.setEnabled(false);
rankW.setEnabled(false);
gamesDB = new GamesDB(this);
rgT = findViewById(R.id.rg_t);
rgRM = rank_M.findViewById(R.id.rg_ranks);
rgRW = rank_W.findViewById(R.id.rg_ranksW);
rankM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rgT.getCheckedRadioButtonId() == R.id.rd_mm) {
rM = true;
dialogRank(view);
} else if (rgT.getCheckedRadioButtonId() == R.id.rd_wm) {
rM = true;
dialogRankW(view);
}
}
});
}
I try to get the checked radio button in the dialogRank(view) method, here it is :
public void dialogRank(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//rgRM = rank_M.findViewById(R.id.rg_ranks);
int radioButtonID = rgRM.getCheckedRadioButtonId();
View radioButton = rgRM.findViewById(radioButtonID);
int idx = rgRM.indexOfChild(radioButton);
if (rM) {
newRankM = idx;
rM = false;
} else if (rW) {
newRankW = idx;
rW = false;
}
}
});
builder.setView(dialogLayout);
builder.show();
}
So in this method a dialog is opened with the list of the radio buttons and the goal for me is to get the index of the checked radiobutton when the user clicks on OK.
But idx returns -1.
I find nothing about external radio buttons, is there a solution to this ?
Thank you for reading (sorry for english).
java android android-layout radio-button radio-group
I am a beginner in android development and am blocked on my project. I searched a solution all this morning and didn't find so here it is.
I have made a layout with a radiogroup and several radiobutton. I use it for different views of my project.
I didn't know that we can't find and element by its id from a view if this element is in an external view. So I searched a solution to "include" the layout with my radiogroup.
No problem with that, I get it with a LayoutInflater and can create the view of my layout.
Then I can get the radiogroup of this view.
The problem is that I can't manipulate it. The radiogroup is recognized but I can't get the checked radiobutton, it always return -1.
Here is the onCreate method, where the view is created.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View rank_W = inflater.inflate(R.layout.dialog_layout_w, (ViewGroup) findViewById(R.id.rank_w));
View rank_M = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.rank_m));
// I also tried the commented way to make the view, it didn't change anything.
//viewRank = getLayoutInflater().inflate(R.layout.dialog_layout, null);
//viewRankW = getLayoutInflater().inflate(R.layout.dialog_layout_w, null);
tvT = findViewById(R.id.tv_T);
tvR = findViewById(R.id.tv_R);
dp = findViewById(R.id.dp);
rankM = findViewById(R.id.rankM);
rankW = findViewById(R.id.rankW);
rankM.setEnabled(false);
rankW.setEnabled(false);
gamesDB = new GamesDB(this);
rgT = findViewById(R.id.rg_t);
rgRM = rank_M.findViewById(R.id.rg_ranks);
rgRW = rank_W.findViewById(R.id.rg_ranksW);
rankM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rgT.getCheckedRadioButtonId() == R.id.rd_mm) {
rM = true;
dialogRank(view);
} else if (rgT.getCheckedRadioButtonId() == R.id.rd_wm) {
rM = true;
dialogRankW(view);
}
}
});
}
I try to get the checked radio button in the dialogRank(view) method, here it is :
public void dialogRank(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//rgRM = rank_M.findViewById(R.id.rg_ranks);
int radioButtonID = rgRM.getCheckedRadioButtonId();
View radioButton = rgRM.findViewById(radioButtonID);
int idx = rgRM.indexOfChild(radioButton);
if (rM) {
newRankM = idx;
rM = false;
} else if (rW) {
newRankW = idx;
rW = false;
}
}
});
builder.setView(dialogLayout);
builder.show();
}
So in this method a dialog is opened with the list of the radio buttons and the goal for me is to get the index of the checked radiobutton when the user clicks on OK.
But idx returns -1.
I find nothing about external radio buttons, is there a solution to this ?
Thank you for reading (sorry for english).
java android android-layout radio-button radio-group
java android android-layout radio-button radio-group
asked yesterday
Morgane
348
348
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I didn't figure it out but I found another way which works. In the onCreate I don't need to get the view of the external layout, but the method dialogRank(View view)
changed, here it is :
public void dialogRank(View view) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("Select a rank");
Button ok = dialog.findViewById(R.id.btnOK);
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
rgRM = dialog.findViewById(R.id.rg_ranks);
int idB = rgRM.getCheckedRadioButtonId();
int index = rgRM.indexOfChild(rgRM.findViewById(idB));
if (rM && index != -1) {
newRankM = index;
changeR = true;
} else if (rW && index != -1) {
newRankW = index;
changeR = true;
}
if (rM) rM = false;
if (rW) rW = false;
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
}
For the context variable I declare in my activity final Context context = this;
.
This opens a dialog of the layout dialog_layout
and in the cancelListner rgRM
, there is the radiogroup of this layout. The method getCheckedRadioButtonId
works and returns the id of the radiobutton. We can then have its index with rgRM.indexOfChild(rgRM.findViewById(idB))
. The changeR and rM/rW variables are for my other treatments.
I use OnCancelListener
because it's the most adapted for the using I'm looking, the onCancel method is used when the user touch the screen outside the dialog or use the back button.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I didn't figure it out but I found another way which works. In the onCreate I don't need to get the view of the external layout, but the method dialogRank(View view)
changed, here it is :
public void dialogRank(View view) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("Select a rank");
Button ok = dialog.findViewById(R.id.btnOK);
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
rgRM = dialog.findViewById(R.id.rg_ranks);
int idB = rgRM.getCheckedRadioButtonId();
int index = rgRM.indexOfChild(rgRM.findViewById(idB));
if (rM && index != -1) {
newRankM = index;
changeR = true;
} else if (rW && index != -1) {
newRankW = index;
changeR = true;
}
if (rM) rM = false;
if (rW) rW = false;
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
}
For the context variable I declare in my activity final Context context = this;
.
This opens a dialog of the layout dialog_layout
and in the cancelListner rgRM
, there is the radiogroup of this layout. The method getCheckedRadioButtonId
works and returns the id of the radiobutton. We can then have its index with rgRM.indexOfChild(rgRM.findViewById(idB))
. The changeR and rM/rW variables are for my other treatments.
I use OnCancelListener
because it's the most adapted for the using I'm looking, the onCancel method is used when the user touch the screen outside the dialog or use the back button.
add a comment |
up vote
0
down vote
I didn't figure it out but I found another way which works. In the onCreate I don't need to get the view of the external layout, but the method dialogRank(View view)
changed, here it is :
public void dialogRank(View view) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("Select a rank");
Button ok = dialog.findViewById(R.id.btnOK);
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
rgRM = dialog.findViewById(R.id.rg_ranks);
int idB = rgRM.getCheckedRadioButtonId();
int index = rgRM.indexOfChild(rgRM.findViewById(idB));
if (rM && index != -1) {
newRankM = index;
changeR = true;
} else if (rW && index != -1) {
newRankW = index;
changeR = true;
}
if (rM) rM = false;
if (rW) rW = false;
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
}
For the context variable I declare in my activity final Context context = this;
.
This opens a dialog of the layout dialog_layout
and in the cancelListner rgRM
, there is the radiogroup of this layout. The method getCheckedRadioButtonId
works and returns the id of the radiobutton. We can then have its index with rgRM.indexOfChild(rgRM.findViewById(idB))
. The changeR and rM/rW variables are for my other treatments.
I use OnCancelListener
because it's the most adapted for the using I'm looking, the onCancel method is used when the user touch the screen outside the dialog or use the back button.
add a comment |
up vote
0
down vote
up vote
0
down vote
I didn't figure it out but I found another way which works. In the onCreate I don't need to get the view of the external layout, but the method dialogRank(View view)
changed, here it is :
public void dialogRank(View view) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("Select a rank");
Button ok = dialog.findViewById(R.id.btnOK);
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
rgRM = dialog.findViewById(R.id.rg_ranks);
int idB = rgRM.getCheckedRadioButtonId();
int index = rgRM.indexOfChild(rgRM.findViewById(idB));
if (rM && index != -1) {
newRankM = index;
changeR = true;
} else if (rW && index != -1) {
newRankW = index;
changeR = true;
}
if (rM) rM = false;
if (rW) rW = false;
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
}
For the context variable I declare in my activity final Context context = this;
.
This opens a dialog of the layout dialog_layout
and in the cancelListner rgRM
, there is the radiogroup of this layout. The method getCheckedRadioButtonId
works and returns the id of the radiobutton. We can then have its index with rgRM.indexOfChild(rgRM.findViewById(idB))
. The changeR and rM/rW variables are for my other treatments.
I use OnCancelListener
because it's the most adapted for the using I'm looking, the onCancel method is used when the user touch the screen outside the dialog or use the back button.
I didn't figure it out but I found another way which works. In the onCreate I don't need to get the view of the external layout, but the method dialogRank(View view)
changed, here it is :
public void dialogRank(View view) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("Select a rank");
Button ok = dialog.findViewById(R.id.btnOK);
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
rgRM = dialog.findViewById(R.id.rg_ranks);
int idB = rgRM.getCheckedRadioButtonId();
int index = rgRM.indexOfChild(rgRM.findViewById(idB));
if (rM && index != -1) {
newRankM = index;
changeR = true;
} else if (rW && index != -1) {
newRankW = index;
changeR = true;
}
if (rM) rM = false;
if (rW) rW = false;
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
}
For the context variable I declare in my activity final Context context = this;
.
This opens a dialog of the layout dialog_layout
and in the cancelListner rgRM
, there is the radiogroup of this layout. The method getCheckedRadioButtonId
works and returns the id of the radiobutton. We can then have its index with rgRM.indexOfChild(rgRM.findViewById(idB))
. The changeR and rM/rW variables are for my other treatments.
I use OnCancelListener
because it's the most adapted for the using I'm looking, the onCancel method is used when the user touch the screen outside the dialog or use the back button.
answered 18 hours ago
Morgane
348
348
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227197%2fuse-getcheckedradiobuttonid-with-an-external-radio-group%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password