0
I am learning to develop using Android Studio and I am with a beginner’s question: which class I perform data processing from a click of a fragment in a Tabbed Activity. To illustrate simply, I created a simple application using Tabbed Activity with a fragment containing an Edit text and a button. In the code of the screen, I try to call from the click of the button a class called "saved":
<Button
android:id="@+id/button3"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginStart="132dp"
android:layout_marginTop="61dp"
android:layout_marginEnd="191dp"
android:layout_marginBottom="382dp"
android:text="Button"
android:onClick="salvaDados"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
The idea (at the moment) is simply to test the value of Edit text, with the following code:
public void salvaDados(){
Button botao = findViewById(R.id.button3);
EditText texto = findViewById(R.id.editText);
if(texto.getText().length() == 0){
Toast.makeText(getApplication(),"nao tem nada",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getApplication(),"tem dados",Toast.LENGTH_SHORT).show();
}
The question is, in what class do I treat it? 1. I tried to create the above class in fragment "mother activity" and gave error; 2. I tried to create a separate class only to process fragment data and also gave error. 3. the above example is just for me to know where I should treat all the code, since later I will do more complex things like save the data in the database, generate graphs on other screens, etc.
It has at least 15 hours that I am trying to solve this and I could not find any tutorial to help. Can someone give me a light? Thanks for your help!
Send the class of your fragment where the button is and edittext is. By the fragment you can do this, depends on how you are doing. Inside the fragment in the onCreateView method, you have how to call.
– Murillo Comino
Hi Murillo, I tried to call you from the fragment and I still can’t. At present, the onCreateView class simply calls the layout with Layoutinflater Inflater and does not allow using the findViewById method. I have to declare that I am inheriting the layout screen data elsewhere on onCreate?
– Wellington
have as yes, I will answer on the basis of your code, then you implement and give me a return to see if it works.
– Murillo Comino