Why is programatic layout invisible? - android

my TextView that I add to my LinearLayout is not visible...why ?
layoutVenues = (LinearLayout) findViewById(R.id.layoutv);
layoutVenues.addView(genTextView(v.getName()));
layoutVenues.addView(genLineView());
and the genTextView Method:
public TextView genTextView(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextColor(Color.BLACK);
return tv;
}

You need to set layout parameters otherwise you will not have a proper layout
public TextView genTextView(String text) {
TextView tv = new TextView(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutPararms.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(text);
tv.setTextColor(Color.BLACK);
return tv;
}

Try to add visiblity to your view. .setVisibility(View.VISIBLE);
or place it in your xml and instantiating in the code will also be good idea.

I build most of my view hierarchies for Android using XML layout files, so I'm not an expert on programmatically assembling view hierarchies. However, one thing that jumps out at me is that you don't appear to set any layout parameters on the TextView that is returned by genTextView(). Also, take a look at the layout parameters of the LinearLayout in your XML file and make sure that it is actually getting assigned screen real estate.

The default background is black I believe? So you have black text on a black background. Its probably not that easy though :P Might want to post the xml where the linearlayout is defined.

Related

To add textview dynamic at run time Android

I want to add Text-view at run time on Android to display the contents of my database on a activity,which can change every time.
Please Help
You could create a TextView dynamically like this for example:
//create a TextView with Layout parameters according to your needs
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//if your parent Layout is relativeLayout, just change the word LinearLayout with RelativeLayout
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
//get the parent layout for your new TextView and add the new TextView to it
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
linearLayout.addView(tv);
And in the tv.setText(...); line you could set the text, that your got from your database.

Android programmatic relative layout: BELOW rule not working?

I want to create a layout (see class RosterPlayerView below) that comprises an image with text below it and then instantiate that view multiple times in a relative layout. I used relative layout instead of linear as the layout will become more complex.
When I first ran the code below (but without the setId calls) the text appeared above the image. Thanks to this stack overflow article I discovered that relative layout needs unique widget ids to work. But when I added the setId() calls the text view is not displayed at all.
What am I doing wrong?
public class RosterPlayerView extends RelativeLayout {
ImageView imageView;
TextView textView;
static int layoutId = 100;
public RosterPlayerView(Context context, int playerId, Drawable photo) {
super(context);
imageView = new ImageView(context);
textView = new TextView(context);
addView(imageView, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setId(layoutId++);
RelativeLayout.LayoutParams timeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
timeLayoutParams.addRule(RelativeLayout.BELOW, imageView.getId());
addView(textView, timeLayoutParams);
imageView.setImageDrawable(photo);
textView.setId(layoutId++);
textView.setText("0:00");
}
}
a LinearLayout would be an awful lot simpler for what you are trying to do. So would inflating an XML layout, for that matter.
Try to set the Id of you imageView before adding it to the layout.
You can also create a LinearLayout with the imageView and textView inside before adding it to the RelativeLayout

Textview style after adding dynamically

My question is how to set a textView style after adding it dynamically.
Here is the code:
LinearLayout layout = (LinearLayout)findViewById(R.id.linarLay);
TextView textView = new TextView(this);
textView.setText("TEST1");
layout.addView(textView);
I can see the text view that has been added but.. I need to style it now..
So far I tried this:
textView.setTextAppearance(getApplicationContext(),R.style.textStyle);
I tried this code after layout.addView(textView); and before it is just same doesn't change a thing..
Any idea/solution would be appreciated... Thanks
Style doesn't change because though you use the same TextView object to set style after adding it to Layout,it is not a part of layout. You have to get the View added,from layout using its id and when you change its style,it would directly be affected to your view on Layout.
Try this: (I have not tested but *should work)
LinearLayout layout = (LinearLayout)findViewById(R.id.linarLay);
TextView textView = new TextView(this);
textView.setText("TEST1");
textView.setId(999); // give some id
layout.addView(textView);
TextView tv=(TextView)findViewById(999);
tv.setTextAppearance(getApplicationContext(),R.style.textStyle);
I had a similar problem. I wanted to do the same with a button. You can set every property programmatically.
You can create a class with a set of methods like the one below:
private void setButtonStyle(Button b, String text)
{
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
b.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_button));
b.setGravity(Gravity.CENTER);
b.setText(text);
b.setLayoutParams(param);
b.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
b.setTypeface(null, Typeface.BOLD);
b.setShadowLayer(2, 1, 1, R.color.button_shadow_colour);
b.setTextColor(context.getResources().getColor(R.color.button_text_colour));
}
As you can see it is possible to set everything you need. As an example the param variable has 3 arguments in its contructor which are layout_width, layout_height and weight. So you can do the same with TextView.
deprecated since API 23:
textView.setTextAppearance(getContext(), R.style.Headline);
so choose:
textView.setTextAppearance(R.style.Headline);

setting the children of ViewFlipper at centre

I am adding dynamic text view to view flipper like below.
Everything is working perfectly, but how can i center each text view at center of View Flipper, i have looked for the gravity option but i think it doesn't support. As the text view contains different text length i want every text view to be at center of view flipper.
for(int i = 0; i < 10; i++){
TextView tv = new TextView(this);
vf.addView(tv, i);
}
Thanks
I think this may help :
ViewFlipper flipper = new ViewFlipper(this);
TextView textView = new TextView(this);
textView.setGravity(Gravity.CENTER);
textView.setText("Hello World");
flipper.addView(textView);
setContentView(flipper);
Are you talking about gravitiy we can set this way, or a layout gravity you have tried actually?
tv.setGravity(Gravity.CENTER);
Try to set ViewFlippers width in xml as
android:layout_width="wrap_content"
I had an similar problem and this worked fine.
You can do this way,
TextView tv = new TextView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.gravity = Gravity.CENTER;
tv.setLayoutParams(param);
Ok i got the answer, actually you need to add linearlayout to viewflipper then add children to linearlayout with giving gravity parameter to linear layout.

Dynamically Add A TextView - Android

How can I dynamically add a TextView to this? The commented out code doesn't work.
public class myTextSwitcher extends Activity {
private TextView myText;
public myTextSwitcher(String string){
//myText = new TextView(this);
//myText.setText("My Text");
}
}
You're creating a text view and setting its value but you're not specifying where and how it should be displayed. Your myText object needs to have a container of some sort which will make it visible.
What you're trying to do is dynamically layout a view. See here for a good starter article. From the article:
// This is where and how the view is used
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
// this part is where the containers get "wired" together
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
First of all, you shouldn't be adding it in the constructor, non-default constructors are pretty much useless for an Activity. Finally, you are correctly creating a new TextView but you are not adding it anywhere. Get ahold of some layout in your content view (probably with findViewById), and call layout.addView(myText) with it.
Did you add the your text view to the activity using setContentView(myText);
make this
myText = new TextView(this);
myText.setText("foo");
setContentView(myText);
in oncreate() method
final TextView tv1 = new TextView(this);
tv1.setText("Hii Folks");
tv1.setTextSize(14);
tv1.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout ll = (LinearLayout) findViewById(R.id.lin);
ll.addView(tv1);
Your activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
</LinearLayout>

Categories

Resources