Android - In Fragment Text look a lot smaller. Tiny tiny ... tiny - android

No really, I haven't done anything specific.
I was following this tuto : http://developer.android.com/training/basics/fragments/fragment-ui.html
I'm just loading my fragment with replace from FragmentManager.
I tried to change textSize in Fragment's Layout.
I tried on other devices, other rom...
I tried lauching him in a FrameLayout or LinearLayout...
There is nothing to do :>
I'm sure you have used Fragment. And I'm sure it happened to you ! Tell me. Tell me what I need to know :)
As Random Data, here is :
My fragment's layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="3dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="7dp"
android:layout_weight="1">
<TextView
android:id="#+id/BillId"
android:text="BillId : "
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/BuyerLastName"
android:text="BuyerLastName : "
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/BuyerFirstName"
android:text="BuyerFirstName : "
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/CarId"
android:text="CarId : "
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="7dp"
android:layout_weight="1">
<TextView
android:id="#+id/BillIdValue"
android:text="BillId"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/BuyerLastNameValue"
android:text="BuyerLastName"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/BuyerFirstNameValue"
android:text="BuyerFirstName"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/CarIdValue"
android:text="CarId"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
And here is my fragment Java Code :
public class ShowCarFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_show_car, container, false);
}
}
And here I launch the fragment in my Activity :
if (getMaxPayne() >= 2) {
getFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.fragmentContainer, new ShowCarFragment())
.commit();
} else {
getFragmentManager().beginTransaction()
.addToBackStack(null)
.add(R.id.fragmentContainer, new ShowCarFragment())
.commit();
}
Thank you dear Fragment Master. May Mickey's Force be with you :)

Related

ListFragment onCreateView not called

I'm trying to use a FragmentManager to replace a fragment but for some reason the OnCreateView() isn't called.
This is the code I use to create the fragment:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
This is the code for FeedbackListFragment:
public class FeedbackListFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG,"create feedbacklistview");
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
}
This is my main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_normal"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/overview_scan_text"
android:id="#+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="#+id/cleanjack_image"
android:src="#drawable/logo_clean_jack"
android:layout_gravity="left|center_vertical"
android:contentDescription="#string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
I've removed the android:visibility="gone" to see if that was the issue but it didn't work.
I tried your Sample code
Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
}
FragmentList :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
XML Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bcbcbc"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="overview_scan_text"
android:id="#+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="#+id/cleanjack_image"
android:src="#drawable/ic_launcher_background"
android:layout_gravity="left|center_vertical"
android:contentDescription="#string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
Xml Fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</android.support.constraint.ConstraintLayout>
Have you set the id of listView of fragmentList to
android:id="#android:id/list"
Hope this is helpful :)
From documentation for onCreateView in ListFragment:
Provide default implementation to return a simple list view. Subclasses
can override to replace with their own layout. If doing so, the
returned view hierarchy must have a ListView whose id
is {#link android.R.id#list android.R.id.list} and can optionally
have a sibling view id {#link android.R.id#empty android.R.id.empty}
that is to be shown when the list is empty. If you are overriding this method with your own custom content,
consider including the standard layout {#link android.R.layout#list_content}
in your layout file, so that you continue to retain all of the standard
behavior of ListFragment. In particular, this is currently the only
way to have the built-in indeterminant progress state be shown.

Why isn't the view found for the id?

I'm trying to launch a fragment from an activity. However, when I run the app and click on the button that has to launch the fragment I get the error:
java.lang.IllegalArgumentException: No view found for id 0x7f0e0074 (com.example.hudhud.islam:id/kontaktfragment) for fragment Kontakt{aaaed67 #1 id=0x7f0e0074}
I can't see where I've made something wrong. It should be correct. This is the class where I implement the fragment. I'm only gonna upload the View onCreateView for this class as no more is needed:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_kontakt, container, false);
sendmail = (Button) view.findViewById(R.id.sendknap);
sendmail.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
msg = (EditText) view.findViewById(R.id.besked);
String message = msg.getText().toString();
sendemail(message);
}
});
return view;
}
I launch the fragment from here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.Kontakt) {
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.add(R.id.kontaktfragment, fragment)
.commit();
}
return super.onOptionsItemSelected(item);
}
What have I missed?
Any help is appreciated!
EDIT:
This is the XML for the activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32c6a6"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/velkomst"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
<FrameLayout
android:id="#+id/Buttons"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#id/velkomst" >
</FrameLayout>
</RelativeLayout>
And this is the kontakfrag XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/kontaktfragment"></FrameLayout>
</RelativeLayout>
And this is the fragment_kontakt XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32c6a6"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/besked"
android:layout_weight="0.70"
android:textSize="20dp"
android:layout_marginTop="30dp"
android:textIsSelectable="true"
android:textColor="#000000"
android:background="#ffffff"
android:gravity="top"
android:paddingTop="30dp"
android:hint="Hvis du har feedback eller nogle spørgsmål, så er du velkommen til at skrive. Vi besvarer mailen hurtigst muligt"
android:scrollIndicators="right"/>
<Button
android:layout_width="144dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendknap"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05"
android:textSize="20dp"/>
</LinearLayout>
My mistake was that I created the fragment as a separate layout and not as a framelayout on the frontpage. So the solution was to create a framelayout on the frontpage. That makes sense :)
First to the layout of activity, add this
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then in that activity java code add
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();

Replacing fragments on click of button android

I am trying to create a swappable fragements on my android app
Only half the part of fragment seems to be replacing
screenshot:
The xml file for the main is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity"
android:id="#+id/contentlayout">
<fragment
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/header_menufragment"
class="com.example.www.newapp.header_menu" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/dynamicfragment"
class="com.example.www.newapp.mainfragment"/>
</LinearLayout>
the whole dynamicfragment is supposed to be replaced by projectfragment on click of Project button.
project.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.replace(R.id.dynamicfragment, new projectfragment())
.addToBackStack(null)
.commit();
}
});
the projectfragment class has
public class projectfragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_project, container,false);
}
}
the layout file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="#color/colorDarkRed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight=".5">
<ImageButton
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<ImageButton
android:id="#+id/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/imgfile"/>
</LinearLayout>
Kindly help me solve this issue.
You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction.
Use FrameLayout as container and replace Fragment inside that.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout android:id="#+id/fragment_container" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
On Activity add Fragment same as you do:
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new projectfragment())
.addToBackStack(null)
.commit();
For more info refer below link:
link1
link2
that worked for me try this way
WishaFriend ff = new WishaFriend();
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);
ft.commit();
dialog.dismiss();
try add this
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);

Fragment Transaction

I have a ListFragment with a floating action button. When the button is clicked, i want to replace the ListFragment with a CreateListFragment. Instead, the CreateListFragment is placed on top of the ListFragment and the button and textview from the ListFragment are still visible. I am not sure what went wrong. How do I completely replace the ListFragment with the CreateListFragment?
Here is the screenshot before the button is clicked
http://i.stack.imgur.com/k2HxP.png
Here is a screenshot after the button is clicked
http://i.stack.imgur.com/TYN47.png
list_fragment
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listFrame">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fab"
android:layout_gravity="bottom|end"
app:backgroundTint="#color/colorAccent"
android:src="#drawable/create"/>
<TextView
android:id="#+id/tvEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="No Shopping Lists Yet"
android:layout_gravity="center"
android:textSize="20sp"/>
</FrameLayout>``
createlist_fragment
<FrameLayout 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:padding="10dp"
tools:context="com.lavineoluoch.helloworld.swiftpay.app.CreateListFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/etListTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/title"/>
<EditText
android:id="#+id/etAddItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/items"/>
<RelativeLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnEditItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnEdit"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
</RelativeLayout>
</LinearLayout>
ListFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.list_fragment, container, false);
fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CreateListFragment createListFragment = new CreateListFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.listFrame,createListFragment);
fragmentTransaction.commit();
}
});
return view;
}
In your root view for your Fragments insert this line of code:
android:background="?android:windowBackground"
As you have a see through background your view can still see the reminiscence of the replaced Fragment as the view hasn't yet updated.
The above answer should solve your problem, however I would recommend re-structuring your approach and instead launching a new Activity which contains the CreateListFragment when the FAB is pressed. Doing so would result in easier to maintain code which is also easier to read and would be best practice. It would also mean that the user can press their back button and navigate back to ListActivity/ListFragment without you needing to manually handle Fragment back stacks.

Layout below Fragment not visible

I have the follwing layout:
<LinearLayout 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:orientation="vertical" >
<View
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
Programatically I'm replacing the view "content" with my Fragment:
View view = inflater.inflate(R.layout.kwiz_game, container, false);
contentView = view.findViewById(R.id.content);
currentFragment = new KwizCardBackFragment(android.R.drawable.btn_plus);
getFragmentManager().beginTransaction().replace(R.id.content,
currentFragment);
However the fragment is taking all the space and the Layout with the TextView is not shown.
I also tried to use RelativeLayouts with alignParentBottom=true and other stuff...
View of the Fragment which replaces the content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_kwiz_item" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:scaleType="centerCrop" />
</RelativeLayout>
OnCreateView of the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_back, container,
false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
if (drawable != null) {
imageView.setImageDrawable(drawable);
} else {
imageView.setBackgroundResource(drawableId);
}
return view;
}
After hours of debugging I finally found the mistake... Some of my layouts had views with the id "content". I thought when I look for R.id.content that the "nearest" element will be used... that's not the case!
Thanks anyone!
I modified your layout a little bit and this seems to achieve what you want:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomNav" />
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
</RelativeLayout>
And a small side note, I see you used fill_parent in your layout, but that's deprecated since API level 8, so you should use match_parent instead :)
Hope this helps, cheers!
You should use FrameLayout as your fragment container, and put (and then replace) your fragments into that FrameLayout container.

Categories

Resources