Android Tabbed Activity on clickbutton not work - android

Can someone tell me how to set a button in tabbed fragment layout to open a new activity? If I set a button in a tab then it shows error.

You have to save the inflated view in a variable, then use findViewById on that view to find the textview and button, then at the end, return the inflated view.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_g, container, false);
TextView tv = (TextView) root.findViewById(R.id.textView);
Button btn = (Button) root.findViewById(R.id.button);
//Other code here
return root;
}
You're misunderstanding some very fundamental things, such as writing code after the return statement. You might want to read some starter guides online.

You can use below code
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_complete_registration, container, false);
final TextView tv=(TextView)view.findViewById(R.id.textView);
final Button button=(Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your required code here
}
});
return view;
}

Related

Cannot resolve symbol in Inflater in findViewById [duplicate]

I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can use it in Fragment as well?
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ImageView imageView = (ImageView)findViewById(R.id.my_image);
return inflater.inflate(R.layout.testclassfragment, container, false);
}
}
The findViewById method has an error on it which states that the method is undefined.
Use getView() or the View parameter from implementing the onViewCreated method. It returns the root view for the fragment (the one returned by onCreateView() method). With this you can call findViewById().
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ImageView imageView = (ImageView) getView().findViewById(R.id.foo);
// or (ImageView) view.findViewById(R.id.foo);
As getView() works only after onCreateView(), you can't use it inside onCreate() or onCreateView() methods of the fragment .
You need to inflate the Fragment's view and call findViewById() on the View it returns.
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
return view;
}
Inside Fragment class you will get onViewCreated() override method where you should always initialize your views as in this method you get view object using which you can find your views like :
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.yourId).setOnClickListener(this);
// or
getActivity().findViewById(R.id.yourId).setOnClickListener(this);
}
Always remember in case of Fragment that onViewCreated() method will not called automatically if you are returning null or super.onCreateView() from onCreateView() method.
It will be called by default in case of ListFragment as ListFragment return FrameLayout by default.
Note: you can get the fragment view anywhere in the class by using getView() once onCreateView() has been executed successfully.
i.e.
getView().findViewById("your view id");
I realise this is an old question, but the prevailing answer leaves something to be desired.
The question is not clear what is required of imageView - are we passing it back as the view, or merely saving a reference for later?
Either way, if the ImageView is coming from the inflated layout, the correct way to do this would be:
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
return v;
}
}
Get first the fragment view and then get from this view your ImageView.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
return view;
}
Inside Fragment class we get onViewCreated() override method where we should always initialize our views because in this method we get view object. Using this object we can find our views like below:
class MyFragment extends Fragment {
private ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment_layout, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//initialize your view here for use view.findViewById("your view id")
imageView = (ImageView) view.findViewById(R.id.my_image);
}
}
You could also do it in the onActivityCreated Method.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
Like they do here: http://developer.android.com/reference/android/app/Fragment.html (deprecated in API level 28)
getView().findViewById(R.id.foo);
and
getActivity().findViewById(R.id.foo);
are possible.
getView() will give the root view
View v = getView().findViewByID(R.id.x);
You can override onViewCreated() which is called right after all views had been inflated. It's the right place to fill in your Fragment's member View variables. Here's an example:
class GalleryFragment extends Fragment {
private Gallery gallery;
(...)
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
gallery = (Gallery) view.findViewById(R.id.gallery);
gallery.setAdapter(adapter);
super.onViewCreated(view, savedInstanceState);
}
}
The method getView() wont work on fragments outside OnCreate and similar methods.
You have two ways, pass the view to the function on the oncreate (what means you can only run your functions when the view is being created) or set the view as a variable:
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_contatos, container, false);
}
public void doSomething () {
ImageView thumbnail = (ImageView) rootView.findViewById(R.id.someId);
}
1) first inflate layout of Fragment then you can use findviewbyId .
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
return view;
EditText name = (EditText) getView().findViewById(R.id.editText1);
EditText add = (EditText) getView().findViewById(R.id.editText2);
agreed with calling findViewById() on the View.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) V.findViewById(R.id.my_image);
return V;
}
Note :
From API Level 26, you also don't need to specifically cast the result of findViewById as it uses inference for its return type.
So now you can simply do,
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = view.findViewById(R.id.my_image); //without casting the return type
return view;
}
Use
imagebutton = (ImageButton) getActivity().findViewById(R.id.imagebutton1);
imageview = (ImageView) getActivity().findViewById(R.id.imageview1);
it will work
According to the documentation on API level 11
Reference, in Back Stack
http://developer.android.com/reference/android/app/Fragment.html
short code
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
Using getView() returns the view of the fragment, then you can call findViewById() to access any view element in the fragment view.
Try this it works for me
public class TestClass extends Fragment {
private ImageView imageView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
findViews(view);
return view;
}
private void findViews(View view) {
imageView = (ImageView) view.findViewById(R.id.my_image);
}
}
1) Declare your layout file.
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState) {
return inflate(R.layout.myfragment, container, false);
}
2)Then, get the id of your view
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView nameView = (TextView) view.findViewById(R.id.textview1);
}
The best way to implement this is as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) rootView.findViewById(R.id.my_image);
return rootView
}
In this way, the rootView can be used for each control defined in the xml layout and the code is much cleaner in this way.
Hope this helps :)
Try This:
View v = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView img = (ImageView) v.findViewById(R.id.my_image);
return v;
try
private View myFragmentView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
myFragmentView = inflater.inflate(R.layout.myLayoutId, container, false);
myView = myFragmentView.findViewById(R.id.myIdTag)
return myFragmentView;
}
Use gradle skeleton plugin, it will automatically generate the view holder classes with the reference to your layout.
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyLayout myLayout = new MyLayout(inflater, container, false);
myLayout.myImage.setImageResource(R.drawable.myImage);
return myLayout.view;
}
}
Now assuming you had an ImageView declared in your my_layout.xml file, it will automatically generate myLayout class for you.
I like everything to be structured. You can do in this way.
First initialize view
private ImageView imageView;
Then override OnViewCreated
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
findViews(view);
}
Then add a void method to find views
private void findViews(View v) {
imageView = v.findViewById(R.id.img);
}
//here you can do it by
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_apple, container,
false);
ist = view.findViewById(R.id.linearLink);
second = view.findViewById(R.id.linearPhone);
return view;
ImageView imageView;
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
imageView = view.findViewById(R.id.my_image);
return view;
}
You can call findViewById() with the Activity Object you get inside your public void onAttach(Activity activity) method inside your Fragment.
Save the Activity into a variable for example:
In the Fragment class:
private Activity mainActivity;
In the onAttach() method:
this.mainActivity=activity;
Finally execute every findViewById through the vairable:
mainActivity.findViewById(R.id.TextView);
Inside onCreateView method
1) first you have to inflate the layout/view you want to add
eg. LinearLayout
LinearLayout ll = inflater.inflate(R.layout.testclassfragment, container, false);
2) Then you can find your imageView id from layout
ImageView imageView = (ImageView)ll.findViewById(R.id.my_image);
3)return the inflated layout
return ll;
You have to inflate the view
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
return v
}}
There is one more method called onViewCreated.
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView imageView = (ImageView) view.findViewById(R.id.imageview1);
}

Fragment/View. How to use a method on a View

I'm doing a Navigation Drawer app. I have all the cases working, I mean, when in touch a section the app goes to the right View. But when I want to start coding I don't know how to use methods inside the View. I think my code will explain better than me:
public class News extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setText();
return inflater.inflate(R.layout.lay_news, container, false);
}
public void setText(){
TextView texts = (TextView) getView().findViewById(R.id.textView);
texto.setText("hi");
}
}
NullPointerException when I call the method setText. I know that it's the TextView definition, but I don't know how to "find" it in the layout without getView() or getActivty(), both return a NullPointerException when the view loads.
Just do
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.lay_news, container, false);
setText(v);
return v;
}
and
public void setText(View v){
TextView texts = (TextView)v.findViewById(R.id.textView);
texto.setText("hi");
}

Detecting the right View

I need a little bit help, defining my in event_filter.xml declared Views.
It is an child of an AbstractDetailListFragment and implements a DatePicker and a Spinner item.
But I dont know how to acces them in an AbstractDetailFragment.
By defining
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = super.onCreateView(inflater, container, savedInstanceState);
ImageButton searchEnterButton = (ImageButton)getNavigationFragment().getNavigationBar().getRightButton();
View view = getView();
final DatePicker datePicker = (DatePicker) container.findViewById(R.id.datePickerEvent);
Spinner inSpinner = (Spinner) container.findViewById(R.id.category_list);
spinner = inSpinner;
downloadCategories();
the variables view, datePicker, inSpinner always are null!
Any Ideas?
thanks a lot.
That's a thing about fragments. I've been looking for the same error for days, until I read a little bit about fragments (i was lazy and i recommend you not to follow my footsteps ). The way i solved it was by overriding onViewCreated and "catching" views there:
public class MyFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container,
false);
return rootView;
}
//and you use rootView to call findViewById
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button myButton = (Button) rootView.findViewById(R.id.mybutton);
sendFeedback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
}
}

Null Pointer Exception when setting new text to text view

I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
text1.setText("NeuerText");
}
}
In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().
You should use getView():
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
switch (v.getId) {
case R.id.button1:
TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
text1.setText("NeuerText");
break;
}
}
}
Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().
You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.
You should do something like:
public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
text1.setText("NewerText"); //Also fixed typo
}
}
In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick();
Some what like this:
public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
tv = (TextView) v.findViewById(R.id.welcome_text);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
tv.setText("NeuerText");
}
}

Android ImageButton worked well in Activity. It doesn't work in Fragment

I'm trying to convert a layout so that it will include fragments.
One of the views is an ImageButton that has a listener.
The code worked fine as an Activity but makes trouble as a Fragment...
First problem was that I couldn't use findViewById,
but I was able to find the answer for that here and fixed it with getView().
I can't find an answer to the second problem...
after I declared:
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
I want to set an on click listener:
camBt.setOnClickListener(listener);
But it keeps acting like the ImageButton (camBt) doesn't exist...
I don't even know what's the problem... it worked fine in the Activity...
Here's the full code.
Many thanks!
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
return V;
}
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
}
Move
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView().
Like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.camera_fragment, container, false);
// Here it is
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(listener);
return v;
}
And one more thing you need to understand that variable name should starts with small letter. So you should use View v instead of View V.
You should add
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView() like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return V;
}
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)V.findViewById(R.id.button1);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
camBt.setOnClickListener(listener);
return V;
}
}

Categories

Resources