Add Viewpager in fragment - android-layout

Hello I created my app using fragments and I need to implement a viewpager in one of them (In that viewpager user can swipe between different information (about using the application that I am currently developing. I assume there will be 3 to 5 views with text and details)). Bellow is the code to current fragment. Can you guys give me an idea what would be the best way to implement Viewpager in fragment with let's say 3 views and every view will have like 3 or 4 things on it(Imageview,Textview etc). I have already implemented Viewpager in other activity but this is my first time I'm implementing it inside a fragment. What's the logic about it. I tried using google but it sound to complicated.
public class AboutSectionFragment extends Fragment {
// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about_section, parent, false);
// Defines the xml file for the fragment
return rootView ;
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
}

I have used ViewPager inside a fragment and its working fine for me.
Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_content, container, false);
fragments = getFragmentsList();
pagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager(),fragments);
ViewPager = (ViewPager) view.findViewById(R.id.Home_View_Pager);
ViewPager.setAdapter(pagerAdapter);
private ArrayList<Fragment> getFragmentsList(){
fragments = new ArrayList<>();
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
return fragments;
}
ViewPager Fragment Class
public class HomeViewPagerFragment extends Fragment {
public static HomeViewPagerFragment getInstance(){
HomeViewPagerFragment fragment = new HomeViewPagerFragment();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_pager_fragment_layout,container,false);
return view;
}
}

Related

How do I put the adapter into the viewpager?

This is the code of MyPageAdapter
public class MyPageAdapter extends PagerAdapter {
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, R.drawable.img13
, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17, R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23, R.drawable.img24
, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29, R.drawable.img30};
private LayoutInflater inflater;
private Context context;
public MyPageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == ((LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_slideshow, container, false);
EditText editText = v.findViewById(R.id.editTextNumberDecimal);
ImageView imageView = (ImageView)v.findViewById(R.id.imageView2);
imageView.setImageResource(images[position]);
int text = position+1;
editText.setText(text);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.invalidate();
}
}
AND this is the code of SlideshowFragment.
public class SlideshowFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
ViewPager2 viewPager2 = (ViewPager2) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager2.setAdapter(adapter);
return root;
}
}
I would like to add a view pager to the slide fragment so that 30 photos can be viewed as a slide show.
But I can't do setAdapter in viewpager.
Really, is there anything other than a way to make and put each fragment for each of the 30 pictures?
Please let me know how to modify my code.
You are mixing Viewpager and Viewpager2 these are implemented in 2 totally different ways.
try
ViewPager viewPager = (ViewPager) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager.setAdapter(adapter);
and change your xml to also be a ViewPager widget.
Alternatively you could probably change your adapter to a RecyclerView.Adapter https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter
As Viewpager2 is just a customised RecyclerView.

Firestore data retrieval not "instantaneous"

I am working with Android Studio and I want to retrieve data from Firestore and use the document data as information for individual list items in the RecyclerView container.
Basically the components are an Activity consisting of a Fragment which houses the RecyclerView. This is the code I used in my Fragment to obtain and display the data (the one in the code is a sample):
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_transport_requests_client, container, false);
firebaseAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
swipeContainer = view.findViewById(R.id.swipeContainer);
final ArrayList<String> list = new ArrayList<>();
DocumentReference documentReference = db.collection("transportRequests").document("T_client#eldereach.com_23 May 20 03:19 PM_");
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
assert document != null;
list.add((String) document.getData().get("email"));
}
}
});
listAdapter = new TransportRequestsListAdapter(list);
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
The issue here isn't that the code doesn't work, I think the retrieval works, however, nothing is displayed when the Fragment is launched. The data is only displayed when I click the Fragment. This doesn't occur when I just add random strings instead of data from the document, e.g. list.add("random data"). Any idea what is the issue here? Thanks in advance!

Passing context for a radio group in a fragment instead of activity?

I'm trying to use a RadioGroup widget inside a fragment instead of an activity. I'm getting an error on the following line of code:
RadioButton button = new RadioButton(this);
I'm not sure what to pass instead of 'this'
I believe the problem is either in the snippet above or here:
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
Full code:
public class fragment1 extends Fragment {
private Button btnNextFrag;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
createRadioButtons();
return view;
}
private void createRadioButtons() {
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
String[] questions1 = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<questions1.length;i++){
String question = questions1[i];
RadioButton button = new RadioButton(this);
button.setText(question);
group.addView(button);
}
}
}
The constructor requires context so use getActivity() instead of this. You can also use getContext().
public class fragment1 extends Fragment {
private Button btnNextFrag;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
createRadioButton(view);
return view;
}
private void createRadioButton(View view) {
RadioGroup group = (RadioGroup)view.findViewById(R.id.radioGroup);
String[] questions1 = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<questions1.length;i++){
String question = questions1[i];
RadioButton button = new RadioButton(getActivity());
button.setText(question);
group.addView(button);
}
}
}

How add custom dynamic layout in a dialog box?

i am new to android java programming. I am adding a custom dynamic layout to a dialog box. i have 2 classes , Mainactivity.java and Getval.java
The Mainactivity.java create a dialoge and Getval generates dynamic layout at run time.
Every thing works good but when it tried to call Getval.java's method, it gives an exception. here is the code of both classes.
The g.setval(); in Mainactivity.java cause problem.
:::::Mainactivity.java:::::
public class MainActivity extends Activity {
private Button button;
public Getval g=new Getval();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.buttonShowCustomDialog);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//set up dialog
g.setval();
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.attributewin);
dialog.setTitle("Attribute Window");
dialog.setCancelable(true);
dialog.show();
}
});
::::Getval::::
public class Getval extends Activity{
String names[]={"test","chaeck","kajsdhasj","dlasdig"};
String values[]={"test","chaeck","kajsdhasj","dlasdig"};
String test[]={"Attribut","Value"};
public void setval(){
TableLayout tl = (TableLayout) findViewById(R.id.tly);
Button b=new Button(this);
for (int i = 0; i < names.length; i++) {
//Row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//TextViews
TextView tv0 = new TextView(this);
tv0.setText(names[i]);
tv0.setMaxWidth(75);
TextView tv1 = new TextView(this);
tv1.setText(values[i]);
tv1.setMaxWidth(150);
View line = new View(this);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
line.setBackgroundColor(Color.BLACK);
//Setting Views
tr.addView(tv0);
tr.addView(tv1);
tl.addView(tr);
tl.addView(line);
}
//Button Row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
tr.setGravity(0x11);
b.setId(100);
b.setText("Return");
b.setGravity(0x11);
tr.addView(b);
tl.addView(tr);
}
}
Looking over this without seeing your xml or complete code (or exception that is thrown), I would be willing to guess that you are trying to inflate/modify a view (R.id.tly) from an activity other than the one where it was originally inflated from (MainActivity).
Generally speaking, you can't do this without doing some tricks with cross activity handlers or other methods. Perhaps change your code to inflate the views (R.id.tly) in MainActivity (and in your dialog if it is inside of R.layout.attributewin), do your processing in Getval, return them to MainActivity so it can populate the views.
But again, this is just a guess, since I don't see your xml or exception.

SherlockFragmentActivity with SherlockListFragment

I have 3 tabs and all of them has lists inside(different ones of course). When i click on tab I see the proper result(result which i would like to get).
I know how to get the clicked item info and make request to database about that item and get result and create new intent and put that result in it as a extra. BUT i don't know how I can display my result inside the same fragment without creating new intent. Because when i create new intent and show result in new intent my tabs obviously disappears, but i need them always to be there.
My main activity:
public class MainActivity extends SherlockFragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab = actionBar.newTab()
.setText("Dictionary")
.setTabListener(new DictionaryFragment())
.setIcon(R.drawable.android);
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Play")
.setTabListener(new PlayFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Manage")
.setTabListener(new ManageFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);
}
}
One of my fragments:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
built_in_DB_setup dbOpenHelper = new built_in_DB_setup(getActivity().getBaseContext(), DB_NAME);
database = dbOpenHelper.openDataBase();
fillFreinds();
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, friends);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
And here is the item click listener inside this class
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String a=getData(((TextView) v).getText().toString());
Intent i = new Intent("com.example.fragmenttest.VIEWWORD");
i.putExtra("result", a);
startActivity(i);
}
Ones I have String a which i have got from onListItemClick how can I replace current Tab data with this string?

Resources