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);
Related
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());
}
}
I am giving one button in all the row,if we click that button dialog box has to appear in that one it has to display the content according to the row.
public void bindView(View view, Context context, final Cursor cursor){
int row_id = cursor.getColumnIndex("_id"); //Your row id (might need to replace)
TextView tv = (TextView) view.findViewById(R.id.text2);
final TextView tv1 = (TextView) view.findViewById(R.id.text1);
TextView tv2 = (TextView) view.findViewById(R.id.date);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
int col1 = cursor.getColumnIndex("expname");
final String expname = cursor.getString(col1 );
int col2 = cursor.getColumnIndex("expcontent");
final String expcontent = cursor.getString(col2 );
int col3 = cursor.getColumnIndex("expdate");
final String expdate = cursor.getString(col3);
cb.setTag(cursor.getPosition());
cb.setChecked(mCheckStates.get(cursor.getPosition(), false));
cb.setOnCheckedChangeListener(this);
// TextView tv2 = (TextView) view.findViewById(R.id.text3);
//cursor.getColumnName(1)
tv.setText( expname);
tv1.setText( expcontent);
tv2.setText(expdate);
//tv2.setText( ""+cursor.getColumnIndex(GinfyDbAdapter.CATEGORY_COLUMN_COUNT));
// String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE, GinfyDbAdapter.CATEGORY_COLUMN_CONTENT, GinfyDbAdapter.CATEGORY_COLUMN_COUNT}
ImageButton button = (ImageButton) view.findViewById(R.id.sms);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb2 = new StringBuffer();
sb2.append("Title:");
sb2.append(Html.fromHtml(expname));
sb2.append(",Content:");
sb2.append(Html.fromHtml(expcontent));
sb2.append("\n");
String strContactList1 = (sb2.toString().trim());
sendsmsdata(strContactList1);
}
});
ImageButton button1 = (ImageButton) view.findViewById(R.id.mail);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb3 = new StringBuffer();
sb3.append("Title:");
sb3.append(Html.fromHtml(expname));
sb3.append(",Content:");
sb3.append(Html.fromHtml(expcontent));
sb3.append("\n");
String strContactList2 = (sb3.toString().trim());
sendmaildata(strContactList2);
}
});
ImageButton button2 = (ImageButton) view.findViewById(R.id.btnaudioprayer);
button2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
typed = expcontent;
speakOut();
}
});
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
dialog.show();
}
});
dialog=new Dialog(ExperiencesActivity.this);
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
textview1 = (TextView)dialog.findViewById(R.id.tv11);
textview1.setText("Title:" + expname);
textview2 = (TextView)dialog.findViewById(R.id.tv22);
textview2.setText("Content:" + expcontent);
Button button23=(Button)dialog.findViewById(R.id.btnSubmit);
button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
But here my problem is it will not take the respective content in dialog box,whenever we click the first item in an button,it will shows the content in all the row of button click. Dialog box content has to change according to the row.
You need to create saperate classe for your textviews. Like
private class ViewHolder {
TextView textview1;
TextView textview2;
Button button23;
}
Update your dialog like...
public void showMyDialog(){
dialog=new Dialog(ExperiencesActivity.this);
ViewHolder viewholder;
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
ViewHolder.textview1 = (TextView)dialog.findViewById(R.id.tv11);
ViewHolder.textview1.setText("Title:" + expname);
ViewHolder.textview2 = (TextView)dialog.findViewById(R.id.tv22);
ViewHolder.textview2.setText("Content:" + expcontent);
ViewHolder.button23=(Button)dialog.findViewById(R.id.btnSubmit);
ViewHolder.button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
And call showMyDialog() method onclick of button.
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
showMyDialog();
}
});
I intend with this program, when you press the button to add a row to the table, but it is giving me error, does someone can idetificar any errors you're doing?
This is main:
public class MainActivity extends Activity {
Button b1;
TableLayout tl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the layout
setContentView(R.layout.activity_main);
// add a click-listener on the button
b1 = (Button) findViewById(R.id.b1);
tl = (TableLayout) findViewById(R.id.tLt);
TableRow tr_head = new TableRow(this);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView label_name = new TextView(this);
label_name.setText("Nome");
label_name.setTextColor(Color.WHITE);
tr_head.addView(label_name);
TextView label_surname = new TextView(this);
label_surname.setText("Apelido");
label_surname.setTextColor(Color.WHITE);
tr_head.addView(label_surname);
tl.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//botão
EditText name = (EditText) findViewById(R.id.eTname);
EditText subname = (EditText) findViewById(R.id.eTsubname);
String nname = name.getText().toString();
String sname = subname.getText().toString();
adicionalinha adicionar = new adicionalinha();
adicionar.adiciona(nname, sname, tl);
}
});
}
}
This is another function adicionalinha:
public class adicionalinha extends Activity {
public void adiciona(String name, String subname, TableLayout tl){
// Cria linha
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView labelNAME = new TextView(this);
labelNAME.setText(name);
labelNAME.setTextColor(Color.WHITE);
tr.addView(labelNAME);
TextView labelAPELIDO = new TextView(this);
labelAPELIDO.setText(subname);
labelAPELIDO.setTextColor(Color.WHITE);
tr.addView(labelAPELIDO);
// adiciona linha
tl.addView(tr);
}
}
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.