I'm trying to get an arbitrary amount of buttons created on the screen, and then have a way to scroll around the buttons. (up/down)
With a bit of research, I've found that I should be using "scrollview" for what I'm doing. So I've set it up like this.. (full code at http://pastebin.com/05mtszi7)
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
row.addView(btnTag);
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
sc.addView(row);
layout.addView(sc);
Do you know what I'm doing wrong? It's probably something incredibly simple/silly, I know. I've looked all over, but I must really be overlooking something important, here. Everything runs fine, but nothing scrolls.
Thanks in advance, for the help.
Use this code. this will help you...
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
TextView menuText = new TextView(this);
menuText.setText("People");
menuText.setId(100);
menuText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(menuText);
for (int i = 0; i < 10; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnTag.setText( " " + i);
btnTag.setId(1 + (i));
row.addView(btnTag);
btnTag.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String buttonText = "Unknown Person";
Button b = (Button) findViewById(v.getId());
buttonText = (String) b.getText();
TextView tx = (TextView) findViewById(100);
tx.setText(buttonText + " pressed.");
}
});
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
sc.addView(row);
layout.addView(sc);
setContentView(layout);
Try this code:
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
sc.setFillViewport(true);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
row.addView(btnTag);
layout.addView(row);
sc.addView(layout);
This will help...
you should add buttons to layout then layout add to scrollview.scrollview only can has a child.
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
TextView menuText = new TextView(this);
menuText.setText("People");
menuText.setId(100);
menuText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(menuText);
for (int i = 0; i < users_registered; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnTag.setText(user_name[i] + " " + i);
btnTag.setId(1 + (i));
row.addView(btnTag);
btnTag.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String buttonText = "Unknown Person";
Button b = (Button) findViewById(v.getId());
buttonText = (String) b.getText();
TextView tx = (TextView) findViewById(100);
tx.setText(buttonText + " pressed.");
}
});
layout.addView(row);
}//end loop
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
sc.addView(layout);
// layout.addView(sc);
Related
I made a 10x10 board , but the visualization gives me rectangles . I was wondering how to create square , leaving the height equal to the width . Could anyone help ?
Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
chronometer = (Chronometer)findViewById(R.id.chronometer);
tableLayout = (TableLayout) findViewById(R.id.tabuleiro);
GradientDrawable gd = new GradientDrawable();
gd.setStroke(1, 0xFF000000);
gd.setColor(Color.rgb(0, 0, 128));
final GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setStroke(1, 0xFF000000);
gradientDrawable.setColor(Color.RED);
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
tableLayout = (TableLayout) findViewById(R.id.tabuleiro);
for (int i = 0; i < tamanho_tabuleiro; i++){
tableRow= new TableRow(this);
tableRow.setLayoutParams(rowParams);
for (int j = 0; j < tamanho_tabuleiro; j++) {
textView = new TextView(this);
textView.setBackgroundDrawable(gd);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundDrawable(gradientDrawable);
}
});
tableRow.addView(textView);
}
tableLayout.addView(tableRow, tableParams);
Image app:
i try to code a dynamicUI in tablelayout but layers are duplicate together.any body have sample javacode for tablelayout or can correct my codes.i write 2 layoutparams at first then tablelayout and tablerow after that imagebuttons then tablerow2 and in that imagebutton at the end i call all of imagebuttons.thanks
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LayoutParams params =
new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,1.0f);
LayoutParams superparams=
new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
TableLayout supertable=new TableLayout(this);
supertable.setLayoutParams(superparams);
supertable.setOrientation(TableLayout.VERTICAL);
TableRow table = new TableRow(this);
ImageButton ib = new ImageButton(this);
ib.setImageResource(R.drawable.one);
ib.setLayoutParams(params);
ib.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();
}
});
ImageButton ib2 = new ImageButton(this);
ib2.setImageResource(R.drawable.two);
ib2.setLayoutParams(params);
ib2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();
}
});
ImageButton ib3 = new ImageButton(this);
ib3.setImageResource(R.drawable.three);
ib3.setLayoutParams(params);
ib3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContex(),"String",Toast.LENGTH_SHORT).show();
}
});
ImageButton ib4 = new ImageButton(this);
ib4.setImageResource(R.drawable.four);
ib4.setLayoutParams(params);
ib4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();
});
LayoutParams params2 =
new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,1.0f);
TableRow table2 = new TableRow(this);
ImageButton ib5 = new ImageButton(this);
ib5.setImageResource(R.drawable.five);
ib5.setLayoutParams(params2);
ib5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContext(),"String",Toast.LENGTH_SHORT).show();
}
});
table.addView(ib);
table.addView(ib2);
table.addView(ib3);
table.addView(ib4);
table2.addView(ib5);
TableRow.LayoutParams layoutParams=
new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT );
this.addContentView(table, layoutParams);
TableRow.LayoutParams layoutParams2=
new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT );
this.addContentView(table2,layoutParams2);
You don't use "supertable" variable.
remove from your code:
this.addContentView(table, layoutParams);
this.addContentView(table2,layoutParams2);
And add:
supertable.addView(table);
supertable.addView(table2);
setContentView(supertable);
I made these codes.
every time I click on the button, an EditText inflate in scroll layout.
How can I get the ID for each EditText I inflate it ?
to take the value of each of them ?
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
//EditText hello;
static int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sum_1 = (Button)findViewById(R.id.sum);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.gamehistory);
Button b = (Button)findViewById(R.id.Button01);
layoutParams = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText hello = new EditText(MainActivity.this);
hello.setText("enter value" + i++);
linearLayout.addView(hello, layoutParams);
hello.setId(i++);
}
});
the only reason you want to add an ID to the layouts is so that you can get a hold of that view in the code.. here you allready have access to the EditText
its easier then to just add all the EditTexts to List then use them from there
or even to a EditText[]
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText hello = new EditText(MainActivity.this);
hello.setText("enter value" + i++);
linearLayout.addView(hello, layoutParams);
hello.setId(i++);
hello.setTag("edit");
}
});
int childcount = linearLayout.getChildCount();
for (int i = 0; i < childcount; i++) {
View v1 = linearLayout.getChildAt(i);
if(v1.getTag.equls("edit"){
System.out.println(v1..getId());
}
}
public void responsePost(Editable editable)
{
TableLayout chatbox = (TableLayout)findViewById(R.id.chatbox);
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
textview.setText(editable);
// textview.getTextColors(R.color.)
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
chatbox.addView(tr1, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
textview.setGravity(Gravity.RIGHT);
}
How can i make it go ALL THE WAY to the right? and not just a little bit? I used setGravity(Gravity.RIGHT) but that did not work.
There is a picture of what it is doing.
I need a little more detail, I couldn't duplicate your layout, what do you have in your main layout, and what code do you have in onCreate for the activity?
All you need is to set columns to stretchable:
chatbox.setColumnStretchable(1, true);
and you need one extra row, leave it blank
TextView col1 = new TextView(this);
col1.setText("");
tr1.addView(col1);
for example the complete code could be:
public class So extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
// anonymous inner class
public void onClick(View view) {
EditText editText1 = (EditText) findViewById(R.id.edit_text);
Editable editable = editText1.getText();
responsePost(editable);
editText1.setText("");
}
});
}
public void responsePost(Editable editable) {
TableLayout chatbox = (TableLayout) findViewById(R.id.chatbox);
TableRow tr1 = new TableRow(this);
// add these lines here
TextView col1 = new TextView(this);
col1.setText("");
tr1.addView(col1);
TextView textview = new TextView(this);
textview.setGravity(Gravity.RIGHT);
textview.setText(editable);
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
// and this line
chatbox.setColumnStretchable(1, true);
chatbox.addView(tr1, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
Hi I am passing an arraylist from one activity to another using Parcelable..
Now the problem is that when I click on BILL, all the rows are populated with the last value of the arraylist..So I guess I am not able to retreive the whole arraylist in the calling activity...
Here is the relevant code..
SpinPizza.java
public class SpinPizza extends Activity{
store temp= new store();
int i=0;
ArrayList<store> B = new ArrayList<store>();
//oncreate method
int n=Integer.parseInt(edittext.getText().toString());
temp.setOrder(s.getSelectedItem().toString(), s1.getSelectedItem().toString(),n);
B.add(temp);
TextView objText=(TextView) findViewById(R.id.pl);
TextView objText1=(TextView) findViewById(R.id.pl2);
objText.setText(B.get(i).getPizzaName());
objText1.setText(temp.getPizzaSize());
i++;
});
Button next1 = (Button) findViewById(R.id.bill);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Bill.class);
myIntent.putParcelableArrayListExtra("myclass",B);
startActivityForResult(myIntent, 0);
}
});
}
Bill.java
public class Bill extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.calc);
ArrayList<store> B1 = new ArrayList<store>();
store temp1=new store();
Bundle bj=getIntent().getExtras();
B1=bj.getParcelableArrayList("myclass");
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i=0;i<B1.size();i++)
{
TableRow tr = new TableRow(this);
tr.setId(100+i);
temp1=B1.get(i);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView b = new TextView(this);
b.setText(temp1.getPizzaName());
b.setId(200+i);
TextView b1 = new TextView(this);
b1.setText(temp1.getPizzaSize());
b1.setId(300+i);
TextView b2 = new TextView(this);
b2.setText(String.valueOf(temp1.getQuantity()));
b2.setId(400+i);
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(b);
b1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(b1);
b2.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.addView(b2);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}}
}
PLEASE HELP.
I can't see the creation of temp, nor i can see the iteration using in your temp modifier line temp.setOrder(s.getSelectedItem().toString(), s1.getSelectedItem().toString(),n);. It seems that you don't use i there that's why you have equal records.