0

I'm pretty new to Android Development and swamped by the possibilities.

I have a fragment, in which I reused a layout component (the searchbar). I also want to reuse the (java) code, that handels the logic of this searchbar like OnClickListeners. How can I do that in my code?

I am happy for any help, even if it's just the right keywords to find what I'm looking for!

This is the SearchBar.java

public class SearchBar extends AppCompatActivity {
    EditText searchText;

    public SearchBar() {
        searchText = (EditText) findViewById(R.id.searchbarSearchText);

        searchText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                // Hit Enter
                if (actionId == EditorInfo.IME_NULL
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    searchForText("lol");
                    return true;
                }
                //Hit Search on Keyboard
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    searchForText("lol");
                    return true;
                }
                return false;
            }});
    }

This is the searchbar.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/width_small_layout"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/button"
    android:backgroundTint="@color/white_variant"
    android:layout_gravity="center_horizontal"

    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="@dimen/icons_big"
        android:layout_height="@dimen/icons_big"
        android:layout_marginStart="@dimen/margin_elements_big"
        android:layout_marginTop="@dimen/margin_elements_medium"
        android:layout_marginBottom="@dimen/margin_elements_medium"
        android:src="@drawable/search_icon"
        android:tintMode="src_in"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tint="@color/black_main" />

    <EditText
        android:id="@+id/searchbarSearchText"
        android:layout_width="wrap_content"
        android:maxWidth="254dp"
        android:layout_height="@dimen/text_highlight_body"
        android:layout_marginStart="@dimen/margin_elements_medium"
        android:textSize="@dimen/text_body"
        android:textColor="@color/black_main"
        android:background="@null"
        android:hint="@string/cerca_trova"
        android:textColorHint="@color/dark_grey"
        android:fontFamily="@font/body"
        android:inputType="text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent"
        android:imeOptions="actionSearch"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Here is the OverviewAnimaliFragment.java in which the search should take place

public class OverviewAnimaliFragment extends Fragment {

    public OverviewAnimaliFragment() {
        // Required empty public constructor
    }


    public static OverviewAnimaliFragment newInstance() {
        OverviewAnimaliFragment fragment = new OverviewAnimaliFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_overview_animali, container, false);

    }
}

And this is the corresponding layout fragment_overview_animali

<androidx.constraintlayout.widget.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=".designclasses.OverviewAnimaliFragment">

    <include
        android:id="@+id/include"
        layout="@layout/searchbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_layout_big"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="@dimen/padding_layout_big"/>

If I understand the Google Documentation on that topic correctly, the concept behind this is called ViewModel and it is possible to add a ViewModel dynamically (as discussed here), but I want to add that only for the included layout (the searchbar in this case).

I could copy all the code from SearchBar.javainto the onCreate() method of my fragment, but this destroys the whole aspect of testability and reusability.

What can I do?

0 Answers0