tabs + swipe trying to make a call from fragment 1 using imgbutton - android

i have set up an pp using the default tabs + swipe layout, i figured out how to display different content on different slides,
the problem im having is how i can start an intent (make a phone call using an imagebutton.
the image button is is fragment 1 or slide 1, i dont know where to declre the button, or how to format the code,
best i have managed is 1 error stating:
Gradle: error: non-static method findViewById(int) cannot be referenced from a static context
my code is as follows,
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if ((getArguments().getInt(ARG_SECTION_NUMBER)==1)) {
View view = inflater.inflate(R.layout.test, container, false);
return view;
ImageButton ib1;
ib1 = (ImageButton) findViewById(R.id.ib1);
// add button listener
ib1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
startActivity(callIntent);
}
});
}
if ((getArguments().getInt(ARG_SECTION_NUMBER)==2)) {
View view = inflater.inflate(R.layout.test2, container, false);
return view;
}
if ((getArguments().getInt(ARG_SECTION_NUMBER)==3)) {
View view = inflater.inflate(R.layout.test3, container, false);
return view;
}
else {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
}
}

maybe remove the static in the DummySectionFragment declaration if this is not an inner class.
Also the findViewById should be getView().findViewById. Im no expert on Fragment but try this.

Related

How to call a fragment method in adapter

I have a fragment holds a recyclerview and textview so i want to call a method which i declared in the fragment inside the recyclerview adapter because i have a plus button in each recyclerview item so when i click on it the method will being called so the textview which is in the fragment change its value.
Fragment code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, container, false);
ItemsAddedToCart = view.findViewById(R.id.itemsAddRV);
Total = view.findViewById(R.id.TotalPrice);
fillCart = new FillCart(c,cartList);
ItemsAddedToCart.setAdapter(fillCart);
ItemsAddedToCart.setLayoutManager(new LinearLayoutManager(c));
return view;
}
fragment method
public void updateTotal()
{
Total.setText(fillCart.Total());
}
ADAPTER
adapter method
public String Total()
{
return String.valueOf(price * Qt);
}
plus button
((ViewHodler)holder).plusBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
price = Integer.parseInt(((ViewHodler)holder).price.getText().toString());
Qt = Integer.parseInt(((ViewHodler)holder).quantity.getText().toString());
Qt++;`
// FRAGMENT METHOD WILL CALLED HERE
}
});
As Hassan Tareq say:
Use this concept - > https://developer.android.com/training/basics/fragments/communicating

How to get views from fragments in onClick

I have an activity with a String array and a Drawable array. I create fragments using a for loop in the onCreate according to the length of the arrays and use the String array to fill TextViews in the fragments.
I made the fragments clickable and added onClick in the fragment class.
I want to get the text from the fragment the user clicks, but when I click it only gets the text from the first fragment. How can I read the textviews in the different fragments.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int layoutId = getArguments().getInt("layout");
View rootView = inflater.inflate(layoutId, container, false);
TextView fragmentText = (TextView) rootView.findViewById(R.id.fragTextView);
fragmentText.setText(getArguments().getString("fragText"));
ImageView fragmentImage = (ImageView) rootView.findViewById(R.id.fragImageView);
fragmentImage.setImageResource(getArguments().getInt("pic"));
rootView.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
String s = ((TextView)v.getRootView().findViewById(R.id.fragTextView)).getText().toString();
Intent intent = new Intent(v.getContext(), LevelActivity.class);
intent.putExtra("exerciseId", s);
startActivity(intent);
}
Thanks

ViewPager - Events in all page occurs only in last page

I have a viewPager with say 4 pages. All 4 pages uses same Xml. When i do an event in 1st page somehow it always triggers in the last page.
Here is my PagerAdapter
#Override
public Object instantiateItem(ViewGroup container, int pos) {
View desktopView;
OnTouchListener tl = null;
desktopView = act.getLayoutInflater().inflate(
act.getViewPagerLayout(groupName), null);
RelativeLayout rr_appContainer, rr_dialogContainer;
ImageView rr_home_container = (ImageView) desktopView
.findViewById(R.id.imageView_forClick);
Button buttonChange = (Button)desktopView.findViewById(R.id.B1);
Button buttonDelete = (Button)desktopView.findViewById(R.id.B2);
rr_appContainer = (RelativeLayout) desktopView
.findViewById(R.id.rr_home_container);
rr_dialogContainer = (RelativeLayout) desktopView
.findViewById(R.id.rr_dialogView);
..........
buttonDelete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
deletestuff();
}
buttonChange.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
changeColorOfStuff();
}
.....
return desktopView;
}
What is happening is, When i click on buttonChange from 1st page it supposed to change the color of text on 1st page, but actually it is changing color of the last page. Similarly buttonDelete is deleting color from last page.
Regardless of what page i am in, its reflecting those changes on last page.
Any help would be appreciated.
From the context given here, the deleteStuff() and changeColorOfStuff() can only be members of the Fragment/Activity that owns the adapter, or the adapter itself. So these methods can only act on members of those classes. ViewPager asks the adapter for the fragments it is going to display. However, the text in the fragment being shown by the ViewPager belong to the that fragment. To act on that text, you need a method that's a member of that fragment. The usual way to do this is to use a custom fragment. For example:
Custom Fragment (inner class):
public static class CustomFragment extends Fragment {
//members of the fragment
TextView yourTextView;
...
public static CustomFragment newInstance(int pos) {
CustomFragment fragment = new CustomFragment();
//get whatever info you need for this page
Bundle args = getInfoSomehow(pos);
fragment.setArguments(args)
return fragment;
}
#Override
public View onCreateView(Layout inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(....
yourTextView = root.findViewById(...) //this is the text view you want to change things in
//all the stuff you're currently doing in instantiateItem()
return root;
}
private void deleteStuff() {
//whatever you need to do. But notice that here it's acting on the TextView that belongs to this particular fragment
}
private void changeColorOfStuff() {...}
...
}
Then in your instantiateItem(...)
#Override
public Object instantiateItem(ViewGroup container, int pos) {
return CustomFragment.newInstance(pos);
}

Add Programatically created LinearLayout as resource to View in Fragment

My eventual goal is to have users be able to switch between tabs with dynamically generated content (based on user input) in an activity. I am trying to create a dynamic layout and have it load as the layout within a fragment. My code is throwing an error because the layout I am trying to provide is not an xml resource. Is what I'm trying even possible to do?
public static class JobsFragment extends Fragment{
LinearLayout l;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//set button vars here
Log.i("aaa","called frag onCreate");
l=new LinearLayout(getActivity());
Button b=new Button(getActivity());
b.setText("HI");
l.addView(b);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Log.i("aaa","called frag onCreateView()");
//View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
View rootView = inflater.inflate(l, container, false);
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter{
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new JobsFragment();
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return "Section " + (position + 1);
}
}
}
Make the fragment constructor accept linearlayout as parameter and use this field to be return from onCreateView
Something like following psuedo code
CustomFragment{
private LinearLayout mLayout;
CustomFragment(LinearLayout layout){
mLayout=layout;
| }
onCreatView(....)
{
return mLayout;
}
}
You need to add layout width and height programatically.
Without which it will throw error about mandatory layout parameters not provided.
addLayoutParams is the method name

Exception in ListFragment: The specified child already has a parent

I'm making a downloader app, and i got child's parents error after the onCreateView return section.
I tried a lots of things, but nothing help. I tried:
((ViewGroup)dlistView.getParent()).removeView(dlistView);
after that the compiler says:
01-12 12:05:15.558: E/AndroidRuntime(10740): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
or remove the container/ i got an npe/ or remove the V but the V.getParent() is null.
Which view is the parent, and which is the child?
The code:
public static class DownloadingFragment extends ListFragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DownloadingFragment() {
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate (R.layout.list, null);
ListView dlistView = (ListView) V.findViewById(R.id.dlist);
List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
downloadInfo.add(new DownloadInfo("File", 1000));
DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
getActivity().getApplication(),R.layout.list, downloadInfo);
dlistView.setAdapter(diaa);
return dlistView; // throw exception
}
}
//...
}
Thank you in advance for your help.
EDIT:
the new code:
public static class DownloadingFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DownloadingFragment() {
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate (R.layout.list, null);
return V;
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
ListView dlistView = (ListView) getView().findViewById(R.id.dlist);
List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
downloadInfo.add(new DownloadInfo("File", 1000));
DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
getActivity().getApplication(),R.layout.list, downloadInfo);
dlistView.setAdapter(diaa);
}
}
//...
}
that's because you return the listview which has a parent (somewhere in V's layout tree) .
for any type of fragment , onCreateView should return a view that doesn't have any parent .
in this case , since it's a listFragment , create the listview (either using xml or programmatically ) , and then return it . do not allow it to have any parents since it needs to have a parent as a fragment .
read here for more information :
The system calls this when it's time for the fragment to draw its
user interface for the first time. To draw a UI for your fragment, you
must return a View from this method that is the root of your
fragment's layout. You can return null if the fragment does not
provide a UI.
Note that the third argument of inflate is false.
so replace this line
View V = inflater.inflate (R.layout.list, null);
with this
View V = inflater.inflate (R.layout.list, null,false);
and you are done.hope this helps.

Categories

Resources