I have a RelativeLayout called current_layout which I place my views on. When I attempt to addView(TextView) , nothing is displayed. However when adding an ImageView, it works just fine. Why is my TextView not displaying?
public static void draw_shard(int x, int y, int amount_collected){//X and Y are GAMESURFACE values. Needs to increment by gamesurface y.
ImageView shard = create_iv(); // Creates a new instance of an ImageView (parameter is the context of MainActivity)
shard.setBackgroundDrawable(shard_icon);
shard.setX(x);
shard.setY(y+ImageLoader.get_score_bar_height());
TextView tv = new TextView(MainActivity.current_context);
tv.setX(shard.getX() + shard.getWidth());
tv.setY(shard.getY());
tv.setTypeface(Variables.joystix);
tv.setTextSize(shard.getHeight());
tv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
tv.setText("+" + amount_collected);
tv.setTextColor(Color.WHITE);
current_layout.addView(shard);
current_layout.addView(tv);
}
I am adding the TextView on top of a black background also.
The problem was with shard.getWidth() and shard.getHeight() , which were returning 0.
An alternate and easy way to do that is:
Add the TextView in the layout file and set its visibility to gone, and when you need to show the TextView, just change the visibility of that TextView.
Sample code for XML file:
<TextView
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout-width="wrap_content"
android:visibility="gone">
<!-- Add other attributes too -->
And when you need that TextView, add this line of code:
findViewById("textview").setVisibility(View.VISIBLE);
findViewById("textview").setText("" + amount_collected);
// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create TextView
TextView product = new TextView(this);
product.setText(" Product");
ll.addView(product);
Please try it.
But why are you adding TextView using java code?
You can easily do it in XML.
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="#color/colorPrimary"
android:textColor="#color/colorPrimary" />
It will help you to understand
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
textView.setText("Your Text that you want to add");
linearLayout.addView(textView);
Thanks
Related
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.
I'm trying to put some TextView programmatically in my activity. The problem is that I cannot set their margins (top margin), so that there is some space between them.
The XML layout structure is the following:
<ScrollView>
<RelativeLayout_1>
<RelativeLayout_2>
<TextView/>
<EditText/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I omitted all the unnecessary information.
Then in my Activity I do
mLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativeParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.handicap_layout);
relativeParams.addRule(RelativeLayout.BELOW, R.id.handicap_layout);
TextView[] texts = new TextView[6];
for(int i=0; i<6; i++) {
texts[i] = new TextView(MyActivity.this);
texts[i].setText("Text " + (i+1));
texts[i].setTextSize(20);
relativeParams.setMargins(0, 10+(10*i), 0, 0); /* trying to increase the margin */
texts[i].setLayoutParams(relativeParams);
mLayout.addView(texts[i]);
}
where mLayout refers to RelativeLayout_1 and handicap_layout to RelativeLayout_2.
The problem is that the margins do not increase and the TextView are showed one above the other.
Has anyone a solution? Thank you!
EDIT: SOLVED
Ok, I managed to solve the problem: the LayoutParams have to be declared inside the for cycle.
Thanks.
Take one textview xml file. In that take margins. Once that is done inflate that textview.
TextView itemview = (TextView)getLayoutInflater().inflate(R.layout.equipment_item, null);
once that is done set your text to that layout.
I think you alredy find the relative layout right. Then add that itemview to relative layout like below.
equipmentdetails_layout.addView(itemview);
Then you can get the margins.
I'm using a ScrollView to show all the products that users add to his shopping cart.
I create a scroll view and, into it, i create a linear layout 'intern'.
Then, i build every single row in loop.
Here's the XML
<ScrollView
android:id="#+id/scroll"
android:layout_width="300dp"
android:layout_height="230dp"
android:layout_marginLeft="10dp" >
<LinearLayout
android:id="#+id/intern"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
</ScrollView>
Here's the Java code.
for(Carrello cns : carrello){
LinearLayout prodotto = new LinearLayout(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(/**/LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
prodotto.setLayoutParams(lp);
//Product name
TextView nome_p = new TextView(context);
nome_p.setWidth(dpToPx(115));
nome_p.setTextColor(getResources().getColor(R.color.grey));
nome_p.setText(cns.getNomeProdotto()); /**/
//Counts
TextView quantita = new TextView(context);
quantita.setWidth(dpToPx(80));
quantita.setGravity(Gravity.CENTER);
quantita.setTextColor(getResources().getColor(R.color.grey));
quantita.setTextAppearance(context, R.style.quantita);
quantita.setText(cns.getQuantita());
//Price
TextView prezzo = new TextView(context);
prezzo.setWidth(dpToPx(70));
prezzo.setPadding(dpToPx(5), 0, 0, 0);
prezzo.setTextColor(getResources().getColor(R.color.grey));
prezzo.setTextAppearance(context, R.style.prezzo);
prezzo.setText("€"+cns.getCosto());
//Cancella
Button canc = new Button(context);
LinearLayout.LayoutParams lp_btn = new LinearLayout.LayoutParams(/*LinearLayout.LayoutParams.WRAP_CONTENT*/40,40/*LinearLayout.LayoutParams.WRAP_CONTENT*/);
lp_btn.setMargins(0, dpToPx(10), 0, 0);
canc.setLayoutParams(lp_btn);
canc.setBackground(getResources().getDrawable(R.drawable.btn_canc));
canc.setId(Integer.parseInt(cns.getIDprodotto()));/**/
Log.d("ID-CANC",String.valueOf(canc.getId()));
//create the line
prodotto.addView(nome_p);
prodotto.addView(quantita);
prodotto.addView(prezzo);
prodotto.addView(canc);
intern.addView(prodotto);
i++;
}//end for
Then, add intern to the scroll view:
scroll.addView(intern);
But when i run the emulator, logCat says that "ScrollView can host only one direct child", and the app crashes.
Don't use scroll.addView(intern);,
because you have already defined child in SCrollView in your layout.xml
Instead try as below.
LinearLayout intern = (LinearLayout) findViewById(R.id.intern);
then use your for loop to add views in intern
That,s it...
No need to call addview for scroll
Hope this helps
The ScrollView already has a child defined in the layout file.
scroll.addView(intern); will cause error.
Just add
scroll=(ScrollView) findViewById(R.id.intern) before the loop and remove scroll.addView(intern);.
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.
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>