I have set onclick event on a button(Submit). If the radio buttons of a radio group is not selected then I try to Toast message and focus/listview scroll the first unchecked radio group with error image beside or text(please select answer) as well as show error beside all unchecked radiogroup. I was able to achieve Toast message but not able to achieve focus and error beside on radiogroup which is unchecked.
**CustomAdapter**
public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
resetAnswers();
inflter = (LayoutInflater.from(applicationContext));
}
public void resetAnswers(){
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("3");
}
}
@Override
public int getCount() {
return questionsList.length;
}
@Override
public Object getItem(int i) {
return questionsList[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getViewTypeCount() {
return questionsList.length;
}
@Override
public int getItemViewType(int i) {
return i;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
if (inflter != null) {
view = inflter.inflate(R.layout.list_items, null);
}
}
TextView question = (TextView) view.findViewById(R.id.question);
question.setText(questionsList[i]);
final RadioGroup rg = (RadioGroup)view.findViewById(R.id.radio_group);
final RadioButton rb_yes = (RadioButton) rg.findViewById(R.id.yes);
final RadioButton rb_no = (RadioButton) rg.findViewById(R.id.no);
if(selectedAnswers.get(i).equals("1")){
rg.check(R.id.yes);
}else if(selectedAnswers.get(i).equals("2")){
rg.check(R.id.no);
}else{
rb_yes.setBackgroundColor(Color.GRAY);
rb_no.setBackgroundColor(Color.GRAY);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb_yes = (RadioButton) group.findViewById(R.id.yes);
RadioButton rb_no = (RadioButton) group.findViewById(R.id.no);
switch (checkedId){
case R.id.yes:
rb_yes.setBackgroundColor(Color.GREEN);
rb_no.setBackgroundColor(Color.BLACK);
selectedAnswers.set(i, "1");
break;
case R.id.no:
rb_yes.setBackgroundColor(Color.BLACK);
rb_no.setBackgroundColor(Color.rgb(255,165,0));
selectedAnswers.set(i, "2");
break;
}
if (checkedId !=-1){
Log.d("USB", "oniew "+checkedId);
rg.clearCheck();
}
else
{
Log.d("USB", "towie "+checkedId);
}
}
});
if (rb_yes.isChecked()||rb_no.isChecked()){
Log.d("USB", "checked");
}
else
{
Log.d("USB", "null ");
}
Log.d("USB", "I value "+i);
return view;
}
public List<String> getSelectedAnswers(){
return selectedAnswers;
}
**Main Activity**
public class MainActivity extends AppCompatActivity {
private ListView simpleListView;
private CustomAdapter customAdapter;
private String[] questions;
private Button submit, clear;
private RadioGroup radioGroup;
FileOutputStream fstream;
private boolean forceClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
questions = getResources().getStringArray(R.array.questions);
simpleListView = (ListView) findViewById(R.id.simpleListView);
View Listview = getLayoutInflater().inflate(R.layout.list_items,null);
RadioButton rb_yes = Listview.findViewById(R.id.yes);
RadioButton rb_no = Listview.findViewById(R.id.no);
final RadioGroup radioGroup= Listview.findViewById(R.id.radio_group);
View footerView = getLayoutInflater().inflate(R.layout.footer,null);
clear= (Button) footerView.findViewById(R.id.clear);
submit = (Button) footerView.findViewById(R.id.submit1);
simpleListView.addFooterView(footerView);
View headerView = getLayoutInflater().inflate(R.layout.header, null);
simpleListView.addHeaderView(headerView);
customAdapter = new CustomAdapter(getApplicationContext(),questions);
simpleListView.setAdapter(customAdapter);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customAdapter.resetAnswers();
customAdapter.notifyDataSetChanged();
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {LayoutInflater inflater=getLayoutInflater();
View customToastroot=inflater.inflate(R.layout.mycustom_toast, null);
String message = "";
boolean found_unanswered = false;
if(customAdapter != null){
for(int i = 0 ;i<customAdapter.getSelectedAnswers().size() ; i++){ if(customAdapter.getSelectedAnswers().get(i).equals("3")){
String p = customAdapter.getSelectedAnswers().get(i);
Log.d("USB", "ID check main "+customAdapter.getSelectedAnswers().get(i));
radioGroup.requestFocus();
radioGroup.requestFocusFromTouch();
Context context=getApplicationContext();
Toast customtoast= new Toast(context);
customtoast.setView(customToastroot);
customtoast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0);
customtoast.setDuration(Toast.LENGTH_LONG);
customtoast.show();
found_unanswered = true;
break;
}
message = message + "\n" + (i + 1) + " " + customAdapter.getSelectedAnswers().get(i);
}
}
if(!found_unanswered){
try {
fstream = openFileOutput("user_answer", Context.MODE_PRIVATE);
fstream.write(message.getBytes());
fstream.close();
Toast.makeText(getApplicationContext() ,message , Toast.LENGTH_LONG).show();
Intent inent = new Intent(view.getContext(), DetailsActivity.class);
startActivity(inent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if(menu instanceof MenuBuilder){
MenuBuilder m = (MenuBuilder) menu;
m.setOptionalIconsVisible(true);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == R.id.about) {
Intent inent = new Intent(MainActivity.this, devicedetailsActivity.class);
startActivity(inent);
return true;
}
if (id == R.id.admin) {
Intent inent = new Intent(MainActivity.this, adminactivity.class);
startActivity(inent);
return true;
}
if (id == R.id.SerialPort) {
Intent inent = new Intent(MainActivity.this, DeviceListActivity.class);
startActivity(inent);
return true;
}
if (id == R.id.customservice) {
Intent inent = new Intent(MainActivity.this, testactivity_main.class);
startActivity(inent);
return true;
}
if (id == R.id.exit) {
ActivityCompat.finishAffinity(MainActivity.this);
return true;
}
return super.onOptionsItemSelected(item);
}
Thanks