I would like to put a fragment inside a custom dialog in activity.
I receive this error:
java.lang.IllegalArgumentException: No view found for id 0x7f0c007e (copyworld.rebootcw:id/container_schedule1) for fragment FragmentGiorno{418215c0 #0 id=0x7f0c007e}
This is my code:
layout.xml
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/spinner_dialog_schedulazione"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:id="#+id/container_schedule1">
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Popup_schedule();
}
//******POPUP PER LA SCHEDULAZIONE******************
public void Popup_schedule() {
dialog_schedule=new Dialog(MainActivity.this);
dialog_schedule.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog_schedule.setContentView(R.layout.dialog_schedule);
dialog_schedule.setCancelable(false);
lp = new WindowManager.LayoutParams();
window = dialog_schedule.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog_schedule.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window = dialog_schedule.getWindow();
window.setAttributes(lp);
FragmentGiorno fragment = new FragmentGiorno();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_schedule1, fragment);
fragmentTransaction.commit();
dialog_schedule.show();
}
}
How I add a fragment inside a custom dialog in MainActivity.java that extend Activity?
Try this
dialog_schedule.findViewById(R.id.container_schedule1)
Instead Of
R.id.container_schedule1
In your Code
If you want to load fragment inside dialog then you can use DialogFragment instead.
Below is the working example of using DialogFragment.
public class Demo extends DialogFragment{
View mainView;
LinearLayout layoutArrow;
Bundle bundle;
ArrayList<BookingDetail> bookingDetails;
View.OnClickListener onClickListener;
public static Demo newInstance(#NonNull ArrayList<BookingDetail> bookingDetails) {
Demo alertPopUpMapReservedBedInfo = new Demo();
Bundle args = new Bundle();
args.putSerializable("bookingDetails", bookingDetails);
alertPopUpMapReservedBedInfo.setArguments(args);
return alertPopUpMapReservedBedInfo;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, R.style.InvitationDialogWithSimpleAnim);
bundle = getArguments();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.alert_info, container, false);
getDialog().setCanceledOnTouchOutside(true);
return mainView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
layoutArrow = (LinearLayout) mainView.findViewById(R.id.layoutArrow);
FragmentGiorno fragment = new FragmentGiorno();
android.support.v4.app.FragmentTransaction fragmentTransaction =
this.getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_schedule1, fragment);
fragmentTransaction.commit();
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if(onClickListener!=null){
onClickListener.onClick(null);
}
}
}
And call it by,
Demo newFragment = Demo.newInstance(bookingDetails);
newFragment.show(ft,"dialog");
Related
I have two different classes that i will show
public class Iniziale extends AppCompatActivity {
Button credits,gioca,giocamyquiz,newquestion;
TextView testo;
public boolean whatgame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iniziale);
gioca = findViewById(R.id.gioca);
giocamyquiz = findViewById(R.id.giocamyquiz);
newquestion= findViewById(R.id.newquestions);
whatgame=true;
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
gioca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
whatgame=true;
Intent homeIntent = new Intent(Iniziale.this, MainActivity.class);
startActivity(homeIntent);
finish();
}
});
giocamyquiz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
whatgame=false;
Intent homeIntent = new Intent(Iniziale.this, MainActivity.class);
startActivity(homeIntent);
finish();
}
});
}
public boolean FragmentMethod() {
return whatgame;
}
}
and a fragment
public class MainActivityFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_main_activity, container, false);
what=true; //sceglie a quale quiz giocare
what=((Iniziale) getActivity()).FragmentMethod();
I want use method FragmentMethod() in my Fragment class because I need the value of the whatgame variable.
How the code is write it doesn't work.
In Iniziale class I have 2 buttons and based of what button i click I will modify my whatgame variable and modify some parameters in fragment class
MainActivity has 2 buttons and when I click the buttons I want 2 fragments to appear in the same page.But when I click on itsMeContact button, it comes up, but when I click on the PhoneContact button, I overwrite the other one.
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button itsMeContact,phoneContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itsMeContact= (Button) findViewById(R.id.its_me_contact);
phoneContact= (Button) findViewById(R.id.phone_contact);
itsMeContact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
UserFragment userFragment = new UserFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.frame_layout, userFragment, "userFragment");
transaction.commit();
}
});
phoneContact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
FragmentB fragmentB = new FragmentB();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.frame_layout, fragmentB, "fragg");
transaction.commit();
}
});
}
}
UserFragment.java
public class UserFragment extends Fragment {
RecyclerView recyclerView ;
UserAdapter userAdapter ;
ArrayList<Person> arrayList ;
private LinearLayoutManager layoutManager;
Button itsMeContact,phoneContact;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_user,container,false);
recyclerView = view.findViewById(R.id.recycler_view);
itsMeContact= view.findViewById(R.id.its_me_contact);
layoutManager = new LinearLayoutManager(getActivity());
layoutManager.scrollToPosition(0);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
arrayList=new ArrayList<Person>();
initial();
return view;
}
private void initial() {
Factory.getInstance().user().enqueue(new Callback<List<User>>() {
#Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
// textView.setText(response.body().get(0).name);
userAdapter= new UserAdapter(response.body(), getActivity());
recyclerView.setAdapter(userAdapter);
}
#Override
public void onFailure(Call<List<User>> call, Throwable t) {
}
});
}
}
FragmentB.java
public class FragmentB extends Fragment {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_fragment_b,container,false);
textView=view.findViewById(R.id.textView);
textView.setText("naber");
return view;
}
}
Your advice important for me !
Thank you very much
My app:
enter image description here
Each Fragment transaction you did use the same framelayout : "R.id.frame_layout".
So this is normal if "it looks overhead". If you want 2 fragments displayed in a same layout, you should use 2 differents framelayout.
I have a activity with only 2 buttons and a FrameLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<Button
android:id="#+id/btPrincipal1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ver Fragment 1"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
/>
<Button
android:id="#+id/btPrincipal2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ver Fragment 2"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<FrameLayout
android:layout_width="250dp"
android:layout_height="match_parent"
android:id="#+id/contenedor"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/linearLayout">
</FrameLayout>
When I click button 1, I call the fragment and, when I click button 2, I want to call the same fragment but with different properties. For example:
I click button 1 , call fragment A with background color green.
I click button 2 , call fragment A with background color red.
It's possible? and how can I do this? Thanks.
In your MainActivity, do:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.btPrincipal1);
Button button2 = (Button) findViewById(R.id.btPrincipal2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString(COLOR_PARAM, "green");
supportFragmentManager.beginTransaction()
.replace(R.id.contenedor, YourFragment.newInstance("Button1"))
.commit();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString(COLOR_PARAM, "red");
supportFragmentManager.beginTransaction()
.replace(R.id.contenedor, YourFragment.newInstance("Button2"))
.commit();
}
});
}
Then, in YourFragment.java, do:
public class YourFragment extends Fragment {
public static final String COLOR_PARAM = "param1";
private String currentColor;
public YourFragment() {}
public static Fragment newInstance(String param) {
Fragment fragment = new Fragment();
Bundle args = new Bundle();
args.putString(COLOR_PARAM, param);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
currentColor = getArguments().getString(COLOR_PARAM);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_layout);
if (currentColor.equals("green")) {
frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_green_dark));
}
else if (currentColor.equals("red")) {
frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_red_dark));
}
return view;
}
}
If you want to change the background just create and object of your fragment, and after clicking on buttons call a method to change the fragment background. but if you wanna to change fragments layout or other things create a new fragment cause of code readability.
try this:
public class FragmentA extends Fragment {
public static FragmentA newInstance(int colorResId) {
FragmentA fragment = new FragmentA();
Bundle bundle = new Bundle();
bundle.putInt("colorId", colorResId);
fragment.setArguments(bundle);
return new FragmentA();
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
int resId = getArguments().getInt("colorId", 0);
if (resId != 0) {
//set background
}
}
public void changeBackground(int colorId){
//set background
}
}
I want to open a fragment from my activity, but when I do both the Activity layout and fragment layout is inflated, I only want the fragment layout to display. Why is this?
Activity Code:
public class Login extends Activity {
public static String res="";
private EditText login_username,login_pass;
private TextView test_login;
private SharedPreferences sp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button btn_login = (Button) findViewById(R.id.btnLogin);
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login(login_username.getText().toString(),login_pass.getText().toString());
}
});
private void login(final String user,String pass){
new Login_Server("http://192.168.1.100/test/index.php/client_users/Login",user,pass).execute();
final ProgressDialog pd=new ProgressDialog(Login.this);
pd.setMessage("Please Wait...");
pd.show();
final Timer tm=new Timer();
tm.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (res.equals("login")) {
Index index = new Index();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, index); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
tm.cancel();
res = "";
}
}
});
}
}, 1, 1500);
}
Fragment code:
public class Index extends Fragment implements View.OnClickListener {
private Button Index,Share;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.index, container, false);
Index=(Button)rootview.findViewById(R.id.index_page);
Share=(Button)rootview.findViewById(R.id.share_page);
btn_click();
return rootview;
}
public void btn_click(){
Index.setOnClickListener(this);
Share.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.share_page:
Share share_page = new Share();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, share_page); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
break;
}
}
}
xml layout fragment:
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:weightSum="100">
<include layout="#layout/main_index"></include>
<include layout="#layout/bottom_menu"></include>
</LinearLayout>
On your login activity
setContentView(R.layout.login);
inflates the activity layout
and on your fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.index, container, false);
Index=(Button)rootview.findViewById(R.id.index_page);
Share=(Button)rootview.findViewById(R.id.share_page);
btn_click();
return rootview;
}
inflates the fragment's layout
therefor you inflate 2 layouts
anyway if u wish to use the fragment layout allover the activity screen
u should change the activity layout to host a container with an ID
like in your code on the activity u have this line:
transaction.replace(android.R.id.content, index);
you should just have a view on the activity layout with the id=content and make it match_parent on height and width
this one should be the parent of all the other childs on the layout
that way the fragment will cover all of the activity layout when u replace the container
gl..
I have a MainActivity like this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} }
and this
public class MyListFragment extends ListFragment implements OnItemClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Planets, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
int count = getListView().getCount();
getListView().setSelection(getListView().getCount() - 1);
test();
}
public void test(){
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Planets, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
int count = getListView().getCount();
getListView().setSelection(getListView().getCount() - 1);
Toast.makeText(getActivity(), "ccc"+count, Toast.LENGTH_SHORT)
.show();
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT)
.show();
} }
The activity_main.xml has a
fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.mike.mqtt.MyListFragment"
android:id="#+id/fragment_content" />
When the application starts I get the fragment_content filled up with some rows.
How can I access the ListFragment(MyListFragment.java) from inside the MainActivity.java in the onCreate.
When I press a button the get the ListFragment content count, and to set focus at the end of the list.
I have no idea.Thank you.
MyListFragment fragment = (MyListFragment) getFragmentManager().findFragmentById(R.id.fragment_content)
I avoid using fragments in XML as it alows me to replace them on-the-fly. I usually use something like this:
private MyListFragment mListFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the usual business
FragmentManager fm = getSupportFragmentManager();
if (savedInstanceState == null) {
// the activity opened for the first time, we create and commit fragment
mListFragment = new MyListFragment();
fm.beginTransaction().replace(android.R.id.content, f).commit);
} else {
// activity is recreating so it's safe to find already added fragments
mListFragment = fm.findFragmentById(android.R.id.content);
}
}