i know that i can use this for each tablerow but it's very long code
setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//perform action
}
});
but i have 12 table row i think that this is very long.. maybe with this structure
tablelayout {
if (tablerow.id = 1) {
// action if i pressed the first row
} else if (tablerow.id = 2) {
// action if i pressed the second row
}
}
You can call setOnItemClickListener directly on your ListView. So, instead of calling setOnClickListener in your adapter's getView method, just use this:
ListView mListView = (ListView) findViewById(R.id.listview);//or whatever the id is
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//then use a switch statement
switch(position) {
case 1 :
//do something if first row is clicked
break;
case 2 :
//row 2 clicked
break;//don't forget these breaks
}
}
});
You could use View.setTag(Object obj):
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
Integer tag = (Integer)v.getTag();
switch (tag.intValue()) {
case ROW1: ...
}
}
});
and the time you fill your rows just do (maybe in a loop over all row-ids):
TableRow row = tableLayout.findViewById(ROW1);
row.setTag(ROW1);
row.setOnClickListener(mListener);
By the way, the Tag can also be set in XML...
If you are using a TableLayout, you can add the individual OnClickListener to the elements that are inside of your TableRow (which is inside of your TableLayout).
For example if you have a Button and a TextView inside of a TableRow and you want your button to do something when clicked, then you should add the OnClickListener to the button. If all of the buttons go to the same place, but send different data depending on the position in the TableLayout then you can do something like this:
String[] texts; //The different texts your button could say
for(int i = 0; i < 12; i++){
final Button yourButton = new Button(this);
yourButton.setText(texts[i]);
yourButton.setOnClickListener(new OnClickListener(){
Intent i = new Intent(this, YourClass.class);
i.putExtra(yourButton.getText());
startActivity(i);
}
}
If your TableRow only contains one element, you should be using a ListView instead of a TableLayout as Phil suggested.
I hope that kind of gives you an idea on a direction you can go, but it really depends on what you need to do with the item that is being clicked on.
/************ if table row is dynamic then this method else method 2 is perfect************/
//if we want to applay listener on dynamic tablerow then use this
//sure that perfect
TablRowe tr = new TableRow(this);
tr.setClickable(true);
tr.setId(100);// if in loop then add 1 counter with 100 like (100+counter) at end count++ it
tr.setOnClickListener(this);
#Override
public void onClick(View v)
{
switch (v.getId())
{
case 100:
Toast.makeText(getApplicationContext(), "100", Toast.LENGTH_SHORT).show();
break;
case 101:
Toast.makeText(getApplicationContext(), "101", Toast.LENGTH_SHORT).show();
break;
}
/************************** for simple like this ************************/
TableRow row1 = (TableRow)findViewById(R.id.row1);
row1.setonClickListener(this);
public void onClick(View v)
{
switch (v.getId())
{
case R.id.row1:
// do work
break;
}
}
Related
i want to make a table layout in which there are 2 rows.
In both rows there are one Label and a Text Field when i press the 1st row or 2nd row the prompt Dialog will open and u enter any value it will set on Text field of selected row.
please guide me how to make a click listener on row with example and how to call a dialog when the row is selected.
TableRow row1 = findViewById(R.id.row2);
row1.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
// do any thing
}
});
// Method 2 ********************
TableRow row1 = findViewById(R.id.row2);
row1.setonClickListner(this);
public void onClicl(View v)
{
switch (v.getId())
{
case R.id.row1:
break;
}
}
Simply give each TableRow element a unique id and define an onClick
method:
<TableRow
android:id="#+id/one"
android:onClick="rowClick">
Find the row by id from layout and then add following in java class
tableRow= (TableRow) findViewById(R.id.one);
tableRow.setClickable(true);
tableRow.setOnClickListener(onClickListener);
private OnClickListener onClickListener= new OnClickListener() {
public void onClick(View v) {
show_dialog();
}
};
Then Call Following Method
public void show_dialog() {
final Dialog dialog = new Dialog(getApplicationContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow();
dialog.setContentView(R.layout.yourlayout);
dialog.setTitle("yor title");
dialog.setCancelable(false);
final Button btnOkDialog = (Button) dialog.findViewById(R.id.ResetOkBtn);
btnOkDialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
try {
dialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
First you have to give your TableRow an id on your xml file
<TableRow
android:id="#+id/row1"
...
>
Set the children of your row(in your case a TextView and an EditText probably) to NOT clickable
android:clickable="false"
Now on your java file find your tablerow with the id and add onClickListener
TableRow row1 = findViewById(R.id.row1);
d.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Now to open a dialog read this
Just a guess.
Make the EditText clickable. It might be what you are really looking for
//if we want to applay listener on dynamic tablerow then use this
//sure that perfect
TablRowe tr = new TableRow(this);
tr.setClickable(true);
tr.setId(100);// if in loop then add 1 counter with 100 like (100+counter) at end count++ it
tr.setOnClickListener(this);
#Override
public void onClick(View v)
{
switch (v.getId())
{
case 100:
Toast.makeText(getApplicationContext(), "100", Toast.LENGTH_SHORT).show();
break;
case 101:
Toast.makeText(getApplicationContext(), "101", Toast.LENGTH_SHORT).show();
break;
}
Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview
https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706
You can do it in two ways:
1. Either by directly using onClickListener like this:
textView.setOnClickListener(new View.OnClickListener(
public void onClick(View arg0) {
// Do anything here.
}
});
OR
2. In XML file, in declaration of <TextView /> add one more attribute as:
android:onClick="onClickTextView"
and in yout activity, add this function:
public void onClickTextView(View view) {
// Do anything here.
}
UPDATE:
Use following code to get click event on TextView:
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) (findViewById(R.id.title));
if (tv != null) {
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CLICKED",
Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, "TV not found",
Toast.LENGTH_LONG).show();
}
}
});
Try this :
When you select one textview the other three will be unclickable
final TextView texta = (TextView) findViewById(R.id.text_a);
final TextView textb = (TextView) findViewById(R.id.text_b);
final TextView textc = (TextView) findViewById(R.id.text_c);
final TextView textd = (TextView) findViewById(R.id.text_d);
texta.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
textb.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textb.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textc.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textd.setClickable(false);
}
});
textd.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textc.setClickable(false);
}
});
Assume you extend BaseAdapter to set the listview content ->
Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.
It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.
Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.
For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them).
You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.
Layout could be something like this :
Follow these links for reference:
for listView
for RadioGroup
I hope this will help
Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.
either use this in adapter class as by Shrikant:
textViewa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do anything here.
}
});
textViewb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Do anything here.
}
});
//and so on...
// or better to use ViewHolder holder; for these type of listviews;
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do what you want to do.
// for my i have to call a method in my parent activity. so in constructor of adapter, I passed the activity and then typecasted it like
ParentActivity parent = (ParentActivity) activity;
parent.chosenAnswer(view.getId());
// then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.
// OR
//you can write like this too..
switch (view.getId()) {
case R.id.textViewa:
break;
case R.id.textViewb:
break;
case R.id.textViewc:
break;
case R.id.textViewd:
break;
}
}
};
I have lots of image buttons. User can press any of those button and i need to know which button is pressed. These buttons appears dynamically so i don't know how much of them will be.
For one image button i would write this listener:
ImageButton ib = new ImageButton(this);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(TravelBite.this, "test", Toast.LENGTH_SHORT).show();
}
});
But how to do one listener for all my Images Buttons? Can i recognize which button was pressed by it's tag ? Like tag would be ID from sqlite.
Also i put image to button with this code:
button.setImageDrawable( testPic );
button is ImageButton and testPict is drawable (image)
But when i press this button it don't show that it is pressed if i do this:
button.setBackgroundColor(R.color.transparent_background);
I had to do this because i just want to see Buuton image which i could press and recognize what i pressed.
Thanks.
ok what you can do is that you can write a single callback function and then set it to each and every button it will allow you to handle each button with a sing function like :
View.OnClickListener btn_listener = View.OnClickListener() {
#Override
public void onClick(View v) {
// Do whatever work you want.
int id = v.getid();
// check for id and do you task.
}
Arraylist<Button> btn_group = new Arraylist<Button>;
or
Arraylist<int> btn_id_group = new ArrayList<int>;
for (int i =0; i < 10; i++) {
Button btn = new Button(getApplicationContext());
btn.setId(i);
btn_id_group.add(i) or btn_group.add(btn);
btn.SetOnClickListener(btn_listener);
}
I think it will work for you.
You can use View.setTag(object) and View.getTag() and store in it sqlite id. Something like this:
View.OnClickListener listener = View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(TravelBite.this, (String)v.getTag(), Toast.LENGTH_SHORT).show();
//or some function that do somthing useful
//if( ((String)v.getTag).equals("image1") ){} or anything else
}
And in for loop:
String tagFromSqlite = "image1";
ImageButton ib = new ImageButton(this);
ImageButton.setTag(tagFromSqlite);
ib.setOnClickListener(listener);
final OnClickLisener listener = new OnClickListener() {
public void onClick(View v){
switch((Integer)v.getTag()){
case R.id.zero:
break;
case R.id.one:
break;
case R.id.two:
break;
}
}
}
//when init the Buttom
ImageButton btn = new ImageButton(Context);
btn.setTag(NUMBER);
you'll have to manually assign ID's to keep them separated - I had to do something similar (1000 was the base ID I chose to add upon as well)
Although View v in the listener refers to the button pressed, when you programmatically create buttons the id's are not unique and caused me issues, so that's why I set them specifically
View.OnClickListener btn_listener = View.OnClickListener()
{
#Override
public void onClick(View v)
{
//you can use v.getID() here
}
}
for (int i =0; i < 10; i++)
{
Button btn = new Button(getApplicationContext());
btn.setID( 1000 + i );
btn.SetOnClickListener(btn_listener);
}
hi i am doing one application here i need to disply 6 images in 3 colunmns and 3 rows.and then when i click each image i need to perform different onclick action.i teried some way using arraylist with forloop.using below code i applied onclick function into all images but here 2 cloumn 3 images onclick function working but i first column 3 images not working onclick function.but i need to apply different onclick action to each button. so please any one help me how to apply onclick action to array of images.
game2 .class:
public class game2 extends Activity implements OnClickListener {
TableLayout layout;
int i=0;
int f=0;
Integer[] images={R.drawable.abig,R.drawable.cbig,R.drawable.dbig,R.drawable.abig,R.drawable.cbig,R.drawable.dbig};
List<Integer> solutionList = Arrays.asList(images);
Integer[] randomNumbers,randomNumbers1;
TableLayout.LayoutParams param,param1;
ImageView[] plus=new ImageView[6];
TableRow[] row = new TableRow[6];
RelativeLayout.LayoutParams lp2,lp1,lp3,lp4,lp5;
RelativeLayout linear;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(40,50) );
param=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
param.setMargins(25, 0, 0, 0);
lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
linear=(RelativeLayout)findViewById(R.id.linear);
Collections.shuffle(solutionList);
randomNumbers = (Integer[])solutionList.toArray();
int unique=0;
for (f=0; f<3; f++) {
row[f] = new TableRow(this);
for (int c=0; c<2; c++) {
plus[f]=new ImageView(this);
plus[f].setBackgroundResource(randomNumbers[unique]);
plus[f].setOnClickListener(this);
row[f].addView(plus[f], 200,100);
unique++;
} // for
layout.addView(row[f],param);
} // for
linear.addView(layout,lp1);
setContentView(linear);
}
public void onClick(View view) {
if(view==plus[0])
{
Toast.makeText(game2.this, "view", Toast.LENGTH_SHORT).show();
}
if(view==plus[1])
{
Toast.makeText(game2.this, "view1", Toast.LENGTH_SHORT).show();
}
if(view==plus[2])
{
Toast.makeText(game2.this, "view2", Toast.LENGTH_SHORT).show();
}
if(view==plus[3])
{
Toast.makeText(game2.this, "view3", Toast.LENGTH_SHORT).show();
}
if(view==plus[4])
{
Toast.makeText(game2.this, "view4", Toast.LENGTH_SHORT).show();
}
if(view==plus[5])
{
Toast.makeText(game2.this, "view5", Toast.LENGTH_SHORT).show();
}
}
}
Can this may be problem for (int c=0; c<2; c++)? use for (int c=0; c<3; c++) .. c<3 for 3 columns.. And let me know what happen..
EDIT:
Also
ImageView[] plus=new ImageView[9];
int unique=0;
for (f=0; f<3; f++) {
row[f] = new TableRow(this);
for (int c=0; c<3; c++) {
plus[unique]=new ImageView(this);
plus[unique].setBackgroundResource(randomNumbers[unique]);
plus[unique].setOnClickListener(this);
plus[unique].setId(unique);
row[f].addView(plus[unique], 200,100);
unique++;
}
And in onClick()
public void onClick(View view) {
switch(view.getId()){
case 0:
{
Toast.makeText(game2.this, "view", Toast.LENGTH_SHORT).show();
}
.
.
.
case 8:
{
Toast.makeText(game2.this, "view8", Toast.LENGTH_SHORT).show();
}
}
}
Use ArrayList instead of array.. Right now you are not adding all the elements.. In nested for loop your elemnts are getting replaced... So use ArrayList... And I don't see a need for such a complex code, You keep UI in XML and still can achieve the Same.. The present code is hard to understand and even you cannot after some days.. You cannot maintain this code..
Use plus[unique] in place of plus[f] in all 3 lines of below loop
for (int c=0; c<2; c++) {
... }
simple set ID to every imageView when creating ImageView. then setOnClickListener().
onClick(View v) {
switch(v.getId()) {
case 1st_ImageViewID:
break;
case 2nd_ImageView_ID:
break;
case 3rd_ImageView_ID:
break;
}
}
I have a parent view LinearLayout i have added a child view TextView, But whenever i click on childview why parentview is also clicked. What i want is to differentiate whether only parent has been clicked and only childview is clicked.
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
then try to implement View.OnClickListener in yout Activity first, then try to override
#Override
public void onClick(View v){
if(v.getId().equals(R.id.linearLayout1) {
//do ur stuffs
} else {
//do your stuffs
}
Hope it will help you. :)
In android, ViewGroups (i.e. all layouts by inheritance) use the property (and corresponding xml attribute ) : addStatesFromChildren
If set, the parent will just be set in the very same state as one of his children view.
try this code...
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(this);
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(this)
#Override
public void onClick(View v){
switch(v.getId()) {
case R.id.TextViewLayout1:
//do ur stuffs
break;
case R.id.linearLayout1:
//do ur stuffs
break;
}
}