Pages

Tuesday, 27 August 2019

android auto resize textview font not increasing after decrease

I have faced following issue when using textview with autoresize
Following is my layout xml to do auto size

<androidx.appcompat.widget.AppCompatTextView 
       android:id="@+id/tvAmount"        
        android:layout_width="match_parent"        
        android:layout_height="match_parent"        
        android:scrollHorizontally="true"
        android:maxWidth="@dimen/_290sdp"
        android:maxHeight="@dimen/_50sdp"
        android:gravity="start|bottom"
        android:textSize="@dimen/_28ssp"
        android:maxLines="1"        
        android:textColor="@color/charcoal"
        android:includeFontPadding="false"
        android:layout_toStartOf="@+id/tvCurrency"
        tools:text="200"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="@dimen/_5ssp"
        app:autoSizeMaxTextSize="@dimen/_25ssp"
        app:autoSizeStepGranularity="1sp"        
        app:layout_constraintTop_toTopOf="parent"        
        app:layout_constraintBottom_toBottomOf="parent"        
        app:layout_constraintEnd_toStartOf="@+id/tvCurrency" />

Above code is working fine and did auto complete resize.
but For ex,
if I set value 100
then after some calculation or button click I want to change value to 10000 then text size is too small even if we have space to show this big value.

To solve above problem need to do following.

tvAmount.setHorizontallyScrolling(true)
tvAmount.text = value?.getDecimalFormat()
tvAmount.setHorizontallyScrolling(false)


I have to enable and disable horizontal scrolling because internally for autosize text calculation, if horizontal scrolling is true text view's available drawing width is "1024 * 1024" and after setting text, we should disable horizontal scroll so text
size may decrease if required.

Wednesday, 20 December 2017

Android click on push notification create new activity instead of resume same activity from background

To solve this issue you need to change android:launchMode="singleTop" in AndroidMenifest.xml file for that activity.

Android Webview loaddata not changing content second time

Android Webview's loaddata method have issue.
use following way to load data to webview

webView.loadDataWithBaseURL(null, htmlContent, null, "utf-8", null);

Thursday, 26 October 2017

Android web view set wrap_content height

Use following code to set android web view set wrap_content height


  • webview's parent should have match parent height
  • and set webview as wrap_content height
---Layout file ---
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_margin="@dimen/twenty"
                android:src="@drawable/img_sc2_logo" />

            <WebView
                android:id="@+id/wvInfo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </WebView>
       </LinearLayout>
</ScrollView>

---java file code---
Following is IMP part

binding.wvInfo.loadUrl(ApiRequestUtil.PAGE_APP_WELCOME);
        binding.wvInfo.getSettings().setLoadWithOverviewMode(true);
        binding.wvInfo.getSettings().setUseWideViewPort(true);
        binding.wvInfo.getSettings().setJavaScriptEnabled(true);
        binding.wvInfo.getSettings().setBuiltInZoomControls(false);

        ViewTreeObserver viewTreeObserver  = binding.wvInfo.getViewTreeObserver();

        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int height = binding.wvInfo.getMeasuredHeight();
                if( height != 0 ){
//                    Toast.makeText(getActivity(), "height:"+height,Toast.LENGTH_SHORT).show();
                    binding.wvInfo.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return false;
            }
        });

Wednesday, 1 March 2017

Android get image from view

To get bitmap from any view use following code.

rootView.setDrawingCacheEnabled(true);
rootView.layout(0, 0, rootView.getWidth(), rootView.getHeight());
rootView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);