I have issue in fragment, i have to remove all added fragment when clicking the check box, but now fragments are not removed, please guide me to resolve this issue, here by i have added my code below,
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView txtVw_showfirstfragment,txtVw_showsecondfragment;
FragmentTransaction ft;
CheckBox checkBox;
fragment1 frag1 = new fragment1();
fragment2 frag2 = new fragment2();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtVw_showfirstfragment = (TextView) findViewById(R.id.txtVw_showfirstfragment);
txtVw_showsecondfragment = (TextView) findViewById(R.id.txtVw_showsecondfragment);
checkBox = (CheckBox)findViewById(R.id.checkBox);
txtVw_showfirstfragment.setOnClickListener(this);
txtVw_showsecondfragment.setOnClickListener(this);
checkBox.setText("I Agree"); // Prompting "Hide Password"
// Begin the transaction
checkBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked) {
if(frag1!=null) ft.remove(frag1);
if(frag2!=null) ft.remove(frag2);
}
}
} );
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txtVw_showfirstfragment:
String backFragment1 = getApplicationContext().getClass().getName();
ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder,frag1);
ft.addToBackStack(backFragment1);
// Complete the changes added above
ft.commit();
break;
case R.id.txtVw_showsecondfragment:
String backFragment2 = getApplicationContext().getClass().getName();
ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder,frag2);
ft.addToBackStack(backFragment2);
// Complete the changes added above
ft.commit();
break;
}
}
}
Use getSupportFragmentManager().beginTransaction().remove(frag1).commit(); instead of ft.remove(frag1);
Related
I have these TextViews on ImageViews. When you click an ImageView, the text on that particular ImageView should appear and the rest of the TextViews on the other ImageViews should disappear. How can I do this?
I know it's to do with if/else statements, but cannot seem to figure out what exactly I should use with these statements.
public class MainActivity extends AppCompatActivity {
//Declaring instance variable of FirebaseAuth as stated on Firebase Authentication file.
private FirebaseAuth mAuth;
//Declaring the Toolbar instance variable.
private Toolbar mMainAppBar;
//ViewPager on the activity_main.xml file
private ViewPager mMainActivityViewPager;
private TextView mShopTextView;
private TextView mSocialTextView;
private TextView mChatTextView;
private TextView mInventoryTextView;
private TextView mSettingsTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing mAuth and the .getInstance() method indicates that there is only a single instance variable for FirebaseAuth.
mAuth = FirebaseAuth.getInstance();
//Setting the App Bar on the MainActivity page.
mMainAppBar = findViewById(R.id.mainToolBar);
//setSupportActionBar a command given in AppCombatActivity to make the Actionbar a Toolbar.
//Toolbar is a new view that was introduced to Android to make the designing of Actionbar more flexible.
setSupportActionBar(mMainAppBar);
mShopTextView = findViewById(R.id.shopTextView);
mSocialTextView = findViewById(R.id.socialTextView);
mChatTextView = findViewById(R.id.chatTextView);
mInventoryTextView = findViewById(R.id.inventoryTextView);
mSettingsTextView = findViewById(R.id.settingsTextView);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
//Setting up the clicking of ImageViews on the Bottom Navigation bar.
//How I did it: https://teamtreehouse.com/community/starting-a-fragment-from-an-activity
ImageView shopImageView = (ImageView) findViewById(R.id.shopImageView);
shopImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShopFragment shopFragment = new ShopFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
transaction.commit();
mShopTextView.setVisibility(View.VISIBLE);
}
});
ImageView socialImageView = (ImageView) findViewById(R.id.socialImageView);
socialImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SocialFragment socialFragment = new SocialFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, socialFragment);
transaction.commit();
TextView socialTextView = (TextView) findViewById(R.id.socialTextView);
socialTextView.setVisibility(View.VISIBLE);
}
});
ImageView chatImageView = (ImageView) findViewById(R.id.chatImageView);
chatImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChatFragment chatFragment = new ChatFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, chatFragment);
transaction.commit();
TextView chatTextView = (TextView) findViewById(R.id.chatTextView);
chatTextView.setVisibility(View.VISIBLE);
}
});
ImageView inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
inventoryImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InventoryFragment inventoryFragment = new InventoryFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, inventoryFragment);
transaction.commit();
TextView inventoryTextView = (TextView) findViewById(R.id.inventoryTextView);
inventoryTextView.setVisibility(View.VISIBLE);
}
});
ImageView settingsImageView = (ImageView) findViewById(R.id.settingsImageView);
settingsImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SettingsFragment settingsFragment = new SettingsFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, settingsFragment);
transaction.commit();
TextView settingsTextView = (TextView) findViewById(R.id.inventoryTextView);
settingsTextView.setVisibility(View.VISIBLE);
}
});
}
EDIT:
Here is what the bar looks like: Screenshot.
all those icons are ImageViews and the "hello" are TextViews. When I click one ImageView, I want that particular TextView to appear and all the other TextViews to disappear.
The answer by Momen Zaqout is correct
I'll just rewrite the code in a more optimized way
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//Declaring instance variable of FirebaseAuth as stated on Firebase Authentication file.
private FirebaseAuth mAuth;
//Declaring the Toolbar instance variable.
private Toolbar mMainAppBar;
//ViewPager on the activity_main.xml file
private ViewPager mMainActivityViewPager;
private TextView mShopTextView,mSocialTextView,mChatTextView,mInventoryTextView,mSettingsTextView;
private ImageView shopImageView,socialImageView,chatImageView,inventoryImageView,settingsImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing mAuth and the .getInstance() method indicates that there is only a single instance variable for FirebaseAuth.
mAuth = FirebaseAuth.getInstance();
//Setting the App Bar on the MainActivity page.
mMainAppBar = findViewById(R.id.mainToolBar);
//setSupportActionBar a command given in AppCombatActivity to make the Actionbar a Toolbar.
//Toolbar is a new view that was introduced to Android to make the designing of Actionbar more flexible.
setSupportActionBar(mMainAppBar);
mShopTextView = findViewById(R.id.shopTextView);
mSocialTextView = findViewById(R.id.socialTextView);
mChatTextView = findViewById(R.id.chatTextView);
mInventoryTextView = findViewById(R.id.inventoryTextView);
mSettingsTextView = findViewById(R.id.settingsTextView);
shopImageView = (ImageView) findViewById(R.id.shopImageView);
socialImageView = (ImageView) findViewById(R.id.socialImageView);
chatImageView = (ImageView) findViewById(R.id.chatImageView);
inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
settingsImageView = (ImageView) findViewById(R.id.settingsImageView);
shopImageView.setOnClickListener(this);
socialImageView.setOnClickListener(this);
chatImageView.setOnClickListener(this);
inventoryImageView.setOnClickListener(this);
settingsImageView.setOnClickListener(this);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.shopImageView:
ShopFragment shopFragment = new ShopFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
transaction.commit();
mShopTextView.setVisibility(View.VISIBLE);
break();
case R.id.socialImageView:
SocialFragment socialFragment = new SocialFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, socialFragment);
transaction.commit();
mSocialTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
break();
case R.id.chatImageView:
ChatFragment chatFragment = new ChatFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, chatFragment);
transaction.commit();
mChatTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
break();
case R.id.inventoryImageView:
InventoryFragment inventoryFragment = new InventoryFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, inventoryFragment);
transaction.commit();
mInventoryTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
break();
case R.id.settingsImageView:
SettingsFragment settingsFragment = new SettingsFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, settingsFragment);
transaction.commit();
mSettingsTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
break();
}
}
you can use this method to disappear all textviews and then appear the desired textview.
void changeTexViews(View desired ){
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
desired .setVisibility(View.VISIBLE);
}
and in each ImageView.setOnClickListener use this method,
for example
shopImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShopFragment shopFragment = new ShopFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
transaction.commit();
changeTexViews(mShopTextView);
}
});
**Here I want to provide some help tips to **
if you want hide or show any element first thing is if you want show all element visible at first time you can set by default visibility to View.VISIBLE else you can use View.Gone.
and after that in which textview or image view click you want show or hide any element you can perform inside the onClickListener() like below example.
ImageView inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
inventoryImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
demotext.setVisibility(View.VISIBLE);
demoImage.setVisibility(View.Gone)
}
});
I need to implement the UI of my app, like the Instagram one. I need to switch from different fragments, with the usage of the bottom navigation view, but I need to keep state of the fragments, like I left them. How Can I achieve this?
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.action_formation:
if (homeFragment == null) {
homeFragment = new HomeFragment();
}
displayFragment(homeFragment);
break;
case R.id.action_result:
if (introResultFragment == null) {
introResultFragment = new IntroResultFragment();
}
displayFragment(introResultFragment);
break;
case R.id.action_market:
displayFragment(new MarketFragment());
break;
}
return false;
}
public void displayFragment(final Fragment fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment, fragment.getClass().toString());
fragmentTransaction.commit();
}
It's been a long time but I want to offer my open source library in github which implements the same UX of Youtube and Instagram:
https://github.com/ZachBublil/ZNavigator
You got to add this dependency to your gradle:
compile 'com.zach.znavigator:znavigator:1.0.0'
The only thing you have to do is to pass the list of fragments you want to be in the BottomNavigationView:
public class SampleActivity extends NavigationActivity {
private BottomNavigationView navigationView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationView = (BottomNavigationView) findViewById(R.id.navigationView);
LinkedHashMap<Integer, Fragment> rootFragments = new LinkedHashMap<>();
rootFragments.put(R.id.tab1, new FirstTab());
rootFragments.put(R.id.tab2,new SecondTab());
rootFragments.put(R.id.tab3, new ThirdTab());
init(rootFragments, R.id.container);
navigationView.setOnNavigationItemSelectedListener(this);
navigationView.setOnNavigationItemReselectedListener(this);
}
#Override
public void tabChanged(int id) {
navigationView.getMenu().findItem(id).setChecked(true);
}
}
If you want to open a new fragment as inner screen in one of the tabs you can do it by using ZNavigation class in the tab fragment:
ZNavigation.openChildFragment(getFragmentManager(),new ChildFragment());
just remember the active fragment, and use userVisiableHint to get active status in each fragment.
private Fragment currentFragment; // need to be init
private void switch2Fragment(Fragment target){
getFragmentManager().executePendingTransactions();
if(target.isAdded){
getFragmentManager().beginTransaction().hide(currentFragment).show(target).commit();
} else {
getFragmentManager().beginTransaction().hide(currentFragment).add(R.id.xxx, target).commit();
}
currentFragment.setUserVisibleHint(false);
currentFragment = target;
target.setUserVisibleHint(true);
}
private boolean isFragmentActive(Fragment target){
return target.getUserVisibleHint();
}
My MainActivity contain buttons which is used to switch to different fragments. Inside each fragment there are also has buttons. Activity button onClickListener work fine before implementing button onClickListener in fragments. I wonder if this is not supported by Android or my code is not correct.
Any pointer is appreciated. Thank you.
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = inflater.inflate(fragment1, container, false);
b2=(Button)v.findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
button2Clicked(v);
}
});
return inflater.inflate(R.layout.fragment1, container, false);
}
public void button2Clicked(View view){
//do something
}
Activity code:
public class MainActivity extends FragmentActivity {
private Button btnHealth;
private Button btnSetting;
private Button btnSleep;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Init component
btnHealth = (Button) findViewById(R.id.btnHealth);
btnSleep = (Button) findViewById(R.id.btnSleep);
btnSetting = (Button) findViewById(R.id.btnSetting);
replaceFragment(1);
btnHealth.setOnClickListener(new AdapterView.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
replaceFragment(0);
}
});
btnSleep.setOnClickListener(new AdapterView.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
replaceFragment(1);
}
});
btnSetting.setOnClickListener(new AdapterView.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
replaceFragment(2);
}
});
}
//Create method replace fragment
private void replaceFragment(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
default:
fragment = new Fragment2();
break;
}
if(null!=fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
It is possible. If you want to handle the OnClick Event of the fragment inside the Activity class, you can do so by using FragmentInteractionListener (interface). Example: Android - handle fragment onClick from activity
My problem is that I want to remove a Fragment from an Activity. There are two steps:
Adding the Fragment into a FrameLayout (R.id.frame_for_des_details) by checking the CheckBox.
Removing the Fragment by clicking a TextView
public class CreateTripActivity extends FragmentActivity {
//Vars for Destination Fragment
private CheckBox checkBox;
private DestinationDetailsFragment destinationDetailsFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Setting the ContentView
setContentView(R.layout.activity_create_trip);
//Set the Destination Checkbox listener
checkBox = (CheckBox) this.findViewById(R.id.checkBox_destination_now);
Listener for the CheckBox:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//Adding the Fragment. THIS WORK
destinationDetailsFragment = new DestinationDetailsFragment();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.frame_for_des_details, destinationDetailsFragment).commit();
}
});
Listener for the TextView:
findViewById(R.id.label_later).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Removing the Fragment. DOES NOT WORK
FragmentManager fm = getFragmentManager();
fm.beginTransaction().remove(destinationDetailsFragment).commit();
}
});
}
When I check the CheckBox, I can see the Fragment. But when I click the TextView to remove it, it remains.
When I check the CheckBox again and again it seems that the new Fragment overlays the older fragment so I can write over placeholders in an EditText.
I hope someone understand me and can help me.
Thanks.
Try The android.support.v4.app.FragmentManager. Here is the code.
destinationDetailsFragment = new DestinationDetailsFragment();
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean b) {
// Adding the Fragment. THIS WORK
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.frame_for_des_details,
destinationDetailsFragment).commit();
}
});
findViewById(R.id.label_later).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.remove(destinationDetailsFragment).commit();
}
});
FIXED
I implement a the Fragment as android.support.v4.app.Fragment but that wasnt the problem.
Before I add the fragment i removed the checkbox. And bevor removing the Fragment i add the checkbox again to the View. But i also called checkbox.setChecked(false). That fired the onCheckedChanged again!
My new Code
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_destination);
//Remove and Add Content
layout.removeView(checkBox);
destinationDetailsFragment = new DestinationDetailsFragment();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.frame_for_des_details, destinationDetailsFragment).commit();
//Enable Later Button
findViewById(R.id.img_later).setVisibility(View.VISIBLE);
findViewById(R.id.label_later).setVisibility(View.VISIBLE);
}
}
Thanks.
Adding fragment works ok, but removing it crashes app. I thought removing will be straight forward but Im still a beginner so I must missing something. Can you take a look?
public class MainActivity extends Activity implements OnClickListener {
Button addFragment, closeFragment;
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction transaction = fragmentManager
.beginTransaction();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
addFragment = (Button) findViewById(R.id.bAddF);
closeFragment = (Button) findViewById(R.id.bCloseF);
addFragment
.setOnClickListener((android.view.View.OnClickListener) this);
closeFragment
.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Instantiate fragment from ExampleFragment class
ExampleFragment fragment = new ExampleFragment();
switch(arg0.getId()){
case R.id.bAddF:
transaction.add(R.id.fragment_holder, fragment).commit();
break;
case R.id.bCloseF:
transaction.remove(fragment).commit();
break;
}
}
}
Your app crashes because you are saving the FragmentTransaction as a member variable. But FragmentTransactions cannot be reused. Furthermore, you are creating a new Fragment everytime, which means that the currently added Fragment will not be removed when clicking the remove Button.
Do it this way:
private ExampleFragment mFragment;
#Override
public void onClick(View v) {
//Instantiate fragment from ExampleFragment class
if(mFragment == null) mFragment = new ExampleFragment();
switch(v.getId()){
case R.id.bAddF:
getFragmentManager().beginTransaction().add(R.id.fragment_holder, mFragment, "tag").commit();
break;
case R.id.bCloseF:
getFragmentManager().beginTransaction().remove(mFragment).commit();
break;
}
}
Create a new FragmentTransaction for each add() or remove() procedure.
As an alternative for keeping your ExampleFragment as a member variable, you can use FragmentManager.findFragmentByTag("tag") to get the currently added fragment.
Alternative way:
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bAddF:
getFragmentManager().beginTransaction().add(R.id.fragment_holder, new ExampleFragment(), "tag").commit();
break;
case R.id.bCloseF:
Fragment frag = getFragmentManager().findFragmentByTag("tag");
getFragmentManager().beginTransaction().remove(frag).commit();
break;
}
}