0

Android Studio has an automated Java-to-Kotlin converter. On some cases, it doesn't work smoothly, which means some manual work is needed.

Here's an example:

private TextView[] dots;

 private void addBottomDots(int currentPage) {
        dots = new TextView[layouts.length];

        int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
        int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

        dotsLayout.removeAllViews();
        for (int i = 0; i < dots.length; i++) {
            dots[i] = new TextView(getContext());
            dots[i].setText(Html.fromHtml("&#8226;"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(colorsInactive[currentPage]);
            dotsLayout.addView(dots[i]);
        }

        if (dots.length > 0)
            dots[currentPage].setTextColor(colorsActive[currentPage]);
    }

The result of auto-conversion + manual tweaking is:

private var dots: Array<TextView?>? = null  

    private fun addBottomDots(currentPage: Int) {
        dots = arrayOfNulls(layouts!!.size)

        val colorsActive = getResources().getIntArray(R.array.array_dot_active)
        val colorsInactive = getResources().getIntArray(R.array.array_dot_inactive)

        dotsLayout!!.removeAllViews()
        for (i in dots!!.indices) {
            dots[i] = TextView(getContext()) // part A
            dots!![i].text = Html.fromHtml("&#8226;") // part B
            dots!![i].textSize = 35f
            dots!![i].setTextColor(colorsInactive[currentPage])
            dotsLayout!!.addView(dots!![i])
        }

        if (dots!!.size > 0)
            dots!![currentPage].setTextColor(colorsActive[currentPage])
    }   

At part A, Android studio gives this error message:

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type TextView?

And at part B:

Smart cast to 'Array' is impossible, because 'dots' is a mutable property that could have been changed by this time.

Another case:

public class MyViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;

        public MyViewPagerAdapter() {
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(layouts[position], container, false);
            container.addView(view);

            return view;
        }
}

which converted into:

inner class MyViewPagerAdapter : PagerAdapter() {
        private var layoutInflater: LayoutInflater? = null

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            layoutInflater = getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE) // part C

            val view = layoutInflater!!.inflate(layouts!![position], container, false)
            container.addView(view)

            return view
        }

At part C:

Type mismatch. Required: LayoutInflater? Found: Any!

How to fix this?

1 Answer 1

2

Try the following

private var dots: ArrayList<TestView> = ArrayList()

private fun addBottomDots(currentPage: Int) {
    val size = layouts?.size ?: 0

    val colorsActive = getResources().getIntArray(R.array.array_dot_active)
    val colorsInactive = getResources().getIntArray(R.array.array_dot_inactive)

    dotsLayout?.removeAllViews()

    for (i in 0 until size) {
        val textView = TextView(getContext()) // part A
        textView.text = Html.fromHtml("&#8226;") // part B
        textView.textSize = 35f
        textView.setTextColor(colorsInactive[currentPage])
        dots.add(textView)
        dotsLayout?.addView(dots[i])
    }

    if (dots.size > 0)
        dots[currentPage].setTextColor(colorsActive[currentPage])
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.