How add first two character in Textdrawable library? - java

Hello friend I am using compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' this library in my project i want add first two character inTextDrawableicon if anyone know about this library please help how add first two character of my text inTextDrawableicon here is my code my it show only first character of myTextViewinTextDrawable` icon but I want it show first two character please help me?
//get first letter of each String item
String firstLetter = String.valueOf(getItem(position).charAt(0));
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate random color
int color = generator.getColor(getItem(position));
TextDrawable drawable = TextDrawable.builder().buildRound(firstLetter,//radius in px color);
My 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="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/image_view"
android:src="#drawable/ic_home_black_24dp"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/text"
android:paddingLeft="20sp"
android:textSize="22sp"
android:fontFamily="sans-serif"
android:textColor="#color/darkcolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="John" />
</LinearLayout>

Use substring() instead of chaAt()
String firstTwoChars = getItem(position).subString(0,2);

Related

How to make FOR LOOP for changing color of text that has been allready through loop?

After searching for hours, I still cant understand how to make this working.
I have some numbers: https://prnt.sc/qaul4u
and a lop that is increasing number size as you can see on a picture.
I want to make another for lop that is turning PAST numbers in GREEN color, so when that number is over and loop goes to a next, past number turns out to a GREEN color, like a checking algorithm.
MainActivity.java (FOR LOOP)
int[] counts;
int currentCount;
TextView countText;
private void createCountsView() {
countsList.removeAllViews();
for(int i = 0; i<counts.length; i++){
int res = currentCount == i ? R.layout.workout_count_current : R.layout.workout_count;
View row = inflater.inflate(res,null);
TextView count = (TextView)row.findViewById(R.id.w_count_value);
count.setText(String.valueOf(counts[i]));
countsList.addView(row);
}
}
Workout_count.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/w_count_value"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5sp"
android:layout_marginEnd="296dp"
android:textColor="#ff0000"
android:textSize="25dp" />
</LinearLayout>
Workout_count_current.xml - increasing text size and changing color to white
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/w_count_value"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5sp"
android:layout_marginEnd="296dp"
android:textColor="#ffffff"
android:textSize="35dp"
android:textStyle="bold" />
</LinearLayout>

How do I create an edit text with currency on left and value on right

How do I create an edit text with currency on left and value on right.
this is the image
Currency can also be part of edit text? or currency is a different edit text?
I hope it can only be 1 edit text.
Why not create a LinearLayout that is horizontally oriented
and align a TextView to the left and an Edit Text on the right?
Use EditText and set this property android:drawableLeft="#mipmap/ic_launcher"
code
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#mipmap/ic_launcher"
android:hint="Add money"
android:gravity="center"
android:padding="3dp"/>
If you need to dynamically change this image then use this code inside your java file
params :- left, top, right, bottom
editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.drawableLeft, 0, 0, 0);
Use of SpannableString you can Achieve this.
In activity_main.xml file.
<EditText
android:id="#+id/edtCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Test"
android:textColor="#000"
android:textSize="26sp" />
Add this in you MainActivity.java.
EditText edtCurrency = (EditText) findViewById(R.id.edtCurrency);
SpannableString spannableString = new SpannableString("USD 123.456");
spannableString.setSpan(new UsdSpannableSuperScript((float) 1.0), 2, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
edtCurrency.setText(spannableString);
Here is UsdSpannableSuperScript.java.
public class UsdSpannableSuperScript extends SuperscriptSpan {
//divide superscript by this number
protected int fontScale = 2;
//shift value, 0 to 1.0
protected float shiftPercentage = 0;
//doesn't shift
UsdSpannableSuperScript() {
}
//sets the shift percentage
UsdSpannableSuperScript(float shiftPercentage) {
if (shiftPercentage > 0.0 && shiftPercentage < 1.0)
this.shiftPercentage = shiftPercentage;
}
#Override
public void updateDrawState(TextPaint tp) {
//original ascent
float ascent = tp.ascent();
//scale down the font
tp.setTextSize(tp.getTextSize() / fontScale);
//get the new font ascent
float newAscent = tp.getFontMetrics().ascent;
//move baseline to top of old font, then move down size of new font
//adjust for errors with shift percentage
tp.baselineShift += (ascent - ascent * shiftPercentage)
- (newAscent - newAscent * shiftPercentage);
}
#Override
public void updateMeasureState(TextPaint tp) {
updateDrawState(tp);
}
}
Here is Screen.
With only single EditText can also work. For that you will have to set prefix and fetch value from edittext with respect to the prefixCount.
You can try this way. This is a bit better approach.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="USD" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#null"
android:singleLine="true" />
</LinearLayout>
Output : Same as image above you mentioned.
With single EditText another approach. You can have a image of currency "USD" and set drawableLeft to edittext.
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:drawableLeft="#drawable/dollar_sign"
android:drawablePadding="5dp"
android:inputType="number"
android:padding="10dp"
android:singleLine="true" />
Currency currency = new Currency(Locale.US)
final String usd = currency.getCurrencyCode(); //returns USD
To use, either extend a TextView and manually add this logic, or do so through code

Add Views Programatically with loop, First Element is not shown

i loop through an array of objects and add a custom view with the objects name to a horizontalscrollview for each. My problem is, that the first one always disappears.
Here is my Activity Layout:
<RelativeLayout 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"
tools:context="lp.german.bischofshofpresenter.app.PraesentationsActivity">
<TextView
android:text="Hier wird das pdf gezeigt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:id="#+id/file_scroll_view"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/file_scroll_view_linear_layout"
android:orientation="horizontal"
android:layout_gravity="center">
</LinearLayout>
</HorizontalScrollView>
And I fill the LinearLayout with the following Code:
for(int i = 0; i<files.length; i++)
{
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.file_template, null);
TextView textView = (TextView)v.findViewById(R.id.file_template_text);
textView.setText(files[i].Name());
Log.v("FILENAME:",files[i].Name());
View fileList = findViewById(R.id.file_scroll_view_linear_layout);
((ViewGroup)fileList).addView(v, ((ViewGroup)fileList).getChildCount());
}
I get the following result:
First Objects name is File0, so this one is missing.
The really strange thing is, that there is an empty space on the right side that matches exactly the size of one of my elements.
See here:
I already checked with the Log output, that it loops through all 10 elements and its there, so I can
t explain that behaviour.
What might be the fault is the position I set in ((ViewGroup)fileList).addView(v, ((ViewGroup)fileList).getChildCount()); but i don't know whats wrong here.
Thanks in forward ;)

How do i make a 4x4 grid of imageviews?

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/music" />
</TableLayout>
This is the original XML, i just need to add more. i have an imageView that's big , so it needs to be shrunk down and copied 16 times into a 4x4 grid. I can only get it to go 4 images in one column
I don't understand the problem with the large image, but I will tell you my suggestion:
there are multiple possible solutions:
Since you have 16 imageViews that you wish to create, you can use a GridView together with a BaseAdapter . If it's important for you to see it in the xml, use isInEditMode for a custo GridView, and set the adapter there to be your adapter with fake items. You should be aware of problems with the sizes of the columns/rows on the gridView , especially when changing orientations.
Another alternative could be the GridLayout
If you insist on using the TableLayout, you can have 4 TableRow instances, each has a weight of 1 . in each of them , add 4 imageViews and there each has a weight of 1.
Add 4 TableRow's and put your ImageView's into them.
Or you can create this grid from code. Like this:
LinearLayout container = null;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
for (int i = 0; i < 16; ++i) {
if (i % 4 == 0) {
container = new LinearLayout(getActivity());
container.setOrientation(LinearLayout.HORIZONTAL);
mGrid.addView(container);
}
LinearLayout view = (LinearLayout) mLayoutInflater
.inflate(R.layout.view_item, container, false);
//populate the view in loop
view.setLayoutParams(params);
container.addView(view);
}

Change ImageView size programmatically

I have a relative layout with 3 ImageViews. The first one is a square image, the second is just used for spacing, and the third one is another square image.
Here is the xml code for that layout:
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radioSV"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/radioLayout"
android:orientation="vertical"
android:gravity="center">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/sssImageView"
android:src="#drawable/radio_sss_400_r"
android:layout_width="10sp"
android:layout_height="10sp"
android:layout_gravity="center"
android:scaleType="centerCrop">
</ImageView>
<!-- spacing -->
<ImageView
android:id="#+id/spacing1"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="10dip"
android:layout_below="#id/sssImageView">
</ImageView>
<ImageView
android:id="#+id/gaImageView"
android:src="#drawable/radio_ga_400_r"
android:layout_width="10sp"
android:layout_height="10sp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:layout_below="#id/spacing1">
</ImageView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Now, I want my app to be useful for various screen densities, so therefore, in the java file, I ask for the density and use a switch-case statement afterwards. In this statement, the size (width, height) of the 2 ImageViews (sssImageView and gaImageView) have to be changed.
For instance, if the density of the screen is high, I want the width and height of the ImageViews to be 200sp. I've just put 10sp in the xml as a 'standard' value.
This is the part of the use-case statement for high screen density:
case DisplayMetrics.DENSITY_HIGH:
layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
sssImageView.setLayoutParams(layoutParams);
sssImageView.setMaxHeight(val_high);
sssImageView.setMaxWidth(val_high);
gaImageView.setLayoutParams(layoutParams);
gaImageView.setMaxHeight(val_high);
gaImageView.setMaxWidth(val_high);
break;
Also, val_high is 200sp:
int val_high = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 200, this.getResources().getDisplayMetrics());
Now, the part with the ImageView sssImageView works perfectly. It correctly enlarges the image from 10sp to 200sp (don't worry - the image is not 10sp originally!).
The problem is the fact that the other ImageView, gaImageView, puts itself on top of sssImageView and destroys the layout. If I comment out the 3 lines with gaImageView, it is on its correct place in the layout, but is still small (10sp) of course.
I have also tried the opposite: commenting out the 3 lines with sssImageView and only manipulating gaImageView. Now the top ImageView (sssImageView) is on its correct place and small as expected. However, the bottom ImageView, gaImageView, positions itself on top of sssImageView, and not below as written in the xml layout file.
What is wrong here?
I managed to solve this problem.
Here is the code for the DENSITY_HIGH case statement:
case DisplayMetrics.DENSITY_HIGH:
display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
// 0.36 is a scaling factor
int val_high = (int) (height*0.36);
sssImageView.setId(1);
gaImageView.setId(2);
spacing.setId(3);
layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
layoutParams2 = new RelativeLayout.LayoutParams(val_high, val_high);
layoutParams3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10, this.getResources().getDisplayMetrics()));
sssImageView.setMaxHeight(val_high);
sssImageView.setMaxWidth(val_high);
relativeLayout.updateViewLayout(sssImageView, layoutParams);
layoutParams3.addRule(RelativeLayout.BELOW, sssImageView.getId());
relativeLayout.updateViewLayout(spacing, layoutParams3);
layoutParams2.addRule(RelativeLayout.BELOW, spacing.getId());
gaImageView.setMaxHeight(val_high);
gaImageView.setMaxWidth(val_high);
relativeLayout.updateViewLayout(gaImageView, layoutParams2);
break;
So generally, it looks like I had to "re-create" the xml file programmatically.

Categories

Resources