Calling Method in onCreateView() - android

Hi I am new to fragments and I am working with this activity.I populated an edit text but I want to highlight the borders when the user focus on one of the edit text.I created the method focus and I am calling it,but it is not working.
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Fragment_Info extends Fragment {
EditText CompanyName;
EditText CompanyOwner;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment__info, container, false);
CompanyName = (EditText) view.findViewById(R.id.CompName);
CompanyOwner = (EditText) view.findViewById(R.id.CompOwner);
focus(CompanyName);
focus(CompanyOwner);
//Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment__info, container, false);
}
public void focus(View view){
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
v.setBackgroundResource(R.drawable.focus_border_style);
else
v.setBackgroundResource(R.color.colorPrimary);
}
});
}
}

You are applying focus on a view and returning a different view from the method.
Change
return inflater.inflate(R.layout.activity_fragment__info, container, false);
to
return view;

Change
return inflater.inflate(R.layout.activity_fragment__info, container, false);
to
return view;

Related

Setting onClicklistener() doesn't work in a fragment

Button onClick doesn't work in a fragment. What happens?
The id and onClickListener function has the right syntax. I don't know what is happening.
public class LoginFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Button btn;
View vista = inflater.inflate(R.layout.fragment_login, container, false);
btn = (Button)vista.findViewById(R.id.btn_login);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View vista) {
//cargarWebService();
//name.setText("");
//email.setText("");
Toast.makeText(getContext(), "Se ah registrado exitosamente", Toast.LENGTH_SHORT).show();
}
});
return inflater.inflate(R.layout.fragment_login, container, false);
}
}
change this
return inflater.inflate(R.layout.fragment_login, container, false);
to
return vista;
let me know if this work or not
You don’t need to inflate the view again when returning it, simply inflate it once at the beginner of the method and then return the view at the end of the method.
View view = inflater.inflate(R.layout.fragment_login, container, false);
return view;

which method when display dialog show in fragment

I opened dialog in a fragment and when come back to fragment, my fragment's view is null Object.
when the dialog is displaying which lifecycle method fragment is calling?
public class MyFragment extends Fragment{
TextView textView;
View view;
FloatingActionButton actionButton;
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_read, container, false);
textView = view.findViewById(R.id.txtSaved);
actionButton = view.findViewById(R.id.floatingActionButton);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogSearch search = new DialogSearch();
search.show(getActivity().getSupportFragmentManager(),"MyDialog");
}
});
return view;
}
}
The problem is the dialog is being showned in the onCreateView life cycle, that is why is the Fragment view is null. You have to do it inside the onViewCreated method.
#Override
public View onViewCreated(View view, Bundle savedInstanceState) {
//Work here, the view argument in the method is the view inflated in the onCreateView method
});
Make a small change,
public class MyFragment extends Fragment{
TextView textView;
View view;
FloatingActionButton actionButton;
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_read, container, false);
textView = view.findViewById(R.id.txtSaved);
actionButton = view.findViewById(R.id.floatingActionButton);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogSearch search = new DialogSearch();
search.show(getActivity().getSupportFragmentManager(),"MyDialog");
}
});
return view; / Add this line -------
}
}
You need to return view
when the dialog is displaying which lifecycle method fragment is calling?
onPause() of fragment gets called.

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;
}
}

How to access a view present in a fragment's

I have a fragment with one editText
<EditText
android:id="#+id/textViewName"
/>
I want to change its text at run time .
How can i access edit Text so that i can change its text?
Inside onCreateView(), you can find out particular view by inflating your layout.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myFragmentLayout, container, false);
EditText editText = (EditText) view.findViewById(R.id.textViewName);
return view;
}
Or
you can use getView() if you don't want to inflate, it will return root view of the fragment.
EditText editText = (EditText) getView().findViewById(R.id.textViewName);
Let's Say you have a class EditFragment
public class EditFragment extends Fragment
{
private EditText edit;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myFragmentLayout, container, false);
edit = (EditText) view.findViewById(R.id.textViewName);
return view;
}
}
You can make a getter to access the EditText of this Fragment
public EditText getEditText()
{
return edit;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
EditText e = (EditText) view.findViewById(R.id.textViewName);
....
}

Categories

Resources