I want to open BottomSheetDialog from fragment. Before it, I tried opening BottomSheetDialog at activity and it worked. I use the same code and replace the getSupportFragmentManager with getChildFragmentManager. I also tried replacing the getSupportFragmentManager with getActivity().getSupportFragmentManager . Is there a solution?
public class MainFragment extends Fragment implements BottomSheetDialog.BottomSheetListener {
private TextView mTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_beranda, container, false);
Button buttonOpenBottomSheet = findViewById(R.id.button_open_bottom_sheet);
buttonOpenBottomSheet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheet = new BottomSheetDialog();
bottomSheet.show(getChildFragmentManager(),"BottomSheet");
}
});
}
}
Try BottomSheetDialogFragment
ModalBottomSheetDialogFragment.Builder()
.add(R.menu.menu_options)
.show(childFragmentManager, "my_bottom_sheet")
Related
I tried but i am getting errors "Onclick listener can't be applied here" Please help me guys
Here is my Fragment.java
public class Fragment extends Fragment {
public Fragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
final Button button = (Button) rootView.findViewById(R.id.payment_info);
final Button button1 = (Button) rootView.findViewById(R.id.redeem);
final Button button2 = (Button) rootView.findViewById(R.id.make_payment);
final Button button3 = (Button) rootView.findViewById(R.id.payment_histories);
// Inflate the layout for this fragment
return rootView;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
}
OnClickListener is an interface, so you have not to only extend the fragment but to implement the OnClick.
Try this on your declaration of the class:
public class Fragment extends Fragment implements OnClickListener
Hope it helps.
How do I call MainActivityFragment from AppCompatActivity in Android? (I am new to Android.)
Code
main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create new fragment and transaction
}
}
Fragment
public class MainActivityFragment extends Fragment {
ArrayAdapter mforecastadapter;
ListView list_view;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.fragment_main, container, false);
String Items[] = {"Sandeep", "Santhosh", "Sangeetha", "Basil", "bibhas", "sumanth", "praveen"};
List<String> weekforecast=new ArrayList<>(Arrays.asList(Items));
mforecastadapter=new ArrayAdapter<String>(getActivity(),R.layout.fragment_main,R.id.list_item_forecast_textview,Items);
list_view = (ListView) rootview.findViewById(R.id.listView);
list_view.setAdapter(mforecastadapter);
return rootview;
}
}
Don't really know what you mean by "call", but if you want to instantiate a fragment in an Activity, write this in your Activity:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
YourFragment frag = new YourFragment ();
ft.replace(R.id.container_for_fragment, frag);
ft.commit();
Notice R.id.container_for_fragment is your xml attribute for containing the fragment instance. it can be <fragment>, <FrameLayout>.
This is the most basic way to do that. You can find some more examples on SO, how to use fragments BackStack, etc.
You can also use add() instead of replace() in FragmentTransaction, depends on your need.
I hope it helps you.
So I got 2 fragments and an activity. Those 2 fragments are communicating each other via an interface.
Problem is, I can't change the TextView in the second fragment from Activity wih some text from fragment1.
There is the same problem( How to change fragment's textView's text from activity ) but there is no real solution so i thought i should open this.
Fragment 1:
public class test_layout extends Fragment {
DearListener activityCommander;
TextView custom_question_bar, list_number;
String selectedCustomQuestion;
boolean buttonClicked;
Button yiyecekButton;
Context c;
public interface DearListener {
public void getMyText(String theQuestion);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCommander = (DearListener) activity;
Log.i("MYLOG", "onAttach");
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_test_layout, container, false);
//custom_question_bar= (TextView) v.findViewById(R.id.custom_question_bar);
//list_number= (TextView) v.findViewById(R.id.list_number);
yiyecekButton = (Button) v.findViewById(R.id.yiyecekButton);
Log.i("MYLOG", "Button referenced");
yiyecekButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
Log.i("MYLOG", "onCLickListener Called");
onClickYiyecek(v);
}
}
);
return v;
}
public void onClickYiyecek(View v){
Log.i("MYLOG", "OnClickYiyecek");
activityCommander.getMyText("En sevdiğim yiyecek ne?");
}
public void onClickQuestionReady(View v){
if( custom_question_bar == null)
{
Toast.makeText(c, "Boşluğu doldurmadınız", Toast.LENGTH_SHORT).show();
}
activityCommander.getMyText(custom_question_bar.getText().toString());
}
}
And the Activity:
public class NewTest extends Activity implements test_layout.DearListener {
FragmentManager manager;
test_layout test_layout_frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_holder);
manager=getFragmentManager();
test_layout_frag = new test_layout();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.the_frame_layout, test_layout_frag);
transaction.commit();
}
#Override
public void getMyText(String theQuestion) {
manager=getFragmentManager();
answer_layout answer_layout_frag = new answer_layout();
answer_layout answer_layout_obj = (answer_layout)getFragmentManager().findFragmentById(R.id.fragment2);
answer_layout_obj.changeText(theQuestion);
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.the_frame_layout, answer_layout_frag);
transaction.commit();
Log.i("MYLOG", "Transaction Called");
//transaction.addToBackStack(null);
}
}
And the second fragment:
public class answer_layout extends Fragment {
TextView yourSelectedQuestion;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_answer_layout, container, false);
yourSelectedQuestion= (TextView) v.findViewById(R.id.yourSelectedQuestion);
return v;
}
public void changeText(String a) {
Log.i("MYLOG", "changeText");
Log.i("MYLOG", a);
yourSelectedQuestion.setText(a);
}
}
No exception is given but the TextView in second fragment isn't changing.
Since i didn't write the xml here, I can if its needed. Using FrameLayout and it contains 2 fragments inside it.
In myFragment class write a static method:
public static myFragment newInstance(String txt) {
myFragment myfragment = new myFragment();
Bundle args = new Bundle();
args.putString("TEXT", txt);
myfragment.setArguments(args);
return myfragment;
}
after that in OnCreate method of myFragment class inflate the textView tv and
tv.setText(getArguments().getString("TEXT"));
In main FragmentActivity class do:
myFragment fragment = myFragment.newInstance(textToBeSent);
before replace,
fragmentTransaction.replace(R.id.fr_dynamic, fragment);
fragmentTransaction.commit();
I have a ParseQueryAdapter that is populating a listview inside of a fragment:
QueryAdapter:
public class CustomAdapter extends ParseQueryAdapter implements AdapterView.OnItemClickListener {
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Invoice");
query.whereEqualTo("CustomerID", ParseUser.getCurrentUser());
return query;
}
});
}
#Override
public View getItemView(ParseObject object, View cellview, ViewGroup parent) {
if (cellview == null) {
cellview = View.inflate(getContext(), R.layout.cell_layout, null);
}
TextView cellDate = (TextView) cellview.findViewById(R.id.dateTextView);
TextView invoice = (TextView) cellview.findViewById(R.id.invoiceNoTextView);
cellDate.setText(object.getString("Date"));
invoice.setText(object.getString("InvoiceNumber"));
return cellview;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
Fragment Containing Listview:
public class InvoiceHistory extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_invoice_history, container, false);
ListView invoicesListView = (ListView) layout.findViewById(R.id.invoiceList);
ParseQueryAdapter adapter = new CustomAdapter(getActivity());
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment detailView = new InvoicesFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.placeholder, detailView);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
invoicesListView.setAdapter(adapter);
adapter.loadObjects();
return layout;
}
}
This populates the ListView with the proper fields from the query, but when I click on one of the cells the fragment transaction takes place, but i can't figure out how to set textfields on that new fragment with ParseObject Keys from the Query.
This is the fragment that gets Presented that I want to set the Textfields in:
public class InvoicesFragment extends Fragment {
public InvoicesFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
return layout;
}
Android is super new to me so maybe this is a wrong way to approach it. But all i want to do here is have that detail fragment show up with populated TextViews. Im not really sure what I'm doing.
You could approach this by setting up a constructor in your DetailsFragment that you can pass in the textview info you want to show. In the example below, the data and invoice number are passed into the constructor. When the fragment is set up, this fragment is set up with the Parse object's data:
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ParseObject object = adapter.getItem(i);
Fragment detailView = DetailsFragment.newInstance(object.getString("Date"),
object.getString("InvoiceNumber"));
// ...
}
});
Here's a portion of the DetailsFragment showing the new constructor as well as how you could use them in setting up your view:
// InvoicesFragment.java
public static class InvoicesFragment extends Fragment {
// ...
public static InvoicesFragment newInstance(String invoiceDate, String invoiceNumber) {
InvoicesFragment f = new InvoicesFragment();
Bundle args = new Bundle();
args.putString("invoiceDate", InvoiceDate);
args.putString("invoiceNumber", InvoiceNumber);
f.setArguments(args);
return f;
}
public String getInvoiceDate() {
return getArguments().getString("invoiceDate", "");
}
public String getInvoiceNumber() {
return getArguments().getString("invoiceNumber", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
// Assign some TextView, ex: dateTextView
dateTextView.setText(getInvoiceDate());
return layout;
}
}
I seek to receive data from AsyncTask inside a fragment. I'm aware to achieve this with activity but having difficulties in fragment.
GetResult class
public interface GetResult {
void getData(ArrayList<String> result);
}
myFrag class
public class myFrag extends Fragment implements GetResult{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.myFrag_layout, container, false);
Button myBtn = (Button) view.findViewById(R.id.Button1);
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
myAsync obj = new myAsync();
obj.setListener(getActivity());
}
});
}
}
myAsync class
public class myAsync extends AsyncTask<Void, Void, String>{
GetResult interfaceObj = null;
public void setListener( GetResult interfaceObj ) {
this.interfaceObj = interfaceObj;
}
}
I'm having problem with obj.setListener(getActivity). It say The method setListener(GetResult) in the type myAsync is not applicable for the arguments (FragmentActivity). Thanks in advance to everyone.
I didn't figured out wt the problem is but there is another way to do it. I'll just share it here.
1st the interface class GetResult.class is same
2nd Changes in the fragment class myFrag.class
public class myFrag extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.myFrag_layout, container, false);
Button myBtn = (Button) view.findViewById(R.id.Button1);
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
getLoc obj = new getLoc(getActivity(), new GetResultInterface() {
#Override
public void processFinish(ArrayList<String> result) {
// U can get the result here
}
obj.execute(url);
});
}
}
3rd In AsyncTask myAsync.class
public class myAsync extends AsyncTask<Void, Void, String>{
Context context;
GetResult interfaceObj = null;
public myAsync(Context context, GetResultInterface interfaceObj) {
this.context = context;
this.interfaceObj = interfaceObj;
}
#Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
interfaceObj.processFinish(result);
}
}