I beg some leniency here, I'm just starting with the Android SDK tutorials and I'm attempting something out of interest that's not in the tutorial itself, but I would hope would be easy.
I am trying to center a TextView item via code horizontally and vertically (I can do it in XML just fine). I've seen several examples of how to do this when the parent is a table or some other object, but I hope this would be easier for me to grasp. (p.s. Feel free to correct my terminology).
Here is the example code from the tutorial / my working model:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
textView.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
setContentView(textView);
}
}
I've managed to locate the setGravity method, and I've tried to dabble in the setLayoutParams for it, but I'm not sure what the scope is for it as I can't locate what I should be importing to get the WRAP_CONTENT constant to resolve. From what I understood, centering and content_wrapping+gravity are two separate things. I'd like an example of how to do both in this case and maybe how/where I would have found the answer in the API documentation?
yourTextView.setGravity(Gravity.CENTER);
For dynamically center
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
this will work for sure..
RelativeLayout layout = new RelativeLayout(R.layout.your_layour);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
layout.addView(textView);
setcontentView(layout);
TextView text = new TextView(this);
text.setGravity(Gravity.CENTER);
and
text.setGravity(Gravity.TOP);
and
text.setGravity(Gravity.BOTTOM);
and
text.setGravity(Gravity.LEFT);
and
text.setGravity(Gravity.RIGHT);
and
text.setGravity(Gravity.CENTER_VERTICAL);
and
text.setGravity(Gravity.CENTER_HORIZONTAL);
And More Also Avaliable
Try adding the following code for applying the layout params to the TextView
LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);
if your text size is small, you should make the width of your text view to be "fill_parent". After that, you can set your TextView Gravity to center :
TextView textView = new TextView(this);
textView.setText(message);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
You can use the following to programmatically center TextView text in Kotlin:
textview.gravity = Gravity.CENTER
try this method
public void centerTextView(LinearLayout linearLayout) {
TextView textView = new TextView(context);
textView.setText(context.getString(R.string.no_records));
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(18.0f);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
linearLayout.addView(textView);
}
These two need to go together for it to work.
Been scratching my head for a while.
numberView.textAlignment = View.TEXT_ALIGNMENT_CENTER
numberView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
Related
Hello I am trying to make 2 TextViews apear above each other through code.
My question is:
How can I stack them over each other?
Sample Code
private TextView DurrationView;
private TextView DurrationViewoverall;
DurrationView = new TextView(getContext());
DurrationView.setText("");
addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("");
addView(DurrationViewoverall);
I am trying to have DurrationView appear above DurrationViewoverall.
The class they are in extends Linear Layout.
Add LayoutParams to the textview and make your linear layout as orientation vertical
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
DurrationView = new TextView(getContext());
LinearLayout.LayoutParams durrationViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationView.setLayoutParams(durrationViewParams);
DurrationView.setText("Text1");
linearLayout.addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
LinearLayout.LayoutParams durrationViewoverallParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationViewoverall.setLayoutParams(durrationViewoverallParams);
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("Text2");
linearLayout.addView(DurrationViewoverall);
addView(linearLayout);
Check that the LinearLayout is set to have a vertical orientation. If that is set properly, you may need to set the LayoutParams of the TextViews programatically as well.
I have a view, where main heading and its associated sub heading but i don't know length of string, so, i want to create button based on length of string and if not capable on same line then display next line like a picture.
Could you please suggest me, how to display such button and its heading in textview or button in android, because i am new, it sucks me since 1 day, i couldnot get any solution.
Thank you in advance.
You can achieve this by using FolowLayout. Refer the following link for details
FlowLayout
Please check bellow code for creating textview and a button dynamically and show button below to textview. I think it's helpful to you.
RelativeLayout ll = (RelativeLayout) findViewById(R.id.rl);
TextView tv = new TextView(this);
tv.setText("My Text fgfdgd fdgdfg dgdfg dffgdf dfgdfg My Text fgfdgd fdgdfg dgdfg dffgdf dfgdfg My Text fgfdgd fdgdfg dgdfg dffgdf dfgdfg");
tv.setId(1);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ll.addView(tv, lp);
Button myButton = new Button(this);
myButton.setText("Push Me");
LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp1.addRule(RelativeLayout.BELOW, tv.getId());
ll.addView(myButton, lp1);
I have 2 TextViews and I want to add those to LinearLayout, but when I ran the project, only one TextView appreared.
Here's my code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Thank you, Jesus!");
textView.setTextColor(Color.BLACK);
TextView textView2 = new TextView(this);
textView.setText("Dont give up on me!");
textView.setTextColor(Color.BLACK);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.WHITE);
layout.addView(textView);
layout.addView(textView2);
setContentView(layout);
}
}
After running, textView2 was the only view present in the LinearLayout.
Can someone explain to me what was going on?
Use textView2 for calling setText and setTextColor method for textView2 because currently you are using textView :
TextView textView2 = new TextView(this);
textView2.setText("Dont give up on me!");
textView2.setTextColor(Color.BLACK);
Suggestion also set height/width for all views by calling setLayoutParams method
Another Suggestion : Add Orientation to Linear layout by using: layout.setOrientation(LinearLayout.VERTICAL);
I have done some research, but the answer i found does not work for me. Here is some part of my code. the R.id.relative is the id of the relativelayout in the xml file
RelativeLayout RL = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title = new TextView(this);
title.setText(" History ");
title.setId(99099);
title.setTextSize(30);
title.setGravity(Gravity.CENTER);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
title.setLayoutParams(params);
RL.addView(title);
RelativeLayout.LayoutParams test_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test = new Button(this);
test.setText(" Back ");
test_params.addRule(RelativeLayout.BELOW,99099);
test.setId(199291);
test.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params);
RL.addView(test);
RelativeLayout.LayoutParams test_params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test2 = new Button(this);
test2.setText(" Clear ");
test_params2.addRule(RelativeLayout.BELOW,test.getId());
test2.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params2);
RL.addView(test2);
all 3 items did show up, but they stack together. I can't get them below another.
Could anyone help ?
From what I've been able to find out, you have to add the view using LayoutParams. Here's an example:
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
And to relatively position your items programmatically you have to assign ids to them, this stops them from 'overlapping'.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Change test.setLayoutParams(test_params2); to test2.setLayoutParams(test_params2);
Explanation: You have inadvertently set the layout params of test a second time, with params that instruct it to be below itself, which I guess just puts it top-left (default placement in a RelativeLayout). Since you never give test2 any layout params, it also gets default placement. So everything is at the top and thus appear atop each other.
Incidentally, if you just want them arranged linearly, why not use a vertical LinearLayout?
The rule you have added is causing your view to be stacked together.If you need to add it should be used by the following rule.
test_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
RL.addView(yourAdView, rLParams);
same goes for the each cases.try to run.It will solve your problem
I need to overlap an image view with textview. And this combined view will be repeated 100 times in a LinearLayout. I was thinking of using FrameLayout in LinearLayout and Repeating the FrameLayout in LinearLayout 100 times when FrameLayout holds the imageview and textview overlapped.
Need to do this programatically not from xml file.
I added the image and textview to framelayout first then tried to add the framelayout to linearlayout. But it says : the specified child has already a parent.. so not working. Can you please show me in code? Thanks for your help.
it is going to be like this, but need to be done programmaticaly
---linear layout--------------
------------------------------
|frame layout----------------|
||txt view on top of img view|
------------------------------
frame layout will be repeated|
---/end of linear layout------
Also here is the separated code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout dynamicview = (LinearLayout) findViewById(R.id.main_layout);
FrameLayout barFrameLayout = new FrameLayout(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
Gravity.CENTER);
barFrameLayout.setLayoutParams(params);
LinearLayout.LayoutParams slparams1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 65; i <= 75; i++) {
TextView catTV = new TextView(this);
catTV.setLayoutParams(slparams1);
catTV.setText("===" + Character.toString((char) i) + "===");
catTV.setTextSize(32);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
iv.setLayoutParams(slparams1);
barFrameLayout.addView(catTV);
barFrameLayout.addView(iv);
dynamicview.addView(barFrameLayout);
}
}
Here is the code to demonstrate what you are trying to achieve. I have used RelativeLayout, which is very flexible, you can position the elements easily relative to others.( if you need to change to FrameLayout you can change ).
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ExampleLayout extends LinearLayout{
public ExampleLayout(Context context,AttributeSet attrs){
super(context,attrs);
for(int i =0; i< 100; i++){
RelativeLayout childLayout = new RelativeLayout(context);
ImageView img = new ImageView(context);
TextView text = new TextView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
childLayout.addView(img, params);
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
childLayout.addView(text, params);
LinearLayout.LayoutParams parentParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.addView(childLayout,parentParams);
}
}
}
You can then use the ExampleLayout class to add it to any of the layout.xml file.
FrameLayout is designed to block out an area on the screen to display
a single item
(source: http://developer.android.com/reference/android/widget/FrameLayout.html).
Anyway, you have to create new FrameLayouts not use the same.
When you're doing:
barFrameLayout.addView(catTV);
barFrameLayout.addView(iv);
dynamicview.addView(barFrameLayout);
you're always adding these new objects (catTV and iv) to the same instance of a FrameLayout (barFrameLayout).
I don't think that's what you wanted to do.