I have hit a problem when trying to align my text to the top of a text view dynamically. I have a method that creates textviews dynamically. I have set the gravity of the text view to Gravity.TOP. Which a believe should place the text at the very top of the textView object. However when I run the project there is a gap of about 2 lines of text from the top of the object. I can work around this by changing the height of the textView but I would rather it loaded in an set the size depending on how much text there is too load into the textView.
Here is my function, it works well and does what it is supposed to do:
public void addItems(LinearLayout page,int id,String text,int row){
LinearLayout item = new LinearLayout(this);
item.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView itemNum = new TextView(this);
//itemNum.setLayoutParams(new ViewGroup.LayoutParams(50,50));
itemNum.setWidth(50);
itemNum.setTextSize(40);
itemNum.setTextAppearance(getApplicationContext(), R.style.large_text);
itemNum.setLeft(0);
itemNum.setText(row+":");
LayoutParams numParams = new LinearLayout.LayoutParams(70, 70);
numParams.setMargins(25, 0, 0, 0);
TextView itemDetails = new TextView(this);
itemDetails.setLayoutParams(new ViewGroup.LayoutParams(150,LayoutParams.WRAP_CONTENT));
itemDetails.setText(text);
itemDetails.setWidth(100);
itemDetails.setLeft(5);
itemDetails.setRight(5);
itemDetails.setTop(0);
itemDetails.setVisibility(View.VISIBLE);
itemDetails.setGravity(itemDetails.getGravity() | Gravity.TOP);
itemDetails.setPadding(0,0,0,0);
Button delete = new Button(this);
//delete.setLayoutParams(new ViewGroup.LayoutParams(50,50));
delete.setText("Delete");
LayoutParams btnParams = new LinearLayout.LayoutParams(70, 70);
btnParams.setMargins(0, 0, 15, 0);
item.addView(itemNum,numParams);
item.addView(itemDetails);
item.addView(delete,btnParams);
page.addView(item);
Log.d("Loop","Loop is adding function in");
}
If anybody can see where I have gone wrong I would really appreciate it. The method above may seem very long winded but I am new to Java.
Not sure if it will help but here is my string to show in the textView:
String formatText = lat + ", " + lng + " /n" + result;
Related
Im trying to create LinearLayout programmaticaly and I would want to allow user to put number range.
Now it looks like that:
But when I try to enter more digits eg. 100, 101 or 3,50 it dissapears.
I guess there is not enough space for it to be shown, but I can't figure out what is wrong. Generally I don't want to move + and - buttons when the user enters some values so I guess it should be hardcoded. There would be up to 5-6 digits only, so I need space just for it, but as I said, I can't find the place, where I can change it as my changes eiter move entire layout or doesn't do anything.
Below is my code:
LinearLayout horizontalLayout = new LinearLayout(mContext);
LinearLayout titleLayout = new LinearLayout(mContext);
LinearLayout countLayout = new LinearLayout(mContext);
ImageButton buttonAdd = new ImageButton(mContext);
ImageButton buttonSub = new ImageButton(mContext);
TextView titleTextView = new TextView(mContext);
EditText countEditText = new EditText(mContext);
final int[] currentCount = {defaultValue};
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
LinearLayout.LayoutParams utilParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setLayoutParams(params);
utilParams.gravity = Gravity.CENTER_VERTICAL;
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
titleLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
titleLayout.setLayoutParams(linearLayout);
countLayout.setOrientation(LinearLayout.HORIZONTAL);
countLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
countLayout.setLayoutParams(linearLayout);
utilParams.setMargins(0,0,pxFromDp(mContext, 16f),0);
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
titleTextView.setText(title);
titleTextView.setLayoutParams(utilParams);
titleLayout.addView(titleTextView);
utilParams.setMargins(pxFromDp(mContext, 16f),0,pxFromDp(mContext, 16f),0);
buttonSub.setImageResource(R.drawable.ic_remove);
buttonSub.setLayoutParams(utilParams);
buttonSub.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
buttonSub.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
countLayout.addView(buttonSub);
countEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
countEditText.setText(String.valueOf(defaultValue));
countEditText.setLayoutParams(linearLayout);
countEditText.setGravity(Gravity.CENTER);
countLayout.addView(countEditText);
buttonAdd.setImageResource(R.drawable.ic_add_24);
buttonAdd.setLayoutParams(utilParams);
buttonAdd.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
buttonAdd.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
countLayout.addView(buttonAdd);
horizontalLayout.addView(titleLayout);
horizontalLayout.addView(countLayout);
What you can do is to create separate LinearLayout.LayoutParams for the edittext and add addTextChangedListener to that edit text and on onTextChanged
write switch statement to increase the weight of the edittext on increase of length of the input number.
Is there a way to add a drawable in any position within a text view programmatically without having to position it on a particular side of a text view? The following code works when using unicode character but I want to try the same with a vector drawable.
textView.text = getString(R.string.app_settings) + " \u2794 " + getString(R.string.display)
For me, ImageSpan works.
You can put a delimiter and replace it with the drawable. I used a google icon in this example
:
Code with delimiter replacement:
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.google_icon);
drawable.setBounds(0, 0, 100,100);
String text = " Google %google_icon% icon";
String delimiter = "%google_icon%";
int icon_index = text.indexOf("%google_icon%");
text = text.replace(delimiter," ");
Spannable span = new SpannableString(text);
ImageSpan image = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, icon_index, icon_index+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(span);
Or, you can place the drawable on any index like:
span.setSpan(image, start_index, end_index, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
PS: I used Display1 in text appearance. You need to change drawable bounds according to your own needs.
How can I put that little number in the same column (textView) or how can I add two of them .
I have tried linearlayout horizantal but it did not worked How I want that text view to be look like;
ı want to add number like this image
//this my textView for adding to rows
txtColumnArray[i] = new TextView(this);
txtColumnArray[i].setTextColor(Color.BLUE);
txtColumnArray[i].setAllCaps(true);
txtColumnArray[i].setGravity(Gravity.CENTER);
txtColumnArray[i].setTypeface(Typeface.DEFAULT.DEFAULT_BOLD);
txtColumnArray[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
// my last try is spanable but it is not working , I thought it may be
// coz of textView properties but couldnt find it.
spanableForQuestionNumbers[questionNumbersCount] = new SpannableString(questionNumbersCount + " " + LetterFormat[count]);
spanableForQuestionNumbers[questionNumbersCount].setSpan(new SuperscriptSpan(), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txtColumnArray[i].setText(spanableForQuestionNumbers[questionNumbersCount]);
trCrosswordRow.addView(txtColumnArray[i]);
I am a newbie to Android Programming. I want a table layout that has 3 rows and 3 columns(may be I will dynamically change these numbers), with each table row having a relative layout inside it. This is similar like the view shown in Google Play Store which shows the new arrivals on Play etc.. I have tried using buttons to achieve this. But I want to inflate an xml file, having Relative Layout as the root with an ImageView and TextView. Is this possible? Or what is the best way to acheive this type of layout? Also, how to find the imageview and textview of the inflated layout so that I can programatically add properties to it. This is what I have tried.
private void populateCategoryGrid() {
TableLayout categoriesLayout;
TableRow row;
Button btn_category; int category_count = 12;
int NUM_ROW = category_count/3;
int NUM_COL = category_count/4;
for (int r=0;r<NUM_ROW;r++) {
row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f
));
categoriesLayout.addView(row);
for (int c = 0; c <NUM_COL; c++) {
btn_category = new Button(this);
btn_category.setId(c);
btn_category.setPadding(0, 0, 0, 0);
btn_category.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
1.0f
));
row.addView(btn_category);
}
}
}
You pretty much did all the work by looping and inflating buttons into rows. Few adjustments and you're done:
private void populateCategoryGrid() {
TableLayout categoriesLayout;
int category_count = 12;
int NUM_ROW = category_count/3;
int NUM_COL = category_count/4;
LayoutInflater inflater = LayoutInflater.from(this);
// Add rows
for (int r=0;r<NUM_ROW;r++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f
));
// Add inflated layouts
for (int c = 0; c <NUM_COL; c++) {
// Inflate layout
RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.rel_layout, null);
// Modify inflated layout
ImageView img = (ImageView) rl.findViewById(R.id.img);
img.setBackgroundColor(Color.RED);
TextView tv = (TextView) rl.findViewById(R.id.text);
tv.setText("Some text");
// Add the modified layout to the row
row.addView(rl);
}
// Add row to the table
categoriesLayout.addView(row);
}
}
A few notes:
You can use a local variable for each TableRow since once the row is done, you won't change anything inside of it
LayoutInflater.inflate returns the root view, in which you can findViewById any views located inside of it
I am trying to add multiple components into linear layout programatically. Here are the codes:
private View createCalloutView(Graphic graphic) {
LinearLayout ret = new LinearLayout(this);
ret.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView reportContent = new TextView(this);
reportContent.setText(eventName + "\n" + eventBy + "\n" + eventAddress + "\n" + eventDesc
+ "\n" + eventDate + "\n" + eventTime);
reportContent.setTextColor(Color.BLACK);
reportContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
reportContent.setPadding(1, 0, 1, 0);
Button viewDtlEventBtn = new Button(this);
viewDtlEventBtn.setText("View details");
viewDtlEventBtn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ret.addView(reportContent);
ret.addView(viewDtlEventBtn);
return ret;
}
With these codes, I only manged to see the textview and my button is missing. Any ideas? Thanks in advance.
that depends on how do you want to arrange the items in the LinearLayout. If you want to arrange the button next to the TextView, then, probably, the button width should be WRAP_CONTENT instead of FILL_PARENT. If you want to show the button under the TextView, then, your LinearLayout should have vertical as orientation (default is horizontal). Imo, the easiest way is to have your layout defined in a xml file. At least you can see the output at compile time, and use an inflater to retrieve the View's object at run time
The default orientation of linear layout is horizontal. You need to set the orientation first.
LinearLayout ret = new LinearLayout(this);
ret.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ret.setOrientation(LinearLayout.VERTICAL);
This will solve your problem of missing button.
You have forgot to set Layout Orientation for Linear Layout, just set it as below:
ret.setOrientation(LinearLayout.VERTICAL);