Visibility in an OnClickListener - android

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)
}
});

Related

How to set OnClickListener in a View

I have a method like this, in a common class in order to create views wherever I want.
The views are being creating correctly but i need also to add a listener when that layout is clicked.
public class ArticleViews {
public LinearLayout getTinyView(final Context cont){
//Main layout
LinearLayout x = new LinearLayout(cont);
....
My attempt:
x.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArticleTools.selectedArticle = art;
Fragment art = new ArticleDetailsFragment();
FragmentManager fragmentManager = ¿?¿?¿?getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.include_main, art);
fragmentTransaction.commit();
}
});
But it is imposible to get getSupportFragmentManager(); because this class is not an Activity or a Fragment (and it does not have to be one)
So, my question is, how can I do this?
I have also wrote the code in one of those parts of the code in which I call the ArticleView class... This is a fragment, so in my opinion here the code should work.
while(it2.hasNext()){
DataSnapshot ds = it2.next();
Articulo a = ds.getValue(Articulo.class);
a.setUserId(usuerId);
ArticleViews av = new ArticleViews(a);
av.getTinyView(getContext()).
setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setClickListenerToView();
}
});
articles.add(av);
}
And articles is..
final ArrayList<ArticleViews> articles = new ArrayList<>();
But the app doesn't enter in the code
public void setClickListenerToView(){
Log.i("OnClick", "enter");
Fragment art = new ArticleDetailsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.include_main, art);
fragmentTransaction.commit();
}
Thanks for the help
You cannot get getSupportFragmentManager() because you do not have an Activity. You should delegate the code that happens in the onClick() to an Activity so that you can get what you need.
To answer everyone else posting. OP cannot get the FragmentManager like you suggested with:
MainActivity.this.getSupportFragmentManager();
Because ArticleViews is not an Activity.

Remove All Fragment in AppCompatActivity when clicking the Checkbox?

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);

how to add imagebuttons on horizontal scroll view of tabs so as to scroll on left and right by those image buttons in android studio?

Please help
Can anyone help me out plz
public class FragmentForScrollBar extends Fragment {
private HorizontalScrollView hsv;
Button left, right;
int currentScrollX = 0;
Toolbar Toolbar;
RelativeLayout RelativeLayout;
ImageButtonhome,player,download,playlist,stream,wish,hit,instrument,news,extras;
TextView t;
FragmentTransaction fragmentTransaction;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentforscrollbar,null);
MyPlayer playerFragment = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, playerFragment, null);
fragmentTransaction.commit();
hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);
t = (TextView)view.findViewById(R.id.toolbartitle);
home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
player =(ImageButton)view.findViewById(R.id.imageButtonaa);
playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
download =(ImageButton)view.findViewById(R.id.imageButtonba);
stream =(ImageButton)view.findViewById(R.id.imageButtonda);
wish =(ImageButton)view.findViewById(R.id.imageButtonea);
hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
news =(ImageButton)view.findViewById(R.id.imageButtonha);
extras =(ImageButton)view.findViewById(R.id.imageButtonia);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Home");
Fragment Home = new Home();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Home);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Player");
Fragment Myplayer = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Myplayer);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
playlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment yourPlayList = new Your_Playlist();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerView, yourPlayList);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("MyDownload");
Fragment myDownload = new Downloads();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,myDownload);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
stream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Streaming");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
wish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Your Wishes");
Fragment sendWishes = new wishes();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,sendWishes);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
hit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Hit Parade");
Fragment hitParade = new hits();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,hitParade);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
instrument.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Uzbek Instruments");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
news.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("News");
Fragment news = new news();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,news);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
extras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Extras");
Fragment extras = new extras();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, extras);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
sorry code was too long so i captured the image of the xml module file
Please see the above code and let me know the solution for scrolling the mini scroll layout on left and right by image buttons.
I am using two arrow buttons and tell me this also that what to do if i want to hide them in between the scrolling. Now can someone help me out please
For anyone who might be puzzled up with the same issue:
public class FragmentForScrollBar extends Fragment {
SeekBar verticalSeekBar=null;
private HorizontalScrollView hsv, hsv2;
Toolbar Toolbar;
ImageButton home,player,download,playlist,stream,wish,hit,instrument,news,extras, bP,bN;
ImageButton homea,playera,downloada,playlista,streama,hita,wisha,instrumenta,newsa, extrasa;
TextView t;
FragmentTransaction fragmentTransaction;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentforscrollbar,null);
MyPlayer playerFragment = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, playerFragment, null);
fragmentTransaction.commit();
hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);
t = (TextView)view.findViewById(R.id.toolbartitle);
home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
player =(ImageButton)view.findViewById(R.id.imageButtonaa);
playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
download =(ImageButton)view.findViewById(R.id.imageButtonba);
stream =(ImageButton)view.findViewById(R.id.imageButtonda);
wish =(ImageButton)view.findViewById(R.id.imageButtonea);
hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
news =(ImageButton)view.findViewById(R.id.imageButtonha);
extras =(ImageButton)view.findViewById(R.id.imageButtonia);
playera =(ImageButton)view.findViewById(R.id.imageButtonaab);
playlista =(ImageButton)view.findViewById(R.id.imageButtoncab);
downloada =(ImageButton)view.findViewById(R.id.imageButtonbab);
streama =(ImageButton)view.findViewById(R.id.imageButtondab);
wisha =(ImageButton)view.findViewById(R.id.imageButtoneab);
hita =(ImageButton)view.findViewById(R.id.imageButtonfab);
instrumenta=(ImageButton)view.findViewById(R.id.imageButtongab);
newsa =(ImageButton)view.findViewById(R.id.imageButtonhab);
extrasa =(ImageButton)view.findViewById(R.id.imageButtoniab);
verticalSeekBar=(SeekBar)view.findViewById(R.id.seekBar);
ImageButton bP = (ImageButton) view.findViewById(R.id.left);
ImageButton bN = (ImageButton) view.findViewById(R.id.right);
bP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//value 500 is arbitrarily given. if you want to achieve
//element-by-element scroll you should get the width of the
//previous element dynamically or if the elements of the
//list have uniform width just put that value instead
hsv.scrollTo(0,-500);
//if it's the first/last element you can bPrevoius.setEnabled(false)
}
});
bN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hsv.scrollTo(500,0);
}
});
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Home");
Fragment Home = new Home();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Home);
fragmentTransaction.addToBackStack(null);
// fragmentTransaction.popBackStack();
fragmentTransaction.commit();
// t.setText("Player");
}
});
player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Player");
Fragment Myplayer = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Myplayer);
fragmentTransaction.addToBackStack(null);
// fragmentTransaction.popBackStack();
fragmentTransaction.commit();
}
});
playlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DrawerNavigation mainActivity = new DrawerNavigation();
// mainActivity.drawerLayout.closeDrawers();
Fragment yourPlayList = new Your_Playlist();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerView, yourPlayList);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// t.setText("PlayList");
}
});
download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("MyDownload");
Fragment myDownload = new Downloads();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,myDownload);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
stream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Streaming");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
wish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Your Wishes");
Fragment sendWishes = new wishes();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,sendWishes);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
hit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Hit Parade");
Fragment hitParade = new hits();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,hitParade);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
instrument.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Uzbek Instruments");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
news.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("News");
Fragment news = new news();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,news);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
extras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Extras");
Fragment extras = new extras();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, extras);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
Just add image buttons on both ends and seOnclickListener() on them then scroll them up to a desired value as shown in the code.

how to show hide a fragment?

I have a Activity with four buttons at the bottom like tabs. By pressing any button a new Fragment is displayed in the FrameLayout above these buttons like we do in TabActivity. See My Problem here .Now i think i should find a way to hide and show those fragments. Kindly tell me how can i show and hide a fragment without reloading it again.
Main Purpose of showing hiding a fragment is to maintain its current state. In one of my fragment i have an AsyncTask whenever i switch between fragment it call that AsynTask again.
// to show fragment when it is hidden
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.show(fragment1)
.commit();
// to hide fragment
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.hide(fragment1)
.commit();
you can't pass by some view like
declare 4 frameLayout
private FrameLayout fragment1;
private FrameLayout fragment2;
private FrameLayout fragment3;
private FrameLayout fragment4;
and
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, Fragment1.newInstance());
ft.replace(R.id.fragment2, Fragment2.newInstance());
ft.replace(R.id.fragment3, Fragment3.newInstance());
ft.replace(R.id.fragment4, Fragment4.newInstance());
ft.commit();
and play with visible or gone ? like
fragment1.setVisibility(View.Visible);
fragment2.setVisibility(View.gone);
fragment3.setVisibility(View.gone);
fragment4.setVisibility(View.gone);
and by the way : works for me ->
public class Activity extends ActionBarActivity {
private FrameLayout fragment1;
private FrameLayout fragment2;
private Button bt1;
private Button bt2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frg_test_frg);
fragment1 = (FrameLayout) findViewById(R.id.fragment1);
fragment2 = (FrameLayout) findViewById(R.id.fragment2);
bt1 = (Button) findViewById(R.id.button);
bt2 = (Button) findViewById(R.id.button2);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fragment1.setVisibility(View.VISIBLE);
fragment2.setVisibility(View.GONE);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fragment1.setVisibility(View.GONE);
fragment2.setVisibility(View.VISIBLE);
}
});
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, new Fragment1());
ft.replace(R.id.fragment2, new Fragment2());
ft.commit();
}

Android goto another fragment from dialog fragment

I had created DialogFragment in my application. Here is the code:
public static class SkipDialogFragment extends DialogFragment implements
OnClickListener {
private Button btnYes;
private Button btnCancel;
//private PerformActionListener mListener;
private AlertDialog ad;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//mListener = (PerformActionListener) getActivity();
LayoutInflater li = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customDialogView = li.inflate(R.layout.dialog_alert_yes_no,
null);
TextView txtMessage = (TextView) customDialogView
.findViewById(R.id.txtMessage);
txtMessage.setText(getResources().getString(
R.string.disagreement_stmt));
btnYes = (Button) customDialogView.findViewById(R.id.btnOk);
btnYes.setOnClickListener(this);
btnCancel = (Button) customDialogView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(this);
ad = new AlertDialog.Builder(getActivity()).setView(
customDialogView).create();
return ad;
}
#Override
public void onClick(View v) {
if (v.equals(btnYes)) {
new CreateProfileFragment().gotoHome();
//mListener.onActionPerformed();
ad.dismiss();
}
if (v.equals(btnCancel)) {
ad.dismiss();
}
}
}
When the user clicks on Positive button in the dialog, it should navigate to another fragment. Here is the code for fragment to be displayed:
private void gotoHome() {
HomeFragment homeFragment = (HomeFragment) getFragmentManager()
.findFragmentById(R.id.home_fragment);
if (homeFragment != null) {
homeFragment.initializeView();
} else {
homeFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.replace(R.id.linFragmentContainer, homeFragment,
AppUtils.HOME_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
}
}
But, whenever I click on Yes button in the dialog, getFragmentManager() in the gotoHome() returns null. I am not getting why is this happening.

Categories

Resources