How to add function in fragment - android

EDIT:
I'm new in android and i try to use fragments in my app. I try to use a fragment function in my activity but i got a null pointer exception..
Fragment:
public class VideoviewFragment extends Fragment {
private View card;
public static VideoviewFragment create() {
return new VideoviewFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_videoview, container, false);
card = (View) view.findViewById(R.id.card_video);
return view;
}
public void setalphacard(float value){
card.setAlpha(value);
}
}
In main activity i want to do that for example (i simplified my code):
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Viewpager
final SmartFragmentStatePagerAdapter adapterViewPager;
final ViewPager viewPager = (ViewPager) findViewById(R.id.am_view_pager);
adapterViewPager = new MainPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapterViewPager);
final VideoviewFragment videoviewFragment = (VideoviewFragment) adapterViewPager.getRegisteredFragment(2);
videoviewFragment.setalphacard(0.3f);
}
}
Main pager adapter:
public class MainPagerAdapter extends SmartFragmentStatePagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return CameraFragment.create();
case 1:
return OptionFragment.create();
case 2:
return VideoviewFragment.create();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}

You just have to declare the View globally like this:
public class VideoviewFragment extends Fragment {
private View card;
public static VideoviewFragment create() {
return new VideoviewFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_videoview, container, false);
card = (View) view.findViewById(R.id.card_video);
return view;
}
public void setalphacard(float value){
card.setAlpha(value);
}
}

Declare your View Object globally in your fragment.

Related

How to set few fragments that have same structure in viewpager?

I'm a beginner who learning Android basic course
I want to make 6 fragments that have same structure (only title, image, texts will be different) by using single fragment.
I tried this code but I can see only one fragment.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.fragment_container);
MoviePagerAdapter adapter = new MoviePagerAdapter(getSupportFragmentManager());
mainfragment = new MainFragment();
adapter.addItem(mainfragment);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(6);
}
ViewPager Adapter (in MainActivity)
class MoviePagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<Fragment> items = new ArrayList<Fragment>();
#Override
public Fragment getItem(int position) {
return items.get(position);
}
public MoviePagerAdapter(#NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
public void addItem(Fragment item) {
items.add(item);
}
#Override
public int getCount() {
return items.size();
}
}
MainFragment.java
public class MainFragment extends androidx.fragment.app.Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment, container,
false);
//...
'i want to make 6fragments with datas below '
movieData = new MovieData("title1", "rate1", "grade1");
addData(movieData);
movieData = new MovieData("title2", "rate2", "grade2");
addData(movieData);
movieData = new MovieData("title3", "rate3", "grade3");
addData(movieData);
movieData = new MovieData("title4", "rate4", "grade4");
addData(movieData);
movieData = new MovieData("title5", "rate5", "grade5");
addData(movieData);
movieData = new MovieData("title6", "rate6", "grade6");
addData(movieData);
return rootView;
}
public void addData(MovieData movieData){
Bundle bundle = new Bundle();
bundle.putParcelable("data", movieData);
MainFragment mainFragment = new MainFragment();
mainFragment.setArguments(bundle);
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}
change your code like this:
public class MainActivity extends AppCompatActivity {
private MoviePagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.fragment_container);
adapter = new MoviePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(6);
for(int i =0;i<6;i++){
createFrag(i);
}
}
}
private void createFrag(int number) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putInt("NUMBER",number);
fragment.setArguments(args);
adapter.addItem(fragment);
adapter.notifyDataSetChanged();
}
class MoviePagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<Fragment> items = new ArrayList<Fragment>();
#Override
public Fragment getItem(int position) {
return items.get(position);
}
public MoviePagerAdapter(#NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
public void addItem(Fragment item) {
items.add(item);
}
#Override
public int getCount() {
return items.size();
}
}
and change your Fragment like this:
public class MainFragment extends androidx.fragment.app.Fragment {
//1st
private int number;
#Nullable
//this is variant 1 - with static Movie data
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment,
container,
false);
//...
//2d
assert getArguments() != null;
number = getArguments().getInt("NUMBER");
switch (number) {
case 0:
MmovieData movieData1 = new MovieData("title1", "rate1",
"grade1");
addData(movieData1);
break;
case 1:
MmovieData movieData2 = new MovieData("title2", "rate2",
"grade2");
addData(movieData2);
break;
//...
}
return rootView;
}
public void addData(MovieData movieData){
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}
and this is variant 2 ( with dynamic data and listener)
public class MainFragment extends androidx.fragment.app.Fragment {
//1st
private int number;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment,
container,
false);
//...
//2d
assert getArguments() != null;
number = getArguments().getInt("NUMBER");
return rootView;
}
//some method of listener changed
public void dataChanged(MovieData movieData, int position){
if(number==position){
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}

New Activity intent onClick in viewPager Slides

Below is my Code.
Using ViewPager I have made 2 XML file for two Pages and their Class file.
Now I need if I click on First Screen of ViewPager, a new activity should launch.
I got 2 pages, so If I Click First Screen, A.class intent Called. If I click on Second Screen, B.class intent should be Called.
Codes:-
MainActvity:
public class MainActivity extends ActionBarActivity {
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.pager);
/** set the adapter for ViewPager */
mViewPager.setAdapter(new SamplePagerAdapter(
getSupportFragmentManager()));
}
/** Defining a FragmentPagerAdapter class for controlling the fragments to
be shown when user swipes on the screen. */
public class SamplePagerAdapter extends FragmentPagerAdapter {
public SamplePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return new SampleFragment();
} else
return new SampleFragmentTwo();
}
#Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}
}
SampleFragment.java
public class SampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, null);
return rootView;
}
}
SampleFragmentTwo.java
public class SampleFragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_two, container,
false);
return rootView;
}
}
CustomSwipeAdapter:
public class CustomSwipeAdapter extends PagerAdapter {
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
}
As mentioned in one of the comments, In your fragment classes A and B put onClick listener on views and launch intent from there like below:
public class SampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, null);
rootView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// do something when the button is clicked
Intent activityA = new Intent(getActivity(),ActivityA.class);
startActivity(activityA);
}
return rootView;
}
}
similarlly goes for your second fragment.

android onClick change fragment

I want to change the Fragment when i click the button. The fragments are in the same adapter/viewpager.
FROM THIS FRAGMENT:
public class LoginFragment extends Fragment {
TextView linkToRegister;
public static final LoginFragment newInstance()
{
LoginFragment mf = new LoginFragment();
Bundle bd = new Bundle(1);
mf.setArguments(bd);
return mf;
}
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState )
{
View v = inflater.inflate(R.layout.fragment_login, container, false);
linkToRegister = (TextView) v.findViewById(R.id.link_to_register);
linkToRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Change Fragment
}
});
return v;
}
}
TO THIS FRAGMENT:
public class RegisterFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final RegisterFragment newInstance()
{
RegisterFragment mf = new RegisterFragment();
Bundle bd = new Bundle(1);
mf.setArguments(bd);
return mf;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View v = inflater.inflate(R.layout.fragment_register, container, false);
return v;
}
}
PagerAdapter:
public class WelcomePagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> Welcomefragments;
public WelcomePagerAdapter(FragmentManager fm, List<Fragment> Welcomefragments) {
super(fm);
this.Welcomefragments = Welcomefragments;
}
#Override
public Fragment getItem(int position) {
return this.Welcomefragments.get(position);
}
#Override
public int getCount() {
return this.Welcomefragments.size();
}
}
Activity:
public class WelcomeActivity extends AppCompatActivity {
ViewPager welcomeViewPager;
WelcomePagerAdapter welcomePagerAdapter;
List<Fragment> welcomeFragments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
welcomeViewPager = (ViewPager) findViewById(R.id.viewPagerWelcome);
welcomeFragments = getWelcomeFragments();
welcomePagerAdapter = new WelcomePagerAdapter(getSupportFragmentManager(), welcomeFragments);
welcomeViewPager.setAdapter(welcomePagerAdapter);
}
public List<Fragment> getWelcomeFragments() {
List<Fragment> newFragment = new ArrayList<Fragment>();
newFragment.add(LoginFragment.newInstance());
newFragment.add(RegisterFragment.newInstance());
return newFragment;
}
}
Step 1: Define a callback interface in the Fragment and hook in the Activity when the Fragment attaches to it.
public class MyFragment extends Fragment {
public interface OnInteractionListener {
void doAction(); // Can include parameters here if needed
}
private OnInteractionListener listener;
#Override
public void onAttach(Context context) {
if (context instanceof OnInteractionListener) {
listener = (OnInteractionListener) context;
} else {
throw new ClassCastException(context + "must implement " + OnInteractionListener.class.getSimpleName());
}
super.onAttach(context);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// ...
return v;
}
}
Step 2: Make sure your Activity implements that Interface
public class MyActivity extends AppCompatActivity implements MyFragment.OnInteractionListener {
#Override
public void doAction() {
// TODO: Implement this... e.g. switch fragments
}
...
}
Step 3: Inside the Fragment, you can invoke the callback whenever you want to perform some action
if (listener != null) {
listener.doAction();
}
From MainActivity(), get the navController and then use the navigate() function:
NavController navController = Navigation.findNavController(this,R.id.nav_host_fragment);
navController.navigate(R.id.nav_grid);

Log Error in Nested Fragments on View Pager : No view found for id

I have a fragment that Contains view pager.there are three fragments in that Main fragment they are Fragment A,Fragment B,Fragment C. Upto this code runs properly.
Then i added a new Fragment and called via Fragment A then i got the error in the Log
No view found for id 0x7f0d0083 (com.app.sanyog:id/frame) for fragment SearchPlaces{289bf4e #0 id=0x7f0d0083}
Main Fragment:
/**
* Created by Admin on 04-06-2015.
*/
public class ContentFragment_Flight_Bkg extends Fragment {
private View view;
public ViewPager pager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_fragment_flight,container,false);
TabsPagerAdapter adapter = new TabsPagerAdapter(getFragmentManager());
pager = (ViewPager)view.findViewById(R.id.pager);
pager.setAdapter(adapter);
return view;
}
}
Pager Adapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
// Tab Titles
private String tabtitles[] = new String[] { "One Way", "Round Trip", "Multicity" };
Context context;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
OneWayTab fragmenttab1 = new OneWayTab();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
RoundTripTab fragmenttab2 = new RoundTripTab();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
MultiCityTab fragmenttab3 = new MultiCityTab();
return fragmenttab3;
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
return tabtitles[position];
}
}
Fragment A
public class OneWayTab extends Fragment {
private LinearLayout oneway_source,oneway_destiantion;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab3.xml
View view = inflater.inflate(R.layout.content_one_way_tab, container, false);
oneway_source = (LinearLayout)view.findViewById(R.id.oneway_source_layout);
oneway_destiantion = (LinearLayout)view.findViewById(R.id.oneway_destination_layout);
oneway_source.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SearchPlaces fragment_Flight= new SearchPlaces();
android.support.v4.app.FragmentTransaction fragmentTransaction_Flight = getChildFragmentManager().beginTransaction();
fragmentTransaction_Flight.replace(R.id.frame,fragment_Flight,"FLight_booking");
fragmentTransaction_Flight.commit();
}
});
oneway_destiantion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utils.showToast(getActivity(),"Destiantion");
}
});
return view;
}
}
New Fragment (Called from Fragment A)
public class SearchPlaces extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.content_search_places, container, false);
return view;
}
}

Access to getString() in a fragment that implements a viewpager

I am using a nested fragment using SherlockFragment, all works fine. But i am not being able to make viewpager title to string so that i can support multilanguage using string.xml.
Here is my code
public class schedule_pl extends SherlockFragment {
private static String titles[] = new String[] { "Portugal", "Lisbon" , "Azores" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_pager, container, false);
// Locate the ViewPager in viewpager_main.xml
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
// Set the ViewPagerAdapter into ViewPager
mViewPager.setAdapter(new MyAdapter (getChildFragmentManager()));
return view;
}
#Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
return new place();
case 1:
return new place2();
case 2:
return new place3();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
public static class place extends Fragment {
public place() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container,
false);
return rootView;
}
}
public static class place3 extends Fragment {
public place3() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container,
false);
return rootView;
}
}
public static class place2 extends Fragment {
public place2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment3, container,
false);
return rootView;
}
}
}
on getPageTitle i can't that working with strings and already tried several methods.
Access to getString() in android.support.v4.app.FragmentPagerAdapter?
This is a question similar on the site but neither answer got me on the right path.
I tried using this:
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getcontext.getString(R.string.title_section_schedule1).toUpperCase(l);
case 1:
return getcontext.getString(R.string.title_section_schedule2).toUpperCase(l);
case 2:
return getcontext.getString(R.string.title_section_schedule3).toUpperCase(l);
}
return null;
}
But do not work. Anyone knows the solution to this?
You can pass the Activity to your adapter constructor.
mViewPager.setAdapter(new MyAdapter (getChildFragmentManager(), getActivity()));
then in MyAdapter:
//declare a private variable for the activity
private Activity myActivity;
public MyAdapter(FragmentManager fm, Activity myActivity) {
super(fm);
this.myActivity = myActivity;
}
then in your getPageTitle
...
return myActivity.getString(R.String.title_section_scheduale1).toUpperCase(l);
...
Try it out hope it works for you.

Categories

Resources