I am beginner to learn android,I have assigned table row value by dynamically.
But I am not able to get the row value using on click listener method .
Can any one please help to solve the issues .
I have tried following methods
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passbook);
TableLayout tl = (TableLayout) findViewById(R.id.mytable);
tl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//obj.myShow(Passbook.this, v.getTag() + ": Row is clicked" );
}
});
}
You have to set the onClickListener on the row not the layout.
I think this is what u need...try this and modify as u wish...
//...
tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow t = (TableRow) view;
TextView firstTextView = (TextView) t.getChildAt(0);
TextView secondTextView = (TextView) t.getChildAt(1);
String firstText = firstTextView.getText().toString();
String secondText = secondTextView.getText().toString();
}
});
//...
Related
I have programmatically TableRow. Then I loop it like this:
int countData = dbData.readRows();
for(int i = 0;i<countData;i++) {
TableRow tr = new TableRow(getActivity());
TextView tvMessage = new TextView(getActivity());
tvMessage.setText("Click Here");
tr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Clicked", Toast.LENGTH_SHORT).show();
}
});
tr.addView(tvMessage);
}
Any help is appreciated. Thanks.
I can dynamically add the text fields in my LinearLayout container.
Now I need to collapse those added fields in click of the Button those added fields should get collapse with any label.
Can it be done? Below is the code that i can add the EditText dynamically.
txtHeading = (EditText)findViewById(R.id.heading);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0)
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.list_view, null);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_icon_select);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
textIn.setText(null);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}
});
container.addView(addView);
}}});
btnsave = (Button) findViewById(R.id.btn_save);
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What to insert here?
on click to save button in need to collapse all the textView Elements.
let us consider Heading is the label and it is named as Gender so if i added the options as male and female then after clicking the save button this should get collapsed under the Heading.
Based on what I'm kind of guessing that you want... I would create a custom view class based on a linear layout. Each having two edit texts.
Create and add this class to dynamically when the add button is pressed. When the save button is pressed, you can loop over the parent LinearLayout's children and then call a helper method to collapse the second edit text element.
To Loop over the parent and collapse children. Now the CustomElement and the collapseOptionField() call is custom code you must write.
LinearLayout parentLayout = findViewById(...);
for ( int i = 0; i < parentLayout.getChildCount(); i++ ) {
CustomElement ce = parentLayout.getChildAt(i);
ce.collapseOption();
}
Update
Custom class with collapse-able option:
public class TextWithOption extends LinearLayout {
EditText edit0;
EditText edit1;
public TextWithOption(Context context, AttributeSet attrs) {
this(context);
}
public TextWithOption(Context context) {
super(context);
View view = LayoutInflater.from(context).inflate(R.layout.edit_text_option, null);
edit0 = (EditText) view.findViewById(R.id.edit_text0);
edit1 = (EditText) view.findViewById(R.id.edit_text1);
addView(view);
}
public void collapseOption() {
edit1.setVisibility(View.GONE);
}
public void showOption() {
edit1.setVisibility(View.VISIBLE);
}
}
Activity code that will add new options and then collapse them:
final LinearLayout optionsLayout = (LinearLayout) findViewById(R.id.outer_layout);
Button addButton = (Button) findViewById(R.id.button_add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextWithOption to = new TextWithOption(context);
optionsLayout.addView(to);
}
});
((Button) findViewById(R.id.button_save)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for ( int i = 0; i < optionsLayout.getChildCount(); i++ ) {
((TextWithOption)optionsLayout.getChildAt(i)).collapseOption();
}
}
});
public static void collapse(final View v) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
v.startAnimation(anim);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
onclick of button write view.collapse
view equals to whatever view you want collapse
I have a TableLayout with TableRows and CheckBox and TextView inside. When I push a CheckBox I need to know in wich TableRow the CheckBox is placed to get the TextView "associated" to this CheckBox. For example:
TableLayout:
TableRow 1: X TextView1
TableRow 2: X TextView2
...
I'm trying something like this:
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cb.isChecked())
{
int id_cb = cb.getId();
boolean encontrado = false;
for (int i = 0; i < tabla_tareas.getChildCount() && !encontrado; i++)
{
TableRow aux_tr = (TableRow) tabla_tareas.getChildAt(i);
CheckBox aux_cb = (CheckBox) aux_tr.getChildAt(0);
if (id_cb == aux_cb.getId())
encontrado = true;
}...
But it doesn't work.
Utilizing what Elior has said, why don't you just use the folling methods to achieve what you want:
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cb.isChecked())
{
cb.getText().toString(); //This will retrieve the text associated with the Checkbox
cb.setText("Your new string here"); //This will set the text associated with the CheckBox
}...
With the above two methods you should be able to do everything you need to without the need for the TextView, and probably without the TableLayout as well since I am assuming you were using that to align the CheckBoxes and the TextViews.
I am adding programatically and dynamically some elements (buttons and text views) with android. I also need to set the setOnClickListener event for each of these buttons and from that event execute some action on the click of button:
do
{
EditText txt1 = new EditText(this);
EditText txt2 = new EditText(this);
Button showtxt = new Button(this);
linearLayout.addView(showtxt );
linearLayout.addView(txt1);
linearLayout.addView(txt2);
showtxt.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String aaa= txt1 .getText().toString();//HOW TO ACCESS txt1 and txt2 from here
String bbb= txt2 .getText().toString();
}
}
}
while(somecondition)
I am almost new to android. How can I access to txt1 and txt2 in the click callback function?
You need to make the define the variables where they will have class wide scope:
public class Example extends Activity {
EditText txt1;
EditText txt2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
txt1 = new EditText(this);
txt2 = new EditText(this);
...
Now your onClick function will be able to see txt1 and txt2.
Alternatively
Since you appear to be creating a lot of txt1 and txt2 in one LinearLayout, you can pass your Button a reference to its EditTexts:
do {
...
// EditText[] array = { txt1, txt2 };
// is the short version of
EditText[] array = new EditText[2];
array[0] = txt1;
array[1] = txt2;
showtxt.setTag(array);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText[] array = (EditText[]) v.getTag();
String aaa = array[0].getText().toString();
String bbb = array[1].getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});
} while(some condition)
This may not be not ideal, however without any further context I cannot guess your ultimate goal. Hope that helps!
Last Suggestion
If we call the Button and two EditTexts a row, you could store each row in a ViewGroup or View of its own. Say you wanted to have background colors for each row:
View row = new View(this); // or this could be another LinearLayout
row.setBackgroundColor(0x0000ff);
// Create and add the Button and EditTexts to row, as in row.addView(showtxt), etc
...
linearLayout.addView(row);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View row = v.getParent()
String aaa = ((EditText) row.getChildAt(1)).getText().toString();
String bbb = ((EditText) row.getChildAt(2)).getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});
I have created rows dynamically in a Table Layout.
I am setting onClickListener on the row element. But the onClick event is triggered only after the second click.
row = new TableRow(this);
row.setClickable(true);
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View pView) {
if(pView instanceof TableRow){
TableRow lTableRow = ((TableRow) pView);
TextView lTextView = (TextView)lTableRow.getChildAt(0);
Toast.makeText(BusDetails.this, lTextView.getText(), Toast.LENGTH_SHORT)
.show();
}
}});
Any one has any idea why this is happening?