How to position a TextView contents after changing .setScaleY() and .setScaleX()? - android

I'm trying to make an android ESC/POS printer editor UI. ESC/POS has few options when printing text and they can be mixed together: small, underlined, bold, double width(wide), double height(tall).
While the ESC/POS printer part is no problem, I have some issues while trying to represent it with text views in android UI.
while 'small' could be solved changing the font size, i'm trying to achieve
wide and tall by setting View.setScaleX(1.5f) and View.setScaleY(1.5f).
I am creating those views programmaticaly and adding them to a linearLayout container.
After changing scale the view bounds does not get updated. and wide option goes out of view bounds. I have tried to use view.invalidate() on view and its parent thinking it would recalculate bounds but it did not work.
onClick(View v) {
PrinterCommands.FormatedText textComand = new PrinterCommands.FormatedText("Hello world");
textComand.setTall(r.nextBoolean());
textComand.setUnderlined(r.nextBoolean());
textComand.setBold(r.nextBoolean());
textComand.setWide(r.nextBoolean());
textComand.setSmall(r.nextBoolean());
TextView textView = new TextView(CommandsViewActivity.this);
StringBuilder sb = new StringBuilder("Hello world");
// v.setText(String.valueOf(position));
if (textComand.isSmall()) {
sb.append(" small");
textView.setScaleX(0.5f);
textView.setScaleY(0.5f);
}
if (textComand.isBold()) {
sb.append(" bold");
textView.setTypeface(null, Typeface.BOLD);
}
if (textComand.isTall()) {
sb.append(" tall");
textView.setScaleY(1.5f);
}
if (textComand.isWide()){
sb.append(" wide");
textView.setScaleX(1.5f);
}
if (textComand.isUnderlined()) {
sb.append(" underlined");
SpannableString s = new SpannableString(sb.toString());
s.setSpan(new UnderlineSpan(), 0, s.length(), 0);
textView.setText(s);
} else textView.setText(sb.toString());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setGravity(Gravity.START|Gravity.CENTER);
p.setMargins(16,16,16,16);
textView.setLayoutParams(p);
container.addView(textView);
}
});
Layout output I am getting

Related

Programmatically set constraints to textViews created at run-time?

The code below creates a varying amount of textViews at run-time depending on the number of keys in friendMap:
generatedViews = new TextView[numberOfFriends];
int count = 0;
for (String k : friendMap.keySet()) {
TextView textView = new TextView(this);
textView.setVisibility(View.VISIBLE);
textView.setText(k);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
textView.setGravity(Gravity.CENTER);
String mutual = friendMap.get(k);
if (mutual.equals("no")) {
textView.setBackgroundColor(Color.RED);
} else {
textView.setBackgroundColor(Color.GREEN);
}
linLayout.addView(textView);
generatedViews[count] = textView;
count++;
}
My question is: how can I set constraints to the created textViews so they don't bunch?
It's simple:
Set the layout parameters of the LinearLayout with:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(100, 20, 100, 20);
linLayout.addView(textView, layoutParams);
As #TheWandered said you should probably use RecyclerView.
But if for some reason it's not an option, here is how you can do it:
First thing, you are going to have to generate ID for each TextView, so you can target the constraints and then:
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(text_view_id,ConstraintSet.TOP, previous_text_view_id,ConstraintSet.BOTTOM,0);
// is equal to "layout_constraintTop_toBottomOf="previous_text_view_id"
constraintSet.applyTo(constraintLayout);

Extract text lines as displayed in textview

Is it possible to get each line of a multiline text as it will appear in a textview of a fixed width, with a font, size, alignment without actually drawing the textview ?
My Text is stored in a string. I can apply different font, size, alignment for the text. When passing it to my api, I want to split it into a list of Strings just how it was displayed in the app. Is this possible ?
In the view the text is formatted using the following:
TextView textView = new TextView(getContext());
LayoutParams textLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (itemWidth > 0) {
textLayoutParams.width = itemWidth;
}
if (itemHeight == -1) {
textLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
}
textLayoutParams.leftMargin = xPosition;
textLayoutParams.topMargin = yPosition;
textView.setLayoutParams(textLayoutParams);
textView.setPaintFlags(Paint.ANTI_ALIAS_FLAG);
font = getFontName(font);
if (!StringUtils.isEmpty(font)) {
CalligraphyUtils.applyFontToTextView(getContext(), textView, CalligraphyConfig.get(), "fonts/" + font);
}
if (!StringUtils.isEmpty(color))
textView.setTextColor(Color.parseColor(color));
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, (int)size );
if (alignment.equalsIgnoreCase("center")) {
textView.setGravity(Gravity.CENTER);
} else if (alignment.equalsIgnoreCase("left")) {
textView.setGravity(Gravity.START);
} else if (alignment.equalsIgnoreCase("right")) {
textView.setGravity(Gravity.END);
} else if (alignment.equalsIgnoreCase("left_center")) {
textView.setGravity(Gravity.START | Gravity.CENTER);
}
addView(textView);
For multi line strings, it will display on different lines. In a background process, I want to know which substring was displayed on 1st line, which substring was displayed on second line and so on. Is this even possible ?

Programmatically fill LinearLayout with a single line of TextViews

I'm trying to display a bunch of text on the screen by placing TextViews inside rows of single-line LinearLayouts. Each word is stored in its own separate TextView, and I want to be able to place as many TextViews as will fit on a single LinearLayout line and detect when I've run out of horizontal space so that I can move to the next line.
The problem I'm facing is that I can't seem to find a way to measure the changing layout sizes as I create the display, because I can't get a reference width using getWidth() on the parent layout, and even after I add the TextViews, I can't seem to control the width.
We had a working version before, but it did everything using on hard-coded numbers based on the number of characters in a TextView at a fixed size. I'm trying to extend the app to work with all text and screen sizes. If this needs to be completely overhauled, I understand - I just want to be able to fill up the screen with an indefinite number of lines of text.
An obvious solution would be to just place all the text inside one TextView, but we need to be able to access each Word/Ponctuation object and its attributes through the displayed TextViews.
// layout_row is the current LinearLayout row I'm adding my TextViews to
// layout is the LinearLayout parent of all layout_rows
// text.text_content is a linked list of Word and Ponctuation objects
// each Word and Ponctuation object has a TextView attribute called view
private void display_views() {
if (text != null)
{
boolean prochainLigneSuivante; // if new line is to follow
int widthSoFar = 0;
int layoutWidth = layout_row.getWidth();
for (Object o : text.text_content) {
if (o instanceof Word ) {
Word w = (Word) o;
Object next = text.next(o);
if (noNeedForSpace(w)) {
// by default all TextViews have
// right padding to simulate spaces
w.view.setPadding(0, 0, 0, 0);
}
layout_row.addView(w.view);
widthSoFar += w.view.getWidth();
// Am I out of space?
prochainLigneSuivante = widthSoFar >= layoutWidth;
if(prochainLigneSuivante) {
layout_row.removeView(w.view);
widthSoFar = 0;
layout_row = new LinearLayout(context);
layout_row.setOrientation(LinearLayout.HORIZONTAL);
layout_row.addView(w.view);
layout_row.setBackgroundColor(Color.BLACK);
layout_row.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT));
layout.addView(layout_row);
}
}
else if (o instanceof Ponctuation) {
Ponctuation p = (Ponctuation) o;
if (p.text.contains("CR")) {
layout_row = new LinearLayout(context);
layout_row.setOrientation(LinearLayout.HORIZONTAL);
layout_row.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT));
widthSoFar = 0;
layout.addView(layout_row);
}
else {
if (p.view.getText().equals(" "))
p.view.setPadding(0, 0, 0, 0);
layout_row.addView(p.view);
if(!p.view.getText().equals(""))
widthSoFar += p.view.getWidth();
}
}
}
}
else {
Log.e("Text", "text est nul");
}
scroll.refreshDrawableState();
transition.startTransition(0);
}

Android Textview width in RelativeLayout

I'm writing a chat program. In chat activity, my sent and received messages are displaying with EditTexts like bubbles. I want to set my width of edittexts with the same width of its messages. In my code, when message is created, new edittexts are being created programmatically in the relativelayout. How can I set it?
public void sendmyMessage(String msg){
// mRR is my RelativeLayout
RelativeLayout.LayoutParams params;
EditText e = new EditText(mRR.getContext());
e.setId(arr.size()+2);
e.setEnabled(false);
e.setFocusable(false);
e.setClickable(false);
e.setText(msg);
e.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
e.setTypeface(null,Typeface.BOLD);
e.setTypeface(Typeface.SANS_SERIF);
e.setTextColor(Color.parseColor("#080808"));
params = new RelativeLayout.LayoutParams(setDp(150, e.getContext()),RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(0, setDp(8, e.getContext()), 0, 0);
if(arr.isEmpty()){
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
}else{
params.addRule(RelativeLayout.BELOW,arr.get(arr.size()-1).getId());
}
Spannable str = e.getText();
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
e.setLayoutParams(params);
e.setBackgroundResource(R.drawable.mytext);
arr.add(e);
mRR.addView(e);
}
The function is this. I don't want to set the width fixsize. What is the solution?
something like this..?
editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

Android Creating button dynamically and fill layout

I'm creating a button dynamically. The number of button is depend on the size of arraylist. the problem is, after creating the button I will add to the layout using addview method. The problem is I'm using linear layout, as by default orientation for linear layout is horizontal, so the button will fill the layout horizontally. Because of that some of the button is not visible. What I'm trying to achieve is something look like this
My code is like below:
Button[] tv = new Button[arraylist.size()];
for(int i=0;i<arraylist.size();i++){
tv[i] = new Button(getApplicationContext());
tv[i].setText(arraylist.get(i).toString());
tv[i].setTextColor(Color.parseColor("#000000"));
tv[i].setTextSize(20);
tv[i].setPadding(15, 5, 15, 5);
linearlayout.addView(tv[i]);
}
If I set the orientation of linear layout to vertical the button will fill vertically. So if there any solution to create the button dynamically and fill the layout both horizontal and vertical as shown by image.
There is not a canned layout in the SDK that does exactly what you are aiming for (i.e. lay out as many children horizontally as will fit, then flow to the next line to lay out some more), so you will need to create a custom ViewGroup that accomplishes this purpose. Luckily for you, Romain Guy created one live on-screen during a presentation at Devoxx.
Here is a link to that presentation video.
Here is a link to the sample code and slides.
HTH
After 2 days struggling thinking bout this problem finally I've found the solution. I've try put all my contact list, store it in arraylist and create button for each element and I'm quite satisfy with the result after display on the screen. Here is how I do the trick. I really appreciate for any comment from others.
variable declaration;
int currWidth;
int currCounter;
boolean isNewLine;
LinkedList<HashMap<String,Object>> button;
ArrayList<String> nameNumber = new ArrayList<String>();
contactWrapper = (LinearLayout) findViewById(R.id.multiple_selection);
create button onClick event;
for(int i=0;i<nameNumber.size();i++){
tv[i] = new Button(getApplicationContext());
String[] namePhone = nameNumber.get(i).toString().split("##");
phoneNumber.add(namePhone[1]);
tv[i].setText(namePhone[0]);
tv[i].setTag(namePhone[1]);
tv[i].setTextColor(Color.parseColor("#000000"));
tv[i].setTextSize(20);
tv[i].setPadding(15, 5, 15, 5);
tv[i].measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
HashMap<String, Object> map = new HashMap<String,Object>();
map.put("button", tv[i]);
map.put("width", tv[i].getMeasuredWidth());
button.add(map);
}
drawLayout();
drawlayout method is where I add button and arrange accordingly to fit the layout;
public void drawLayout(){
int counter=0;
contactWrapper.setOrientation(LinearLayout.VERTICAL);
currCounter=0;
currWidth=0;
isNewLine=false;
LinearLayout[] row = new LinearLayout[nameNumber.size()];
row[currCounter] = new LinearLayout(getApplicationContext());
#SuppressWarnings("rawtypes")
Iterator it = button.iterator();
for(int i = 0; i<button.size(); i++){
it.next();
row[currCounter].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
currWidth += Integer.parseInt(button.get(i).get("width").toString());
if(isNewLine){
if(currWidth < contactWrapper.getWidth()){
row[currCounter].addView((View) button.get(i).get("button"));
if(!it.hasNext()){
contactWrapper.addView(row[currCounter]);
}else{
if(contactWrapper.getWidth()<(currWidth+Integer.parseInt(button.get(i+1).get("width").toString()))){
isNewLine=true;
contactWrapper.addView(row[currCounter]);
currCounter+=1;
row[currCounter] = new LinearLayout(getApplicationContext());
currWidth=0;
}else{
isNewLine=false;
}
}
}else{
isNewLine=true;
contactWrapper.addView(row[currCounter]);
currCounter+=1;
row[currCounter] = new LinearLayout(getApplicationContext());
currWidth=0;
}
}else{
if(currWidth < contactWrapper.getWidth()){
if(!it.hasNext()){
row[currCounter].addView((View) button.get(i).get("button"));
contactWrapper.addView(row[currCounter]);
}else{
row[currCounter].addView((View) button.get(i).get("button"));
if(contactWrapper.getWidth()<(currWidth+Integer.parseInt(button.get(i+1).get("width").toString()))){
isNewLine=true;
contactWrapper.addView(row[currCounter]);
currCounter+=1;
row[currCounter] = new LinearLayout(getApplicationContext());
currWidth=0;
}else{
isNewLine=false;
}
}
}else{
isNewLine=true;
contactWrapper.addView(row[currCounter]);
currCounter+=1;
row[currCounter] = new LinearLayout(getApplicationContext());
currWidth=0;
}
}
counter++;
}
}
this code quite messy + I'm not fully utilize the size of array for
LinearLayout[] row = new LinearLayout[nameNumber.size()];
but it work for me.
use TableLayout instead of LinearLayout this is tutorial hope this will help you to get the idea
Does you set android:layout_width="fill_parent"?
Do this if you don't.
Well, you can try using more sophisticated way. You can create horizontal linear layout, and add buttons to it. Every time you're attempting to add new button, you check if there is place for it, by finding difference between layout's and buttons widths.
Each time your horizontal layout is filled, you add it to another vertical layout, and create another horizontal layout to store buttons left.
I used that trick in my apps.
try this its working fine
this.row = (LinearLayout)findViewById(R.id.tags);
this.row.setOrientation(LinearLayout.VERTICAL);
LinearLayout one = new LinearLayout(this);
//get the size of the screen
Display display = getWindowManager().getDefaultDisplay();
this.screenWidth = display.getWidth(); // deprecated
this.screenHeight = display.getHeight();// depreceted
for(int i=0; i<6; i++) {
one.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
this.button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
if(i==0) {
this.button.setText("Muhammad Aamir");
} else if(i==1) {
this.button.setText("Ahsan");
} else if(i==2) {
this.button.setText("Mujahid");
} else if(i==3) {
this.button.setText("Waqas");
} else if(i==4) {
this.button.setText("Ali");
} else {
this.button.setText("Ahmer");
}
//get the size of the button text
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(button.getTextSize());
mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.NORMAL));
float size = mPaint.measureText(button.getText().toString(), 0, button.getText().toString().length());
size = size+14;
this.totalTextWidth += size;
if(totalTextWidth < screenWidth) {
one.addView(button);
} else {
this.row.addView(one);
one = new LinearLayout(this);
one.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
one.addView(button);
this.totalTextWidth = size;
}
}
this.row.addView(one);
}

Categories

Resources