<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="com.demo.Model" />
<variable
name="model"
type="Model" />
</data>
<Textview
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorLableBg"
android:ems="10"
android:text="#={model.name}"
android:textCursorDrawable="#null/>
</layout>
Here is fragment
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_authentication, container, false);
Model model = getModel()// get model return model data from server
binding.setModel(model);
binding.text.setText("This is demo")
}
I am getting data from api call and set the model.everything fine in case but when i set text its noting to change.
you need to do set that value after setting model otherwise value will be overwrite.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_authentication, container, false);
Model model = getModel()// get model return model data from server
binding.setModel(model);
binding.getModel.setName("this is demo");
}
You can try it:
Model model = getModel()// get model return model data from server
binding.setModel(model);
model.name.set("This is demo")
in your Model make sure name is:
public ObservableField<String> name = new ObservableField<>();
Related
In turn of data, I have checked that my photo url has already passed into Glide in the binding adapter. Why the image view is not visible under DialogFragment/BottomSheetDialogFragment?
My Dialog Layout xml with data binding
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="photoUrl"
type="String" />
</data>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:photoUrl='#{photoUrl}'
android:adjustViewBounds="true">
</ImageView>
</layout>
My BottomSheetDialogFragment.java
public class MyDialog extends BottomSheetDialogFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = MyDialogBinding.inflate(inflater, container, false).getRoot();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.setVariable(BR.photoUrl, MyDialogArgs.fromBundle(getArguments()).getPhotoUrl());
}
}
Remark:
I know there is a workaround method (i.e. Nest the image view in a frame layout or linear layout). I just curious why image view does not work as root view of Dialog.
public class GroupFragment extends Fragment {
private View groupFragmentView;
private ListView list_View;
here i have created instances as well
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
groupFragmentView = inflater.inflate(R.layout.fragment_group, container, false);
InitializeFields();
return groupFragmentView;
}
`
private void InitializeFields()
{
list_View = (ListView) groupFragmentView.findViewById(android.R.id.**list_View**);
}}
1 now here im getting error for listView
2 on the left side of equals im having no issue even for the casting there is no error
3 i have tried everything but still couldnt resolve it
4 im still learning android
error showing is-----
error: cannot find symbol
list_View = (ListView) groupFragmentView.findViewById(android.R.id.list_View);
^
symbol: variable list_View
location: class id
`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".GroupFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
`
You have to pass the object of view in your method.
#Override public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
groupFragmentView=inflater.inflate(R.layout.fragment_group,container,false);
InitializeFields(groupFragmentView);
return groupFragmentView;
}
private void InitializeFields(View view)
{
list_View = (ListView) view.findViewById(android.R.id.**list_View**);}
I want to use onClick method in fragment but data binding doesn't see method.
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
FragmentTodayBinding fragmentTodayBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_today, container, false);
View view = fragmentTodayBinding.getRoot();
final MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
fragmentTodayBinding.setMainViewModel(mainViewModel);
fragmentTodayBinding.searchButton.setOnClickListener(this::onSearchClick);
});
return view;
}
#Override
public void onSearchClick(View v) {
mainViewModel.getCity();
Toast.makeText(getActivity(), "X", Toast.LENGTH_SHORT).show();
}
Button xml:
<Button
android:id="#+id/search_button"
android:onClick="#{(v)-> todayFragmentInterface.onSearchClick(v)}"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/ic_search_white_24dp" />
How to use it properly in fragment?
Follow this step:
1) Add this line in onCreateView
fragmentTodayBinding.fragment = this;
2) Add this line in your layout.xml
<variable name="fragment" type="yourPackage.ClassName" />
3) Edit onClick in xml with
android:onClick="#{(v)-> fragment.onSearchClick(v)}"
You can also remove:
fragmentTodayBinding.searchButton.setOnClickListener(this::onSearchClick);
I saw that you use #Override so needs an Interface, the goal is the same, just use variable interface in the xml variable (and refer to it with package of interface), than use fragmentTodayBinding.myInterface = this;
So:
1) fragmentTodayBinding.myInterface = this;
2) <variable name="myInterface" type="yourPackage.Interface" />
3) android:onClick="#{(v)-> myInterface.onSearchClick(v)}"
I would like to use a Tab/Fragment to show RecyclerView, but I don't know how to do it because the method of the Fragment class return a View, not a Reclycler View:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
return myFragmentView;
}
The code of the fragment.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".NeedsFragment">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/all_user_need_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
</RelativeLayout>
Thank u.
First off, your onCreateView() method makes no sense:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
return needsFragmentRecyclerView; //you should be returning myFragmentView here, not whatever needsFragmentRecyclerView is
}
Second, once you inflate your layout, just use findViewById():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
RecyclerView recyclerView = myFragmentView.findViewById(R.id.all_user_need_list); //assign to a global variable if needed
return myFragmentView; //replaced with the proper variable
}
Alternatively, you can get the RecyclerView from anywhere in your Fragment, as long as it's called after onCreateView() with the following:
RecyclerView recyclerView = getView().findViewById(R.id.all_user_need_list);
You can return any type of view component and Custom view that is inherit from android.view class in onCreateView() method.
Hello I have a problem when I search the page tomorrow the process is successful but when I search the page today there is a problem , I used Fragment
the problem here : https://www.youtube.com/watch?v=FiHGME6i97I&feature=youtu.be
codes :
public void btn_search(View view) {
TextView txtexample = (TextView)findViewById(R.id.txtexample);
txtexample.setText("hello");
}
fragment days
<FrameLayout 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"
tools:context="com.example.user.weather.Fragment_days">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/txtexample"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
fragment days
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_days, container, false);
return v;
}
and fragment today and tomorrow are empty.
please help me , two weeks ago I was looking for a problem
Use the following (note two lines with txtexample):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_days, container, false);
TextView txtexample = (TextView)v.findViewById(R.id.txtexample);
return v;
}
and
public void btn_search(View view) {
txtexample.setText("hello");
}