Hi I am busy making a program where you get to rate people
I've been playing around with the Rating Bar widget however I am struggling to make it show a rating after selecting the amount of stars. What I'm I doing wrong. I would also like to to know if the android sdk have anything on up and down votes and would it be wise to use images for a rating system.
The reason why some of the code is commented out is because my app won't run when its uncommented but I know its needed to do what I want to do.
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class rating_system_fragment extends Fragment implements OnRatingBarChangeListener{
RatingBar ratingBar;
TextView ratingResult;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater.inflate(R.layout.rating_system_fragment,container,false);
//setContentView(R.layout.activity_main);
return inflater.inflate(R.layout.rating_system_fragment, container, false);
//ratingResult = (TextView) ratingResult.findViewById(R.id.textViewRating);
//((RatingBar) ratingBar.findViewById(R.id.ratingBar))
// .setOnRatingBarChangeListener(this);
}
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
final int numStars = ratingBar.getNumStars();
ratingResult.setText(rating + "/" + numStars);
}
}
You have several syntax erros in your commented code. It should look like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.rating_system_fragment,container,false);
ratingResult = (TextView) rootView.findViewById(R.id.textViewRating);
ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(this);
return rootView;
}
The key here is that ratingBar and textViewRating are childs of your root view, and should only be accessed when you have aready inflated them.
Related
I have here a problem with the onClicklistener method in a fragment. If I click on the button nothing happens and I don't know why. No error and no output.
I've tried to implements the OnClickListener or set a (Button) before the view.findViewById(R.id.button); but nothing helps.
I've seen much questions here in Stack Overflow about this problem but no solution from there helps me :/
Do you have any ideas? Thank You!
package com.christoph.myapplication.ui.home;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.christoph.myapplication.R;
public class HomeFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
final Button button= view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setBackgroundColor(Color.RED);
System.out.println("Hallo");
}
});
return view;
}
}
You can add click listener rather in onViewCreated function. Try it and let me know if it works. Check android fragment lifecycles documentation for more info.
Cheers
This question already has answers here:
How do I "findViewById" with fragments?
(2 answers)
Closed 4 years ago.
I wanted to know something, I want to make an application on android with Tabbed Activity, I have everything done, but I have a problem, I want to put TextViews in one of the tab and it does not let me instantiate, if I put the
match = (TextView) findViewById (R.id.match);
The program tells me:
Can not resolve method 'findViewById (int)'
How could I do it? Is there any way?
package com.example.jose.firebaseimportante;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class TabbedUsuarioFutbol extends Fragment{
public TabbedUsuarioFutbol() {}
private TextView partido1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.futbol_tabbed_usuario, container, false);
return rootView;
match = (TextView)findViewById(R.id.match);
}
}
Change
match = (TextView)findViewById(R.id.match);
to
match = rootView.findViewById(R.id.match);
and
return rootView;
should be the last statement in your onCreateView method because any statement after a return statement is unreachable statement, i.e. it will never execute.
Your onCreateView method should look like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.futbol_tabbed_usuario, container, false);
match = rootView.findViewById(R.id.match);
return rootView;
}
I have created an android application using Android Fragments as described in Dynamically Prevent Rotation On Android Fragment.
The application is working fine but the problem is that say a particular fragment say Photo Gallery might calls several http calls and might certain time for bringing all the images. So when we click the Photo Gallery it seems stuck for sometime.
How can we show some slider or progress bar like something till the fragment view get loaded completely?
An example like is as shown below:
My PhotoGalleryFragment is given below
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PhotoGalleryFragment extends Fragment {
public PhotoGalleryFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
// some http calls to be triggered
return rootView;
}
}
You can use ProgressDialog, for example:
public class PhotoGalleryFragment extends Fragment {
private ProgressDialog mProgressDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
// some http calls to be triggered
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mProgressDialog = ProgressDialog.show(activity, "", "Loading...", true, false);
}
}
When you http calls are ended you can dismiss the ProgressDialog with mProgressDialog.dismiss().
I want to set wordtoSpan string at OnCreateView.
my code:
package com.tachles;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
return rootView;
horef = (TextView) findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
}
}
Eclipse says that I cant use findViewById, so I tried to insert getView() but still the same problem. Please show me my mistake
Thank you
If history_moadim_a is your layout containing the textView3, change your onCreateView() to something like
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
You want to find the TextView in the layout you just inflated (rootView), and return the layout at the end (and not in the middle of the method).
use
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
}
}
i can not see a return statement at onCreateView at perfect place so please do return rootView ; inside onCreateView at the end of the logics by means at the end of the onCreateView
I have a class that manages a fragment .. in a TextView I have to put the phone features, but because by mistake?
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
return view;
}
String model = Build.MODEL;
String product = Build.PRODUCT;
String androidOS = Build.VERSION.RELEASE;
String scheda = model+"\n"+androidOS+" \n "+product+"";
TextView text = (TextView) findViewById(R.id.textView3);
text.setText(scheda);
private TextView findViewById(int textview3) {
// TODO Auto-generated method stub
return null;
}
}
I did not understand a thing. I have three fragment, one of which is called Home, one faq and one about. How do I change the contents of the various "screens"?
Firstly you cannot have operations on objects in the main class body without a helper method in java. I also see when you are creating a reference to you TextView, you need to find the TextView from your inflated view, so the right way to do it would be something like
TextView text = (TextView)view.findViewById(R.id.textview3);
Lastly, when inflating the view, pass the ViewGroup as the parent for that view, and a false for the attachToRoot
You should have something like:
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page,container,false);
String model = Build.MODEL;
String product = Build.PRODUCT;
String androidOS = Build.VERSION.RELEASE;
String scheda = model+"\n"+androidOS+" \n "+product+"";
TextView text = (TextView) view.findViewById(R.id.textView3);
text.setText(scheda);
return view;
}
}
Hope that helps