switching activities with textview - android

am trying to make the text under my login button "no account yet? signup now" when clicked to send me to my RegisterFragment.so i added an OnClickListener for it like this in my LoginFragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView signup = (TextView) getView().findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginFragment.this.startActivity(new Intent(getActivity(), RegisterFragment.class));
}
});
but when i run the code the app crashes on this line
TextView signup = (TextView) getView().findViewById(R.id.signup);
this is my fragment_login.xml file
<TextView android:id="#+id/signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="No Account Yet? Signup_now"
android:gravity="center"
android:textSize="16dp"
android:clickable="true"/>
please guys i really need your help.

onCreateView() method is responsible for creating view, so you should first create it.
You can't directly use getView() inside onCreateView().
You need to inflate it first like
View view = inflater.inflate(R.layout.my_layout, container, false);
Then use
TextView signup = (TextView) view.findViewById(R.id.signup);
So full code should look like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
TextView signup = (TextView) view.findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}
On side note, setting up click listener initializing TextView's etc should be done inside onViewCreated(). onCreateView() should just create & return the view.
like
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
TextView signup = (TextView) view.findViewById(R.id.signup);
// and so on...
}

I would recommend not setting a click listener into something you want to type into
You can't call getView before onCreateView has actually returned a proper View
findViewById in Fragment
RegisterFragment.class is not an Activity, you cannot startActivity for it.
Use the FragmentManager
getSupportFragmentManager().beginTransaction()
.replace(R.id.<someContainer>, RegisterFragment.class)
.commit();
Or you can see the documentation on Communication between Fragments to see how you might implement on onLogin() or onRegistrationSelected() action to swap out to the registration fragment or "post-login" main fragment.

Related

Button setOnClickListener onCreateView() or onActivityCreated() in Fragment

by this answer
I can't understand where to put my onClickListener() - inside onCreateView() or inside onActivityCreated() , below codes describe it better:
CODE A: (Setting Button click listener inside onActivityCreated())
private FloatingActionButton bt;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something.
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);
bt = (FloatingActionButton) v.findViewById(R.id.fab);
return v;
}
CODE B: (Setting Button click listener inside onCreateView())
private FloatingActionButton bt;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);
bt = (FloatingActionButton) v.findViewById(R.id.fab);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something.
}
});
return v;
}
I may have not understood which code is better because of my poor English, anyway, thank you all :)
Both will have no effect as far as I know. Once the view is inflated you can put it anywhere either in onCreateView() or in onActivityCreated().
After all, for binding views and setting click listeners, onViewCreated() is a better candidate though, as it will be called immediately after onCreateView. It clearly suggests that your view has been inflated.
There is no specific reason or rule for it. Google itself doesn't care much about it. As a rule of thumb, you can put it anywhere you want once the view is inflated.
I would suggest putting the onClickListener it inside the onActivityCreated. And binding the button to the view inside onCreateView. Just like you have done the first time in your question.
To read more regarding the methods you can go through this post
Since onActivityCreated was deprecated in API level 28, it's probably wise to put it in onCreateView!

How to choose android onclick method?

How do I choose a method called "favorited" through an xml layout file with the
android:onClick
xml method?
the "favorited" method is located here:
tk.talcharnes.popularmovies.MovieDetailsFragment
I have tried many things but nothing works!
Here is the code for favorited
/**
* A placeholder fragment containing a simple view.
*/
public class MovieDetailsFragment extends Fragment {
public MovieDetailsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movie_details, container, false);
//get movie object in order to extract details
Intent intent = getActivity().getIntent();
int movie_number = intent.getIntExtra("Movie_number", 0);
MovieModel movie = PostersFragment.getMovieModelList().get(movie_number);
//set title in details view
TextView titleView = (TextView) rootView.findViewById(R.id.movie_details_text);
titleView.setText(movie.getTitle());
//set poster into details view
ImageView poster = (ImageView)rootView.findViewById(R.id.poster);
Picasso.with(getContext()).load(movie.getPoster_path()).placeholder(R.drawable.temp_poster).into(poster);
// set movie year in details view
TextView release_date = (TextView)rootView.findViewById(R.id.release_date);
if(movie.getRelease_date().length() > 3){
release_date.setText(movie.getRelease_date().substring(0,4));}
else if (movie.getRelease_date() == null){
release_date.setText("Release date not available");
}
else{
release_date.setText((movie.getRelease_date()));
};
//set vote average in details view
TextView vote_average = (TextView) rootView.findViewById(R.id.vote_average);
vote_average.setText(movie.getVote_average() + " /10");
//set overview in details view
TextView overview = (TextView) rootView.findViewById(R.id.overview);
overview.setText(movie.getOverview());
return rootView;
}
public void favorited(){
CheckBox favorited = (CheckBox) getView().findViewById(R.id.favorite);
if (favorited.isChecked()){
Toast.makeText(getContext(), "ITS CHECKED", Toast.LENGTH_SHORT).show();
}
}
}
if you declared android:onClick="favorited" in your xml you need to implement the following method in the activity the fragment is in:
public void favorited(View view) {
//handle click
}
Important: the method needs to be in the activity. You can find more info at this answer
Try below code
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/btn_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="favorited" />
In your xml view specify onClick method like this:
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="favorited" />
and then in your fragment define method like this:
public void favorited(View v) {
// does something very interesting
}
Remember without favorited(View v) your method won't be called from xml.
Use onClick listener instead xml click declaration:
in your xml ad id to clickable element.
<Button android:id="#+id/btn_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
And in your fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movie_details, container, false);
Button faw = (Button) rootView.findViewById(R.id.favorite);
faw.setOnclickListener(new View.OnClickListener(){
#Override
onClick(){
favorited(); //or do another action
}
});
}
This method better to reusing and more safety
add this to the xml
android:onClick="ButtonClick"
and this in the code
public void ButtonClick(View v) {
favorited();
}
you can also add multiple buttons to the same ButtonClick method, just simply add this in there:
switch (v.getId()) {
case R.id.button1:
doSomething1();
break;
}
all of this and more is available all over the place though, here for example: Android OnClickListener - identify a button

Android fragments onClick connect with method

I have the problem with connect method of button in fragment.
It doesn't work ... my application is always closed when I click a button.
In normal activity it work correctly, but why not in fragments? How can I repare it?
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="green"
android:text="#string/green" />
and
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
and my method green
public void green(View v){
//here everything is good
}
import android.view.View.OnClickListener; is marked "is never used"
You cannot do this since there is no context reference to a Fragment without the Activity. So therefore this is only possible in an Activity not a Fragment. Important thing to note here is that your Fragment must call getActivity() to find a reference to the context, since a fragment can be placed in any Activity fragment's themselves really have no context so referencing a Fragment's method in xml will not be possible in this respect.
Directly from Android :
public static final int onClick added in API 4
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).
Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.
This may also be a reference to a resource (in the form "#[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
Constant Value: 16843375 (0x0101026f)
UPDATE
Just Use this then:
Button button = (Button) getView().findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Move your green(View v) method logic here instead of calling green(v)
}
});
UPDATE 2
your fragment code should be doing this before anything else:
public class menu1_fragment extends Fragment {
View rootview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
// Probably safer for you to use onViewCreated(View, Bundle)
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
// use rootView or getView()
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Move your green(View v) method logic here instead of calling green(v)
}
});
}
}
you cannot reference a UI element before the View is inflated, which was the cause of your error. Alternatively you can use onActivityCreated(Bundle savedInstanceState).

Displaying dialog box within fragment

I would like to set up a dialog box in my fragment class. I have done this before with normal activity class, but not with fragment.
I am following this answer HERE that someone has already answered, but I'm getting an error. Will that answer work within a fragment?
I am wanting to open the dialog on image click that I have set up already from an ImageView.
Any help would be great, thanks in advanace.
I would like the dialog code to go:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
/HERE
}
});
return v;
}
If you are using fragments then you should be using DialogFragments instead of the old Dialog box.
http://developer.android.com/reference/android/app/DialogFragment.html

Fragments Navigation in android

Hai i'm new to this fragments development in android. My Question is navigation between the fragments. I have a button in fragment 1 and a text view in fragment2 and i have activity activity1 where i declared both these fragments in xml. My question is when i press the button in fragment1, it has to carry some text and display in the textview of fragment2 before that it has to check whether the fragment2 is in the same layout or not?
Code will be very helpful to me. Thanks in advance..........
At first declare your inflater into onCreateView (in 2ndFragmentClass) like below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
Please consider fragment_content must at lease have a TextView inside itself (So that we set its value inside the fragment). Then we have to change the value of this text from the 1st fragment. So we add this constructor inside our 2nd Fragment (the fragment containing TextView) like below:
public void setText(String name) {
TextView txt= (TextView ) getView().findViewById(R.id.textView1);
txt.setNewText(name);
}
Simply, it would be like below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
public void setText(String name) {
TextView txt= (TextView ) getView().findViewById(R.id.textView1);
txt.setNewText(name);
}
Then we must define which text must be set into 2nd Fragment from 1stFragmentClass. Then we set the text of the 2nd fragment by pressing button in 1st fragment like below:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
String url = "Hello, This is the text from 1st Fragment:)";
//Here we try to declare 2nd fragment.
2ndFragmentClass fragment = (2ndFragmentClass) getFragmentManager()
.findFragmentById(R.id.detailFragment);
fragment.setNewText(url);
}

Categories

Resources