I have three Edit text 1)is for service 2)is for charges 3)is for time so i don't know how many service a beauty saloon is given .so I make a dynamic layout where the service provider choose there service dynamically .I'm using two layout One is parent layout and second is child view Layout ..all work is good but when it pick the time. the time picker dialog box open only in parent layout .it cant open in child view layout ..if some one know please help me ..here is my complete code and picture
childview.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="50dp"
android:orientation="horizontal"
>
<EditText
android:id="@+id/service"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:hint="@string/edittext_service"
android:inputType="text"/>
<EditText
android:id="@+id/charges"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:hint="@string/edittext_charge"
android:inputType="number"/>
<EditText
android:id="@+id/time"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:hint="@string/edittext_time"
android:inputType="number"/>
<Button
android:id="@+id/Sub"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
ServicesAndRateList.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="false">
<LinearLayout 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:orientation="vertical"
android:padding="16dp"
android:id="@+id/parent_linear_layout"
tools:context=".ServicesAndRateList">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/service"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:hint="@string/edittext_service"
android:inputType="text"/>
<EditText
android:id="@+id/charges"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:hint="@string/edittext_charge"
android:inputType="number"/>
<EditText
android:id="@+id/time"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:hint="@string/edittext_time"
android:inputType="number"/>
<Button
android:id="@+id/Sub"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:onClick="INSERT"
android:background="@android:drawable/ic_delete"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="vertical">
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/ripple"
android:layout_gravity="center"
android:onClick="onAddField"
android:textColor="#000"
android:text="Add Field"
android:paddingLeft="5dp"/>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/ripple"
android:layout_gravity="center"
android:onClick="INSERT"
android:textColor="#000"
android:text="Register"
android:paddingLeft="5dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
ServicesAndRateList.java
public class ServicesAndRateList extends AppCompatActivity {
EditText service, charge, time;
TimePickerDialog timePickerDialog;
private LinearLayout parentLinearLayout;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services_and_rate_list);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
time = findViewById(R.id.time);
button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < parentLinearLayout.getChildCount() - 1; i++) {
service = (EditText) (parentLinearLayout.getChildAt(i).findViewById(R.id.service));
charge = (EditText) (parentLinearLayout.getChildAt(i).findViewById(R.id.charges));
time = (EditText) (parentLinearLayout.getChildAt(i).findViewById(R.id.time));
String Service = service.getText().toString();
String Charge = charge.getText().toString();
String Time = time.getText().toString();
if (TextUtils.isEmpty(Service)) {
service.requestFocus();
} else if (TextUtils.isEmpty(Charge)) {
charge.requestFocus();
} else if (TextUtils.isEmpty(Time)) {
time.requestFocus();
}
}
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timePickerDialog = new TimePickerDialog(ServicesAndRateList.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
time.setText(hourOfDay + ":" + minutes);
}
}, 0, 0, true);
timePickerDialog.show();
}
});
}
public void onDelete(View view) {
parentLinearLayout.removeView((View) view.getParent());
}
public void onAddField(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.childview, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
}
picture of my xml
The problem is that you setting View.ClickListener
only to a single view EditText time
that is a direct child of R.layout.activity_services_and_rate_list
.
Later you add more child views to inflated R.layout.activity_services_and_rate_list
layout but not setting click listeners.
One of the options would be to create a method that will set click listener for each passed in time EditText
.
Note: Consider using RecyclerView
with Adapter. It has much better performance when the number of views grows in your layout.
public class ServicesAndRateList extends AppCompatActivity {
//your variables here ...
@Override
protected void onCreate(Bundle savedInstanceState) {
//your other onCreate code here ...
setTimeEditTextClickListener(time);
}
private void setTimeEditTextClickListener(EditText time) {
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timePickerDialog = new TimePickerDialog(ServicesAndRateList.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
time.setText(hourOfDay + ":" + minutes);
}
}, 0, 0, true);
timePickerDialog.show();
}
});
}
public void onAddField(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.childview, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
setTimeEditTextClickListener((EditText) rowView.findViewById(R.id.time));
}
}