Example for clickable TableRow in Android - android

I have a table of texts with each row having a text. I want to display a popup when one of the rows in the table is clicked. Does TableLayout in android support this? If so, is there an example?

You can attach the onClickListener with the TextView in each row. Or you could set it directly on TableRow directly
android.widget.TableRow tableRow = ..
tableRow.setOnClickListener(new OnClickListener(){
public void onClick() {
}
});
OnClickListener

Programatically doing it i used this code inside onCreate method of my activity:
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//codigoo
}
});
I had to use the View.OnClickListener and the #Override or it wouldnt work!

Related

Android how to show hidden item only for one row on click

im trying to create a listView, that has an Button item on it.
I want to make this button clickable, so I did something like this code in Adapter, getView:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("_myButton_Log", "ShowOnClick");
}
});
And now im trying to change the visibility parameter for my textView:
TextView myDesc = row.findViewById(R.id.my_desc);
myDesc.setVisibility(convertView.GONE);
I want to show this textView in only one row, after click this button.
Now I make that, the button is clickable for each rows but as you can see it's show only the Log. Im a newbie in the ListViews and buttons on it and im trying to get knowledge how to make it work, but for now I cannot find any help...
So im begging here for some help! :)
Anyway if you want me to use the OnItemClickListener it's not possible because im using it for another way.
Ok I found out an response for my question.
Everyone with this problem - just need to make a simple action for a button in list view in your adapter like:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView myPrice = row.findViewById(R.id.price);
Button myButton = row.findViewById(R.id.button);
myPrice.setVisibility(convertView.VISIBLE);
myButton.setVisibility(convertView.GONE);
}
});

Android: Handle Onclick Listerner in included layout [duplicate]

I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!

TextView onclickListener

In my application i have added textviews dynamically using array,and given id's for each with a count variable, which increment's count according to the addition of textviews.In onClickListener of each textview i want to perform some oparations,but when i'm trying to do this operation is getting performed on all textviews.
Below is the code,i'm not getting what's wrong.please help me.
// here i have added textview dynamically
mtxtview[colTextCount]=new TextView(this);
mtxtview[colTextCount].setId(colTextCount);
mtxtview[colTextCount].setLayoutParams(new LayoutParams(20,20));
And In onclickListener-
#Override
public void onClick(View v) {
System.out.println("onclick...");
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
//if i added 3 textview.its giving me all 3 textview's text(getText())
}
}
use switch-case and View.getId() to check which TextView is Clicked before starting for loop . try it as :
#Override
public void onClick(View v) {
System.out.println("onclick...");
switch(v.getId())
{
case _mTextViewId:
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
}
break ;
// same for others....
}
}
see TextView onClick() not working
You should do setOnClickListener for each TextView Object.

I need create a button in java class and replace it in xml

I need create a button in java class and replace it in xml in a specific position like i want to put them in horizontal scrollView that i created in xml please any help + i need a function the allow me to generate button automatically according to my array size thanks. .
Try This code
LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
LayoutParams layoutParam_text=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); layoutParam_text.gravity=Gravity.LEFT;
Button btnTest[]=new Button[array.size];
for(int i=0;i<array.size();i++)
{
btnTest[i] =new Button(this);
btnTest[i].setLayoutParams(layoutParam_text);
btnTest[i].setTag(""+i);
btnTest[i].setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
layout.addView(btnTest[i]);
}

Linearlayout >> empty eventlistener method neccessarry to get focus?

I created a drawable (background_row.xml) with a state_focused and a default item.
Now i want to use this drawable to color a linearlayout when selected.
Linearlayout row = new LinearLayout(this);
row.setFocusableInTouchMode(true);
row.setBackgroundResource(R.drawable.background_row);
This doesn't work.
I tryed a lot and finally i found out that it works, when i implement an empty setOnClickListener like this:
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Sorry.. but that makes no sense for me..
Why do i have to implement an empty OnClickListener and why is it working when i do so?

Categories

Resources