Setting Marquee on dynamically created textview? - android

I have created marquee textview by giving properties in xml. but when I create textview on runtime with the same property then the text does not scroll. I have written below code. Has anybody face this problem?
TextView textView = new TextView(this);
textView.setText("This is marquee text it should scroll");
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setFocusableInTouchMode(true);
textView.setFreezesText(true);
textView.setSingleLine(true);
textView.setMarqueeRepeatLimit(-1);
textView.setFocusable(true);
textView.setSelected(true);

I tested your code on my Galaxy Nexus (working on Jelly Bean), and it works fine. Could you give more detail information about your test setting?
Ah, one thing I added is layout param to ensure the width of view is shorter than text itself.
textView.setLayoutParams(new ViewGroup.LayoutParams(100,
ViewGroup.LayoutParams.MATCH_PARENT));

Animation mAnimation = new TranslateAnimation(START_POS_X, END_POS_X,
START_POS_Y, END_POS_Y);
mAnimation.setDuration(TICKER_DURATION);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
TextView tvTitulo = new TextView(this);
tvTitulo.setText("Some loooooooooooooooooooooong text");
tvTitulo.setAnimation(mAnimation);
jUST CHECK this code .....

Related

programmatically setTextColor in TextView in library 22 in Android

Recently I changed a little bit of my app and for a reason I don't understand the "setTextColor" method seems that it no longer works.
In my XML, I have a listview and I programmatically add TextViews in this listView.
XML:
<LinearLayout
android:id="#+id/activity_game_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:orientation="vertical"
android:padding="7dp" >
</LinearLayout>
Java:
textView = new TextView(getContext());
textView.setText("some text");
textView.setTextSize(20f);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.BLACK);
textView.setTextAppearance(getContext(), android.R.style.TextAppearance_Medium);
addView(textView);
But this text is white whatever I do.
Why?
I did try your code and I guess the troubling factor was setTextAppearance. In fact calling setTextColor() after this call fixed the issue. The below code is working perfectly for me:
TextView textView = new TextView(this);
textView.setText("some text");
textView.setTextSize(20f);
textView.setGravity(Gravity.CENTER);
// textView.setTextColor(Color.RED);
textView.setTextAppearance(this, android.R.style.TextAppearance_Medium);
textView.setTextColor(Color.RED);
// setContentView(textView);
I don't know the true reason for this issue.
Use the following to set color of your text programmatically:
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
Starting with the support library 23 you have to use the following code, because getColor is deprecated:
textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));
See this: TextView setTextColor() not working
Use:
textView.setTextColor(Color.parseColor("#000000"));
instead of:
textView.setTextColor(Color.BLACK);
You can use:
ResourceCompact.getColor(getResources(), R.color.your_id, null);
The getResources().getColor() method is deprecated.

android textview set height does not work

I am trying to create a layout with nested linear layouts and textviews which are placed vertically in a linear layout. They are created programatically. While I can set the height of the linear layout using layout Params I am not able to do this for textview. Using Textview setLayoutParams or setHeight function provides no response. The text is always wrapped around the content (though this is no where mentioned by me in the code). Can someone help me fix this?
this code might help you:
LinearLayout.LayoutParams paramsLayoutExpire = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
TextView txtView=new TextView(this);
txtView.setLayoutParams(paramsTextViewExpire);
LayoutParams.MATCH_PARENT: Height
LayoutParams.WRAP_CONTENT: Width
I hope this might help
TextView textView = new TextView(this);
textView.setText("Hellow World");
textView.setHeight(prefered_height_in_pixel);
this.addView(textView)

Android: adding programmatically a TextView is not working

I have an issue while trying to add a TextView to a RelativeLayout. My code is this one:
TextView lectureView = new TextView(this);
LayoutParams lp=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//lp.setMargins(0,60,0,0);
lectureView.setTextColor(0xffffff);
lectureView.setBackgroundColor(0x000080);
lectureView.setText(lect.getCourse().getName());
((RelativeLayout)findViewById(R.id.mondayRelativeLayout)).addView(lectureView);
((RelativeLayout)findViewById(R.id.mondayRelativeLayout)).invalidate();
But actually it seems to be ignored. No TextView is added and the code is executed, because I have a log debug statement before those lines and it is printed out correctly. I don't see anything strange in the logs... The only thing is that the TextView is not showed.
Does anybody know which is the problem with my code?
Thanks.
You have not set your layout params for the textview. Do it while you are adding the textview to the RelativeLayout like this
((RelativeLayout)findViewById(R.id.mondayRelativeLayout)).addView(lectureView,lp);

Can I set android:layout_toLeftOf programmatically?

I have a RelativeLayout where I have one TextView and this element has its android:layout_toLeftOf attribute set to another TextView in this same layout.
Depending on the data received by the app from the server, I need to set the visibility of this one TextView to GONE and set the visibility of a ProgressBar to VISIBLE and then I need the first TextView left align with this Progress bar.
Is this possible to accomplish programmatically?
If not, could you think of a way to tackle the issue?
EDIT
Following below advice, this is how my working code looks like:
TextProgressBar txtProgressBar = (TextProgressBar) v.findViewById(R.id.progressBarPointsInProgress);
TextView offerName = (TextView) v.findViewById(R.id.textViewOfferName);
android.widget.RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)offerName.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.progressBarPointsInProgress);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
offerName.setLayoutParams(params);
addRule
RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)ProgressBar.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.youtTextView);

Can't center TextView dynamically in TableRow

I have dynamically made a TableRow with an ImageButton and a TextView.
I've set a gravity on my TableRow using TableRow.setGravity(Gravity.CENTER_VERTICAL);
Then, on my TextView, I'm using this code
TextView tv = new TextView(this);
tv.setText(functie[beschrijving]);
tv.setWidth(300);
//tv.setHeight(60);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setPadding(15, 0, 10, 0);
tr.addView(tv);
When I do this, I'm getting this result. It's not quite centered, it looks like the text right below the center. Does anyone know how to put the text completely in the center?
Hi
I think you should use the following property of Parent Layout
android:layout_centerVertical="true"
maybe this code help you.
android.widget.TableRow.LayoutParams params = new android.widget.TableRow.LayoutParams();
params.gravity = Gravity.CENTER_VERTICAL;
tr.addView(tv, params);
But I'm not sure.

Categories

Resources