Im new to android and Im wondering if there is a standard method which is used to accomplish the retrieval of all the views' positions with in a GridLayout.
I'm dynamically adding and removing views from a GridLayout like this
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addButton.setVisibility(View.INVISIBLE);
Button newButton = new Button(LayoutAnimationsByDefault.this);
gridContainer.getColumnCount();
newButton.setText(String.valueOf("position#"+"huh"));
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridContainer.removeView(v);
addButton.setVisibility(View.VISIBLE);
}
});
gridContainer.addView(newButton, Math.min(-1, gridContainer.getChildCount()));
}
});
I've also tried this..
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addButton.setVisibility(View.INVISIBLE);
Button newButton = new Button(LayoutAnimationsByDefault.this);
Object tag =v.getTag();
if(tag!=null)
{
int position = (Integer)tag;
Log.v("onClick", "Position: " + position);
newButton.setTag(position);
newButton.setText(String.valueOf("position#"+position));
TextView text1 = (TextView) newButton;
String message = "positionis" + position + text1.getText().toString();
Toast.makeText(LayoutAnimationsByDefault.this, message, Toast.LENGTH_LONG).show();
}
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridContainer.removeView(v);
addButton.setVisibility(View.VISIBLE);
}
});
gridContainer.addView(newButton, Math.min(-1, gridContainer.getChildCount()));
gridContainer.getColumnCount();
}
This creates a new button,but with nothing inside, not text or anything
Im trying to figure out how to update and retrieve the updated position of each view in the gridlayout, each time an item is added, removed re-arranged in the GridLayout.
By position I don't mean x,y coordinates, but the 0,1,2,3,4 position as seen with in listviews.
I've tried getColumnCount(); getChildAt(); ActionListener() ActionPerformed() etc.
Any help, pointers, hints?Thanks!!
Related
What I want is when I click the "x" button of the second item, it will remove the second item and then make the "+" of the first item visible. Here is what I try but not work, the "+" button is not showed.
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildAt(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
Try this code,
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildItemId(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
I hope that will help you for solving your problem.
I'm trying to create pinterest like layout. I find a way to achieve this: Android heterogeneous gridview like pinterest?!
However the problem is: I want to view item details after clicking each picture. But as I am using LinearLayout.addView() to add all the ImageViews, I'm not sure how I can get it clickable?
Is there anyway to be able to click each item on the view?
You can do this pretty easily by adding tag information to your image view that can be displayed when clicked.
Adding your image view would look like:
ImageView iv = new ImageView(context);
iv.setOnClickListener(your_listener);
iv.setTag("Item information");
linearLayout.addView(iv);
Then in your click listener:
public void onClick(View v) {
if(v instanceof ImageView) {
String info = (String)v.getTag();
/* Show information here */
}
}
Use this :
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
}
});
or make your class implement the OnClickListner Interface and override the onClick() method
Use:
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do magic
}
});
And in your layout file mark the ImageView as clickable:
<ImageView
...
android:clickable="true">
...
</ImageView>
Building on the code that you linked, you can add a listener to each image view as you create it:
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
linear3 = (LinearLayout) findViewById(R.id.linear3);
for(int i=0;i<n;i++)
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.id.icon);
iv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Your click code
}
}
int j = count % 3; <----
if(j==0)
linear1.addView(iv);
else if(j==1)
linear2.addView(iv);
else
linear3.addView(iv);
}
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 an application where i want to add onclicklistener to my individual items in pager.
Below is my screen shot . please some one tell me how to add onclicklistener to individual image in this current page .
Thanks
below is my code for pager view.
here when 1st time run the app initialize the every view page, when i try to click on image it return only last image info. But i want the current image info.
scroller = ((Pager)findViewById(R.id.scrollView));
indicator = ((PageIndicator)findViewById(R.id.indicator));
indicator.setPager(scroller);
// Pager pager = new Pager(this, attrs)
//indicator.getActivePage();
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pageView = null;
for (int i = 0; i < NUM_PAGES; i++) {
pageView = layoutInflater.inflate(R.layout.page, null);
// ((TextView) pageView.findViewById(R.id.pageText)).setText("Page " + (i+1));
c=4*i;
imageView1 = (ImageView) pageView.findViewById(R.id.imageView1);
imageView1.setImageResource(icons[c]);
imageView2 = (ImageView) pageView.findViewById(R.id.imageView2);
imageView2.setImageResource(icons[c+1]);
imageView3 = (ImageView) pageView.findViewById(R.id.imageView3);
imageView3.setImageResource(icons[c+2]);
imageView4 = (ImageView) pageView.findViewById(R.id.imageView4);
imageView4.setImageResource(icons[c+3]);
// pageView.setBackgroundColor(COLORS[i % COLORS.length]);
imageView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
scroller.addPage(pageView);
}
It seem to be you didn't read information available at android developers.
Use below code to add onClickListener for images. I don't know whether you are using ImageView or ImageButton. I took ImageView.
ImageView iv = new ImageView(/**Context you have to pass**/);
iv.setOnClickListener(new View.OnClickListener) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do whatever you want
}
});
I hope it may help you.
set this on your images..or button
in the xml such that
<Button [...]
android:onClick: "onClick" [...]
/>
"onClick": name this whatever you want your method name that handles clicks to be
This can be done by assigning onClickListeners to Image Buttons. You can use Image buttons to display these images and set onClickListeners on them. Perhaps something like this can help?
ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Do your stuff here
}
});
If you use ImageView then set the property OnClick to true.
Please help.
As I have stated in the title I am trying to make that individual elements of a row of a List adapter launch different actions depending on what the user click.
It "kind of" works but it takes LONG for it to react to user clicks. What is it that I am doing wrong?
Thanks in advance,
So I tried the following code in
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Cursor c = (Cursor) this.getListAdapter().getItem(position);
// c.moveToNext();
prescription_id = c.getString(0);
TextView pName = (TextView) v.findViewById(R.id.text2);
TextView paName = (TextView) v.findViewById(R.id.text3);
TextView rDateLabel = (TextView) v.findViewById(R.id.textView1);
TextView rDate = (TextView) v.findViewById(R.id.text4);
TextView rLeftLabel = (TextView) v.findViewById(R.id.text5);
TextView rLeft = (TextView) v.findViewById(R.id.text6);
ImageView callPhone = (ImageView) v.findViewById(R.id.Call_Pharmacy);
pName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
pa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDateLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeftLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeft.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
callPhone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Some Code
}
});
}
All those onClick listeners (those on single sub-views of one ListView element) probably shouldn't be here in the onListItemClick method, but in the getView method of your Adapter instead (with proper use of the convertView argument).
The way you do it seems quite wrong, maybe your onListItemClick method isn't even needed if you correctly implement the various onClick listeners at the right place.
Using an xml based layout for your list item is key here. Set each individually clickable View with two attributes android:clickable="true" and android:onClick="<your click handler>" the method will need to be implemented with this signature: public void <your click handler> (View v) {...} in your Activity. A side note is that you'll have to make a design decision to implement a click handler to overlap handling (one click hanlder for more than one View) or a single view handler per View, the former is best for when click are substantially similar in function and the latter is when they are different.
The next step is to implement the click handler, the key here is to use ListView.getPositionForView(View v) so you can associate the row, the data, and the View clicked.
Don't forget to implement ListActivity.onListItemClick() as a catch-all for clicking on the root layout of the list item and as a catch-all for Views that don't have their own onClick handler set.
The above technique will have good performance and makes use of several Android API's to speed your development.
If you decide to implement the listeners in code, please study getView() closely (as darma mentioned) and for the sake of performance (if you have several items in your list) reuse the click listeners with the above discussion about how to associate the data and row.