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
Related
I have been using the fragments(Java). The list is showing in the RecyclerView but when I swipe left or right, it is not getting detected.
I do not know Kotlin. Need help in Java implementation.
Bible - Chapter view
The expected behavior is to go to the next chapter or previous chapter when I swipe left or right. Each chapter is a List.
I have tried with ViewPager2 but I am unable to understand how to use RecyclerView inside ViewPager2.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.handsoncloud.holybible.R;
import com.handsoncloud.holybible.adapters.RecyclerBibleAdapter;
import com.handsoncloud.holybible.ui.chapter.ChaptersFragment;
import com.handsoncloud.holybible.utils.Books;
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
RecyclerView recyclerView = rootView.findViewById(R.id.recycler_bibleBooks);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecyclerBibleAdapter recyclerBibleAdapter = new RecyclerBibleAdapter(getContext(), Books.getBooks(), (book, position) -> {
ChaptersFragment.BOOK_NAME_ENGLISH = Books.booksInEnglish.get(position);
ChaptersFragment.BOOK_NAME_TELUGU = Books.booksInTelugu.get(position);
});
recyclerView.setAdapter(recyclerBibleAdapter);
return rootView;
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
}
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 seems like a common issue with lots of accepted answers but none of them seem to be working for me.
Most answers suggest using this piece of code to add a toolbar into a fragment:
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
Alas, this still crashes my app even though I'm using AppCompatActivity. I think the error may lie in the fact that my Fragment class extends Fragment and not AppCompatActivity but I don't know enough about android as of yet to be sure of this. It crashes on the setSupportActionBar line.
My Fragment code:
package erikligai.ribbitapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by erik on 2017-06-07.
*/
public class MessageFragment extends Fragment {
Toolbar mToolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// return inflater.inflate(R.layout.message_fragment_layout, container, false);
View rootView = inflater.inflate(R.layout.message_fragment_layout, container, false);
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
return rootView;
}
Would appreciate any suggestions.
You need to cast your activity from getActivity() to AppCompatActivity
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle();
Toolbar getting from Activity, not from Fragment
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
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.
When I click at the menu items, then layouts not coming to view.
And MainActicity wanna "menu1_Fragment.java" encode with "android.support.v4.app.Fragment"
İf I encode only "Fragment", MainActivity is getting error.
In compatible types.
Required :android.support.v4.app.Fragment
Found :intizamyazilim.navigationdrawernew.menu1_Fragment
Here is menu1_Fragment.java
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 01.03.2015.
*/
public class menu1_Fragment extends android.support.v4.app.Fragment {
View rootview;
#Nullable
// #Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
}
I Fix it.
I changed this "menu1_Fragment.java" with this codes:
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class menu1_Fragment extends android.support.v4.app.Fragment {
public menu1_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu1_layout, container, false);
return rootView;
}
}
No it is not the proper way to fix it. The proble here is that you are extending your menu1_Fragment using support library and on the other hand you are importing fragment from SDK(import android.app.Fragment;). Make it consistent. Either use fragment from support or from android SDK.