RadioGroup listener dont work - android

I have a Fragment that displays a Dialog.
Inside that Dialog there is a RadioGroup.
I tried to create a listener to this RadioGroup, but i didn't successed.
Fragment --> Dialog --> RadioGroup
I tried four ways:
OnCheckedChangeListener on RadioGroup.
OnClickListener on RadioGroup.
OnCheckedChangeListener on RadioButton.
OnClickListener on RadioButton
No one worked.
Here is my code:
The Dialog - layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="#+id/bS_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RadioButton
android:id="#+id/r_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="ID"
android:textSize="24sp" />
<RadioButton
android:id="#+id/r_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Title"
android:textSize="24sp" />
<RadioButton
android:id="#+id/r_genre"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Genre"
android:textSize="24sp" />
<RadioButton
android:id="#+id/r_author"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Author"
android:textSize="24sp" />
</RadioGroup>
<AutoCompleteTextView
android:id="#+id/c_bSearch"
android:layout_width="278dp"
android:layout_height="72dp"
android:singleLine="true"/>
</LinearLayout>
The Fragment - layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<de.codecrafters.tableview.TableView
android:id="#+id/tableView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
And the Fragment - code: (I marked where is the problem, the other code is not important)
package com.example.adiel.library_app_30;
import .....;
public class ShowAllBooks extends Fragment {
View v;
TableLayout table;
TableRow row;
String[] spaceProbeHeaders = {"ID", "Title"};
String[][] spaceProbes;
AutoCompleteTextView c_search;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.show_all_books, container, false);
setHasOptionsMenu(true);
/*
------The problem is here----
*/
LayoutInflater li = LayoutInflater.from(v.getContext());
View promptsView = li.inflate(R.layout.b_search_dialog, null);
c_search = (AutoCompleteTextView) promptsView.findViewById(R.id.c_bSearch);
RadioGroup rg = (RadioGroup) promptsView.findViewById(R.id.bS_rg);
rg.check(R.id.r_id);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Log.d("Log", "Clicked");
}
});
rg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Log", "Clicked");
}
});
RadioButton r_id = (RadioButton) promptsView.findViewById(R.id.r_id);
r_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("Log", "Clicked");
}
});
r_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Log", "Clicked");
}
});
/*
------Until here----
*/
final TableView<String[]> tb = (TableView<String[]>) v.findViewById(R.id.tableView);
tb.setColumnCount(2);
tb.setHeaderBackgroundColor(Color.parseColor("#2ecc71"));
Ask a = new Ask("books", "getAllCopiesOfBooks", null);
Client client = (Client) new Client(a, a.getIp(), v.getContext(), new Client.ClientReply() {
#Override
public void processFinish(Object output) {
if (output != null) {
ArrayList<CopyId> data = (ArrayList<CopyId>) output;
spaceProbes = new String[data.size()][2];
for (int i = 0; i < data.size(); i++) {
CopyId c = data.get(i);
spaceProbes[i][0] = String.valueOf(c.getId());
spaceProbes[i][1] = c.getTitle();
}
tb.setHeaderAdapter(new SimpleTableHeaderAdapter(v.getContext(), spaceProbeHeaders));
tb.setDataAdapter(new SimpleTableDataAdapter(v.getContext(), spaceProbes));
}
}
}).execute();
tb.addDataClickListener(new TableDataClickListener() {
#Override
public void onDataClicked(int rowIndex, Object clickedData) {
Toast.makeText(v.getContext(), ((String[]) clickedData)[0], Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), ShowBook.class);
i.putExtra("id", ((String[]) clickedData)[0]);
startActivity(i);
}
});
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.showallbooks_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add:
return true;
case R.id.action_search:
search_func();
return true;
}
return super.onOptionsItemSelected(item);
}
public void search_func(){
final Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.b_search_dialog);
dialog.setTitle("Search");
dialog.show();
//RadioButton r_id = (RadioButton) promptsView.findViewById(R.id.r_id);
/*RadioButton r_title = (RadioButton) v.findViewById(R.id.r_title);
RadioButton r_genre = (RadioButton) v.findViewById(R.id.r_genre);
RadioButton r_author = (RadioButton) v.findViewById(R.id.r_author);*/
}
}

Related

RadioButton state and Button validation

I am facing two issues.
Radio buttons in radiogroup loosing state: when I click yes or no and scroll down the list view it looses the radiobutton value which is checked. I tried many ways to fix it but unable to achieve.
Submit button validation(Please refer image four): list of questions user should select either yes or no before clicking on submit button, if user click on submit button without selecting either yes or no it should them a toast message. all the questions should be selected with either yes or no. More specific each radiogroup should give me yes or no, not empty string.
Thanks in advance.
Custom Adapter
public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public static ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("");
}
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return questionsList.length;
}
#Override
public Object getItem(int i) {
return 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 view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.list_items, null);
TextView question = (TextView) view.findViewById(R.id.question);
final RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
final RadioButton no = (RadioButton) view.findViewById(R.id.no);
final RadioGroup rg = (RadioGroup) view.findViewById(R.id.radio_group);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (yes.isChecked()) {
yes.setBackgroundColor(Color.GREEN);
no.setBackgroundColor(Color.BLACK);
}
if (no.isChecked()){
no.setBackgroundColor(Color.rgb(255,165,0));
yes.setBackgroundColor(Color.BLACK);
}
}
});
yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "1");
}
});
no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "2");
}
});
question.setText(questionsList[i]);
return view;
}
Main Activity
public class MainActivity extends AppCompatActivity {
ListView simpleList;
String[] questions;
Button submit,submit1;
FileOutputStream fstream;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
questions = getResources().getStringArray(R.array.questions);
simpleList = (ListView) findViewById(R.id.simpleListView);
View footerView = getLayoutInflater().inflate(R.layout.footer,null);
submit = (Button) footerView.findViewById(R.id.submit1);
simpleList.addFooterView(footerView);
View headerView = getLayoutInflater().inflate(R.layout.header, null);
simpleList.addHeaderView(headerView);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
simpleList.setAdapter(customAdapter);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String message = "";
for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
}
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(v.getContext(), DetailsActivity.class);
startActivity(inent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
XML main layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/Black"
android:padding="10dp">
<RelativeLayout
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#drawable/round_relativelayout"
>
<ListView
android:id="#+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/Black"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
/>
</RelativeLayout>
List item XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/LightGrey">
<!-- TextView for displaying question-->
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="#dimen/activity_horizontal_margin"
android:textColor="#000"
android:textSize="30dp"
android:text="Which is your most favorite?"
/>
<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"
tools:context=".MainActivity"
android:id="#+id/main">
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:background="#color/Black"
android:button="#null"
android:paddingHorizontal="30dp"
android:paddingVertical="5dp"
android:text="YES"
android:textColor="#color/White"
android:textSize="50dp" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="#color/Black"
android:button="#null"
android:paddingHorizontal="30dp"
android:paddingVertical="5dp"
android:text="NO"
android:textColor="#color/White"
android:textSize="50dp" />
</RadioGroup>
</FrameLayout>
Image oneImage twoImage threeImage four with submit button
Try the following:
1) MainActivity_.class:-----
public class MainActivity_ extends AppCompatActivity {
private ListView lv;
private CustomAdapter customAdapter;
private String[] questions;
private Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout8);
questions = new String[10];
for(int i = 0 ; i<10 ; i++){
questions[i] = "Q " + i;
}
lv = (ListView) findViewById(R.id.lv);
customAdapter = new CustomAdapter(getApplicationContext() , questions);
lv.setAdapter(customAdapter);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean found_unanswered = false;
if(customAdapter != null){
for(int i = 0 ; i<customAdapter.getSelectedAnswers().size() ; i++){
if(customAdapter.getSelectedAnswers().get(i).equals("3")){
found_unanswered = true;
break;
}
}
}
if(!found_unanswered){
Toast.makeText(getApplicationContext() , "All Answered" , Toast.LENGTH_LONG).show();
//Go to other activity
}
}
});
}
}
2) CustomAdapter.class:------
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;
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("3");
}
inflter = (LayoutInflater.from(applicationContext));
}
#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]);
// initialize/ restore UI Radio Button State
final RadioGroup rg = (RadioGroup) view.findViewById(R.id.radio_group);
RadioButton rb_yes = (RadioButton) rg.findViewById(R.id.yes);
RadioButton rb_no = (RadioButton) rg.findViewById(R.id.no);
if(selectedAnswers.get(i).equals("1")){
rg.check(R.id.yes);
rb_yes.setBackgroundColor(Color.GREEN);
rb_no.setBackgroundColor(Color.GRAY);
}else if(selectedAnswers.get(i).equals("2")){
rg.check(R.id.no);
rb_yes.setBackgroundColor(Color.GRAY);
rb_no.setBackgroundColor(Color.BLACK);
}else{
// no answer.
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.GRAY);
selectedAnswers.set(i, "1");
break;
case R.id.no:
rb_yes.setBackgroundColor(Color.GRAY);
rb_no.setBackgroundColor(Color.BLACK);
selectedAnswers.set(i, "2");
break;
}
}
});
return view;
}
public List<String> getSelectedAnswers(){
return selectedAnswers;
}
}
3) layout8.xml:------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:id="#+id/lv">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/lv"
android:layout_weight="20"
android:text="Submit"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/submit"/>
</LinearLayout>
4) list_items.xml:-----
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<!-- TextView for displaying question-->
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000"
android:textSize="30dp"
android:text="Which is your most favorite?"
/>
<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"
tools:context=".MainActivity"
android:id="#+id/main">
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:button="#null"
android:paddingHorizontal="30dp"
android:paddingVertical="5dp"
android:text="YES"
android:textSize="50dp" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:button="#null"
android:paddingHorizontal="30dp"
android:paddingVertical="5dp"
android:text="NO"
android:textSize="50dp" />
</RadioGroup>
</FrameLayout>
</LinearLayout>
Try this.
Use ViewHolder so it does not lose the data set
Custom Adapter class
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
view = inflter.inflate(R.layout.list_items, null);
viewHolder = new ViewHolder();
ViewHoldwe.question = (TextView) view.findViewById(R.id.question);
viewHolder.yes = (RadioButton) view.findViewById(R.id.yes);
viewHolder.no = (RadioButton) view.findViewById(R.id.no);
viewHolder.rg = (RadioGroup) view.findViewById(R.id.radio_group);
view.setTag(viewHolder)
}else{
viewHolder = (ViewHolder) view.getTag();
viewHolder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (viewHolder.yes.isChecked()) {
viewHolder.yes.setBackgroundColor(Color.GREEN);
viewHolder.no.setBackgroundColor(Color.BLACK);
}
if (viewHolder.no.isChecked()){
viewHolder.no.setBackgroundColor(Color.rgb(255,165,0));
viewHolder.yes.setBackgroundColor(Color.BLACK);
}
}
});
viewHolder.yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "1");
}
});
viewHolder.no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "2");
}
});
question.setText(questionsList[i]);
}
return view;
}
Make a private inner class
private class ViewHolder{
RadioButton yes;
TextView question;
RadioButton no ;
RadioGroup rg;
}

RadioGroup not working in fragment android

I have faced an issue radio check change listener not working in the android fragment, I tried this within an activity it will work fine, while I convert into fragment it will give no response, please help me, here is my code.
K06_Away.java
public class K06_Away extends Fragment {
private View kf06_view;
protected Typeface tfLatoBold,tfLatoMedium,tfLatoRegular;
private Button button_kf06_back,button_kf06_next;
private TextView txtVw_kf06_resident_away;
private EditText edTxt_kf06_visiting_lastname;
private RadioGroup radioGp_kf06_resident_away;
List<RadioButton> radioButtons = new ArrayList<RadioButton>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
kf06_view = inflater.inflate(R.layout.k06_away, container, false);
return kf06_view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
configViews();
}
private void configViews() {
button_kf06_back = (Button) kf06_view.findViewById(R.id.button_kf06_back);
button_kf06_next = (Button) kf06_view.findViewById(R.id.button_kf06_next);
txtVw_kf06_resident_away= (TextView) kf06_view.findViewById(R.id.txtVw_kf06_resident_away);
radioGp_kf06_resident_away = (RadioGroup) kf06_view.findViewById(R.id.radioGp_kf06_resident_away);
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_1_2_hours) );
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_halfday) );
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_allday) );
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_moreday) );
configClickListeners();
radioButtonAction();
}
private void configClickListeners() {
button_kf06_back.setOnClickListener(this);
button_kf06_next.setOnClickListener(this);
}
private void radioButtonAction(){
for (RadioButton button : radioButtons){
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) processRadioButtonClick(buttonView);
String radio_Text = buttonView.getText().toString();
int radio_Id = buttonView.getId();
System.out.println("Selected the Radio:"+radio_Text+", Radio-Id:"+radio_Id);
}
});
}
}
private void processRadioButtonClick(CompoundButton buttonView){
for (RadioButton button : radioButtons){
if (button != buttonView ) button.setChecked(false);
}
}
}
k06_away.xml
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/lLayout_kf06_resident_away"
android:id="#+id/radioGp_kf06_resident_away">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="150dp"
tools:ignore="RtlHardcoded">
<RadioButton
android:id="#+id/radio_kf06_1_2_hours"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_1"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:paddingStart="20dp"
android:layout_marginTop="150dp"
android:textColor="#color/colorWhite"
tools:ignore="NestedWeights,RtlSymmetry" />
<RadioButton
android:id="#+id/radio_kf06_halfday"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_2"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:textColor="#color/colorWhite"
android:paddingStart="20dp"
android:layout_marginTop="50dp"
tools:ignore="NestedWeights,RtlSymmetry"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="50dp"
tools:ignore="RtlHardcoded">
<RadioButton
android:id="#+id/radio_kf06_allday"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_3"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:textColor="#color/colorWhite"
android:paddingStart="20dp"
android:layout_marginTop="150dp"
tools:ignore="NestedWeights,RtlSymmetry"/>
<RadioButton
android:id="#+id/radio_kf06_moreday"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_4"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:textColor="#color/colorWhite"
android:paddingStart="20dp"
android:layout_marginTop="50dp"
tools:ignore="NestedWeights,RtlSymmetry,RtlHardcoded"/>
</LinearLayout>
</LinearLayout>
</RadioGroup>
1. Try to call method configViews() from onCreateView() and pass the view kf06_view as parameter.
2. Implement View.OnClickListener to handle button_kf06_back and button_kf06_next click events.
Update your K06_Away Fragment as below:
public class K06_Away extends Fragment implements View.OnClickListener {
protected Typeface tfLatoBold,tfLatoMedium,tfLatoRegular;
private Button button_kf06_back,button_kf06_next;
private TextView txtVw_kf06_resident_away;
private EditText edTxt_kf06_visiting_lastname;
private RadioGroup radioGp_kf06_resident_away;
List<RadioButton> radioButtons = new ArrayList<RadioButton>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View kf06_view = inflater.inflate(R.layout.k06_away, container, false);
// Init
configViews(kf06_view);
return kf06_view;
}
private void configViews(View view) {
button_kf06_back = (Button) view.findViewById(R.id.button_kf06_back);
button_kf06_next = (Button) view.findViewById(R.id.button_kf06_next);
txtVw_kf06_resident_away= (TextView) view.findViewById(R.id.txtVw_kf06_resident_away);
radioGp_kf06_resident_away = (RadioGroup) view.findViewById(R.id.radioGp_kf06_resident_away);
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_1_2_hours) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_halfday) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_allday) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_moreday) );
configClickListeners();
radioButtonAction();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_kf06_back:
// Do something...
break;
case R.id.button_kf06_next:
// Do something...
break;
}
}
private void configClickListeners() {
button_kf06_back.setOnClickListener(this);
button_kf06_next.setOnClickListener(this);
}
private void radioButtonAction(){
for (RadioButton button : radioButtons){
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) processRadioButtonClick(buttonView);
String radio_Text = buttonView.getText().toString();
int radio_Id = buttonView.getId();
System.out.println("Selected the Radio:"+radio_Text+", Radio-Id:"+radio_Id);
}
});
}
}
private void processRadioButtonClick(CompoundButton buttonView){
for (RadioButton button : radioButtons){
if (button != buttonView ) button.setChecked(false);
}
}
}
RadioGroup Directly contain only RadioButtons,No other Layouts,if you add LinearLayout inside RadioGroup,it will not works as you expect.Only use RadioButtons as child of RadioGroup.

How do I replace my popupmenu code with ListPopupWindow?

Currently , I am programatically creating a popupmenu which displays a list of floors and a title. However, changing the background color of just the title and adding a close button to title is turning out to be a nightmare.
I want to replace this popupmenu with a list popup window so I can add an XML file with background attribute for the title with a black color as the background and a close button on the right and white background for items in the menu. Is there a way I can achieve this with list popup window? Here's my code for that:
private void floorMenu(ImageView btnFloorMenu){
MapData data = new MapDao(MyPlugin.mapId);
final List<Floor> flList = dao.getFloors();
// set popupMenu
final PopupMenu floorsPm = new PopupMenu(MapViewActivity.this,btnFloorMenu);
MenuItem titleItem = floorsPm.getMenu().add(Menu.NONE, Menu.NONE, Menu.NONE, "Floors");
int i = 1;
for(Floor fl : flList)
{
floorsPm.getMenu().add(Menu.NONE, i,i, fl.getName());
if(i>3)
break;
i++;
}
// add popup listener
floorsPm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
// onClick
#Override
public boolean onMenuItemClick(MenuItem item){
// get floorname
int flOrder = item.getOrder();
if(flOrder == Menu.NONE )
return true;
flOrder--;
final String floorId = flList.get(flOrder).getMapId();
// set camera to floor
runOnUiThread(new Runnable() {
#Override
public void run() {
floorsPm.dismiss();
mapFragment.getMapManager().setCameraLayer(floorId, false);
Log.d(TAG, "post cameraLayer set");
changedSteps = true;
pauseNav();
}
});
return true;
}
});
floorsPm.show();
}
Here is my example to create show a ListPopupWindow
First, create layout item_list_popup_window for each item of ListPopupWindow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e4e4e4"
android:paddingTop="1dp"
android:orientation="horizontal">
<TextView
android:id="#+id/text_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
</LinearLayout>
Second, create an Adapter for your ListPopupWindow like
public class ListPopupWindowAdapter extends BaseAdapter{
private Activity mActivity;
private List<String> mDataSource = new ArrayList<>();
private LayoutInflater layoutInflater;
private OnClickDeleteButtonListener clickDeleteButtonListener;
ListPopupWindowAdapter(Activity activity, List<String> dataSource, #NonNull OnClickDeleteButtonListener clickDeleteButtonListener){
this.mActivity = activity;
this.mDataSource = dataSource;
layoutInflater = mActivity.getLayoutInflater();
this.clickDeleteButtonListener = clickDeleteButtonListener;
}
#Override
public int getCount() {
return mDataSource.size();
}
#Override
public String getItem(int position) {
return mDataSource.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.item_list_popup_window, null);
holder.tvTitle = (TextView) convertView.findViewById(R.id.text_title);
holder.btnDelete = (Button) convertView.findViewById(R.id.button_delete);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
// bind data
holder.tvTitle.setText(getItem(position));
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickDeleteButtonListener.onClickDeleteButton(position);
}
});
return convertView;
}
public class ViewHolder{
private TextView tvTitle;
private Button btnDelete;
}
// interface to return callback to activity
public interface OnClickDeleteButtonListener{
void onClickDeleteButton(int position);
}
}
Third, You create a function for create and show ListPopupWindow
private void showListPopupWindow(View anchorView) {
final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
listPopupWindow.setWidth(600);
List<String> sampleData = new ArrayList<>();
sampleData.add("A");
sampleData.add("B");
sampleData.add("CCCCCCCCCCCCCC");
sampleData.add("D");
sampleData.add("EEEEEEEEE");
listPopupWindow.setAnchorView(anchorView);
ListPopupWindowAdapter listPopupWindowAdapter = new ListPopupWindowAdapter(this, sampleData, new ListPopupWindowAdapter.OnClickDeleteButtonListener() {
#Override
public void onClickDeleteButton(int position) {
Toast.makeText(MainActivity.this, "Click delete " + position, Toast.LENGTH_SHORT).show();
listPopupWindow.dismiss();
}
});
listPopupWindow.setAdapter(listPopupWindowAdapter);
listPopupWindow.show();
}
Finally, you can show the ListPopupWindow by
showListPopupWindow(v);
for example, if you want to show it when click button
anyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showListPopupWindow(v);
}
});
Full Demo is here
Please Try this code, Maybe you wont like this
private void floorMenu(ImageView btnFloorMenu){
final Dialog customDialog = new Dialog(this);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.item_dialog_coustom_design);
TextView clickItem = (TextView)customDialog.findViewById(R.id.item_click);
TextView clickItem1 = (TextView)customDialog.findViewById(R.id.item_click1);
TextView clickItem2 = (TextView)customDialog.findViewById(R.id.item_click2);
Button btnClose = (Button)customDialog.findViewById(R.id.btn_close);
clickItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
// wright your Button Action
}
});
clickItem1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
// wright your Button Action
}
});
clickItem2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
// wright your Button Action
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
customDialog.show();
}
Create Linearlayout layout_width="280dp" layout_height="wrap_content"
android:orientation="vertical" file name item_dialog_coustom_design.xml then put this code
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Title"
android:background="#000"
android:textColor="#fff"
android:padding="12dp"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#fff"
android:padding="10dp"
android:text="Your Item"
android:id="#+id/item_click"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#fff"
android:padding="10dp"
android:text="Your Item"
android:id="#+id/item_click1"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#fff"
android:padding="10dp"
android:text="Your Item"
android:id="#+id/item_click2"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="#fff"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_close"
android:text="Close"/>
</LinearLayout>

How to get value of all selected radio buttons from all the views in sliding Fragment

I have three views in sliding Fragment with Survey Questions having dynamic radiobuttons (the layout for all views are same, i am just repeating). I can get the data of selected radiobuttons from one screen, but how can i get the data of all selected radiobuttons from all screens ? Below is my working code
FragmentActivity.java
public class MainActivity extends FragmentActivity {
static final int ITEMS = 3;
MyAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
Button button = (Button) findViewById(R.id.first);
Button submitaldatabutton = (Button) findViewById(R.id.first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.last);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(ITEMS - 1);
}
});
submitaldatabutton = (Button) findViewById(R.id.submitalldata);
submitaldatabutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Get all data
}
});
}
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return ITEMS;
}
#Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0: // Fragment # 0 - This will show image
return LayoutFragment.init(position);
case 1: // Fragment # 1 - This will show image
return LayoutFragment.init(position);
default:// Fragment # 2-9 - Will show list
return LayoutFragment.init(position);
}
}
//#Override
/*public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show image
return ImageFragment.init(position);
case 1: // Fragment # 1 - This will show image
return ImageFragment.init(position);
default:// Fragment # 2-9 - Will show list
return ArrayListFragment.init(position);
}
}*/
}
}
LayoutFragment.java
public class LayoutFragment extends Fragment {
int fragVal;
private String[] application = { "Country1", "Country2", "Country3", "Country4", "Country5", "Country6", "Country7", "Country8" };
private String[] device = { "Country9", "Country10", "Country11", "Country12", "Country13", "Country14", "Country15", "Country16" };
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;
private RadioButton btn;
private RadioButton btn2;
private String text1;
private String text2;
RadioButton button1;
RadioButton button2;
Button selectall;
Context thiscontext;
static LayoutFragment init(int val) {
LayoutFragment truitonFrag = new LayoutFragment();
// Supply val input as an argument.
Bundle args = new Bundle();
args.putInt("val", val);
truitonFrag.setArguments(args);
return truitonFrag;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
thiscontext = container.getContext();
View layoutView = inflater.inflate(R.layout.activity_main, container, false);
Button myButton = (Button) layoutView.findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("");
// Get selected radiobuttons
if (radioGroup1.getCheckedRadioButtonId() != -1) {
text1 = btn.getText().toString();
Log.d("Button", "Text 1 : " + text1);
}
if (radioGroup2.getCheckedRadioButtonId() != -1) {
text2 = btn2.getText().toString();
Log.d("Button", "Text 2 : " + text2);
}
Toast.makeText(
thiscontext,
"Data Posting : APPLICATION : "
+ text1 + " \nDEVICE : " + text2,
Toast.LENGTH_LONG).show();
}
});
//Draw Radiobuttons
radioGroup1 = (RadioGroup) layoutView.findViewById(R.id.radio1);
radioGroup2 = (RadioGroup) layoutView.findViewById(R.id.radio2);
ViewGroup hourButtonLayout = (ViewGroup) layoutView.findViewById(R.id.radio1);
for (int i = 0; i < application.length; i++) {
button1 = new RadioButton(thiscontext);
button1.setId(i);
button1.setText(application[i]);
hourButtonLayout.addView(button1);
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup2,
int checkedId2) {
for (int i = 0; i < mRadioGroup2.getChildCount(); i++) {
btn = (RadioButton) mRadioGroup2.getChildAt(i);
int t = mRadioGroup2.getId();
System.out.println(t);
if (btn.getId() == checkedId2) {
text1 = btn.getText().toString();
Toast.makeText(thiscontext,
"You selected : " + text1,
Toast.LENGTH_SHORT).show();
return;
}
}
}
});
}
ViewGroup hourButtonLayout2 = (ViewGroup) layoutView.findViewById(R.id.radio2);
for (int i = 0; i < device.length; i++) {
button2 = new RadioButton(thiscontext);
button2.setId(i);
button2.setText(device[i]);
hourButtonLayout2.addView(button2);
radioGroup2
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup,
int checkedId) {
for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
btn2 = (RadioButton) mRadioGroup.getChildAt(i);
int t = mRadioGroup.getId();
System.out.println(t);
if (btn2.getId() == checkedId) {
text2 = btn2.getText().toString();
Toast.makeText(thiscontext,
"You selected : " + text2,
Toast.LENGTH_SHORT).show();
return;
}
}
}
});
}
return layoutView;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000"
android:text="Select Question1"
android:textSize="18sp"/>
<Button
android:id="#+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="200dip"
android:orientation="vertical"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="200dip"
android:orientation="vertical">
<RadioGroup
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:background="#fff"
android:checkedButton="#+id/sound" >
</RadioGroup>
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:text="Select Question2"
android:textColor="#000"
android:textSize="18sp"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="360dip"
android:orientation="vertical"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="560dip"
android:orientation="vertical">
<RadioGroup
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:background="#fff"
android:checkedButton="#+id/sound">
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
fragment_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:measureWithLargestChild="true"
android:orientation="horizontal" >
<Button
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First" >
</Button>
<Button
android:id="#+id/last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last" >
</Button>
<Button
android:id="#+id/submitalldata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit AllData" >
</Button>
</LinearLayout>
</LinearLayout>

Android opOptionsItemSelected menu doesn't work

I have a problem with a menu in my Android app: it displays when I click the button, but when I try to click the option which will start another activity, it does nothing. It doesn't crash, either.
Here's the code for the menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bookmark:
this.item.setBookmark(!this.item.getBookmarks()) ;
if(this.item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1);
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
return true;
case R.id.settings:
Intent settings = new Intent(this,Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And here's the full code of my activity:
public class EventList extends Activity implements OnClickListener {
ImageView bookmark;
ImageView palau;
ImageView noimage;
TextView nom;
TextView cognom;
TextView data;
TextView descripcio;
Button map;
Button join;
Context ct;
ItemEvent item;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list);
item = (ItemEvent) getIntent().getSerializableExtra("item");
ct = this;
Toast.makeText(this, item.getNom(), Toast.LENGTH_LONG).show();
element_confi(); //line 63!
}
public void element_confi() {
bookmark = (ImageView) findViewById(R.id.bookmark);
nom = (TextView) findViewById(R.id.nom);
data= (TextView) findViewById(R.id.data);
descripcio = (TextView) findViewById(R.id.descripcio);
map = (Button) findViewById(R.id.mapa);
join = (Button) findViewById(R.id.join);
nom.setText(item.getNom());
data.setText(item.getDate());
descripcio.setText(item.getDescription());
if( item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1); //line 80!
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
map.setOnClickListener(this);
join.setOnClickListener(this);
ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(item.getRutaFoto1(),item.getRutaFoto2());
viewPager.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.event, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bookmark:
this.item.setBookmark(!this.item.getBookmarks()) ;
if(this.item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1);
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
return true;
case R.id.settings:
Intent settings = new Intent(this,Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mapa:
Toast.makeText(ct, "mostrar mapa", Toast.LENGTH_LONG).show();
break;
case R.id.join:
Intent apuntar = new Intent(ct, JoinEvents.class);
startActivity(apuntar);
break;
}
}
private class ImageAdapter extends PagerAdapter {
private int[] mImages;
public ImageAdapter(int image1Recurso, int image2Recurso){
mImages = new int[] { image1Recurso,
image2Recurso,
};
}
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = EventList.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_START);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
Thank you very much for your help.
Yours sincerely,
Mauro.
EDIT:
Settings.java
(Some spots are pending polishing: don't mind them)
public class Settings extends Activity implements OnItemSelectedListener, OnClickListener {
CheckBox view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//onCheckBoxClicked();
//onRadioButtonClicked();
Spinner_Menu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
//checkbox
public void onCheckBoxClicked (View v)
{
view = (CheckBox)findViewById(R.id.Setting1);
boolean checking = ((CheckBox) v).isChecked();
switch (view.getId())
{
case R.id.Setting1:
if (checking)
{
// //checkbox checked!
}
else
{
//not checked!
}
break;
}
}
//Menú desplegable
public void Spinner_Menu()
{
Spinner spinner = (Spinner) findViewById(R.id.Settings2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.menu, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onRadioButtonClicked(View v)
{
boolean checked = ((RadioButton) v).isChecked();
switch (view.getId())
{
case R.id.Setting3_1:
if (checked)
{
//Option 1
}
break;
case R.id.Setting3_2:
if (checked)
{
//Option 2
}
break;
}
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
And the layout for EventList:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".EventList" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
</ListView>
</LinearLayout>
<TextView
android:id="#+id/nom"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom Event"
android:layout_below="#+id/linear"
/>
<ImageView
android:id="#+id/bookmark"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/favorito"
android:layout_below="#+id/nom"
/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_below="#id/bookmark"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/view_pager"
android:text="TextView" />
<Button
android:id="#+id/mapa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/data"
android:text="Com arribar" />
<Button
android:id="#+id/join"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/mapa"
android:text="Join event" />
<TextView
android:id="#+id/descripcio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/join"
android:text="Descripció event" />
</RelativeLayout>
Just in case, the Settings.xml file too:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Settings" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting1"/>
<CheckBox
android:id="#+id/Setting1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox1"
android:layout_below="#+id/linear1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/linear1">
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting2"/>
<Spinner
android:id="#+id/Settings2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Desplegable"
android:layout_below="#+id/linear2"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/linear2">
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting3"/>
<RadioButton
android:id="#+id/Setting3_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Opció1"
android:layout_below="#+id/linear3"/>
<RadioButton
android:id="#+id/Setting3_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Opció2"
android:layout_below="#+id/Setting3_1"/>
</LinearLayout>
</RelativeLayout>
Thank you very much.
Try adding this to EventList.java:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.settings:
Intent settingsIntent = new Intent(this, Settings.class);
startActivity(settingsIntent);
return true;
}
}
can you please paste your event.xml as well as is Settings.class extending PreferenceActivity ?
Also this is happening sometimes if your are using eclipse. Just clean try to clean and rebuild your project.
if it wont work, then try:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.createOnOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.event, menu);
return true;
}

Categories

Resources