How create TextView programatically with this specifics Attributes? - java

I'm currently creating TextView like that:
LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.ll1);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int l=0; l<4; l++)
{
pairs[l] = new TextView(context);
pairs[l].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText("asd");
myLayout.addView(pairs[l]);
}
Now I want set this attribute to all of this TextView:
FontFamily: cursive
SetTextSize not sp but dp (RESOLVED)
SetGravity: central_horizontal (RESOLVED)
I couldn't find a way for set those attribute when I create a TextView programatically, How can I do that?

Text size in dp can be set using setTextSize(TypedValue.COMPLEX_UNIT_DIP, <float>) - see documentation [here](https://developer.android.com/reference/android/widget/TextView.html#setTextSize(int, float)).
As for the font family, I'm afraid I don't know - hopefully someone else can help you with this :)

In order to change the font family of a TextView use setTypeface
for instance:
Typeface tf = Typeface.create("cursive", Typeface.NORMAL);
for(int l=0; l<4; l++)
{
pairs[l] = new TextView(context);
pairs[l].setTypeface(tf);
...
}
Also, this may interest: How to change fontFamily of TextView in Android

You can set the font using Typeface object.
TextView tv = new TextView(context);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/filename.ttf");
tv.setTypeface(face);
Put the font in the assets folder of your project.

Related

How to change LinearLayout programmaticaly to fit numbers entered to EditText without moving other parts of layout?

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.

get and set layout_margin java

I to want get and set margin of my LinearLayout from java. I do not want set like right,left, top, bottom etc. I just want set simple margin from all side. I know I can do it via XML but I do know how can I do it via java.
I have done via xml is like below
android:layout_margin="20dp"
Anyone can please suggest me how can I do it via java?
To set margin to the view you can use this code:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
To get the margin of view use this code
View view = findViewById(...) //or however you need it
LayoutParams lp = (LayoutParams) view.getLayoutParams();
// margins are accessible via
lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;
// perhaps ViewGroup.MarginLayoutParams will work for you. It's a base class for //other LayoutParams.
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
Note: Sorry for using snippet...
It will work like a charm...
You can use following code to do so.
LinearLayoutview ll= findViewById(R.id.linearLayout); //or however you need it
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
margins are accessible via
lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;
Now you can use the code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(20,20,20,20);
ll.setLayoutParams(params);
Need use type of: MarginLayoutParams
Try this:
MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;
Code Example for MarginLayoutParams:
http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams

How to dynamically insert a relative layout into a table layout

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

Android TextView horizontal scroll does not work

I read some thread in this topic, but I have not found the solution.
I would like to use the scrollHorizontally attribute on a TextView, but from Java code.
Here's what I tried:
nameTextView = new TextView(context);
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
nameTextView.setId(R.id.header_text_id);
nameTextView.setGravity(Gravity.CENTER | Gravity.LEFT);
nameTextView.setSingleLine();
nameTextView.setHorizontallyScrolling(true);
nameTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
nameTextView.setFocusableInTouchMode(true);
nameTextView.setMarqueeRepeatLimit(-1);
nameTextView.setFocusable(true);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/robotocondensed.ttf");
nameTextView.setTypeface(tf);
nameTextView.setTextColor(getResources().getColor(android.R.color.white));
I set the text later. The text does not scroll, what can be the problem here?
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
Then set:
yourTextView.setMovementMethod(new ScrollingMovementMethod())
excelent article about how to make an horizontal scrollable textview in android, It works!!!
http://gabrielbl.com/2013/08/09/android-auto-horizontal-scrolling-marquee-textview/

TableRow with two TextView in separate line

I have the following code:
TextView name = new TextView(this);
name.setText(venues.get(j).name);
name.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView address = new TextView(this);
address.setText(venues.get(j).getFullAddress());
address.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(name);
tr.addView(address);
And now it gives me a layout like this:
I want the TextView to be in separate lines. How do I do this?
First of all, don't use LayoutParams for Views that are going into a TableRow... use TableRow.LayoutParams.
Second (pls tell me if I misunderstood your question), if you want your edittexts to show up on different lines... that means you want them on different rows. In other words, don't put them into the same TableRow. You can put them into separate TableRows or use a LinearLayout.
Try adding addr to another TableRow.
TextView name = new TextView(this);
TextView addr = new TextView(this);
name.setText("Name");
addr.setText("Addr");
TableRow tableRow1 = (TableRow) findViewById(R.id.tableRow1);
TableRow tableRow2 = (TableRow) findViewById(R.id.tableRow2);
tableRow1.addView(name);
tableRow2.addView(addr);

Categories