How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.
Here is my generation code:
for(int i=0; i<num_enter; i++){
final int cuco = i;
LinearLayout linlay = new LinearLayout(this);
linlay.setOrientation(0);
TextView text = new TextView(this);
text.setText(name[cuco] + " ");
linlay.addView(text);
TextView num = new TextView(this);
num.setId(cuco);
num.setText("" + current[cuco]);
linlay.addView(num);
Button minus = new Button(this);
minus.setText("-");
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] - 1;
current[cuco] = current[cuco] -1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
minus.setLayoutParams(p);
linlay.addView(minus);
Button plus = new Button(this);
plus.setText("+");
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] + 1;
current[cuco] = current[cuco] + 1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
plus.setHeight(LayoutParams.WRAP_CONTENT);
plus.setWidth(50);
plus.setLayoutParams(w);
linlay.addView(plus);
main.addView(linlay);
}
Hope you find a way to set the button gravity without changing it size.
1) Get Reference of TextView say tv.
2) tv.setGravity(Gravity.CENTER);
It is not sure work with LinearLayout.. Try Relative Or Absolute
Use View.setLayoutParams(ViewGroup.LayoutParams params). Look at http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout for the LayoutParams.
Since nobody has addressed margins
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;
textView.setLayoutParams(p);
When using setLayoutParams make sure the type of layout params matches the parent view which, in this case, is LinearLayout
setGravity(Gravity.CENTER); Will set the gravity of the text within the view
Related
I don't know how to get the id of an item in a programmatically created linear layout. I want to "catch" the right id of a button and associate that with fields.
LinearLayout layout = (LinearLayout) findViewById(R.id.content_doodle_linearlayout3);
layout.setOrientation(LinearLayout.VERTICAL);
String[] data = {"1","2","3","4"};
String[] users = {"1","2","3"};
for (int i = 0; i < users.length; i++) {
row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText("Name " + (i));
text.setId(1000 + i);
row.addView(text);
int ergebnis = -1;
for (int j = 0; j < daten.length; j++) {
CheckBox btnTag = new CheckBox(this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//btnTag.setText("Button " + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
The Buttons can't not be found, because there "isn't" a (R.id.{XML-Field}).
How can i "find" the clicked button from the specific row. Do i have to code every button?
You can set a specified tag for each child view in your layout in this way:
View childView = ...;
childView.setTag("Some tag");
And then in your onClickListener:
child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Object tag = view.getTag();
if (tag instanceof String) {
String stringTag = (String) tag;
if (stringTag.equals("Some tag") {
// do something
}
}
}
});
How to add view programmatically to existing view, for example I have textview in XML and now I need to add imageview below that textview programmatically, here is what I tried,
HorizontalScrollView scroll = new HorizontalScrollView(getApplicationContext());
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
scroll.setLayoutParams(new ViewGroup.LayoutParams(imageLayout.getWidth(),
imageLayout.getHeight()));
scroll.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}but its not adding any imageview, I'm trying to do this in **AlertDialog**
ScrollView cannot have more than one child. You should wrap your layout inside of a LinearLayout and then after you can add any number of views into that LinearLayout. cheers :)
You are doing with wrong way.
Once check with below code snippet.
LinearLayout yourlayout = (LinearLayout)findViewById(R.id.layoutid);//Layout which is inside scrollview
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your LinearLayout below your TextView object
params.addRule(RelativeLayout.BELOW, TextView.getId());
// set the layoutParams on the LinearLayout
imageLayout.setLayoutParams(params);
yourlayout.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}
I solved it by adding
<HorizontalScrollView
android:id="#+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
in XML
&
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
for (int i = 0; i < 10; i++) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
imageView.setImageBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher));
imageView.setScaleType(ScaleType.FIT_XY);
layout.addView(imageView);
}
in MainActivity.java
ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
params.addRule(RelativeLayout.BELOW, imageView.getId());
I am getting a NullPointerException near the beginning of my onCreate method in my Android app. I am getting that the clock TextView variable in null, but I don't know why or how to fix it.
My code:
//Some of the variables declared outside of any method:
EditText filename;
TextView clock;
private Handler clockHandler = new Handler();
//onCreate and the onClick method that flips the view and starts the timer:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
createLayout();
flipper = (ViewFlipper) findViewById(R.id.marker_flipper);
flipper.setDisplayedChild(0);
time_marks = (LinearLayout) findViewById(R.id.time_marks);
stop_time_marks = (LinearLayout) findViewById(R.id.stop_time_marks);
times = new ArrayList<String>();
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
flipper.setDisplayedChild(1);
clock = (TextView) findViewById(R.id.clock);
mark = (Button) findViewById(R.id.mark);
stop = (Button) findViewById(R.id.stop);
startTime = System.currentTimeMillis();
clockHandler.postDelayed(updateTimerThread, 0);
}
});
createLayout Method:
#SuppressWarnings("deprecation")
public void createLayout() {
// Define Screen Size
Display display = getWindowManager().getDefaultDisplay();
// Define LayoutParams
LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams clockParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 8);
clockParams.gravity = (Gravity.CENTER);
// Root Element
ViewFlipper flipper = new ViewFlipper(this);
flipper.setLayoutParams(parentParams);
flipper.setId(R.id.marker_flipper);
// Child 0
com.jenglanddev.marktime.Scaling_Linear_Layout markerStart = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
markerStart.setLayoutParams(parentParams);
markerStart.setOrientation(LinearLayout.VERTICAL);
// Children of markerStart
Button start = new Button(this);
start.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 2));
start.setText("Start Marking");
start.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
start.setTextSize(display.getHeight() / 4);
start.setId(R.id.start);
markerStart.addView(start);
flipper.addView(markerStart);
// Child 1
com.jenglanddev.marktime.Scaling_Linear_Layout marker = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
marker.setLayoutParams(parentParams);
marker.setOrientation(LinearLayout.VERTICAL);
// Children of marker
Button mark = new Button(this);
mark.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(display.getHeight() / 10) * 4));
mark.setText("Mark Time");
mark.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
mark.setTextSize(display.getHeight() / 5);
mark.setId(R.id.mark);
marker.addView(mark);
Button stop = new Button(this);
stop.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
display.getHeight() / 10));
stop.setText("Stop Marking");
stop.setTextSize(display.getHeight() / 20);
stop.setId(R.id.stop);
marker.addView(stop);
TextView clock = new TextView(this);
clock.setLayoutParams(clockParams);
clock.setGravity(Gravity.CENTER);
clock.setText("");
clock.setTextColor(Color.BLACK);
clock.setTextSize(display.getHeight() / 10);
clock.setId(R.id.clock);
marker.addView(clock);
ScrollView sv1 = new ScrollView(this);
sv1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
LinearLayout marks = new LinearLayout(this);
marks.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
marks.setOrientation(LinearLayout.VERTICAL);
marks.setId(R.id.time_marks);
sv1.addView(marks);
marker.addView(sv1);
flipper.addView(marker);
// Child 2
com.jenglanddev.marktime.Scaling_Linear_Layout markerStop = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
markerStop.setLayoutParams(parentParams);
markerStop.setOrientation(LinearLayout.VERTICAL);
// Children of markerStop
Button save = new Button(this);
save.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 4));
save.setText("Save Times");
save.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
save.setTextSize(display.getHeight() / 8);
save.setId(R.id.save);
markerStop.addView(save);
Button reset = new Button(this);
reset.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 4));
reset.setText("Reset");
reset.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
reset.setTextSize(display.getHeight() / 8);
reset.setId(R.id.reset);
markerStop.addView(reset);
ScrollView sv2 = new ScrollView(this);
sv2.setLayoutParams(parentParams);
LinearLayout stopMarks = new LinearLayout(this);
stopMarks.setLayoutParams(parentParams);
stopMarks.setOrientation(LinearLayout.VERTICAL);
stopMarks.setId(R.id.stop_time_marks);
sv2.addView(stopMarks);
markerStop.addView(sv2);
flipper.addView(markerStop);
setContentView(flipper);
}
How can I fix this?
I want to dynamically create 10 Buttons with Margin between each Button but things I tried won't work.
Here is the code I'm using:
//Create Button
for(int i = 1; i <= 10; i++){
MarginLayoutParams params = new MarginLayoutParams(MarginLayoutParams.MATCH_PARENT, MarginLayoutParams.MATCH_PARENT);
params.setMargins(10, 0, 10, 0);
params.leftMargin = xpos;
params.topMargin = ypos;
params.width = 250;
params.height = 150;
Button btn = new Button(this);
btn.setId(i);
final int _id = btn.getId();
btn.setLayoutParams(params);
btn.setText("Button " + _id);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button clicked index =" + _id, Toast.LENGTH_LONG).show();
//Intent einauslagern = new Intent(v.getContext(), JockeyEinauslagern.class);
//startActivityForResult(einauslagern, 0);
}
});
xpos += 20;
ypos += 50;
this.addContentView(btn, params);
}
You might want to get a container in your Activity class like this. (let it be LinearLayout for example).
Your XML -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:orienation="vertical"
/>
Your Java -
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
//set your margins here
for (int i = 0; i < 10; i++) {
Button button = new Button(this);
// some stuff
container.addView(button, llp);
}
With minor modifications this should work mostly fine.
Try this
for (int i = 0; i < count; i++) {
// creates button
final Button btn = new Button(this);
btn.setLayoutParams(new ViewGroup.LayoutParams(
250,150));
btn.setPadding(0, 8, 0, 8); //or set margin if u need
btn.setTag(i);
yourContainserView.addView(channelBtn, i);
}
I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the output over the other outputs
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++) {
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
ll.addView(cb);
}
So how can i put the output on the bottom of the screen linearly.
You should use LinearLayout to automatically add one TextView after another.
Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:
public class HelloWorld extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
Random rnd = new Random();
int prevTextViewId = 0;
for(int i = 0; i < 10; i++)
{
final TextView textView = new TextView(this);
textView.setText("Text "+i);
textView.setTextColor(rnd.nextInt() | 0xff000000);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
layout.addView(textView, params);
}
}
}
You've to provide the location of your newly added view. As #Adinia said, with no position, it will be aligned to the top by default. So you can use the following code to do it with RelativeLayout;
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
If you want to show multiple textviews one after the other, then you should go with LinearLayout.
You may also add Dynamic textview to relative layout. Here with i have attached some code this may help you.
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++)
{
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
cb.setId(2000+i);
RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100);
if (i != 0 )DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1));
ll.addView(cb,TextViewLayoutParams);
}