Sorry if I ask silly question, but so far had no luck in figuring out this one. I have form with 2 radio buttons that decide layout and used components of layout bellow them. Here is one of the layouts
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/arrival_date_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/arrival_date_label"/>
<EditText
android:id="@+id/arrival_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/arrival_date_label"
android:hint="10/05/2012"/>
<Spinner
android:id="@+id/arrival_time_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/arrival_date"/>
<TextView
android:id="@+id/return_date_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/return_date_label"
android:layout_below="@id/arrival_time_spinner"/>
<EditText
android:id="@+id/return_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/return_date_label"
android:hint="10/05/2012"/>
<Spinner
android:id="@+id/return_time_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/return_date"/>
</RelativeLayout>
and Java class to initialize some of the components used
public class ReturnFixedDays extends RelativeLayout {
private EditText arrivalDate;
private Spinner arrivalTime;
private EditText returnDate;
private Spinner returnTime;
private final String pattern = "dd/MM/yyyy";
public ReturnFixedDays(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.return_fixed_days, null);
arrivalDate = (EditText) view.findViewById(R.id.arrival_date);
arrivalDate.setText(arrivalDate());
arrivalTime = (Spinner) view.findViewById(R.id.arrival_time_spinner);
ArrayAdapter adapter = new ArrayAdapter(context, R.array.travel_time_entries,android.R.layout.simple_spinner_item);
arrivalTime.setAdapter(adapter);
returnDate = (EditText) view.findViewById(R.id.return_date);
returnDate.setText(returnDate());
returnTime = (Spinner) view.findViewById(R.id.return_time_spinner);
ArrayAdapter adapter2 = new ArrayAdapter(context, R.array.travel_time_entries,android.R.layout.simple_spinner_item);
returnTime.setAdapter(adapter2);
}
private String arrivalDate(){
return dateToString(today());
}
private String returnDate(){
return dateToString(weekLater());
}
private String dateToString(Date date){
return formatter().format(date);
}
private SimpleDateFormat formatter(){
return new SimpleDateFormat(pattern);
}
private Date weekLater(){
return adjustDateBy(today(), 7);
}
private Date today(){
return Calendar.getInstance().getTime();
}
private Date adjustDateBy(Date currentDate, int numOfDays) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(Calendar.DATE, numOfDays);
return calendar.getTime();
}
}
Now to add this layout to view I do as following in xml
<RelativeLayout
android:id="@+id/dates_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/flight_layout">
<TextView
android:id="@+id/date_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/dates_label"
style="@style/Label"/>
<RadioGroup
android:id="@+id/dates_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/date_label">
<RadioButton
android:id="@+id/flexible_dates_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/flexible_dates_label"
style="@style/Label"/>
<RadioButton
android:id="@+id/fixed_dates_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/fixed_dates_label"
style="@style/Label"/>
</RadioGroup>
</RelativeLayout>
<RelativeLayout
android:id="@+id/stub_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dates_layout">
<com.flyweekend.android.date.view.ReturnFixedDays
android:id="@+id/return_fixed_days_stub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout="@layout/return_fixed_days"/>
<com.flyweekend.android.date.view.ReturnFlexibleDays
android:id="@+id/return_flexible_days_stub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout="@layout/return_flexible_days"/>
</RelativeLayout>
FlightView.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getActivity();
dateTypeRadioGrp = dateTypeRadioGroup(view);
returnFixedDays = (RelativeLayout) view.findViewById(R.id.return_fixed_days_stub);
updateDatesVisibility();
return view;
}
private RadioGroup dateTypeRadioGroup(View view){
RadioGroup group = (RadioGroup)view.findViewById(R.id.dates_radio_group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup, int selected) {
if(selected == R.id.flexible_dates_radio){
Log.i(TAG, "Flexible dates selected");
updateDatesVisibility();
}else{
Log.i(TAG, "Fixed dates selected");
updateDatesVisibility();
}
}
});
return group;
}
private void updateDatesVisibility(){
if(dateTypeRadioGrp.getCheckedRadioButtonId() == R.id.fixed_dates_radio){
returnFixedDays.setVisibility(View.VISIBLE);
} else{
returnFixedDays.setVisibility(View.INVISIBLE);
}
}
However neither group is ever visible. I guess I need extra pair of eyes to spot where I’m going wrong in adding these layouts dynamically.
With this:
you’ve just inflated a layout file, you didn’t actually attached it to your custom view
ReturnFixedDays. To attach that inflated layout to your custom view you have to passthisas the second parameter(representing the parent of the newly inflated view hierarchy):There could be other things wrong in your code, so check and see if this solves your problem.