Dynamically adding fragments below an existing fragment in a LinearLayout - android

I am making an activity which has a section for comments based on addresses inputed by the user.
The user will add a new comments section by pressing a button. This is a non fixed number of sections, so I have initially added this section as a fragment in the xml.
I have an onClick function that adds another fragment to the linearlayout.
My problem is that the new fragments always add to the top of the linearlayout , i.e. above the existing fragment/fragments (i.e. the new fragment will push all the other fragments down).
How can I add the new fragment so that it displays below the existing fragment?
Code in .java file:
public class SpecialReportAdden extends ActionBarActivity {
int numOfFragments;
LinearLayout addenHolder;
TextView report;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_special_report_adden);
numOfFragments=1;
Button addenSave = (Button)findViewById(R.id.btnAddendumSave);
Button addenAddComment = (Button)findViewById(R.id.btnAddendumAddComment);
addenHolder =(LinearLayout)findViewById(R.id.AddenLinLayHolder);
addenSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this should save all comments including those added in dynamically produced fragments
//save database
Intent i = new Intent (SpecialReportAdden.this, MenuPage.class);
startActivity(i);
}
});
addenAddComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("number of Fragments at start of OnClick", Integer.toString(numOfFragments));
Fragment newAddenFrag = addNewfragment(numOfFragments);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.AddenLinLayInnerHolder, newAddenFrag);
ft.addToBackStack(null);
ft.commit();
numOfFragments++;
Log.i("number of Fragments updated", Integer.toString(numOfFragments));
}
});
}
public Fragment addNewfragment(int number) {
AddendumLinInputFragment adden = new AddendumLinInputFragment();
TextView tx = (TextView) findViewById(R.id.txtVAddendumFragReportNum);
tx.setText("Report "+number+" :");
tx.setTextColor(Color.BLACK);
TextView address = (TextView)findViewById(R.id.txtVAddendumFragAddress);
address.setText("A new address");
address.setTextColor(Color.BLUE);
return adden;
}
xml file for the .java activity
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.danielt.pestcontrol.SpecialReportAdden"
android:id="#+id/RellayAddenComments">
<TextView
android:id="#+id/txtVAddendumTitle" android:text="Service Report Addendum" android:textStyle="bold" android:layout_centerHorizontal="true" android:elegantTextHeight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView
android:id="#+id/txtVAddendumTechName"
android:layout_below="#+id/txtVAddendumTitle"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Technician Name: "
android:textSize="18sp"
/>
<TextView
android:id="#+id/txtVAddendumDate"
android:layout_below="#+id/txtVAddendumTechName"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date: "
android:textSize="18sp"
/>
<ScrollView
android:id="#+id/scrlVAddendum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtVAddendumDate">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/AddenLinLayHolder"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/AddenLinLayInnerHolder"
android:orientation="vertical">
<fragment
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.danielt.pestcontrol.AddendumLinInputFragment"
android:id="#+id/addendum1CommentsFragment"
android:layout_below="#+id/txtVAddendum1Date"
android:layout_marginTop="24dp"
tools:layout="#layout/fragment_addendum_lin_input" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add another Addendum Comment"
android:id="#+id/btnAddendumAddComment"
android:layout_below="#+id/scrlVAddendum"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save / Update Comments"
android:id="#+id/btnAddendumSave"
android:layout_below="#+id/btnAddendumAddComment"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</LinearLayout>
</ScrollView>
I havent added the fragment code but I will if someone wants to see it.
Thanks for any help.

You don't need fragment to hold only comment view. Just inflate new comment view and add it to the parent view.
LayoutInflater inflater = LayoutInflater.from(context);
View inflatedLayout= inflater.inflate(R.layout.yourLayout, container, false);
container.addView(inflatedLayout);
Where container is a view that hold all comments.
Regarding adding fragments, according to Fragments documentation:
If you're adding multiple fragments to the same container, then the
order in which you add them determines the order they appear in the
view hierarchy
Therefore if it's behave different, it could be related to OS version or bug :)

Related

add fragment to fragment working well,but finish() method to remove second fragment not working well

I am adding a fragment into a RelativeLayout which is a small part of other fragment(containing viewpager) which is adding well, but when i am trying to vanish the fragment calling finish() its not working.
xml code:
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Share you thoughts here..."
android:paddingLeft="10dp"
android:background="#drawable/edittext_status_background"
android:padding="20dp"
android:elevation="5dp"
android:id="#+id/editTextWritePostId"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fitsSystemWindows="true"
android:background="#f9f9f9"
android:id="#+id/relativeLayoutIdNewsFeedForPostBar"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post"
android:textColor="#ffffff"
android:background="#1470a6"
android:id="#+id/postButtonId"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkInButtonId"
android:layout_toLeftOf="#+id/smileyButtonId"
android:layout_centerInParent="true"
android:src="#drawable/ic_checkin"
android:background="#android:color/transparent"
android:layout_marginRight="3dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/smiley"
android:layout_toLeftOf="#+id/imageUploadButtonId"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:id="#+id/smileyButtonId"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/camera"
android:layout_toLeftOf="#+id/videoUploadButtonId"
android:layout_centerInParent="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:id="#+id/imageUploadButtonId"
/>
</RelativeLayout>
**//adding new fragment to below layout**
**<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayoutIdForSmiley"
>
</RelativeLayout>**
</LinearLayout>
Fragment code to remove new fragment:
smileyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SmileyShowFragment smileyFragment=new SmileyShowFragment();
FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
if(!(smileyFragment.isVisible())) {
fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit();
}else {
relativeLayoutIdForSmiley.setVisibility(View.GONE);
}
}
});
I am trying to vanish the RelativeLayout with visibility gone and also vanish fragment with getActivity().finish() but none of these working.can you help me please. I have searched internet a lot which are same as i am doing.
I guess I have figured out your issue. Here is what you are doing the mistake. In your smileButton onclick you are creating a new instance of SmileyShowFragment(SmileyShowFragment smileyFragment=new SmileyShowFragment();) and checking weather the fragment is visible using if(!smileyFragment.isVisible()). No matter how many time you trigger your click, this condition will be true always, as the fragment instance is created every time you the click the button. The solution is, create the instance of the fragment outside of the button click. Try some thing like this.
final SmileyShowFragment smileyFragment = new SmileyShowFragment();
smileyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.relativeLayoutIdForSmiley);
if(!(fragment instanceof SmileyShowFragment)) {
fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit();
}else {
fragmentTransaction.remove(smileyFragment).commit();
}
}
});
Have you tryed to use detach or remove on your fragment ?
Go check : https://developer.android.com/reference/android/app/FragmentTransaction.html

ViewPager using Views instead of Fragments

I've a ViewPager which now uses Views instead of Fragments to display each tab.
Every single tab, inflates the same layout file.
Overview
In this ViewPager, I'm supposed to add Mines as Tabs, so basically every tab would correspond to a specific mine (the ones with minerals, not the bomb).
Some mines are already unlocked, but some other need to be purchased first, so I added a button to do just this.
Problem
Fact is, using fragments, it all worked fine, but now I swapped them with Views, I can buy the mine, but then, if I buy another one, the one I previously purchased, gets locked back as I never did buy it.
CODE
I'm really confused now, I don't know where the problem is, but I can surely give you the most relevant parts of the code:
MinerAdapter
public class MineAdapter extends PagerAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
public MineAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
System.out.println("Code executed");
final View itemView = mLayoutInflater.inflate(R.layout.carousal_page, container,
false);
switch (position) {
case 0:
itemView.setBackgroundResource(R.color.iron);
break;
case 1:
itemView.setBackgroundResource(R.color.coal);
break;
case 2:
itemView.setBackgroundResource(R.color.gold);
break;
}
[Some setup skipped]
// Unlock Button
itemView.findViewById(R.id.unlockButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (CenterRepository.getSingletonInstance().getCurrentUser().getGold() >=
CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost()) {
//If User has more gold than cost to unlock remove lock image and buy it
CenterRepository.getSingletonInstance().getCurrentUser().setGold(
CenterRepository.getSingletonInstance().getCurrentUser().getGold()
- CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost()); // Update user's gold
itemView.findViewById(R.id.unlockButton).setVisibility(View.GONE); // Remove lock button
Toast.makeText(mContext,
"Reduced " + CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost() +
"\n Updated Gold " + CenterRepository.getSingletonInstance()
.getCurrentUser().getGold(), Toast.LENGTH_LONG).show();
} else {
// Not enough money
Toast.makeText(mContext, "Not enough money to purchase You need " +
(CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost()
- CenterRepository.getSingletonInstance().getCurrentUser().getGold()) + "More", Toast.LENGTH_SHORT).show();
}
}
});
container.addView(itemView);
return itemView;
}
}
XML Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/mineName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="COAL MINE"
android:textColor="#android:color/white"
android:textSize="25sp" />
<TextView
android:id="#+id/mineCost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:gravity="center"
android:text="1000"
android:textColor="#android:color/white"
android:textSize="50sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingEnd="100dp"
android:paddingStart="100dp">
<TextView
android:id="#+id/mineMineral"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/mineDropRate"
android:text="COAL"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="25sp" />
<TextView
android:id="#+id/mineDropRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="1"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="25sp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/unlockButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Unlock" />
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add Test User from Activity
CenterRepository.getSingletonInstance().setCurrentUser(new User("FET", 2000, 20, 10));
//Add Test Mines
CenterRepository.getSingletonInstance().getListOfLavels().clear();
CenterRepository.getSingletonInstance().addMine(new Mine("Iron", new Mineral("Iron Mineral", 1), 100, 2));
CenterRepository.getSingletonInstance().addMine(new Mine("Coal", new Mineral("Coal Mineral", 3), 200, 2));
CenterRepository.getSingletonInstance().addMine(new Mine("Gold", new Mineral("Gold Mineral", 2), 300, 2));
viewPager = (ViewPager) findViewById(R.id.vpPager);
viewPager.setAdapter(new MineAdapter(this));
}
}
Extra
If you prefer working with GitHub, here's the project with the full code with no skipped code.
ViewPager2 supports using a RecyclerView Adapter for the adapter.
Hence you do not need to use a fragment anymore.
You can use a View just like in recyclerView and it would preserve it's snap behavior as well

Populate fragment with Bundle arguments

I have a listFragment to display a list,
when we click on the list I want to display a Fragment with the details of the clicked element. I'm doing this with dynamic fragment.
My Problem is about the displaying of my detailsFragment. (cf picture).
I'm supposed to have the "designation" and other information .
I only have a part of my DetailsFragment XML.
However I checked the bundle with the debug mode and the communication is OK.
So Why is there a problem for the displaying of this fragment?
Principal Host Activity
public void onListItemClick(int id) {
F_Outils_details detail_frag = (F_Outils_details)
getFragmentManager().findFragmentById(R.id.listFragment2);
if (detail_frag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
detail_frag.setDetails((int)id);
}
else {
// If the frag is not available, we're in the one-pane layout and must swap frags...
F_Outils_details detail_frag_new = new F_Outils_details();
Bundle args = new Bundle();
args.putInt("outil",(int) id);
detail_frag_new.setArguments(args);
FragmentTransaction transaction =getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container1, detail_frag_new);
transaction.addToBackStack(null);
transaction.commit();
}
DetailFragmentActivity
public class F_Outils_details extends Fragment {
ArrayList detail_outils=new ArrayList();
Outil outil;
TextView id_outil;
TextView designation;
TextView date_location;
View a;
ArrayList liste_outils;
int mCurrentPosition=-1;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.f_outils_details, container, false);
}
#Override
public void onStart() {
super.onStart();
// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already been
// applied to the fragment at this point so we can safely call the method
// below that sets the article text.
Bundle args = getArguments();
//si il y a pas d'arguments
if (args != null) {
setDetails(args.getInt("outil"));
} else if (mCurrentPosition != -1) {
System.out.println("mccurent posit");
// Set article based on saved instance state defined during onCreateView
setDetails(mCurrentPosition);
}
}
//methode qui va changer le texte !
public void setDetails(int id){
if(id==-1){
designation.setText("");
date_location.setText("");
}
else{
//on recupere l'outil
DB_Locoutils dbLocoutils=DB_Locoutils.getInst();
liste_outils=dbLocoutils.getOutils();
outil =(Outil) liste_outils.get((int) id);
System.out.println("**************"+outil.getDesignation());
//id_outil=(TextView) getView().findViewById(R.id.id_outil);
designation=(TextView) getActivity().findViewById(R.id.designation);
date_location=(TextView) getActivity().findViewById(R.id.date_location);
if(designation!=null){
System.out.println("DESIGNATION OK");
}
//id_outil.setText(outil.getIdOutil());
designation.setText(outil.getDesignation());
date_location.setText(outil.getDateLocation());
}
}
}
XML detailFragment
<?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" >
<TextView
android:id="#+id/titre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="20dp"
android:text="Details" />
<TextView
android:id="#+id/id_outil"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="id outil"
/>
<TextView
android:id="#+id/designation"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="designation"
/>
<TextView
android:id="#+id/refClient"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="ref Client"
/>
<TextView
android:id="#+id/date_location"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="date location"
/>
I think the problem is that you have all your TextViews with the height attributes to match_parent. This indicates that your first item take all the parent height and the others stay behind. Try to use this layout instead:
<?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" >
<TextView
android:id="#+id/titre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="20dp"
android:text="Details" />
<!-- as the item above, set the height to wrap_content -->
<TextView
android:id="#+id/id_outil"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="id outil"
/>
<TextView
android:id="#+id/designation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="designation"
/>
<TextView
android:id="#+id/refClient"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="ref Client"
/>
<TextView
android:id="#+id/date_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="date location"
/>
</LinearLayout>
Hope this helps and you will have the expected result.

Fragments added to linearLayout are stacking, Causing only final one added is visible

I have a view that contains a YouTubePlayerSupportFragment, with a small box around it for styling. I want to add multiple instances of this view on top of each other (vertically) to another view. I have it working for adding a single view, but when I try to add multiple they just stack on each other, so that only the one that was added last is visible. I was able to prove this was happening, by programatically making the first view added longer and I could see the extra height of it beneath the topmost view.
Here is my code:
fragment_youtube_video:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:layout_marginTop="5dp"
android:background="#FFFFFFFF">
<fragment
android:id="#+id/youTubeVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:textColor="#000000"
android:text="Video" />
</LinearLayout>
fragment_videos:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
<ImageView
android:id="#+id/topBar"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="45dp" />
<ImageView
android:id="#+id/logoImageView"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/CD_logo"
android:src="#drawable/logo" />
<RelativeLayout
android:id="#+id/pagerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/logoImageView"
android:background="#80000000"
android:paddingBottom="10dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="10dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_marginRight="12dp" >
[REMOVED FOR SPACE]
</RelativeLayout>
<ScrollView
android:id="#+id/scrollableVideoBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pagerTitle"
android:layout_alignRight="#+id/pagerTitle"
android:layout_below="#+id/pagerTitle">
<LinearLayout
android:id="#+id/videosLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#a0000000">
</LinearLayout>
</ScrollView>
</RelativeLayout>
VideosFragment.java:
private void addVideoViews() {
int count = 0;
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (String vidURL : videoURLs) {
new YoutubeFragment();
YoutubeFragment player = YoutubeFragment.newInstance(count, vidURL);
fragmentTransaction.add(R.id.videosLayout, player, "x_" + count);
count++;
}
fragmentTransaction.commit();
}
I could provide more code, but I believe these are the necessary pieces. Please also feel free to let me know if I am going about this incorrectly. I am so close, I just can't figure out for the life of me why they are stacking.
Any help would be appreciated!
We went into Chat, and this is what we came to:
LinearLayout parentLayout = (LinearLayout) view.findViewById(R.id.videosLayout);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
LayoutInflater inflater = LayoutInflater.from(getActivity());
for (String vidURL : videoURLs) {
final String vidUrl2 = vidURL;
LinearLayout placeholder = (LinearLayout) inflater.inflate(R.layout.fragment_youtube_video, container, false);
View youtuber = placeholder.findViewById(R.id.youTubeVideo);
YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
fragmentTransaction.add(youtuber.getId(), player);
placeholder.setId(12345);
parentLayout.addView(placeholder);
player.initialize("secret", new OnInitializedListener() {
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
if (!arg2) {
arg1.cueVideo(vidUrl2);
}
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
}
});
}
fragmentTransaction.commit();
Placeholder is a LinearLayout with two children, a LinearLayout to hold the youtube fragment, and a TextView to hold the title.
I'm assuming, that when you add multiple videos, you're just swapping them within your fragment. There aren't multiple fragments, so only one video would ever show at any given time.
I think you should use a ListView instead, that way, you can populate it with numerous videos that all have the same general layout resource. Hopefully this helps.

Code for tap on text view to display text

i am developing an android app,this is my first app development,
As per my requirement , i need code to implement UI as below in the image.
Hopping the image is explaining my requirement. the below image is the App UI view. Thanks..
I have used the below code , but it is not working. can anyone finds the fault..
- DrinkActivity.java
TextView t ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
- activity_drink.xml
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<ListView
android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ListView>
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<TextView android:text="This is my first text"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28sp"
android:editable="true"
android:clickable="true"
android:layout_height="wrap_content"
android:onClick="onClick">
</TextView>
You could achive this behavior by designing 2 fragments:
1. The first one will be a simple TextView fragment that will display your tab text only.
2. The second fragment will be as a LinaerLayout vertical layout that will contain 2 TextViews one after another, the first will be the same TextView as in the first fragment while the second TextView will contain the description of your tab.
then all what you should do is on the click of the first fragment replace it with the second one using this code:
if (!isTabOpen)
{
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(TabFragment)
.add(R.id.containerForFragments,TabAndDescriptionFragment)
.commit();
}
else
{
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(TabAndDescriptionFragment)
.add(R.id.containerForFragments,TabFragment)
.commit();
}
after you have created the plusButtonFragment and the userNewFragment.

Categories

Resources