My RelativeLayout Dynamically adds check boxes and sets text to each of them but they all overlap each other even though i'm using the RelativeLayout.BELOW parameter.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bible_progress);
Bundle progressBundle = getIntent().getExtras();
int positionBook = progressBundle.getInt("position");
if(positionBook == 0) {
for (int i = 0; i < 10; i++) {
RelativeLayout progressLayout = (RelativeLayout) findViewById(R.id.progress_layout);
CheckBox progressBox = new CheckBox(this);
progressBox.setText("Dynamic Checkbox " + i);
progressBox.setId(i + 10);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, progressBox.getId());
progressBox.setLayoutParams(params);
progressLayout.addView(progressBox);
}
}
}
Im not getting any errors so there is no stack trace.
you are providing the wrong id to the rule
params.addRule(RelativeLayout.BELOW, progressBox.getId());
this way you are providing the id of the view you just created, to the rule
Related
i am making a activity that takes int position from another activity to get a class formula that have the formula, some variables and the title , so i want to create the activity by taking that formula and setting the title , making some edit text views for variables so that the user add values for them and then i return the result by a dialogue the problem is that the views that i add programitically are not displayed, plz help
public class DisplayFomula extends AppCompatActivity {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_fomula);
DB db = new DB(this);
Bundle extra = getIntent().getExtras();
int pos = extra.getInt("id");
final Formula form = db.getFormula(pos);
RelativeLayout layout=new RelativeLayout(this);
TextView tw1 =(TextView) findViewById(R.id.textView11);
tw1.setText(form.title);
final String[] var = form.var;
int i=0;
final EditText[] ed = new EditText[50];
for(;i<var.length;i++){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
ed[i]= new EditText(this);
ed[i].setId(i);
ed[i].setHint(var[i]);
ed[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
ed[i].setLayoutParams(params);
if(i!=0) params.addRule(RelativeLayout.BELOW,ed[i-1].getId());
else params.addRule(RelativeLayout.BELOW,R.id.textView11);
layout.addView(ed[i],params);}
Button btn = new Button(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btn.setId(i);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
btn.setLayoutParams(params);
btn.setText("Go!");
layout.addView(btn,params);
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
addContentView(layout,params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double[] varin=new double[var.length];
for(int i =0;i<var.length;i++){
Double dbl = Double.parseDouble(ed[i].getText().toString());
varin[i]=dbl;
}
String res = String.valueOf(form.result(varin));
}
});
}
}
You are adding all the views to RelativeLayout layout. But i think u have'nt add layout view to any activity_display_fomula view.
get a viewgroup from activity_display_fomula and add your RelativeLayout layout to it..
public class TabelaResultados extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabela_resultados);
Bundle in = getIntent().getExtras();
int numero = Integer.parseInt(in.getString("Numero"));
if(in!=null) {
for (int i = 0; i < numero; i++) {
String tv1 = in.getString("Nome"+(i+1));
TextView nome = new TextView(this);
nome.setText(tv1);
nome.setId(i+1);
Button passou=new Button(this);
passou.setText("LOL");
passou.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
RelativeLayout layout= (RelativeLayout) findViewById(R.id.tabelaLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(i == 0) {
params.addRule(RelativeLayout.BELOW, findViewById(R.id.tabelaLayout).getId());
nome.setPadding(0,0,0,30);
layout.addView(nome, params);
params2.addRule(RelativeLayout.RIGHT_OF,i+1);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,i+1);
layout.addView(passou,params2);
} else {
params.addRule(RelativeLayout.BELOW, i);
layout.addView(nome, params);
params2.addRule(RelativeLayout.RIGHT_OF,i+1);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,i+1);
layout.addView(passou,params2);
}
}
}
}
}
How can I make the button smaller , to wrap content?
I've tried several solutions and none of them worked .
I'm kind of a noob in android developing ...
I want to do it dinamically because the number of buttons depends on the number os persons I add in the previous activity.
It stays like This
EDIT: It's a relative layout.
Try replacing the original line with this:
passou.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
If this doesn't work I'm not sure what your problem is.
I'm here today for my soundboard app ! son to make it simple i have a G_Son object which is the controller of the "Son" model. i get the list of sound from my database (everything is fine until here) but then when I dynamically try to create ImageButtons and add them on my activity (manageLayout() ), I have absolutely nothing appearing on my activity ! not even a single button. So if you have any Idea, or want to give me a hand, I'm aware of any suggestion
private G_Son gson;
private OurNiceSoundPlayer soundPlayer;
private List<Son> sons;
RelativeLayout gameBoard;
private Son selectedSound;
private View.OnClickListener mSoundOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlayer.setSound(sons.get(Integer.parseInt(v.getTag().toString())));
Log.i("Board", "Clicked on ImgButton ->" + v.getTag());
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board);
gson = G_Son.getInstance();
sons = gson.getSons(getApplicationContext());
soundPlayer = new OurNiceSoundPlayer(getApplicationContext());
gameBoard = (RelativeLayout) findViewById(R.id.soundboard);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
manageLayout();
gameBoard.invalidate();
}
private void manageLayout() {
if (sons.size()>0)
{
int rawNbr = (int) Math.ceil((double) sons.size() / 3);
int currentSon = 0;
Son displayed = sons.get(currentSon);
for (int i = 0; i < rawNbr; i++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++)
{
ImageButton btnTag = new ImageButton(this);
if (displayed.getIsPerso()) {
File imgFile = new File(displayed.getPathImage());
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
btnTag.setImageBitmap(myBitmap);
} else {
btnTag.setImageResource(R.drawable.defaultimage);
}
}
else
{
int id = getResources().getIdentifier("com.example.m.sbst:drawable/" + displayed.getPathImage(), null, null);
btnTag.setImageResource(id);
}
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setOnClickListener(mSoundOnClickListener);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setTag(currentSon);
btnTag.setId(i);
row.addView(btnTag);
currentSon++;
if (currentSon>=sons.size())
{
break;
}
else
{
displayed = sons.get(currentSon);
}
}
gameBoard.addView(row);
}
}
}
Forgot to mention a size, moreover, after months of learning android, I should advice people to use a grid view to do so, it allows with custom layout to do event better than creating your own layout with dynamic image buttons
I have a weird problem. I derived MessageRow from TableRow. I populated a table with one TableRow (Header) and 100 MessageRows. But when I add the table to the HorizontalScroll view only the Messagerows are showing.
If I inspect the table in the debugger, all the rows are there, including the Header, with the right children and text.
This is the simplified code:
public class MessageRow extends TableRow {
public TextView tvData1;
public TextView tvData2;
public TextView tvData3;
public MessageRow(Context context) {
this(context, null);
}
public MessageRow(Context context, AttributeSet attrs) {
super(context, attrs);
tvData1 = new TextView(context);
tvData2 = new TextView(context);
tvData3 = new TextView(context);
}
public void setData(String data1, String data2, String data3) {
tvData1.setText(data1);
tvData2.setText(data2);
tvData3.setText(data3);
addView(tvData1);
addView(tvData2);
addView(tvData3);
}
}
The activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
mTable = new TableLayout(this);
HorizontalScrollView hview = (HorizontalScrollView) findViewById(R.id.hscroll);
mTable.setBackgroundColor(Color.WHITE);
populate(mTable);
hview.addView(mTable);
}
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
mHeader.addView(getColumnHeader("Data#1"));
mHeader.addView(getColumnHeader("Data#2"));
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
public void populate(TableLayout tl) {
setHeader(tl);
for(int i = 0; i < 100; i++) {
MessageRow mr = new MessageRow(getContext());
mr.setMessage("xxx"+i,"yyy"+i,"zzz"+i);
mr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mr);
}
}
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Now, I tried bypassing the getColumnHeader() function like this:
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
//mHeader.addView(getColumnHeader("Data#1"));
--> Button bt1 = new Button(getContext());
--> bt1.setText("Data#1");
--> mHeader.addView(bt1);
//mHeader.addView(getColumnHeader("Data#2"));
--> Button bt2 = getColumnHeader("Data#2");
--> mHeader.addView(bt2);
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
and then bt1 (Data#1) is displayed!!! but not Data#2 or Data#3. This hints that somehow the function getColumnHeader() is bad. But not acording to the debugger, when I inspect bt1 and bt2 they seem good, I cannot detect what is the difference between the manually instantiated button bt1 and the returned button bt2.
Side note: I don't know if this is the best way, but to compare the two variables I just right-clicked and Copy Variable, then pasted it to notepad++ in two different files (one for bt1 and one for bt2) and used the compare plugin.
Some more info. I managed to display the buttons, further debug showed that setting the Layout params for each button in the function getColumnHeader() caused the problem. Still I don't know why.
The button member mLayoutParams being null is OK, but when created (.setLAyoutParams()) and assigned values (for example width and height) the suddenly the button is not displayed.
This is the patch,... until I figure out why.
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
//LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//params.rightMargin = params.leftMargin = 2;
//bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Last piece of the puzzle.
I tried many variations and finally it worked!
Replacing LayoutParams with TableRow.LayoutParams did the trick.
Their structure is very similar, probably even TableRow.LayoutParams is derived from LayoutParams.
I will leave the mystery here, until some expert sheds light on why this happened.
The final code:
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
This problem is pretty simple, I think.
The thing is: My TextView (Which is in a TableRow which is in a TableLayout) gets out of the visible screen.
A screenshot should clarify:
I tried
tvValue.setLinksClickable(true);
tvValue.setEllipsize(TruncateAt.MIDDLE);
1) No truncation is applied
2) Obviously, they show links, but they are not clickable!
As its a highly dynamic thing i need to do all initialization in code.
So, here's the code:
public class StartUp extends Activity {
private TableLayout mTableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context appContext = getApplicationContext();
mTableLayout = new TableLayout(appContext);
mTableLayout.setBackgroundColor(0xffffffff);
String[] h = new String[] { "email", "facebook", "youtube" };
String[] v = new String[] { "lol#xd.de", "http://www.facebook.com/010101010101010", "http://www.youtube.com/lalala" };
LayoutParams rowLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < h.length; ++i) {
TableRow row = new TableRow(appContext);
row.setLayoutParams(rowLayoutParams);
TextView tvHead = new TextView(appContext);
tvHead.setTextColor(0xff336699);
tvHead.setPadding(0, 0, 10, 0);
tvHead.setText(h[i]);
TextView tvValue = new TextView(appContext);
tvValue.setTextColor(0xff191919);
tvValue.setLinksClickable(true);
tvValue.setText(v[i]);
tvValue.setEllipsize(TruncateAt.MIDDLE);
row.addView(tvHead);
row.addView(tvValue);
mTableLayout.addView(row);
}
setContentView(mTableLayout);
}
}
Have you tried setColumnShrinkable()?
http://developer.android.com/reference/android/widget/TableLayout.html#setColumnShrinkable%28int,%20boolean%29