ScrollView inside fragment not scrolling - android

In my Activity I have a FrameLayout, that suppose to use as container for fragments.
In my fragment I got a ScrollView that doesn't respond. ScrollView contains TextView set android:layout_height="wrap_content", with text from my java code that clearly longer than the size of the ScrollView.
My fragment xml:
<?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="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="#dimen/bookview.imageWidth"
android:layout_height="250dp"
android:id="#+id/bookView.image"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:id="#+id/bookView.name"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.name"
android:id="#+id/bookView.author"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.author"
android:id="#+id/bookView.pages"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.pages"
android:id="#+id/bookView.category"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.category"
android:id="#+id/bookView.publishingDate"/>
</RelativeLayout>
<ScrollView
android:layout_height="200dp"
android:layout_width="match_parent"
android:layout_gravity="end">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="#dimen/custom_book_text"
android:id="#+id/bookView.description"/>
</ScrollView>
</LinearLayout>
the xml of the activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/use.drawer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/user_custom_book_height"
android:id="#+id/user.customBookContainer"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/user.container"/>
</LinearLayout>
<include
layout="#layout/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
i use FrameLayout because i want to change the fragment in that area,in runtime.better idea? i like to hear.
i have tried to decrese the heigth of the ScrollView.
my fragment java code:
public class BookView extends Fragment {
private Bitmap bitmap;
private String name;
public static BookView newInstance(String book,Bitmap bitmap) {
Bundle args = new Bundle();
BookView fragment = new BookView();
fragment.bitmap = bitmap;
fragment.name = book;
fragment.setArguments(args);
return fragment;
}
public BookView() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_book_view, container, true);
Backend backend = BackendFactory.getInstance();
try {
Book b = backend.readBook(name);
TextView textView = (TextView)v.findViewById(R.id.bookView_name);
textView.setText(b.getBookName());
textView = (TextView)v.findViewById(R.id.bookView_author);
textView.setText(b.getAuthor());
textView = (TextView)v.findViewById(R.id.bookView_category);
textView.setText(b.getCategory().name());
textView = (TextView)v.findViewById(R.id.bookView_description);
textView.setText(b.getDescription());
textView = (TextView)v.findViewById(R.id.bookView_pages);
textView.setText(Integer.toString(b.getPages()));
textView = (TextView)v.findViewById(R.id.bookView_publishingDate);
textView.setText(b.getPublishingDate());
final ImageView imageView = (ImageView)v.findViewById(R.id.bookView_image);
if(bitmap != null){
imageView.setImageBitmap(bitmap);
} else{
ImageLoader imageLoader = new ImageLoader();
imageLoader.setListener(new ImageLoader.ImageTaskListener() {
#Override
public void onActionEnd() {}
#Override
public void onImageDownload(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
});
imageLoader.execute(b.getImage());
}
}
catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(e.getMessage());
builder.create().show();
}
return inflater.inflate(R.layout.fragment_book_view, container, false);
}
}
imageLoader is a class i wrote,it extened AsyncTask for download image from a url and update the UI when it ready.

I think other parts (TextViews and ImageViews) of this fragment full all area of screen and ScrollView render out of screen.
I suggest you put all layout inside ScrollView.
like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/bookView.image"
android:layout_width="#dimen/bookview.imageWidth"
android:layout_height="250dp"/>
<TextView
android:id="#+id/bookView.name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.author"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.name"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.pages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.author"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.pages"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.publishingDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.category"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
</RelativeLayout>
<TextView
android:id="#+id/bookView.description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/custom_book_text"/>
</LinearLayout>

I assume you are able to see the fragment's view.
If so a few things to try:
Set the scroll view to fillViewPort="true"
and then set the text view to match parent since this is the only view in the scroll view. As a note you should really avoid using hard coded widths & heights (even dp heights).
Instead of returning
return inflater.inflate(R.layout.fragment_book_view, container, false);
just return the original view: v
Nother' bit of readability: instead of reassigning textview each time.
do:
((TextView)v.findViewById(R.id.blabla)).setText("BLA");
Edit:
I have tested this & it works.
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:fillViewport="true">
<TextView
android:id="#+id/faq_output_answer"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="match_parent" />
</ScrollView>

Try putting an empty view as the last item in your scrollview.
<View
android:layout_width = "match_parent"
android:layout_height = "50dp" />
This worked for me.

Related

FrameLayout with two includes only set correctly the first one. Why?

I have a ScollView Layout with a FrameLayout that includes two different layouts. If some condition is met, I set one or the other as visible.
dialog_interface_login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/landing_login_view"/>
<include layout="#layout/mobile_login_view"/>
</FrameLayout>
</ScrollView>
My class:
final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
View mobileLayout = v.findViewById(R.id.mobile_root_view);
View landingLayout = v.findViewById(R.id.landing_root_view);
if (landingLogin) {
mobileLayout.setVisibility(View.INVISIBLE);
landingLayout.setVisibility(View.VISIBLE);
} else {
mobileLayout.setVisibility(View.VISIBLE);
landingLayout.setVisibility(View.INVISIBLE);
}
Inside theese layouts that was set on the include, I have another include:
landing_login_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:id="#+id/landing_root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:paddingLeft="#dimen/twenty_eight_dp"
android:paddingRight="#dimen/twenty_eight_dp"
android:orientation="vertical">
...
<include layout="#layout/sms_login"/>
...
</LinearLayout>
And mobile_login_view.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:id="#+id/mobile_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_white"
android:paddingLeft="#dimen/twenty_eight_dp"
android:paddingRight="#dimen/twenty_eight_dp"
android:orientation="vertical">
...
<include layout="#layout/sms_login"/>
</LinearLayout>
This is my sms_login.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_six_dp"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/btn_white_enabled">
<br.com.fs.fslogin.ui.support.views.GothamEditText
android:id="#+id/et_phone_number"
style="#style/AppTheme.RectangularEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:digits="0123456789"
android:gravity="center"
android:hint="#string/hint_enter_phone"
android:inputType="number|none"
android:maxLength="11"
android:textColorHint="#color/task_done_color"
android:textSize="#dimen/sixteen_sp"
font:name="#string/font_gotham_medium" />
</FrameLayout>
<FrameLayout
android:id="#+id/dialog_error_phone"
android:layout_width="match_parent"
android:layout_height="#dimen/thirty_two_dp"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/bg_warning_phone"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/eight_dp"
android:layout_marginStart="#dimen/eight_dp"
android:contentDescription="#null"
android:src="#drawable/ic_error_phone_red" />
<br.com.fs.fslogin.ui.support.views.GothamTextView
android:id="#+id/error_phone_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:lineSpacingExtra="#dimen/two_sp"
android:textColor="#color/error_login_phone_color"
android:textSize="#dimen/fourteen_sp"
font:name="#string/font_gotham_medium" />
</FrameLayout>
<FrameLayout
android:id="#+id/btn_sms_login"
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_six_dp"
android:background="#drawable/btn_login_vivo_purple">
<br.com.fs.fslogin.ui.support.views.GothamButton
style="#style/AppTheme.RectangularButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#null"
android:clickable="false"
android:gravity="center"
android:text="#string/btn_login"
android:textColor="#android:color/white"
android:textSize="#dimen/eighteen_sp"
font:name="#string/font_gotham_bold" />
</FrameLayout>
</LinearLayout>
Programmatically, I set some behaviors, such as a mask for the phone field etc.
But these behaviors that are programmatically configured in sms_login.xml will only occur if the layout is set first in my dialog_interface_login.xml. I have the same include, with the same information, but only works on the first one that is defined at ContainerLayout. Even the onclick event does not trigger.
Do you have any idea why? Is it possible to define two different includes?
----- Edited -----
Including some actions done
v.findViewById(R.id.btn_sms_login).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (phoneWatcher.isPhoneNumberValid()) {
String phoneNumber = etPhoneNumber.getText().toString().replaceAll("\\D+", "");
onSuccessClick.OnSuccess(phoneNumber);
LoginInterfaceVivoSyncDialog.this.dismiss();
}
}
});
You should use Visibility.GONE instead of INVISIBLE
You can directly assign id to include block why you made two extra different layouts to give just an id.
I was shocked to see #dimen/twelve_dp (really shocked). What is use of dimens.xml if you are writing twelve_dp there. You can directly write 12dp. It should be generic like space_small or space_large or text_size_small.
Also removed some extra wrapping layouts from your xml.
dialog_interface_login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/landing_root_view"
layout="#layout/sms_login" />
<include
android:id="#+id/mobile_root_view"
layout="#layout/sms_login" />
</FrameLayout>
</ScrollView>
and
final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
View mobileLayout = v.findViewById(R.id.mobile_root_view);
View landingLayout = v.findViewById(R.id.landing_root_view);
if (landingLogin) {
mobileLayout.setVisibility(View.GONE);
landingLayout.setVisibility(View.VISIBLE);
} else {
mobileLayout.setVisibility(View.VISIBLE);
landingLayout.setVisibility(View.GONE);
}
sms_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<br.com.fs.fslogin.ui.support.views.GothamEditText
android:id="#+id/et_phone_number"
style="#style/AppTheme.RectangularEditText"
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_six_dp"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/btn_white_enabled"
android:digits="0123456789"
android:gravity="center"
android:hint="#string/hint_enter_phone"
android:inputType="number|none"
android:maxLength="11"
android:textColorHint="#color/task_done_color"
android:textSize="#dimen/sixteen_sp"
font:name="#string/font_gotham_medium" />
<FrameLayout
android:id="#+id/dialog_error_phone"
android:layout_width="match_parent"
android:layout_height="#dimen/thirty_two_dp"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/bg_warning_phone"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/eight_dp"
android:layout_marginStart="#dimen/eight_dp"
android:contentDescription="#null"
android:src="#drawable/ic_error_phone_red" />
<br.com.fs.fslogin.ui.support.views.GothamTextView
android:id="#+id/error_phone_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:lineSpacingExtra="#dimen/two_sp"
android:textColor="#color/error_login_phone_color"
android:textSize="#dimen/fourteen_sp"
font:name="#string/font_gotham_medium" />
</FrameLayout>
<br.com.fs.fslogin.ui.support.views.GothamButton
android:id="#+id/btn_sms_login"
style="#style/AppTheme.RectangularButton"
android:layout_width="wrap_content"
android:layout_height="#dimen/fifty_six_dp"
android:layout_gravity="center"
android:background="#drawable/btn_login_vivo_purple"
android:clickable="false"
android:gravity="center"
android:text="#string/btn_login"
android:textColor="#android:color/white"
android:textSize="#dimen/eighteen_sp"
font:name="#string/font_gotham_bold" />
</LinearLayout>
Do you have any idea why?
This happens because findViewById(R.id.xyz) returns the first View which has the requested attribute android:id="#+id/R.id.xyz".
Quoting from the documentation on View:
Finds the first descendant view with the given ID, the view itself if the ID matches getId(), or null if the ID is invalid (< 0) or there is no matching view in the hierarchy.
To access the correct View, you'll have to use some property which makes it unique. In your case, for example the value R.id.et_phone_number can be found twice in the FrameLayout. But if you write
View myView = mobileLayout.findViewById(R.id.et_phone_number);
then there will be just one child View of mobileLayout with the requested id.
Your code snippet can be changed as follows:
final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
View mobileLayout = v.findViewById(R.id.mobile_root_view);
View landingLayout = v.findViewById(R.id.landing_root_view);
if (landingLogin) {
mobileLayout.setVisibility(View.INVISIBLE);
landingLayout.setVisibility(View.VISIBLE);
} else {
mobileLayout.setVisibility(View.VISIBLE);
landingLayout.setVisibility(View.INVISIBLE);
}
setupViewGroup(mobileLayout);
setupViewGroup(landingLayout);
The method setupViewGroup(View layout) should look like this:
private void setupViewGroup(View layout)
{
final EditText etPhoneNumber = layout.findViewById(R.id.et_phone_number);
layout.findViewById(R.id.btn_sms_login).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (phoneWatcher.isPhoneNumberValid()) {
String phoneNumber = etPhoneNumber.getText().toString().replaceAll("\\D+", "");
// ...
}
}
});
}
By calling findViewById() on layout you will get the desired child Views of the ViewGroups which were added by using <include .../>

TextView setText and setVisibility not working in fragment

I have ControllerFragment which has 3 child fragments. Children fragments change are provided in the tabLayout via ViewPager. Unsend Apple and Banana badge count are computable variable values. unSendAppleCountTxtView and unSendBananaCountTxtView are changed according to these variable values.
My function is running main Thread.
Tried methods:
I have controlled null check and textView isn't null
setUnSendAppleCount function is taken in these threads (runOnUIThread, new Handler(), new Handler(Looper.getMainLooper()))
I have tried AsyncTask and not working setView and setText in postExecute too.
And finally I scan all related questions and tried.
All methods didn't work. TextView is empty value.
public class ControllerFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.controller_fragment, viewGroup, false);
tabs = ViewUtil.findById(rootView, R.id.tabs);
viewPager = ViewUtil.findById(rootView, R.id.controllerViewPager);
setFragments();
setupViewPager(viewPager);
tabs.setupWithViewPager(viewPager);
setupTabIcons(viewGroup);
return rootView;
}
private void setupTabIcons(ViewGroup viewGroup) {
LinearLayout appleListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);
RelativeLayout pearListTab = (RelativeLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text, viewGroup, false);
LinearLayout bananaListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);
unSendAppleCountTxtView = appleListTab.findViewById(R.id.badgeView);
unSendBananaCountTxtView = bananaListTab.findViewById(R.id.badgeView);
TextView appleTextView = appleListTab.findViewById(R.id.textView);
TextView pearTextView = pearListTab.findViewById(R.id.textView);
TextView bananaTextView = bananaListTab.findViewById(R.id.textView);
setUnSendAppleCount(2); //....this function is not working
setUnSendBananaCount(2); //.....this function is not working
bananaTextView.setText(getString(R.string.bananas));
tabs.getTabAt(0).setCustomView(bananaListTab);
appleTextView.setText(getString(R.string.apples));
tabs.getTabAt(1).setCustomView(appleListTab);
pearTextView.setText(getString(R.string.pears));
tabs.getTabAt(2).setCustomView(pearListTab);
}
private void setupViewPager(ViewPager viewPager) {
TabsViewPagerAdapter adapter = new TabsViewPagerAdapter(getFragmentManager());
adapter.addFrag(bananaFragment, getString(R.string.bananas));
adapter.addFrag(appleFragment, getString(R.string.apples));
adapter.addFrag(pearFragment, getString(R.string.pears));
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1);
}
private void setUnSendAppleCount(int unSendCount) {
if (unSendAppleCountTxtView != null) {
if (unSendCount > 0) {
unSendAppleCountTxtView.setVisibility(View.VISIBLE);
unSendAppleCountTxtView.setText(String.format(getMyLocale(), "%d", unSendCount));
} else {
unSendAppleCountTxtView.setVisibility(View.GONE);
}
}
}
}
controller_fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/tab_unpressed_color">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/tabColor" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/controllerViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/app_bar_layout" />
</RelativeLayout>
tab_title_text_and_badge.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_gravity="center"
android:gravity="center"
android:weightSum="2"
android:orientation="horizontal"
tools:background="#color/Black">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:layout_gravity="center"
android:maxLines="1"
android:layout_weight="1.99"
android:textColor="#color/white"
android:textSize="#dimen/infoTitle"/>
<TextView
android:id="#+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="#drawable/tab_controller_badge"
android:gravity="center"
android:textColor="#color/white"
android:textSize="#dimen/miniTextSize"
android:visibility="gone"
tools:text="11"
tools:visibility="visible" />
</LinearLayout>
Please help me
try to setVisibility android:visibility="visible" or android:visibility="invisible" delete gone attribute it remove TextView.
<TextView
android:id="#+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="#drawable/tab_controller_badge"
android:gravity="center"
android:textColor="#color/white"
android:textSize="#dimen/miniTextSize"
android:visibility="visible" //change here
tools:text="11"
tools:visibility="visible" />
Please use android:text instead tools:text
tools:text="text" is used only for Android Studio layout preview (wont be visible while running the app)
android:text="text" is used to set text to a a layout element, which you can see when the app is run
Try using the below xml,
<?xml version="1.0" encoding="utf-8"?>
<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:layout_gravity="center"
android:gravity="center"
android:weightSum="2"
android:orientation="horizontal"
android:background="#color/Black">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:layout_gravity="center"
android:maxLines="1"
android:layout_weight="1.99"
android:textColor="#color/white"
android:textSize="#dimen/infoTitle"/>
<TextView
android:id="#+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="#drawable/tab_controller_badge"
android:gravity="center"
android:textColor="#color/white"
android:textSize="#dimen/miniTextSize"
android:text="11"/>
</LinearLayout>
Hope it helps!
I guess different thread problem. I have tried that eventBus for activity passing data to fragment and it solved.
ControllerFragment(){
#Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
#Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMain(ControllerEvent event) {
if (event.getSelectedEventType() == ControllerEvent.tabControllerEventTypes.refreshCount.ordinal()) {
setUnSendAppleCount(event.getAppleCount());
}
}
}

View width for dynamically added view

I want to show a notification counter like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:alpha=".5"
android:id="#+id/gvIcon"
android:src="#drawable/ic_person_reading"
android:scaleType="centerCrop"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp" />
<LinearLayout
android:id="#+id/llTexts"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="#style/gridItemsText"
android:id="#+id/gvText"
android:text="Guten Morgen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partNoticeTooltip"
android:orientation="vertical"
android:background="#drawable/bg_tooltip_red"
android:layout_width="#dimen/tooltipWH"
android:layout_height="#dimen/tooltipWH">
<TextView
android:id="#+id/tvCounter"
android:text="4"
android:textColor="#color/white"
android:textSize="#dimen/h6"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which has an output like this:
This is my expectation, but with dynamic way. So I wrote this in the setOnItemClickListener of my GridView:
View tooltip = context.getLayoutInflater().inflate(R.layout.part_tooltip, null);
TextView tvCounter = (TextView) tooltip.findViewById(R.id.tvCounter);
tvCounter.setText("" + counter);
LinearLayout llText = (LinearLayout) view.findViewById(R.id.llTexts);
llText.addView(tooltip);
the part_tooltip has exactly the same code but with another layout. And here is the output:
The red layout does not being displayed with full width. What am I missing?
You need to use a global layout listener and also the view tree observer:
ViewTreeObserver mVTO = parentLayoutOfTheViewAddedDynamically.getViewTreeObserver();
mVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(viewAddedDynamically != null){
int viewWidth = viewAddedDynamically.getWidth();
// any operation based on viewWidth is to be done here
}
}
});

Shared Element Transition in GridView Weird behavior

I'm using Shared Elements Transition between an imageView in a gridView adapter and an imageView in a details activity , but when i click on an item in the GridView the imageView dosen't start to animate from the right position it always starts under the original place, and when i hit back the image animates to the right place correctly so the glitch happens only when entering the new activity.
https://youtu.be/cprBHWPVNbk (i've slowed down the animation so that the glitch is clear).
In the BrowseFragment's OnCreate :
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView gridPoster = (ImageView) view.findViewById(R.id.movie_poster);
gridPoster.setTransitionName("poster" + position);
listener.onItemSelected(movie,gridPoster);
Then the BrowseActivity's listener starts the detailsActivtiy :
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation
(this, posterView,posterView.getTransitionName());
Intent detailsIntent = new Intent(this, MovieDetalisActivity.class);
detailsIntent.putExtra(getString(R.string.movie_details_extra_key), movie);
detailsIntent.putExtra("transition",posterView.getTransitionName());
ActivityCompat.startActivity(this, detailsIntent, optionsCompat.toBundle());
Then in the DetailsFragment's OnCreate :
ImageView poster = (ImageView) rootView.findViewById(R.id.poster);
poster.setTransitionName(getActivity().getIntent().getStringExtra("transition"));
GridViewItem layout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/gird_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#4e585c"
android:clipChildren="false">
<ImageView
android:layout_width="match_parent"
android:layout_height="240dp"
android:id="#+id/movie_poster" />
<RelativeLayout
android:id="#+id/thumb_bottom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/movie_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:fontFamily="sans-serif"
android:textColor="#aaa"
android:textSize="16sp"
/>
</RelativeLayout>
</LinearLayout>
Part of the DetailsView layout :
<android.support.v4.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/scrollView"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:clipChildren="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.rashwan.popularmovies.MovieDetailsActivityFragment"
android:orientation="vertical"
android:focusableInTouchMode="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="310dp"
android:orientation="horizontal"
android:padding="10dp"
android:baselineAligned="false"
android:clipChildren="false">
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:clipChildren="false">
<ImageView
android:layout_width="wrap_content"
android:minWidth="220dp"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:id="#+id/poster" />
</FrameLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ReleaseDate"
android:id="#+id/release_date"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserRating"
android:id="#+id/user_rating"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:textAppearanceLarge"/>
</LinearLayout>
</LinearLayout>
And in my Theme i declared <item name="android:windowContentTransitions">true</item>i'm not using any custom animation although i tried some and i also tried to postpone the transition but nothing solved the problem.
I've found that the problem was that i'm using picasso library to load the images in the details fragment so i needed to use postponeEnterTransition(); in the details activity then in the details fragment i use getActivity().startPostponedEnterTransition(); to start the transition but only after the image is loaded using picasso.
Here's the code in the details fragment:
Picasso.with(getActivity()).load(posterUri).fit().into(poster, new Callback() {
#Override
public void onSuccess() {
poster.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if (isLollipop) {
poster.getViewTreeObserver().removeOnPreDrawListener(this);
getActivity().startPostponedEnterTransition();
}
return true;
}
});
}
Try putting your classes in manifest.AS

add elements in fragment's view android

I'm new to Android, I have a fragment which have this XML code
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:id="#+id/linearRoot"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:text="bonjour"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_age"
android:layout_below="#+id/person_name"
android:text="ce test a réussi"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
With this one, I want to add several CardView Dynamically.So, as a test, I'm trying to add a second CardView in the LinearLayout this way :
public class TopRatedFragment extends Fragment {
LinearLayout ll;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
ll = (LinearLayout)rootView.findViewById(R.id.linearRoot);
CardView cv = new CardView(getActivity().getApplicationContext());
ll.addView(cv);
return rootView;
}
}
but it does nothing at all. I'm wondering what is the right way to do this.
you have to add LayoutParams (width, height) in order to actually show your CardView
cv.setLayoutParams(new ViewGroup.LayoutParams(20,20));
for example with 20dp width and height

Categories

Resources