How can i add this popup.xml to this fragment? - android

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

Related

Moving from fragment to activity giving error [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
findViewById returns NULL when using Fragment
(7 answers)
Closed 1 year ago.
Actually I saw a yt video of how to intent fragment and acticity and that is what I tried but don"t know how the error is coming.The error which I am having is ** java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.photoframeapp.HomeFragment.onCreateView(HomeFragment.java:31)**.I tried to sort out but I am not able to .
this is my HomeFragment.java class
public class HomeFragment extends Fragment {
TextView beginners;
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
beginners.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),mainpage.class));
}
});
return view;
}
}
this is my mainpage.java file
public class mainpage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
}
}
fragment_home.xml file
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="387dp"
android:alpha="0.4"
android:background="#color/Purple" />
<TextView
android:id="#+id/beginners"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-27dp"
android:layout_marginBottom="433dp"
android:freezesText="true"
android:text=" BEGINNERS CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="195dp"
android:alpha="0.4"
android:background="#color/DeepPink" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-9dp"
android:layout_marginBottom="67dp"
android:text=" ADVANCED CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
android:alpha="0.4"
android:background="#color/DarkBlue" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-8dp"
android:layout_marginBottom="261dp"
android:freezesText="true"
android:text=" INTERMEDIATE CALLIGRAPHY"
android:textSize="25dp" />
</RelativeLayout>
You have to initialize textView before using it.
View view = inflater.inflate(R.layout.fragment_home, container, false);
beginners = view.findViewById(R.id.beginners)
beginners.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),mainpage.class));
}
});

Viewing an image on a Android Fragment

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" />

DialogFragment is not going up when soft keyboard is on

I have dialogs in other places in the app that work just fine(keyboad doesn't hide the view) , but in this particular new dialog I wrote it doesn't move the view up when the keyboard is on. I can't figure out why..
My dialog's layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/MyLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:background="#drawable/dialog_background"
android:clickable="true"
android:orientation="vertical"
custom:maxHeight="#dimen/dialog_max_height"
custom:maxWidth="#dimen/dialog_max_width" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="something" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:id="#+id/SomeEditText"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:minHeight="100dp"
android:layout_margin="15dp"
android:background="#ffffff"
android:gravity="right|top"
android:inputType="textMultiLine"
android:singleLine="false"
android:textColor="#color/edit_text_text_color"
android:textSize="16dp" />
</LinearLayout>
<Button
android:id="#+id/SomeButton"
android:layout_width="match_parent"
android:layout_height="#dimen/dialog_button_height"
android:layout_margin="15dp"
android:text="text" />
</LinearLayout>
I have
android:windowSoftInputMode="adjustPan" >
set in my manifest.
Found the solution :
I put : getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
in the onCreateDialog callback
I also faced the same issue and resolved by keeping getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
inside oncreateview
Since SOFT_INPUT_ADJUST_RESIZE is deprecated starting android 30, you should use Window.setDecorFitsSystemWindows(boolean decorFitsSystemWindows)
Here is example for DialogFragment
class MyDialog extends DialogFragment {
#Override
public void onDestroyView() {
super.onDestroyView();
getDialog().getWindow().setDecorFitsSystemWindows(true);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_dialog, container, false);
v.setOnApplyWindowInsetsListener((v1, insets) -> {
Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
v1.setPadding(0, 0, 0, imeInsets.bottom);
return insets;
});
getDialog().getWindow().setDecorFitsSystemWindows(false);
return v;
}
}

Change a particuar view in Fragment

I am new to android developing and I am currently stumped at this situation.
Is there a way to change a particular child view of a fragment?
For example, I have a TextView and Button in a Fragment. Now after inflating this fragment and committing it, I would like to only change the text of the TextView based on the user input.
EDIT: The same layout is used to create multiple fragments. I would like to just change the TextView of a particular Fragment.
MainActivity Code:
public class MainActivity extends Activity {
Button bt;
EditText et;
LinearLayout ly;
String m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.badd);
ly=(LinearLayout) findViewById(R.id.lay_ver);
et=(EditText) findViewById(R.id.etadd);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// multiple fragments are created
MyFragment f=new MyFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction t=fm.beginTransaction();
t.add(R.id.sv1, f, "hi");
t.commit();
et.setText("");
}
});
//would like to change the text of the textview of the fragment
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Fragment Code:
public class MyFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.inflate(R.layout.frag_lay, null);
return v;
}
}
activity_main XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lay_ver"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/lay_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/etadd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:hint="Enter Subject"
android:paddingTop="20dp" />
<Button
android:id="#+id/badd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Add" />
</LinearLayout>
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/sv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layfrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.53"
android:text="HiHow"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
You can add a list of Fragments in your activity and each fragment to the list when you create them. Then you can get the fragment you want to change and do as cdblades said :
View parent = fragment.getView();
View childTextView = (TextView)parent.findViewById(R.id.myChildTextView);
But why are you adding the fragments in the same container?
The base View class has the findById() method that you can use to get a reference to child views like so:
View parent = fragment.getView();
View childTextView = (TextView)parent.findViewById(R.id.myChildTextView);

Find List View in fragment from Activity

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"!

Categories

Resources