So I want to center a text View inside a linear layout where I have 2 other objects that populate the linear layout. I have a imageView and another textView. How would I go about centering the textView so the text is in the middle of the screen? This is what I have so far.
View linearLayout = findViewById(R.id.rockLayout);
ImageView mineralPicture = new ImageView(this);
TextView mineralName = new TextView(this);
TextView mineralProperties = new TextView(this);
mineralProperties.setText("The properties are: " + Statics.rockTable.get(rockName).getDistinctProp());
mineralProperties.setId(2);
mineralName.setText("This mineral is: " + rockName);
mineralName.setGravity(Gravity.CENTER);
mineralName.setId(1);
mineralName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralPicture.setId(3);
mineralPicture.setImageResource(R.drawable.rocks);
mineralPicture.setAdjustViewBounds(true);
mineralPicture.setMaxHeight(100);
mineralPicture.setMaxWidth(200);
mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
((LinearLayout)linearLayout).addView(mineralName);
((LinearLayout)linearLayout).addView(mineralProperties);
((LinearLayout)linearLayout).addView(mineralPicture);
I should mention I've tried doing such things as mineralName.setGravtiy(Gravity.CENTER); and it hasn't worked.
a) let the textview match_parent on the width and use gravity center
b) on the textview layout params set the layout_gravity to center (sorry for the terms I normally do this in XML)!
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 need to programmatically align an ImageView to the bottom of a LinearLayout.
I, of course, already tried to add the ImageView as a child of the LinearLayout, but the image get shrinked (cause the LinearLayout is smaller than the image), and I need the image not be resized, only bottom-aligned.
I also tried this :
RelativeLayout main_layout = (RelativeLayout) view.findViewById(R.id.main_layout);
LinearLayout line_0_layout = (LinearLayout) view.findViewById(R.id.line_0_layout);
ImageView horse_img = new ImageView(getActivity());
horse_img.setImageResource(R.drawable.horse);
RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout_params.addRule(RelativeLayout.ALIGN_BOTTOM, line_0_layout.getId());
horse_img.setLayoutParams(layout_params);
main_layout.addView(horse_img);
but it doesn't work : the image is added to the view but not aligned to the LinearLayout.
It seems simple question, but it's not.
Thanks in advance for your help.
layout_params.addRule(RelativeLayout.BELOW, line_0_layout.getId());
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout3);
TextView tv = new TextView(this);
ImageButton imb=new ImageButton(this);
ll.addView(tv);
tv.setSingleLine();
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setHorizontallyScrolling(true);
tv.setFocusableInTouchMode(true);
tv.setText(myString1);
This is my code and I'm able to display data in textView now. But I want to put one imageButton to the left of textView so that will work as cancel strip. but I'm having a problem. How can I set imageButton left of text view programatically?
You have two solutions for your question. You can use the below code if you just want to show image on the right of textview.
TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon,0, 0,0);
or else
set orientation of your linearlayout as Horizontal and add the imageview first and then textview second. So they will be aligned as your requirement.
Below is the example.
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton imb=new ImageButton(this);
imb.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(imb);
ll.addView(tv);
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.
I have created following layout.which has two relative layouts and one scrollview ,following code contains headings of the layout.The I need to create sheet where i will use loop.
RelativeLayout inner=new RelativeLayout(this);
inner.setBackgroundColor(Color.RED);
outerLayout=new RelativeLayout(this);
ScrollView scroll=new ScrollView(this);
TextView qty=new TextView(this);
qty.setId(1111);
inner.addView(qty);
qty.setTextColor(Color.WHITE);
qty.setText("Qty");
setContentView(outerLayout);
RelativeLayout.LayoutParams p=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,30);
p.addRule(RelativeLayout.RIGHT_OF,qty.getId())
;
p.leftMargin=30;
TextView partNo=new TextView(this);
partNo.setText("Part no");
partNo.setId(2222);
partNo.setLayoutParams(p);
partNo.setTextColor(Color.WHITE);
inner.addView(partNo);
RelativeLayout.LayoutParams descLayout=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,30);
descLayout.addRule(RelativeLayout.RIGHT_OF,partNo.getId())
;
descLayout.leftMargin=30;
TextView desc=new TextView(this);
desc.setText("Description");
desc.setId(3333);
desc.setLayoutParams(descLayout);
desc.setTextColor(Color.WHITE);
inner.addView(desc);
RelativeLayout.LayoutParams commentLayout=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,30);
commentLayout.addRule(RelativeLayout.RIGHT_OF,desc.getId())
;
commentLayout.leftMargin=30;
TextView comment=new TextView(this);
comment.setText("Comments");
comment.setId(4444);
comment.setLayoutParams(commentLayout);
comment.setTextColor(Color.WHITE);
inner.addView(comment);
RelativeLayout.LayoutParams retailLayout=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,50);
retailLayout.addRule(RelativeLayout.RIGHT_OF,comment.getId())
;
retailLayout.leftMargin=30;
TextView retail=new TextView(this);
retail.setText("Retail \n Reference");
retail.setId(5555);
retail.setLayoutParams(retailLayout);
retail.setTextColor(Color.WHITE);
inner.addView(retail);
scroll.setVerticalScrollBarEnabled(true);
scroll.setHorizontalScrollBarEnabled(true);
scroll.addView(inner);
outerLayout.addView(scroll);
I tried to add scroll view to the layout but unfortunately i am unable to see the scrollview .Please help that how can i add the scrollview to above layout.
Not sure if its your problem but I don't see where you are setting layout params on the ScrollView.