Adds Monster fragment to view a monster's card.
Adds query box and search button to search fragment. Makes the search button show a monster card.
This commit is contained in:
		| @@ -0,0 +1,35 @@ | |||||||
|  | package com.majinnaibu.monstercards.ui.monster; | ||||||
|  |  | ||||||
|  | import android.os.Bundle; | ||||||
|  | import android.view.LayoutInflater; | ||||||
|  | import android.view.View; | ||||||
|  | import android.view.ViewGroup; | ||||||
|  | import android.widget.TextView; | ||||||
|  |  | ||||||
|  | import androidx.annotation.NonNull; | ||||||
|  | import androidx.annotation.Nullable; | ||||||
|  | import androidx.fragment.app.Fragment; | ||||||
|  | import androidx.lifecycle.Observer; | ||||||
|  | import androidx.lifecycle.ViewModelProviders; | ||||||
|  |  | ||||||
|  | import com.majinnaibu.monstercards.R; | ||||||
|  |  | ||||||
|  | public class MonsterFragment extends Fragment { | ||||||
|  |  | ||||||
|  |     private MonsterViewModel monsterViewModel; | ||||||
|  |  | ||||||
|  |     public View onCreateView(@NonNull LayoutInflater inflater, | ||||||
|  |                              ViewGroup container, Bundle savedInstanceState) { | ||||||
|  |         monsterViewModel = | ||||||
|  |                 ViewModelProviders.of(this).get(MonsterViewModel.class); | ||||||
|  |         View root = inflater.inflate(R.layout.fragment_monster, container, false); | ||||||
|  |         final TextView textView = root.findViewById(R.id.text_monster); | ||||||
|  |         monsterViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() { | ||||||
|  |             @Override | ||||||
|  |             public void onChanged(@Nullable String s) { | ||||||
|  |                 textView.setText(s); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         return root; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,19 @@ | |||||||
|  | package com.majinnaibu.monstercards.ui.monster; | ||||||
|  |  | ||||||
|  | import androidx.lifecycle.LiveData; | ||||||
|  | import androidx.lifecycle.MutableLiveData; | ||||||
|  | import androidx.lifecycle.ViewModel; | ||||||
|  |  | ||||||
|  | public class MonsterViewModel extends ViewModel { | ||||||
|  |  | ||||||
|  |     private MutableLiveData<String> mText; | ||||||
|  |  | ||||||
|  |     public MonsterViewModel() { | ||||||
|  |         mText = new MutableLiveData<>(); | ||||||
|  |         mText.setValue("This is monster fragment"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public LiveData<String> getText() { | ||||||
|  |         return mText; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -4,6 +4,7 @@ import android.os.Bundle; | |||||||
| import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||||
| import android.view.View; | import android.view.View; | ||||||
| import android.view.ViewGroup; | import android.view.ViewGroup; | ||||||
|  | import android.widget.Button; | ||||||
| import android.widget.TextView; | import android.widget.TextView; | ||||||
|  |  | ||||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||||
| @@ -11,6 +12,8 @@ import androidx.annotation.Nullable; | |||||||
| import androidx.fragment.app.Fragment; | import androidx.fragment.app.Fragment; | ||||||
| import androidx.lifecycle.Observer; | import androidx.lifecycle.Observer; | ||||||
| import androidx.lifecycle.ViewModelProviders; | import androidx.lifecycle.ViewModelProviders; | ||||||
|  | import androidx.navigation.NavDirections; | ||||||
|  | import androidx.navigation.Navigation; | ||||||
|  |  | ||||||
| import com.majinnaibu.monstercards.R; | import com.majinnaibu.monstercards.R; | ||||||
|  |  | ||||||
| @@ -23,13 +26,22 @@ public class SearchFragment extends Fragment { | |||||||
|         searchViewModel = |         searchViewModel = | ||||||
|                 ViewModelProviders.of(this).get(SearchViewModel.class); |                 ViewModelProviders.of(this).get(SearchViewModel.class); | ||||||
|         View root = inflater.inflate(R.layout.fragment_search, container, false); |         View root = inflater.inflate(R.layout.fragment_search, container, false); | ||||||
|         final TextView textView = root.findViewById(R.id.text_search); |         final TextView textView = root.findViewById(R.id.search_query); | ||||||
|         searchViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() { |         searchViewModel.getSearchQuery().observe(getViewLifecycleOwner(), new Observer<String>() { | ||||||
|             @Override |             @Override | ||||||
|             public void onChanged(@Nullable String s) { |             public void onChanged(@Nullable String s) { | ||||||
|                 textView.setText(s); |                 textView.setText(s); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|  |         final Button btnSearch = root.findViewById(R.id.button_search); | ||||||
|  |         btnSearch.setOnClickListener(new View.OnClickListener() { | ||||||
|  |             @Override | ||||||
|  |             public void onClick(View view) { | ||||||
|  |                 NavDirections action = SearchFragmentDirections.actionNavigationSearchToNavigationMonster(); | ||||||
|  |                 Navigation.findNavController(view).navigate(action); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|         return root; |         return root; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,14 +6,14 @@ import androidx.lifecycle.ViewModel; | |||||||
|  |  | ||||||
| public class SearchViewModel extends ViewModel { | public class SearchViewModel extends ViewModel { | ||||||
|  |  | ||||||
|     private MutableLiveData<String> mText; |     private MutableLiveData<String> mSearchQuery; | ||||||
|  |  | ||||||
|     public SearchViewModel() { |     public SearchViewModel() { | ||||||
|         mText = new MutableLiveData<>(); |         mSearchQuery = new MutableLiveData<>(); | ||||||
|         mText.setValue("This is search fragment"); |         mSearchQuery.setValue(""); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public LiveData<String> getText() { |     public LiveData<String> getSearchQuery() { | ||||||
|         return mText; |         return mSearchQuery; | ||||||
|     } |     } | ||||||
| } | } | ||||||
							
								
								
									
										22
									
								
								app/src/main/res/layout/fragment_monster.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/src/main/res/layout/fragment_monster.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | <?xml version="1.0" encoding="utf-8"?> | ||||||
|  | <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=".ui.monster.MonsterFragment"> | ||||||
|  |  | ||||||
|  |     <TextView | ||||||
|  |         android:id="@+id/text_monster" | ||||||
|  |         android:layout_width="match_parent" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:layout_marginStart="8dp" | ||||||
|  |         android:layout_marginTop="8dp" | ||||||
|  |         android:layout_marginEnd="8dp" | ||||||
|  |         android:textAlignment="center" | ||||||
|  |         android:textSize="20sp" | ||||||
|  |         app:layout_constraintBottom_toBottomOf="parent" | ||||||
|  |         app:layout_constraintEnd_toEndOf="parent" | ||||||
|  |         app:layout_constraintStart_toStartOf="parent" | ||||||
|  |         app:layout_constraintTop_toTopOf="parent" /> | ||||||
|  | </androidx.constraintlayout.widget.ConstraintLayout> | ||||||
| @@ -4,20 +4,30 @@ | |||||||
|     xmlns:tools="http://schemas.android.com/tools" |     xmlns:tools="http://schemas.android.com/tools" | ||||||
|     android:layout_width="match_parent" |     android:layout_width="match_parent" | ||||||
|     android:layout_height="match_parent" |     android:layout_height="match_parent" | ||||||
|     tools:context=".ui.search.SearchFragment"> |     tools:context=".ui.search.SearchFragment" | ||||||
|  |     tools:targetApi="o"> | ||||||
|  |  | ||||||
|     <TextView |     <Button | ||||||
|         android:id="@+id/text_search" |         android:id="@+id/button_search" | ||||||
|         android:layout_width="match_parent" |         android:layout_width="wrap_content" | ||||||
|  |         android:layout_height="0dp" | ||||||
|  |         android:layout_marginEnd="8dp" | ||||||
|  |         android:text="@string/action_search" | ||||||
|  |         app:layout_constraintBaseline_toBaselineOf="@+id/search_query" | ||||||
|  |         app:layout_constraintEnd_toEndOf="parent" /> | ||||||
|  |  | ||||||
|  |     <EditText | ||||||
|  |         android:id="@+id/search_query" | ||||||
|  |         android:layout_width="0dp" | ||||||
|         android:layout_height="wrap_content" |         android:layout_height="wrap_content" | ||||||
|         android:layout_marginStart="8dp" |         android:layout_marginStart="8dp" | ||||||
|         android:layout_marginTop="8dp" |         android:layout_marginTop="8dp" | ||||||
|         android:layout_marginEnd="8dp" |         android:layout_marginEnd="8dp" | ||||||
|         android:textAlignment="center" |         android:importantForAutofill="no" | ||||||
|         android:textSize="20sp" |         android:ems="10" | ||||||
|         app:layout_constraintBottom_toBottomOf="parent" |         android:inputType="textPersonName" | ||||||
|         app:layout_constraintEnd_toEndOf="parent" |         android:hint="@string/label_search_query" | ||||||
|  |         app:layout_constraintEnd_toStartOf="@+id/button_search" | ||||||
|         app:layout_constraintStart_toStartOf="parent" |         app:layout_constraintStart_toStartOf="parent" | ||||||
|         app:layout_constraintTop_toTopOf="parent" /> |         app:layout_constraintTop_toTopOf="parent" /> | ||||||
|  |  | ||||||
| </androidx.constraintlayout.widget.ConstraintLayout> | </androidx.constraintlayout.widget.ConstraintLayout> | ||||||
| @@ -4,4 +4,6 @@ | |||||||
|     <string name="title_search">Search</string> |     <string name="title_search">Search</string> | ||||||
|     <string name="title_collections">Collections</string> |     <string name="title_collections">Collections</string> | ||||||
|     <string name="title_library">Library</string> |     <string name="title_library">Library</string> | ||||||
|  |     <string name="action_search">Search</string> | ||||||
|  |     <string name="label_search_query">Query</string> | ||||||
| </resources> | </resources> | ||||||
| @@ -7,6 +7,9 @@ buildscript { | |||||||
|     dependencies { |     dependencies { | ||||||
|         classpath "com.android.tools.build:gradle:4.0.1" |         classpath "com.android.tools.build:gradle:4.0.1" | ||||||
|  |  | ||||||
|  |         def nav_version = "2.3.0" | ||||||
|  |         classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" | ||||||
|  |  | ||||||
|         // NOTE: Do not place your application dependencies here; they belong |         // NOTE: Do not place your application dependencies here; they belong | ||||||
|         // in the individual module build.gradle files |         // in the individual module build.gradle files | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user