I have setup this activity so that when you press the FloatingActionButton it creates a new XML Button and adds it to the LinearLayout. I want to manage these new buttons that I create, but I dont know how to. Do I need to give each button an individual name? Or put them in a button array? Here is my code and xml. Hope I explained this well enough.
Code:
public class CataFragment extends Fragment {
private LinearLayout mLayout;
private FloatingActionButton mButton;
private String newCataLine;
//private ArrayList<Button> buttonsList = new ArrayList<>();
public static CataFragment newInstance() {
CataFragment fragment = new CataFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cata, container,
false);
mLayout = (LinearLayout) rootView.findViewById(R.id.LinearLineLayout);
mButton = (FloatingActionButton) rootView.findViewById(R.id.plus);
mButton.setOnClickListener(onClick());
Button cataButton = new Button(getContext());
return rootView;
}
private View.OnClickListener onClick() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
showChangeLangDialog();
}
};
}
private TextView createNewTextView(String text) {
final LinearLayout.LayoutParams lparams =
new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final Button cataButton = new Button(getContext());
cataButton.setAllCaps(false);
cataButton.setTextSize(40);
cataButton.setLayoutParams(lparams);
cataButton.setText(text);
return cataButton;
}
public void showChangeLangDialog() {
final AlertDialog.Builder dialogBuilder = new
AlertDialog.Builder(getContext());
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle("Create New Category");
dialogBuilder.setPositiveButton("Add", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
newCataLine = edt.getText().toString();
mLayout.addView(createNewTextView(newCataLine));
}
});
dialogBuilder.setNegativeButton("Don't Add", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
Xml:
<android.support.constraint.ConstraintLayout
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"
tools:context=".HomeFragment">
<FrameLayout
android:id="@+id/frame_fragmentholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/LinearLineLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
android:src="@android:color/holo_blue_dark"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/InputText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/InputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/black"
android:cursorVisible="false"
android:inputType="textNoSuggestions"
android:textCursorDrawable="@null"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/plus"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
android:clickable="true"
android:src="@drawable/plus"
app:backgroundTint="@android:color/holo_blue_light"
app:elevation="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Since you are creating new buttons that are textviews in createNewTextView
you could create an ArrayList<TextView>
to store references to all of the text views you've created then you could access them by their index. If they have special ids you could instead use a map for easy retrieval.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.