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.
Related
my Problem is that i want to create a Checklist with multiple Checkboxes. The biggest Problem is i have more than 100 Checkboxes. I would like an CLEAR Button that clears all Checkboxes by clicking.
How can I do that? And have you an example how to solve it?
The only way i know is that:
Button clear = (Button) findViewById(R.id.clearbtn);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox2);
cb1.setChecked(false);
}
});
But that way isnt really effective with over 100 checkboxes ...
If you are keeping all the checkboxes on single ViewGroup then it can be done by getting all the childs of that ViewGroup and unchecking. For example 'parent' is the layout that contains all the checkboxes. You can uncheck all by:
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
if (view instanceof CheckBox) {
((CheckBox) view).setChecked(false);
}
}
I have solve it. Thanks to #Tuby and #android_hub for the idea with getChildCount().
And special thanks to #Hammad Akram. Now it works :D. My code now:
final LinearLayout ll = (LinearLayout)findViewById(R.id.ll_a320_main);
final int ccount = ll.getChildCount();
Button clear = (Button)findViewById(R.id.btn_a320_clear);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(ACT_A320.this, "Child: "+ccount,Toast.LENGTH_LONG).show(); -- Test for checking count of Child
for(int i=1; i<ccount;i++){
v = ll.getChildAt(i);
if(v instanceof CheckBox ){
((CheckBox) v).setChecked(false);
}
}
}
});
Now all Checkboxes would detected and set to false.
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();
}
});
//...
I have a screen that add dynamically a row with two textView and one CheckBox. My problem is that I only want that the user could check one, and the others have to be uncheck. If the user push another checkbox, enable this and disable all the other checkbox. I've test some things but I don't get the functionality that I want.
I can't change the configuration from checkbox to radioButton and I have to use the method that I'm using:
private selected_position = -1;
protected void addGreetingToListView(final GreetingListJson greetings) {
View row = inflater.inflate(R.layout.main_greetings_settings_row, greetingListView, false);
row.setTag(greetings);
playButton = (ToggleButton) row.findViewById(R.id.detail_play_greeting);
TextView greetingName = (TextView)row.findViewById(R.id.greetingTitle);
greetingName.setDuplicateParentStateEnabled(true);
TextView greetingDescription = (TextView)row.findViewById(R.id.greetingDescription);
greetingDescription.setDuplicateParentStateEnabled(true);
checkBox = (CheckBox) row.findViewById(R.id.checkBox_greeting_list);
checkBox.setTag(greetings);
checkBox.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
if (((CheckBox) view).isChecked())
{
selected_position= greetingListView.getChildCount();
}
else
{
selected_position=-1;
}
}
});
greetingName.setText(greetings.Name);
greetingDescription.setText(greetings.Description);
row.setOnClickListener(this);
row.setOnLongClickListener(this);
row.setFocusable(true);
greetingListView.addView(row);
}
The XML of the Checkbox:
<CheckBox
android:id="#+id/checkBox_greeting_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"/>
Firstly, declare an ArrayList in the class:
ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();
Then in addGreetingToListView add every new checkbox to mCheckBoxes and modify the click listener of the checkbox:
checkBox.setTag(greetings);
mCheckBoxes.add(checkBox);
checkBox.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
if (((CheckBox) view).isChecked())
{
for (int i = 0; i < mCheckBoxes.size(); i++) {
if (mCheckBoxes.get(i) == view)
selected_position = i;
else
mCheckBoxes.get(i).setChecked(false);
}
}
else
{
selected_position=-1;
}
}
});
I have a array which I get from another Activity. I want to display it in the current Activity as CheckBox items and then have ability to check some of them and delete by button clicking. I implement it next way:
public void addFiles()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
if(!FileManagerActivity.finalAttachFiles.isEmpty())
{
for (int i=0; i<FileManagerActivity.finalAttachFiles.size();i++)
{
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView= new CheckBox(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(16);
informationView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
informationView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.file_icon, 0, 0, 0);
informationView.setText(FileManagerActivity.finalAttachFiles.get(i).getName().toString());
informationView.setId(i);
layout.addView(informationView, 0);
layout.addView(line, 1);
layout.postInvalidate();
}
}
}
, and call it in the onCreate() method. it can be added successfully. Then I get items which were checked like this:
btnFilesRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
View v = null;
for(int i=0; i<FileManagerActivity.finalAttachFiles.size(); i++) {
v = layout.getChildAt(i);
CheckBox checkBox = (CheckBox) findViewById(i);
boolean checked=checkBox.isChecked();
if (checked)
{
Log.i("was checked",checkBox.getText().toString());
ViewGroup parent = (ViewGroup) v.getParent();
parent.removeView(v);
FileManagerActivity.finalAttachFiles.remove(i);
Log.i("this is view,baby",v.toString());
}
}
}
});
But items can't be deleted. And I'm thinking - what is better - to implement it in this way or maybe it will be more efficient with ListView using. I mean to create ArrayAdapter e.t.c..
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?