So, is there any easy way to identify the tablerow clicked?
I have a tablelayout with static data, like a menu. When the user click any tablerow I want to start the specified activity.
I've googled a couple of hours but everyone seem to use the view inside the tablerows text property to identify it, which sucks if you translate the app. I tried to find some getIndex property of the TableLayouit but no luck, and the views .getId property is useless.
I guess the solution is to have specific onclicklisteners on each tablerow but that will generate a lot of (unnecessary) code and there must be a better solution?
Regards
if you make your activity as an extension of ListView() and you put your options in a list you can actually override the method
public void onListItemClick(ListView parent, View v, int position, long id) {
}
and the position attribute of that method is an int which indicates what row you actually click
this is a solution that i am sure actually works, so if your table layout is not too complicated i suggest you to make a list and override this method!
ok, I'll try another one since that one causes you other problems, you could make something like this:
in the onCreate():
firstTextVIew = (TextView) findViewById(R.id.firstText);
firstTextView.setOnClickListener(this);
secondTextVIew = (TextView) findViewById(R.id.secondText);
secondTextView.setOnClickListener(this);
after the onCreate():
public void onClick(View v) {
/*make a list of cases for any text view, where __TextView is the name you gave to the textview when you made the getViewById() on the start of the activity*/
if(v == firstTextView){
//start activity
}
if(v == secondTextView){
//start activity
}
}
Related
How to use ContextMenu on long click on the screen(on tablelayout for which parentlayout is scrollview)?
I have used scrollview within which i used tablelayout and on longclicking the that i need to display contextmenu.
On longclick i am able to display a toast but not the contextmenu. Within the OnLongClick() method i am not able to implement onCreateContextMenu() and onCreateContextMenu() methods.
Can anybody please me in this regard??If so I will be thankful to u...Thanks !!!
Some code would be useful, but I am guessing you are defining and OnLongClickListener inline with something like this
OnCreate
{
TableLayout t = (TableLayout) this.findViewById(R.id.myTable)
t.setOnLongclickListener(new OnLongClickListener()
{
void OnLongClick(View v)
{
//here is where I think you're trying to make a contextmenu
}
}
}
If I'm right, then because you're trying to make the context menu from an inner class, you need to reference your outer class. So you would need to call:
[nameofyourclass].this.onCreateContextMenu(.....)
I have a set of View States that each contain a CheckBox view and an EditText View. When the user checks the CheckBox, I'd like to set the visibility of the EditText.
To get a handle of the EditText from the CheckBox, I just put the EditText as a tag on the CheckBox in OnCreate():
// In OnCreate()
v = f1.findViewById(R.id.imgNotes);
v.setTag(f1.findViewById(R.id.noteText));
v.setOnClickListener(this);
Later, in onClick() I retrieve the EditText from the tag:
public void onClick(View v)
{
super.onClick(v);
switch (v.getId())
{
case R.id.imgNotes:
View noteText = (View) v.getTag();
if (null != noteText)
{
if (noteText.getVisibility() == View.VISIBLE)
noteText.setVisibility(View.GONE);
else
noteText.setVisibility(View.VISIBLE);
}
break;
…
Everything is working fine. I was just wondering if there is a better way. Or is this okay? Or is this the ideal way?
Thanks!
This would work, no issues, but as a practice, avoid tagging views to other views. Few other ways you can try.
Field variable
Declare two private members, for EditText and CheckBox, and directly access them, whenever a checkbox is clicked. It works fine if lets say you have one pair of CheckBox and EditText, but the code gets messy if too many check_box and edit_text ids are wired around.
Custom view
Define a custom view class which holds such View state pairs, that way you can manage multiple such pairs, with each custom view holding business logic inside that view, thereby providing flexibility to add more behaviours in future, just edit this class.
Hope that helps. :)
In onCreate save EditText to a global variable likes this:
mEditText = (EditText) f1.findViewById(R.id.noteText)
Then in your onClick just use mEditText instead of noteText
Let me explain myself:
As you know, when you've a view which have to be inflated several times, but changing values, you use a GridView or a ListView. Those two Composite views, have some methods like onItemClick. This method is so useful, as it returns the position of the view clicked.
With this position you can perform some concrete tasks, like retreiving from an ArrayList, the information of that object. Here's an example:
ArrayList<DocumentInfo> documents;
And when you set a setOnItemClickListener() you can get the correct values:
gallery.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
getDocumentInfoOf(pos);
}
});
public void getDocumentInfoOf(int position){
DocumentInfo doc = documents.get(position);
}
However, when you aren't using a GridView or a ListView, you're in your own. You don't have a clear way (AFAIK) to know which layout inflated is the one clicked (I mean like the previous example, the "position" value).
What I am currently doing, is the following:
for (int i=0; i<10;i++){
RelativeLayout documentInflated = (RelativeLayout) this.mInflater.inflate(R.layout.open_document_per_inflar, null);
documentInflated.setContentDescription(""+i);
documentInflated.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
openDocument(v);
}
});
container.addView(documentInflated);
}
public void openDocument(View v){
int idDocument = Integer.parseInt(v.getContentDescription());
//idDocument is the view clicked
}
Do you guys think this is a clear way of doing this?
Thank you!!!
If I'm not mistaken you want to get some data from your created Relative Layout when you click on it. The best solution here is to use the method setTag(Object tag). After that you get the informatiom with the method getTag(). This method allows you to add extra information to your view. As it says in the documentation:
Tags
Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
Also depending on your needs you can seperate every tag with a key -> value pair with the method setTag(int key, Object tag), after that you can retrieve this object with getTag(int key);
So in your case you will have
documentInflated.setTag(i)
in the onClick yo will then have:
int i = (int)v.getTag();
Unless I'm not understanding your desire...
#Override
public void onClick(View v) {
openDocument(v);
}
v IS the view being clicked. Your code looks like it should do what you're hoping it will do. What are you actually seeing happen?
What i want to know?
I was little curious about if i can add a footer to an empty ListView in android. I checked the documentation of ListView footer but they haven't mentioned anything related to this. So just wanted to know if it is possible?
Why i need this?
Actually i have edittext(search box) in my application which will accept characters as input from user. On the basis of this entered text i will do some search in DB & display the matching results in the ListView. What i am planning to do is to display a button at the bottom of ListView(something like footer) which will provide a extended search option to user.
Some code for reference :
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
#Override
public void afterTextChanged(Editable s)
{
strGuestCardToSerach = s.toString();
if(strGuestCardToSerach.equals(""))
{
if(listview.getFooterViewsCount() > 0)
{
listview.removeFooterView(footerView);
listviewAdapter.notifyDataSetChanged();
}
}
else
{
if(listview.getFooterViewsCount() == 0)
{
footerView = inflater.inflate(R.layout.footer_view_layout, null);
listview.addFooterView(footerView);
listview.setAdapter(recentGuestAdapter);
listviewAdapter.notifyDataSetChanged();
}
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
}
#Edit :
It does work once you have at least one entry in the ListView. So just wanted to understand if at all it is possible to add a Footer to an empty ListView.
Thanks in advance.
I am posting this just in case if anyone is looking for the similar functionality as mine. Answer is NO, you can't add a footer view to an empty ListView. To achieve the similar functionality you will have create your own layout & add it explicitly to the ListView & manage it's state.
Rupesh
I suppose you use a ListAdapter on your ListView? Just add a "footer item" to your ArrayList/ whatever of list items. In the getView method, check if the item is your footer, then set a custom layout on it where you have your buttons or whatever. Just ask if it's not clear what I mean.
To achieve this scenario when list is empty:
Create to adapter and assign them layouts:
when list is not empty assign adapter to it with layouts.
when list is empty assign list a dummy adapter and with one initial value.
donot assign value to dummy layout in getview or give size to dummy view to 0 dp.
so you can achieve this scenario...
ask further i have solve this scenario
public void addFooterView (View v)
Added in API level 1
Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Parameters
v
The view to add.
I am populating a TableLayout (i.e. rows and columns) with data (strings).
When I click on a cell, I would like the data stored in this cell to be displayed in my console.
How can I do that? Is there any other way than to go over IDs?
As stated in http://developer.android.com/resources/tutorials/views/hello-tablelayout.html , there is no Column or TD (Table Data) or Cell equivalent in android. Each element is treated as an individual cell unless otherwise noted.
Having this in mine, and given the fact that you don't specify what kind of View you're using inside your rows, may I guess it's a Button which you can of course click with something like this:
Button b = (Button)findViewById(R.id.oneOfMyButtons); // You could create this "on the go"
b.setOnClickListener(new View.OnClickListener{
public void onClick(View v){
System.out.println(v.getText());
}
}
Hope this helps.
TableLayout extends LinearLayout, which do not have an OnItemClickListener method. You will need to implement OnClickListener in the child Views instead.
What you can do instead is to use a GridView, that implements AdapterView and thus you can use OnItemClickListener
http://developer.android.com/resources/tutorials/views/hello-gridview.html
http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29
abstract void onItemClick(AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.