i wanna open the browser from my fragment
but when i start my app it crashes
what is wrong ?
public class C_spider extends Fragment {
Button btn_off2;
public C_spider() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cspider, container, false);
btn_off2 = (Button)view.findViewById(R.id.btn_off2);
btn_off2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com"));
getActivity().startActivity(i);
}
});
return view;
}
}
try putting your codes in the onActivityCreated()
public class C_spider extends Fragment {
Button btn_off2;
public C_spider() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cspider, container, false);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btn_off2 = (Button)getActivity().findViewById(R.id.btn_off2);
btn_off2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com"));
getActivity().startActivity(i);
}
});
}
}
Related
I am a beginner in android development and I want to navigate to an activity from a fragment when an image in ImageView is clicked. My Java class does not let me declare variables to link to my XML file. Also, I am not sure as to in which java class I should be declaring my variables.
I have also tried writing the same code in my MainActivity but it still shows an error.
public class HomeFragment extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home,container,false);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edSheeranSongs = (ImageView)findViewById(R.id.ed_sheeran);
justinBieberSongs = (ImageView)findViewById(R.id.justin_bieber);
shawnMendesSongs = (ImageView)findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),SheeranSongs.class);
startActivity(intent);
}
});
I expect to go to another activity which I have already created called SheeranSongs, But I get an error message when I try to declare anything in a fragment
You should inflate the view in onCreateView of the fragment and initilize views with the created custom view. Override onAttach to get the context of the activivty to which the fragment is attached to.
public class HomeFragment extends Fragment{
private Context context;
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.context=context;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
edSheeranSongs = view.findViewById(R.id.ed_sheeran);
justinBieberSongs = view.findViewById(R.id.justin_bieber);
shawnMendesSongs = view.findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, SheeranSongs.class);
startActivity(intent);
}
});
return view;
}
}
You should call findViewById() from the inflated view within onCreateView of the Fragment. Exapmle:
public class HomeFragment extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
edSheeranSongs = view.findViewById(R.id.ed_sheeran);
justinBieberSongs = view.findViewById(R.id.justin_bieber);
shawnMendesSongs = view.findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(requireContext(), SheeranSongs.class);
startActivity(intent);
}
});
return view;
}
}
When working with fragment you can't call findviewbyid() in onCreate() as you do in Activity since the fragment is created in onCreate() but the user interface isn't drawn yet (More about fragment).
You can do it in onCreateView() or onViewCreated(). From my perspective, I do it in onViewCreated() so it looks a bit cleaner.
public class HomeFragment extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home,container,false);
}
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView edSheeranSongs = view.findViewById(R.id.ed_sheeran);
ImageView justinBieberSongs = view.findViewById(R.id.justin_bieber);
ImageView shawnMendesSongs = view.findViewById(R.id.shawn_mendes);
edSheeranSongs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(requireActivity(), SheeranSongs.class);
startActivity(intent);
}
});
}
Code :
public class CustomerDetails extends Fragment {
EditText CustomerCode,FullName,AddressLine1,AddressLine2,MobileNo,EmailId,CustomerType;
Button Submit;
IonExchangeDataBasehelper ionexchangedatabase;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//super.onCreate(savedInstanceState);
// View v =inflater.inflate(R.layout.fragment_customer_details,container,false);
View v =getActivity().getLayoutInflater().inflate(R.layout.fragment_customer_details,null);
CustomerCode=(EditText)v.findViewById(R.id.CustomerCode);
FullName=(EditText)v.findViewById(R.id.FullName);
AddressLine1=(EditText)v.findViewById(R.id.AddressLine1);
AddressLine2=(EditText)v.findViewById(R.id.AddressLine2);
MobileNo=(EditText)v.findViewById(R.id.MobileNo);
EmailId=(EditText)v.findViewById(R.id.EmailId);
CustomerType=(EditText)v.findViewById(R.id.CustomerType);
Submit=(Button)v.findViewById(R.id.Submit);
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String abc = CustomerCode.getText().toString();
Toast.makeText(getActivity(),"hhh :" ,Toast.LENGTH_SHORT).show();
// database();
}
});
return v;
}
Cleaned your code; try it and say if you still get errors
public class CustomerDetails extends Fragment {
EditText customerCode,fullName,addressLine1,addressLine2,mobileNo,emailId,customerType;
Button submit;
IonExchangeDataBasehelper ionexchangedatabase;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_customer_details, container,
false);
// get the reference of views
customerCode=(EditText)v.findViewById(R.id.CustomerCode);
fullName=(EditText)v.findViewById(R.id.FullName);
addressLine1=(EditText)v.findViewById(R.id.AddressLine1);
addressLine2=(EditText)v.findViewById(R.id.AddressLine2);
mobileNo=(EditText)v.findViewById(R.id.MobileNo);
emailId=(EditText)v.findViewById(R.id.EmailId);
customerType=(EditText)v.findViewById(R.id.CustomerType);
submit=(Button)v.findViewById(R.id.Submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String abc = customerCode.getText().toString();
Toast.makeText(getActivity(), abc ,Toast.LENGTH_SHORT).show();
// database();
}
});
return v;
}
i am calling a list inside a dialog fragment which is loaded from a custom adapter.
loading my fragment
ProfileListViewHolder.imageButtonMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfileDialog ProfileDialogFragment = new ProfileDialog();
ProfileDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
android.support.v4.app.FragmentTransaction fragmentManager =((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
fragmentManager.replace(R.id.drawer_layout,ProfileDialogFragment) .addToBackStack(null).commit();
}
});
and this is my Dialog that i am using
public class ProfileDialog extends android.support.v4.app.DialogFragment {
private Context context;
public View profileView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setTitle(String title) {
Dialog dialog = getDialog();
dialog.setTitle(title);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (profileView == null) {
profileView = inflater.inflate(R.layout.fragment_profile_dialog, container,false);
} else {
((ViewGroup) profileView .getParent()).removeView(profileView );
}
ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile);
return profileListView;
}
#Override
public void onStart() {
super.onStart();
//addInnerFragment();
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
return dialog;
}
}
You are returning the profileListView instead of profileView. change the code like this ,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
profileView = inflater.inflate(R.layout.fragment_profile_dialog, container, false);
ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile);
return profileView;
}
Try
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
profileView = inflater.inflate(R.layout.fragment_profile_dialog, container,false);
ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile);
return profileView;
}
I have two fragments in my mainactivity, from the first one, the user clicks on a button and send through my listener a char to the second fragment, depends of that char a textview in the second fragment must print a text. But in my metho onActivityCreated my textview is always null.
This is my code:
FragmentOne
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnChefcito = (Button)getView().findViewById(R.id.btnChefcito);
btnChefcita = (Button)getView().findViewById(R.id.btnChefcita);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
btnChefcita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sexo="f";
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
}
This is my second fragment
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tviBienvenida = (TextView)getView().findViewById(R.id.tviBienvenida);
}
public void getSex(String sex){
if(sex.equals("m")){
tviBienvenida.setText(bienvenido);
}else if(sex.equals("f")){
tviBienvenida.setText(bienvenida);
}
}
Instead of doing in onActivityCreated Do This in onCreateView:
private static View viewHolder;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
viewHolder = inflater.inflate(R.layout.YourFragmentLayout, container, false);
btnChefcito = (Button) viewHolder.findViewById(R.id.YourButtonId);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
return viewHolder;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.your_fragment_id, null, false);
btnChefcito = (Button)view.findViewById(R.id.btnChefcito);
btnChefcita = (Button)view.findViewById(R.id.btnChefcita);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
btnChefcita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sexo="f";
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
return view;
}
the problem is public void onActivityCreated(Bundle savedInstanceState) not create any view in fragment so replace all code in this method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
sorry for my English.
On Android you have to declare a global variable.
tviBienvenida in you class, and on create view
tviBienvenida = (TextView)getView().findViewById(R.id.tviBienvenida);
The variable tviBienvenida will always have the value, and any change in tviBienvenida will also be refected to your textview.
Implement view in onCreateView method and repleace your stuff there, for example:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
TextView textView = view.findViewById(R.id.yourTextView);
//your code
return view;
}
I am having am mainActivity which consists of 3 fragment activity in a view pager .each fragment consists of edit texts .How to get the value of the edit text when swipe fragment horizontally ? I am trying to do like this.below code
public class MainActivity extends FragmentActivity {
int value = 0;
Context context;
String a = "";
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
// Set up the action bar.
//final ActionBar actionBar = getActionBar();
// actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAddButtonB = (Button) findViewById(R.id.addnewB);
mAddSearchV = findViewById(R.id.addnewSearch);
mAddButtonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mAddSearchV.setVisibility(View.GONE);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.customviewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
});
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
PageZero p0;
PageOne p1;
PageTwo p2;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
p0 = new PageZero();
p1 = new PageOne();
p2 = new PageTwo();
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return p0;
case 1:
return p1;
case 2:
return p2;
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
// PageZero = (Fragment1)getFragmentManager().findFragmentByTag("frag1");
public class PageZero extends Fragment {
private EditText name;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container,
false);
name=(EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
});/*(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(), a,Toast.LENGTH_SHORT).show();
return false;
}
});*/
return view;
}
}
public class PageOne extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentaddress, container,
false);
return view;
}
}
public class PageTwo extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentmobno, container,
false);
return view;
}
}
}
when i try to access the eddittext inside pageZero class like below
name=(EditText)view.findViewById(R.id.nameTV);
a=name.getText().toString().trim();
i am getting null value.help me out..Thanks in advance.
Change the pagezero class oncreateview code like this .....
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =getActivity().getLayoutInflater().inflate(R.layout.componentname,null);
name=(EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
return view;
});
Return the view friend and then use default layout inflater...
At the time you click the edittext field, is there any content? Maybe you should get the text in the lost focus event of the edittext field. Thus you can enter some text first...
Example:
name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus == false){
//get text from edittext field...
}
}
});
The way you try to get the text looks right to me. At least I do it the same way.
In the PagerAdapter's 'getItem' Method you should instantiate your fragments directly:
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new PageZero();
case 1:
return new PageOne();
case 2:
return new PageTwo();
}
return null;
}
The Viewpager only calls getItem if it wants to create a new item. Unfortunatly they called the method getItem and not createItem which would be more obvious.
Get your EditText inside onClick block...
This mean change the code from:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container, false);
name=(EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
});
return view;
}
To:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container, false);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// GET EDIT TEXT HEAR:
EditText name=(EditText)view.findViewById(R.id.nameTV);
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
}
});
return view;
}
To create a 3 different fragments with view pager you should:
1)Create FragmentActivity with view pager and view pager adapter.
public class FragmentActivity extends FragmentActivity
{
private FragmentAdapter adapter;
private ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
adapter = new FragmentAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}
2)Create 3 Fragments with static instance
public final class Fragment1 extends Fragment
{
public static Fragment1 newInstance() {
return new Fragment1();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout layout = new LinearLayout(getActivity());
// your fragment xml view
return view;
}
}
3)Fill view pager adapter with it;
public class FragmentAdapter extends FragmentPagerAdapter
{
public InstallFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment1.newInstance();
case 1:
return Fragment2.newInstance();
case 2:
return Fragment3.newInstance();
}
return null;
}
}
public class PageZero extends Fragment {
public static EditText name; //change this to public static
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container,
false);
name = (EditText)view.findViewById(R.id.nameTV);
name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return view;
}
}
Then, you can access the public static EditText name (after the view for it has been generated of course) from another fragment like this.
public class PageOne extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentaddress, container,
false);
EditText page_one_text = PageZero.name; //you can get that variable in any fragment now, but using the class name of that fragment to access it
return view;
}
}
Cheers. Please mark this as the correct answer if it helps you. You have quite a bit of answers here
I tried below code. Working for me
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main_explore, container, false);
ed1 = (EditText)v.findViewById(R.id.editTextTemp);
if( ed1 == null )
{
Toast.makeText(getActivity(), "ERROR UNABLE TO GET EDIT", Toast.LENGTH_LONG).show();
}
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_main_explore, container, false);
return v;
}