I would like to be able to add a custom view class to a specific layout using layout_name.addView(CUSTOM VIEW CLASS INSTANCE), or add them to a list view.
I intend to add many of these views programmatically, and would like to be able to attain the values of edit texts/spinners etc for each view individually.
When I attempt to add this custom view nothing appears, I also receive this error the second time the view is added.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3577)
at android.view.ViewGroup.addView(ViewGroup.java:3430)
at android.view.ViewGroup.addView(ViewGroup.java:3375)
at android.view.ViewGroup.addView(ViewGroup.java:3351)
at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:192)
at android.view.View.performClick(View.java:4442)
at android.view.View$PerformClick.run(View.java:18473)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
What can I do to add this WorkoutView with a specific layout, into a LinearLayout which is part of the original layout inflated for the View which is used by setContentView()?
UPDATE:
I am trying to insert this view, as many times as necessary into a layout which is housed into a parent layout which is inflated for the view to be setContentView().
R.id.create_fragment_layout (used in original setContentView())
R.id.workout_layout (inside the create_fragment_layout)
I want to insert custom view into this layout, and this custom view needs to have its own R.id.somename_layout with many elements inside.
WorkoutView (Custom Class)
public class WorkoutView extends LinearLayout implements View.OnClickListener {
int res_id;
public Spinner spinnerDifficulty, spinnerTime, spinnerMuscleGroup, spinnerSets, spinnerReps;
public EditText etWorkoutName;
public Button btnRemoveWorkout;
String workoutName, muscleGroup, sets, reps;
LayoutInflater inflater;
public WorkoutView(Context context) {
super(context);
}
public WorkoutView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WorkoutView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void init() {
inflate(getContext(), R.layout.single_workout_layout, this);
spinnerDifficulty = (Spinner) findViewById(R.id.spinnerDifficulty);
spinnerReps = (Spinner) findViewById(R.id.spinnerReps);
spinnerSets = (Spinner) findViewById(R.id.spinnerSets);
spinnerMuscleGroup = (Spinner) findViewById(R.id.spinnerWorkoutGroup);
spinnerTime = (Spinner) findViewById(R.id.spinnerTime);
etWorkoutName = (EditText) findViewById(R.id.etWorkoutName);
btnRemoveWorkout = (Button) findViewById(R.id.btnRemoveWorkout);
btnRemoveWorkout.setOnClickListener(this);
}
public String getReps() {
reps = spinnerReps.getSelectedItem().toString();
return reps;
}
public String getWorkoutName() {
workoutName = etWorkoutName.getText().toString().trim();
return workoutName;
}
public String getMuscleGroup() {
muscleGroup = spinnerMuscleGroup.getSelectedItem().toString();
return muscleGroup;
}
public String getSets() {
sets = spinnerSets.getSelectedItem().toString();
return sets;
}
public SingleWorkout getAll() {
//to be designed
return null;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnRemoveWorkout:
Log.e("DID THIS WORK?", "BUTTON PRESSED");
break;
}
}
}
How I am creating the view
workoutView = new WorkoutView(getActivity());
How I am adding the view to a LinearLayout
workoutLayout.addView(workoutView);