Android check what fragment sent onClick action - java

I have to build a simple app where I have 3 fragments, each contains 1 button.
If one of these buttons is pressed a site should load within a webview which is contained by the main activity. So far i have the basic callback to the activity working, but my question is. How do I check which button is pressed? Since they all seem to send the same view to the activity. I have tried to use the view.setTag() method but I couldn't get it to work.
Here is some of my code:
MainActivity.java
#Override
public void onClick(View v){
if(v.getId() == R.id.btnApple){
wvResult.loadUrl("http://www.apple.com");
}else if (v.getId() == R.id.btnMic){
wvResult.loadUrl("http://www.microsoft.com");
}else if(v.getId() == R.id.btnOracle){
wvResult.loadUrl("http://www.oracle.com");
}
}
One of the fragment classes (each is different in the names (apple, microsoft and oracle), the rest is exactly the same):
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.btn_a, container, false);
btnApple = (Button)v.findViewById(R.id.btnApple);
btnApple.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callBack.onClick(v.getRootView());
}
});
return v;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callBack = (View.OnClickListener) activity;
}
I hope someone can help me

Just pass v in your callback method (not v.getRootView()) because it is the view which was clicked.
View.getRootView() retrieves the root view element of view hieararchy, which is most likely one of the ViewGroup classes (LinearLayout, RelativeLayout, ...). That is the view which contains the clicked button.

Related

Dialog for fragments

Why is my showPopup grey when I already have an OnClick method on that fragment's xml?
I'm trying to make the popup_about_app XML popup when I click on the button with the OnClick.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myDialog = new Dialog(getContext());
return inflater.inflate(R.layout.fragment_settings, container, false);
}
public void showPopup(View view) {
TextView txtclose;
myDialog.setContentView(R.layout.popup_about_app);
txtclose =(TextView) myDialog.findViewById(R.id.txtClose);
txtclose.setText("X");
txtclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myDialog.dismiss();
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}
In Android Studio, gray means its not being used yet. Either nothing is calling showPopup, or possibly that Android Studio isn't smart enough to find the thing calling showPopup (which can happen in various scenarios). I wouldn't worry about it unless you hit the button that's supposed to call showPopup and nothing happens (and then it means you forgot to call it). If the popup shows like you expect, its a non-issue.

Android fragment navigation without button click

I am using a "Drawer Navigation" project using fragments and so far this method works:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.nav_gallery);
}
});
But I want to use the navigation method inside a fragment class calling a function like this:
boolean b;
public void fragmentNavigation() {
if (b) {
Navigation.findNavController(getView()).navigate(R.id.nav_gallery);
}
}
I am a newbie using the navigation architecture and I still don't know if I need to declare some type of action listener for that function or how to make it work.
You can do that, but be careful of :
using getView() is Nullable; so you must make sure that your fragment has already created the view.
You can solve this by a couple of ways
First: Overriding onViewCreated() which has a nonNull view argument
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fragmentNavigation(view);
}
boolean b;
public void fragmentNavigation(View view) {
if (b) {
Navigation.findNavController(view).navigate(R.id.nav_gallery);
}
}
Second: Create a View field in the fragment class, and set it within onCreateView()
View view;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.main_fragment, container, false);
return view;
}
then always call fragmentNavigation(view); on that view field.
Your fragment is hosted by the NavHostFragment of the Navigation Graph; so that you can avoid potential IllegalStateException

How to save Scrolling state of a complex listview in a fragment

I am using a file explorer listview in a fragment.
But not getting how to save its scrolled state .
I searched Stackoverflow quora and google for last 2 days but didnt got any result.
My project support Api 14 +.
In my Fragment Explorer,
I am making onCreateview and onclick like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.artist,container, false);
//My coding stuff here
lv=(ListView) root.findViewById(android.R.id.list);
lv.setAdapter(adaptor);
return root;
}
onclick is as follows:-
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lv=getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
gtuMcaBean1= gtuMcaBean.get(position);
lv.setAdapter(adaptor);
}
In my Fragmentactivity clas i call onBackpressed
#Override
public void onBackPressed() {
// super.onBackPressed();
if(pager.getCurrentItem()==0)//0 is item no for explorer view
{
//My coding stuff here
lv.setadaptor(adaptor);
}
In my Mainactivity also i have an onbackressed which works when i come to fragment view frm an activity
#Override
public void onBackPressed() {
// super.onBackPressed();
if(pager.getCurrentItem()==0)//0 is item no for explorer view
{
//My coding stuff here
lv.setadaptor(adaptor);
}
Now i want to save the scroll state of Listview so if user presses back button it should display previous scroll view not from starting of ListView.
Thanx In advance :)
Referred from here
Answered by ian
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());
// ...
// restore index and position
mList.setSelectionFromTop(index, top);

Android how to save all views in Fragment when user click on next and prev button

i use fragment for my application,they are : Fragment A, Fragment B and Fragment C.
to move from one fragment to another fragment, I use two buttons, the first button is used to move to the next fragment, and the second button is used to return to the previous fragment.
the problems when I move from one fragment to another fragment (such as from A to B and back to A and to B again and to C). all entries in the EditText that is in a fragment is lost and look back empty,it when I go back to the previous fragment, or when I want to go back to the next fragment. I've been using code addToBackStack () when switching to another fragment, indeed EditText that I made not lost when I press the back button on my phone, but it does not affect the buttons (Next And Prev) that I've created. this is Fragment A:
public class AFragment extends Fragment {
EditText text,text2;
public AFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_a, container, false);
ImageView next=(ImageView)rootView.findViewById(R.id.next);
text=(EditText)rootView.findViewById(R.id.text);
text2=(EditText)rootView.findViewById(R.id.text1);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getFragmentManager().beginTransaction().replace(R.id.frame_container, new BFragment()).addToBackStack(null).commit();
}
});
return rootView;
}
this is Fragment B:
public class BFragment extends Fragment {
EditText cut,cut2;
public BFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View Fragment B = inflater.inflate(R.layout.fragment_c, container, false);
ImageView next=(ImageView)rootView.findViewById(R.id.next);
ImageView prev=(ImageView)rootView.findViewById(R.id.prev);
cut=(EditText)rootView.findViewById(R.id.cut);
cut2=(EditText)rootView.findViewById(R.id.cut2);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getFragmentManager().beginTransaction().replace(R.id.frame_container, new CFragment()).addToBackStack(null).commit();
}
});
prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getFragmentManager().beginTransaction().replace(R.id.frame_container, new AFragment()).addToBackStack(null).commit();
}
});
return Fragment B;
}
my question is how can i save view in Fragment when user click on next and prev button in my application? is there something missing in my code? i hope someone can help me to solve my problem. thank you very much.
Each fragments have various life cycle methods, you can use onDestroy method and save the required edit text view value calling Activity. For this expose some methods in the calling activity which can be access via fragment. Also else create a singleton class or use Shared Prefences.
I hope this helps.
try to change:
getFragmentManager().beginTransaction().replace(R.id.frame_container, new BFragment()).addToBackStack(null).commit();
to:
getFragmentManager().beginTransaction().add(R.id.frame_container, new BFragment()).addToBackStack(null).commit();

SlidingMenu in Child Activities Navigation Android

I am trying to add left side slide menu inside my application but i am facing little trouble getting the slide menu to the following child activities of the fragment.
So far i have tried using this sliding menu example and when i got this image now inside the "Find People" fragment class i have static data.
Find People (Parent Activity) on Button Click go to ---> Friends (Child Activity of Find People) on Button Click go to ----> Names (Child Activity of Friends)
I am trying to get the same sliding menu with the Child Activities also that i failed to achieve so far please can any one help me out
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Friends.class);
startActivity(intent);
}
});
return rootView;
}
}
Friends (Child Activity)
public class Friends extends Activity {
Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends_xml);
btnNext = (Button)findViewById(R.id.btnNexts);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Go to Names Child Activity
}
});
}
}
And Similarly Names (Child Activity)
How do i get the same slide menu in both the child activities (Friends & Names). Any idea ??
Plz help me out in this.
Code Snippet will be much helpful.
Thanks
As #Piyush Gupta said you need to call new Fragment instead of calling activities,
In Place of activity you need to use Fragment and move from one Fragment to next used this code
Instead of using Fragment element use Framelayout id of the fragment Xml
Replace ur code with this
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragementDemo fd = new FragementDemo();
android.support.v4.app.FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fd); // content_frame is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}
}
And your next child activity will extend Fragment not Activity like this :
public class Friends extends SherlockFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragementdemo, container, false);
// use button click code as used in FindPeopleFragment
return rootView;
}
}
Hope it works fine now for you!!

Categories