I know that to show a list of views I can use recycler view with but what if I wanna reuse also my fragment logic too?
for example, if I have a fragment like this
XML
<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=".views.fragments.testview">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hi"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment class
public class TestView extends Fragment {
private TextView txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.test, container, false);
initComponent(v);
return v;
}
private void hardTask1(){};
private void hardTask2(){};
//Some other hardtask
}
What I could do if I need to display a list of fragments in some activity but I don't want to rewrite all my TestView in the adapter?
Related
Bottom navigation SSI already done bottom Navigation in my app using android studio. So, I would like to create a button in one of my fragments. How can i do it? Any video references? Thank you.
It's so easy not much harder just put your button in your fragment layout then after creating the instance in your fragment and put listener for that particular button like bellow.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatButton
android:id="#+id/mBtnPresent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
#SuppressLint("ValidFragment")
public class FragmentTest extends Fragment {
private View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_test, container, false);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppCompatButton mBtnPresent=view.findViewById(R.id.mBtnPresent);
mBtnPresent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform action what you want on button click
}
});
}
}
I am new to the android platform so I know I am just missing something by here is what I have
<!-- activity_main -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.example_app.MainActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="#+id/mainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.example_app.MainFragment"
/>
</FrameLayout>
Then I created a fragment called MainFragment.java.
public class MainFragment extends Fragment {
public TextView displayTextView;
public MainFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
this.displayTextView = (TextView)rootView.findViewById(R.id.displayTextView);
return rootView;
}
}
I add the fragment in the OnCreate function in the activity and it seems to be adding everything okay but when I click on the button nothing happens, I logged the function to see its being called but setting the textview doesn't do anything. What am I doing wrong?
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.example_app.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button10"
android:textColor="#ffffff"
android:textSize="34dp"
android:text="10%"
android:layout_below="#id/amountEditText"
android:background="#drawable/pct_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doSomething"
/>
<TextView
android:text="Blah Blah Blah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/displayTextView"
></TextView>
</RelativeLayout>
And here is the click Event in the MainActivity.java
public void doSomething(View v) {
this.mainFragment.displayTextView.setText("Nothing to set for some reason!");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
//
this.mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.mainFragment);
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(mainFragment, "mainFragment").commit();
}
}
Declaring onClick-Listeners via XML is possible if you work with Activities. According to the Google API, however, you need to declare OnClickListeners programmatically if you use Fragments.
[...] You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.
Source:
http://developer.android.com/guide/topics/ui/controls/button.html
So you may want to use this instead:
public class MainFragment extends Fragment {
public TextView displayTextView;
public Button yourButton;
public MainFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
this.displayTextView = (TextView)rootView.findViewById(R.id.displayTextView);
yourButton = (Button)rootView.findViewById(R.id.button10);
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayTextView.setText("Nothing to set for some reason!");
}
});
return rootView;
}
}
The problem is the way you try to execute the button click. From your question i understand that Button and TextView is in Fragment view. So do why are Trying to access it in activity ?? It is wrong, then there is no point of using Fragments. Also this will lead to many serious issues later .
Fragments are Reusable components so all the functionalities of a fragment must stay inside the Fragment Class
Change Fragment code :
public class MainFragment extends Fragment {
public TextView displayTextView;
public Button yourButton;
public MainFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
this.displayTextView = (TextView)rootView.findViewById(R.id.displayTextView);
yourButton = (Button)rootView.findViewById(R.id.button10);
//Setting click listener for button in fragment
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayTextView.setText("Nothing to set for some reason!");
}
});
return rootView;
}
}
and remove android:onClick="doSomething" from XML
I try to set the text of a TextView programmatically within a Fragment.
The code for the classes is as follows:
public abstract class AbstractFragment extends Fragment
{
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data)
{
this.setFileName();
}
protected abstract void setFileName();
}
public class ImplementingFragment extends AbstractFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View fragment = inflater.inflate(
R.layout.fragment_layout, container, false);
return fragment;
}
#Override
protected void setFileName()
{
String fileName = "Test.txt";
TextView textView = ((TextView) getActivity().findViewById(
R.id.text_file_name));
textView.setText(fileName);
}
}
The Layout is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/text_pdf_filename_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_pdf_filename"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/text_pdf_file_name"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
The weird thing is: within one Fragment it works, in another it does not (same parent Activity). Another fact is that after I set the text, I get it via textView.getText(). If I try to get the text in later code, I just get an empty string.
Additionally, if I debug the code, I see the text a view milliseconds, before it disappears.
Does anyone has an solution how to fix that behaviour?
Inside fragment use:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView textView = ((TextView) rootView.findViewById(
R.id.text_file_name));
textView.setText(fileName);
return rootView;
}
I have an xml like this
activity_loginscreen.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="android.arin.LoginScreen"
tools:ignore="MergeRootFrame" />
and then gragment_loginscreen.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/LoginView"
tools:context="android.arin.LoginScreen$PlaceholderFragment" >
<ImageView
android:id="#+id/bubbles"
android:contentDescription="#string/bubbles_cd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:alpha=".75"
android:src="#drawable/bubbles" />
</RelativeLayout>
In my java file I have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
ImageView bubbles = (ImageView) findViewById(R.id.bubbles);
}
But bubbles ends up being null because it can't find it because its looking in the activity xml one, but really the imageview is in the fragment one, how can I get it to look there?
Thanks
Make your PlaceholderFragment fragment onCreateView(....) like
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gragment_loginscreen, container, false);
ImageView bubbles = (ImageView)view.findViewById(R.id.bubbles);
return view;
}
and used getActivity() as a Context in Fragment like
Animation animContentUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up_service);
Place the imageView in your PlaceholderFragment onCreateView and in the PlaceholderFragment inflate the layout gragment_loginscreen which will generate a view..
example:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gragment_loginscreen, container, false);
ImageView bubbles = (ImageView)view.findViewById(R.id.bubbles);
return view;
}
I have a problem with ViewPager. It won't display any of my pre-definied fragments, it just displays a black screen. The onCreateView method is called from inside the fragments.
public class TestViewPager extends FragmentActivity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.test_view_pager);
}
}
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.entry_layout, container, false);
return fragmentView;
}
}
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
class="com.test.TestFragment"
android:id="#+id/embedded1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
class="com.test.AnotherTestfragment"
android:id="#+id/embedded2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.view.ViewPager>
With ViewPager you must implement a PagerAdapter and supply the fragments from there. You can not define views/fragments to be shown in the ViewPager from XML, as it will simply not draw them.
Edit: Since you want to show Fragments, you must use FragmentPagerAdapter.