Basically, I'm using Fragment in my project and all works fine
But I found that I can't return the strings from the value folder to the android:text
Example :
android:text="Hello World" << this works fine in Fragment , return Hello World in my TextView
android:text="#string/text1" << this doesn't work in Fragment ! it return #string/text1 in my TextView as it is
Can someone tell me a solution for this problem ?
my xml layout
<?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"
android:orientation="vertical"
android:background="#fa6a6a" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/text1"
android:textSize="20dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
main
public class AboutMe extends Fragment {
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_aboutme, container, false);
setHasOptionsMenu(true);
return rootView;
}
You need to access it by:
getActivity().getResources().getString(R.string.text1);
Related
I've implemented onClickListener but inside onCreateView() it's unable to find the button's id and i.e "myButton"
My Code is:
public class Category extends Fragment implements View.OnClickListener {
View view;Button b;
public Category() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_category, container,false);
b=(Button)view.findViewById(R.id.myButton);
b.setOnClickListener(this);
return view;
}
My fragment_category.xml is as below it's containing one button and a textView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.Category"
android:id="#+id/constraintLayout">
<TextView
android:layout_width="368dp"
android:layout_height="495dp"
android:text="#string/hello_blank_fragment"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="Button"
tools:layout_editor_absoluteX="135dp"
tools:layout_editor_absoluteY="208dp" />
You are inflating "fragment_category", but you said you posted "fragment" xml code.
You're most likely not inflating the correct layout file.
Actually problem was there in IDE after some rectifications the error is sorted out.
I am having trouble showing an image from the drawable folder on to a fragment.
When I try to show do it, it comes up with a java.lang.NullPointerException.
any help would be appreciated.
This is my code,
public class Fragment4 extends Fragment {
public Fragment4() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate (R.layout.fragment4, container, false);
ImageView image = (ImageView)rootView.findViewById(R.id.image);
image.setImageResource(R.drawable.image123);
return rootView;
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Your ImageView has no id. Set it as follows,
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
It is because you didn't add id attribute to the image in xml
Try this:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Hello i want to add a popup window to a fragment i tried a lot of things still nothing i have. If someone helps me it will be great.
Here is my Fragment code;
public class Info extends Fragment {
public Info() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.info, container, false);
}
Here is my fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/buttonShowCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Dialog" />
</LinearLayout>
Here is my popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:src="#drawable/pozitif"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:text="dfdfdsfs"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
You can use a DialogFragment.
Instead of extending your Activity with Fragment, use DialogFragment
In on createView of the DailogFragment Activity inflate your layout
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
Find your View elements
TextView Message = (TextView) v.findViewById(R.id.text_view1);
To call your DailogFragment use this
MyDialogFragment dialog = MyDialogFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "MyDialogFragment");
For more info on this you can check out this link.
https://developer.android.com/reference/android/app/DialogFragment.html
I include a layout in my fragment1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/family_header"
layout="#layout/header" />
header xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#33cc66"
android:orientation="horizontal" >
<ImageView
android:id="#+id/nav_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/nav_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical" />
<ImageView
android:id="#+id/nav_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I want to change TextView "nav_text" value dynamically in my Fragment1.class . how to do it
you can create class that extends fragement....
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}
}
in this class you can change value of textview dynemically.....please put your code here.so we can understand what is your actual problem.
Use this code in your fragment oncreateview method like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container,false);
final LinearLayout headerLayout= (LinearLayout ) v.findViewById(R.id.family_header);
TextView txtHeaderText=(TextView)headerLayout.findViewById(R.id.nav_text);
txtHeaderText.setText("My New Text");
return v;
}
I hope it's work for you.
in my application I use SherlockActionBar with tab navigation.
In the activity MyFragmentActivity that extend SherlockFragmentActivity I add 3 tab with the fragment
mTabsAdapter.addTab(
bar.newTab().setText(getString(R.string.filmtab)),
FragmentFilm.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.cinematab)),
FragmentCinema.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.dintornitab)),
FragmentPdi.class, null);
The FragmentPdi class have this code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_pdi_info, container, false);
return view;
}
and the layout is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/list_places"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
In the MyFragmentActivity I want find the listview for populate it in this way:
final ListView elencoPlaces = (ListView)findViewById(R.id.list_places);
....
but I have NullPointerException!
Why??
In the other fragment (FragmentFilm and FragmentCinema) I have other view like textview without this
problem!!
Please help me!
Thank you.
P.S. Is this the correct way for populate dinamically the views in fragment, or I must populate the view in the fragment class?? If the second how I can pass varible from the activity to the fragment??
if all you have in the layout file is a listview, don't bother overriding oncreateview. the default layout will give you that much. then you can use the methods built into the ListFragment - like getListView() and setListAdapter().
Ok, but I dont undestand why I have another fragment the FragmentCinema where I have this xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:paddingTop="10dip" android:id="#+id/otherCinemas" android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="horizontal" android:id="#+id/plotSeparatorOtherCin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/txtProgrammation">
<ImageView android:layout_gravity="center_vertical" android:background="#drawable/palm_divider_line" android:layout_width="20.0dip" android:layout_height="wrap_content" android:layout_marginRight="5.0dip" />
<TextView android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/altricinema" android:textStyle="bold" />
<ImageView android:id="#+id/showOtherCinemas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_freccia_cinema"
/>
<ImageView android:layout_gravity="center_vertical" android:background="#drawable/palm_divider_line" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="#+id/listOtherCinemas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_below="#id/plotSeparatorOtherCin"
android:visibility="gone" >
</ListView>
</RelativeLayout>
and this code of class:
public class FragmentCinema extends SherlockFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_cinema_info, container, false);
return view;
}
}
and when I use the findViewbyId
final ListView cinemasOther = (ListView)findViewById(R.id.listOtherCinemas);
it works! But in FragmentPdi dont work!!
If you want to use ListView in ListFragments, your ListView must have id android:id="#+id/android:list"!