-3

I need create TextView Left Align and Right Align Text on the Same Line, and output like this . I'm searching solution by using HTML text or something property Left Text and Right Text, some thing like method setCompoundDrawables

enter image description here

This is current output

enter image description here

and this is current code

PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo);
int inPixels= (int) context.getResources().getDimension(R.dimen.iconsize);
appIcon.setBounds(0, 0, inPixels, inPixels);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);

String appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString();
Date date=new Date(packageInfo.firstInstallTime);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
holder.apkName.setText(Html.fromHtml("<b>"+appName+"</b>("+dateText+")"));
Charuක
  • 12,953
  • 5
  • 50
  • 88
Kamil Ibadov
  • 1,436
  • 2
  • 20
  • 44

3 Answers3

1

If you are trying to avoid creating separate TextView, then you might need to use SpannableString.

I think, the solution is demonstrated here

You might get more idea through this link

Community
  • 1
  • 1
Tahmid Rahman
  • 748
  • 1
  • 8
  • 20
  • final String resultText = LeftText + " " + RightText; final SpannableString styledResultText = new SpannableString(resultText); styledResultText.setSpan((new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), LeftText.length() + 2, LeftText.length() + 2 +RightText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); resultTextView.setText(styledResultText); – Kamil Ibadov Feb 05 '17 at 08:57
  • In the post exists some bugs ,etc but idea SpannableString helped me. Thank! – Kamil Ibadov Feb 05 '17 at 09:06
  • Glad to know it helped! – Tahmid Rahman Feb 05 '17 at 09:44
0

Using two textViews will give you the results: Here's an example:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weight="1"
        android:gravity="left"
        android:text="Text on the Left" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weight="1"
        android:gravity="right"
        android:text="Text on the Right" />
</LinearLayout>
Karun Shrestha
  • 731
  • 7
  • 16
0

This is how look after using SpannableString. And this is what I want. Thank you!

enter image description here

And final code is :

PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo);
int inPixels= (int) context.getResources().getDimension(R.dimen.iconsize);
appIcon.setBounds(0, 0, inPixels, inPixels);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);

String appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString();
Date date=new Date(packageInfo.firstInstallTime);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
String LeftText=appName;
String RightText=dateText;
final String resultText = LeftText + "\n" + RightText;
final SpannableString styledResultText = new SpannableString(resultText);
styledResultText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), LeftText.length() , LeftText.length() +RightText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.apkName.setText(styledResultText);
Kamil Ibadov
  • 1,436
  • 2
  • 20
  • 44