I encounter some issue with getting the reference to a Texview inside a Fragment, and I really have no idea why.
Here is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvLogout"
android:layout_below="#+id/tvSettings"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
And here is the Java Class
public class TabPersonnalSpace extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_personnal_space, container, false);
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
Log.d("Debug", tvSettings.getText().toString());
tvSettings.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT);
}
});
return rootView;
}
}
Thing is : I get the text printed to the logcat when swipe to the Fragment, and nothing happened when I click on the TextView.
Does anyone have a clue for me?
You forget .show() to display Toast
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT).show();
implement onViewCreated like this :
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//instantiate tvSettings
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
}
Related
I created a progress dialog into a fragment by using this effect. Now i must call it in different activities. When I call the effect the buttons at the background must not work. How can I do that?
First i need to learn how to call the fragment in a different activity. Then i must make the buttons at the background unclickable but there are many of them so ‘setclickabla(false)’ would be a tiring choice.
Progress Dialog Fragment XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ProgressDialogFragment">
<com.skyfishjy.library.RippleBackground
android:id="#+id/ProgressDialogs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CB000000"
app:rb_color="#80FFFFFF"
app:rb_duration="2500"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_scale="6">
<ImageView
android:id="#+id/centerImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/logo" />
</com.skyfishjy.library.RippleBackground>
ProgressDialogFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
rippleBackground.startRippleAnimation();
return view;
}
For the buttons at the background to respond to clicks, you have to implement the onClick listener of the various buttons.
<com.skyfishjy.library.RippleBackground
android:id="#+id/ProgressDialogs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CB000000"
app:rb_color="#80FFFFFF"
app:rb_duration="2500"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_scale="6">
<Button
android:id="#+id/centerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/logo" />
In your layout above, you are making use of ImageView which I changed to centerButton and you can make it respond to clicks this way:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
rippleBackground.startRippleAnimation();
Button button=(Button)view.findViewById(R.id.centerButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// your respond to clicks
}
});
return view;
}
Call your fragment from your activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new MainFragment(); // your fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
I searched many threads but couldn't understand the problem in my code. I am trying to add setOnClickListener functionality on a button from a fragment. It gives me
nullpointer exception at allNews.setOnClickListener(new View.OnClickListener().
Upon debugging I understand that the variable allNews is null.
Below is my layout code.
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id = "#+id/b_all"
android:layout_width="70dp"
android:layout_height="70dp"
android:gravity="center"
android:text="All \n news"
android:textColor="#FFF"
android:textSize="15sp"
android:background="#drawable/button_bg_round"
android:padding="5dp"
android:textAllCaps="false"
/>
And here is my java code:
public class CategoriesFragment extends Fragment {
public Button allNews;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
getActivity().setTitle("Play");
allNews = (Button) rootView.findViewById(R.id.b_all);
return rootView;
}
public void onViewCreated(View view, #android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
I am unable to understand what am I missing. There is a scrollview inside the same xml. Should we handle such layouts/fields seperately ?
Any help in this regard is highly appreciated.
Thanks
Satya
SOLVED: I renamed the xml with activity_news_category and then the variable is not null anymore. Prev the xml was named as activity_news_menu. Thnx for all who attempted to help.
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
getActivity().setTitle("Play");
allNews = (Button) rootView.findViewById(R.id.b_all);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
return rootView;
}
Use set onclicklistener in OncreateView method.Hope this helps
public void onViewCreated(View view, #android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
allNews = (Button) view.findViewById(R.id.b_all);
allNews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(getActivity(), Newsmain.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
UPDATE:
instead of rootView it should be view
Add allNews = (Button) rootView.findViewById(R.id.b_all); in onViewCreated() and remove it from onCreateView()
1) Close the LinearLayout properly.
2) Remove the space between the android:id = "#+id/b_all", so you have:
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/b_all"
android:layout_width="70dp"
android:layout_height="70dp"
android:gravity="center"
android:text="All \n news"
android:textColor="#FFF"
android:textSize="15sp"
android:background="#drawable/button_bg_round"
android:padding="5dp"
android:textAllCaps="false"
/>
</LinearLayout>
Then rebuild the project.
Good afternoon,
I am trying to create a fragment with a RecyclerView including items.
Above those items, I need to put a header with some filters.
Here is the fragment's xml (fragment_products_list) :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12sp"
android:id="#+id/filters_frame">
<include layout="#layout/expandable_filters"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Here is the OnCreateView in my Fragment java file (ListFragment.java) :
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(
R.layout.fragment_products_list, container, false);
//Product filters
expandFiltersButton = (ImageButton) getView().findViewById(R.id.expand_filters);
expandableZone = (LinearLayout) getView().findViewById(R.id.expandable_zone);
expandFiltersButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(expandableZone.isShown()){
expandableZone.setVisibility(View.GONE);
expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
}else{
expandableZone.setVisibility(View.VISIBLE);
expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
}
}
});
setupRecyclerView(rv);
mRecyclerView = rv;
loadUpdates();
return rv;
}
I have this error code when I am trying to run the app :
android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView
What is the problem and how can I solve it?
Thanks for your help!
Try this:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.fragment_products_list, container, false);
//Product filters
RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
expandFiltersButton = (ImageButton) view.findViewById(R.id.expand_filters);
expandableZone = (LinearLayout) view.findViewById(R.id.expandable_zone);
expandFiltersButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(expandableZone.isShown()){
expandableZone.setVisibility(View.GONE);
expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
}else{
expandableZone.setVisibility(View.VISIBLE);
expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
}
}
});
setupRecyclerView(rv);
mRecyclerView = rv;
loadUpdates();
return view;
}
the following is my java file for one of the fragments that am using to have multiple image buttons which open multiple weebpages.
please help.
treat me as a noob.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.menu8_layout, container, false);
return rootview;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button bt1 = (Button) findViewById(R.id.btn_click_login);
btn_login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://google.com"));
startActivity(myWebLink);
});
}
}
here is the related xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt1"
android:clickable="true"
android:autoLink="web"
android:cursorVisible="true"
android:linksClickable="true"
android:onClick="goToSo"
android:text="some" />
I'm writing and android 4.4 project using android studio.
I'm new to the fragments idea and trying to create a simple application with a button that the click handler sends a message to the log.
this is the fragment class
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
return rootView;
}
public void addStringClickHandler(View v) {
Log.d("tag","hello");
}
}
this is the fragment layout XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.tuxin.myalcoholist.myalcoholist.myalcoholist.MainMenuActivity$PlaceholderFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_drink"
android:id="#+id/add_drink"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:onClick="addStringClickHandler" />
</RelativeLayout>
as you can see i set in the XML android:onclick to addStringClickHandler
and in the fragment class i created that function, but when I execute the application
I get an error that the runtime could not find a method addStringClickHandler(view)
what am I missing?
Your android:onClick handler method has to belong to the Activity hosting your fragment. Just move addStringClickHandler() method to the activity.
If you want to have a listener method in the fragment you have to set listener in the code like this.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("tag","hello");
}
});
return rootView;
}
Try to register the onClick method when inflating the view.
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do what you need
}
});
return rootView;
Or you can set the fragment to be the on click listener
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(this);
return rootView;
and then make the fragment implement OnClickListener.