I have an activity that displays a list of restaurant dishes. Each view of the list has a number picker associated with it. The application runs fine, however when I start the activity to display the list it crashes...
This is my class file.
NumberPicker np;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starters);
createTestData();
adapter = new MenuItemArrayAdapter(this, starters);
this.setListAdapter(adapter);
np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);
}
This is my xml layout file for the StartersActivity class.
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="670dp">
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
layout="#layout/pricebar" />
This is my xml layout file for the row in the list.
<?xml version="1.0" encoding="utf-8"?>
<NumberPicker
android:id="#+id/numpick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/dishpic"
android:layout_width="140dp"
android:layout_height="150dp"
android:layout_alignBottom="#+id/numpick"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/reviewBtn"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/item_price"
android:text="ITEM NAME"
android:textSize="20sp" />
<TextView
android:id="#+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/reviewBtn"
android:layout_below="#+id/item_name"
android:layout_marginTop="17dp"
android:layout_toRightOf="#+id/dishpic"
android:text="ITEM PRICE"
android:textSize="20sp" />
<Button
android:id="#+id/reviewBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dishpic"
android:layout_toLeftOf="#+id/numpick"
android:layout_toRightOf="#+id/dishpic"
android:text="#string/reviewBtn" />
It seems to be the number picker that is causing the problem because when I take out the following code it works.
np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);
Any ideas as to what might be the problem? The error is a null pointer exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.StartersActivity}: java.lang.NullPointerException
I have a feeling it is something to do with trying to access the number picker before the list is loaded and it's not seeing it.
To fetch the NumberPicker in the onCreate method it would have to be defined in the main layout. Since it is not you get the NPE.
It depends what you do with the NumberPicker where the best place is to fetch it. It must be specific to the list item view in question.
Each view of the list has a number picker associated with it
The above statement makes me believe that the NumberPickers controls should be referenced and initialized in the MenuItemArrayAdapter in the getView() method, versus in onCreate() how it is now:
np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);
Related
I would like to set text for the spinners in the layout reused. However, only the first spinner is set. How to set text for all spinners with same id?
Also, I would like to ask how add another skillfield.xml to fragment_skill.xml when clicking the imageview?
Thank you.
fragment_skill.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/skillInfo" >
<RelativeLayout
android:id="#+id/OCC"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/occ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/occSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/occPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/occ"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oocSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/occ"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOccSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oocSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ORI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/OCC" >
<TextView
android:id="#+id/ori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/oriSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/oriPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/ori"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oriSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ori"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOriSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oriSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
</RelativeLayout>
skillfield.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/addSkillField" >
<Spinner
android:id="#+id/selectSkill"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp" />
<TextView
android:id="#+id/skillPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text=""
android:textSize="20sp" />
<ImageView
android:id="#+id/addSkill"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
In MainActivity.class
skill = (Spinner) findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
how to setup Spinner with the same ID
It is true that if you do this:
skill = (Spinner) findViewById(R.id.selectSkill);
You will be only able to get the first Spinner. I don't think you can add new Spinner dynamically by using <include>.
Here's the main idea of how to achieve what you need:
In fragment_skill.xml, you need to add an empty container view. This container should be inserted in the place you want to add your Spinner every time the button is clicked. This container will hold your Spinners that are added.
Let's say you want to insert here:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<!-- here is where you want to insert your Spinner -->
<ImageView.../>
</RelativeLayout...>
So you add a LinearLayout at that spot, like this:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<LinearLayout
android:id="#+id/container"
android:orientation="vertical
... />
<ImageView.../>
</RelativeLayout...>
You can add any other type of layout, depending what you need, it doesn't have to be LinearLayout. After that, you grab that LinearLayout from your activity/fragment like this:
LinearLayout mContainer; // make this instance variable
mContainer = (LinearLayout)findViewById(R.id.container);
Also grab the button so that you can set up a listener, let's assume it's called Button mButton:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewSpinner();
});
So when the button is clicked, you will call addNewSpinner(). Now, make a method called addNewSpinner(). Inside, the method, we will be inflating skillfield.xml. The method should look something like this:
public void addNewSpinner(){
View view = getLayoutInflater().inflate(R.layout.skillfield, null);
Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
mContainer.addView(skill);
}
If you are not sure what inflate means, here is the explanation:
Instantiates a layout XML file into its corresponding View objects.
Hope it helps!
I am new to Android development. I have created many screens and all are working quite nicely except one.
I am not sure why screen is not coming up as per XML. Below is my code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip"
android:id="#+id/linearMain">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="#2258A2"
android:layout_marginBottom="10dp"
/>
<TextView
android:id="#+id/assignmentTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/lesson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Lesson : Lecture 5"
/>
<TextView
android:id="#+id/dueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Due Date : Nov 20, 2014"
/>
<TextView
android:id="#+id/marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Marks : 20"
/>
<Button android:id="#+id/btnDownload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"
/>
</LinearLayout>
And here is main class :
public class AssignmentDetail extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_announcement_detail);
Intent intent = getIntent();
//String course = intent.getStringExtra(Assignments.COURSE);
//TextView textview = (TextView) findViewById(R.id.courseTitle);
//textview.setText(course);
String announcementTitle = intent.getStringExtra(Announcement.ANNOUCEMENT_TITTLE);
TextView announcementTitleTextView = (TextView) findViewById(R.id.annoucementTitle);
announcementTitleTextView.setText(announcementTitle);
}
And here is output :
You can see there are 4 textView and one button in my xml but i can see only onr textView in output, why it is so and how to fix it.
Thanks..
Anjum
Make sure following stuff is correct in ur activity :
Make some changes in layout file and see it display that changed stuff
Correct xml file layout is specified at setContentView.
It seems that you are using some other layout xml..try removing textview and run it..if it still shows that Assignment #1 then ur xml itslef is wrong
I think you are not inflating the right XML layout file.
You can see that the TextView that you modify at the end of your code is not present in the XLM file. Therefore the layout that you inflate cannot be the one you are showing us or you would have an error at run time.
One of my textViews is not being adjusted when I call the function:
public void wordList() {
setContentView(R.layout.activity_main);
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
Here's the xml:
If I add android:text="Text" to the xml it works.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:background ="#268496" >
<LinearLayout android:id="#+id/linear"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/prefixText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="12pt"
android:maxLength="1"
android:typeface="sans" />
</LinearLayout>
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
</RelativeLayout>
If you call setContentView() after running it here then it will overwrite this call and set the content to its default (what's in the xml). If you call setContentView() say in onCreate() then you don't need to call it again as long as the TextView is inside that xml.
So, call setContentView() in onCreate() then call your function
wordList();
then
public void wordList() {
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
You have white color of textview's text. So if the background color is white, then you just don't see it.
You're not doing anything "wrong" in your posted example. The kinds of things that may be in play here are:
a) is your TextView within the visible area (hard code some text to see if it shows up)
b) are you sure you're calling the method that sets the text?
c) do you have some other overlapping ID somewhere else that is confusing this process?
It has to be something like that.
I have a default ListView that i have added my custom views for the list items, but the buttons inside of these views are not clickable most of the time. I output a Log.v when the button receives a click event, but i have to tap the button almost a dozen times before it will register the click.
The other problem related tot his that i am having is that when the button is pressed i want an animation to happen revealing a menu sliding out from beneath it. At the moment i have tried several different methods like making a custom class for the views versus just using a layout inflater to get the relativeLayout object for the view, but nothing is working properly. I have even tried using listview.getAdapter().notifyDataSetChanged(); but that only has a pop-into-place for the extended view when i want an animation.
I have searched everywhere and it seems like the only possible solutions are to either rewrite my own custom listview or to use a linearlayout with a scrollview. The latter seems easier but i dont think it is nearly as optimized as the listview is.
Any suggestions would be greatly appreciated, if you need to see some code, please let me know...
Thanks!
UPDATE:
the getView() contains this:
Holder hold;
convertView = friends.get(position);
hold = new Holder();
hold.pos = position;
convertView.setTag(hold);
return convertView;
basically i pass an ArrayList<RelativeLayout>, at the moment, to the Adapter so that i dont have to create a new view each time and so that the animation will stay animated when i scroll down...
inside the OnCreate() for this activity i set that ArrayList<RelativeLayout> with this next code, but this is only temporary as i plan to use another method later, like an Async task or something so that this view contains some data...
RelativeLayout temp;
for(int i=0; i<30; i++){
temp = (RelativeLayout) inflater.inflate(R.layout.list_item, null);
final LinearLayout extraListItemInfo = (LinearLayout) temp.findViewById(R.id.extraListItemInfo);
Button infoBtn = (Button) temp.findViewById(R.id.infoBtn);
infoBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.v("ListItem", "Button has been clicked... ");
extraListItemInfo .setVisibility(View.VISIBLE);
ExpandAnimation closeAnim = new ExpandAnimation(extraListItemInfo , animHeight);
closeAnim.setDuration(750);
closeAnim.setFillAfter(true);
if(extraListItemInfo .getTag() == null || !extraListItemInfo .getTag().equals("expanded")){
extraListItemInfo .getLayoutParams().height = 0;
friendInfoList.startAnimation(closeAnim.expand());
extraListItemInfo .setTag("expanded");
}else if(extraListItemInfo .getTag().equals("expanded")){
extraListItemInfo .startAnimation(closeAnim.collapse());
extraListItemInfo .setTag("closed");
}
//((BaseAdapter) listview.getAdapter()).notifyDataSetChanged(); i tried it here once but then left it
//as the only action inside the listview's onitemclick()
}
});
listItems.add(temp);
}
this is the list item that i am using:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/darkgrey"
android:paddingBottom="5dp" >
<LinearLayout
android:id="#+id/extraListItemInfo "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/listItemInfo"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-10dp"
android:background="#color/grey"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:id="#+id/RelativeLayout04"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height"
android:layout_marginTop="5dp" >
<ImageView
android:id="#+id/ImageView04"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView04"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView03"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height" >
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView02"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/imageView1"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView01"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/listItemInfo"
android:layout_width="wrap_content"
android:layout_height="95dp"
android:background="#drawable/friend_cell_background2x"
android:clickable="true" >
<RelativeLayout
android:id="#+id/leftLayout"
android:layout_width="90dp"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imgCompany"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:scaleType="centerInside"
android:src="#drawable/user2x" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="#drawable/online_indicator2s" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/leftLayout"
android:background="#android:color/transparent"
android:gravity="left|center"
android:orientation="vertical"
android:paddingLeft="5dp" >
<TextView
android:id="#+id/lblCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Name"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/lblReawrdDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Played Offer"
android:textColor="#color/white"
android:textSize="17dp" >
</TextView>
</LinearLayout>
<ImageView
android:id="#+id/imageView4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:src="#drawable/facebook_btn2x" />
<Button
android:id="#+id/infoBtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imageView4"
android:background="#drawable/info_btn2x"
android:clickable="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="13dp"
android:layout_toLeftOf="#+id/infoBtn"
android:text="Follows 30+"
android:textColor="#color/white"
android:textSize="11dp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/textView2"
android:background="#drawable/fan_btn2x"
android:text="Fans 30+"
android:textColor="#color/white"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/imageView4"
android:src="#drawable/google_btn2x" />
</RelativeLayout>
</RelativeLayout>
sorry for any layout problems that may make things difficult to read... but i hope this helps you guys to understand my problem... Thanks
UPDATE 2:
All of these answers have been helpful in some way, but i think the main issue that i must first fix is why the buttons do not receive click events until i have first scrolled away from that listItem, then back to it, then clicked the button again... If someone can help find a solution to THAT i think that everything else will be much easier to solve...
Thanks...
A screenshot as requested, but remember that this shot was taken on a samsung galaxy tab 10.1 and due to me using the same layout for this larger screen, it looks much different from what it does on the phone i usually test with (Motorola droid x that isnt rooted and cant take screenshots...)
Another Update:
I managed to get the clicking and animation working nicely by Extending ArrayAdapter instead of base adapter. Sadly i am still experiencing problems as only the bottom half of the list is clickable. The top half of the list still behaves as before with the very glitchy click events... Any ideas as to what is happening this time? Thanks...
Well this isn't really an answer, but after rewriting this a few times I managed to fix it so that it functions exactly the way I wanted.
This time I left all of the data separate from each view and had each list item be a custom class inheriting RelativeLayout and also implementing its own OnClickListener for its specific infoBtn.
My adapter now simply extends ArrayAdapter<> and overrides this getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView != null && (convertView instanceof FriendListItem2))
((FriendListItem2) convertView).setFriendInfo(friends.get(position));
else
convertView = new FriendListItem2(getContext(), friends.get(position));
return convertView;
}
Finally, in the main activity for this page I simply set the ListView with an adapter that I passed the data to.
This is all much cleaner than I had before and I wish it hadn't taken several rewrites of the code to get it right. Hopefully someone can benefit from this, though I still have no clue why I was getting a problem before.
Thanks for all previous suggestions.
try using this property in the top level layout in which your child views are placed.
android:descendantFocusability="blocksDescendants"
Its a bit tricky but I would suggest that if the above line does not work, try toggling between the removing of focusable=true and focusable="false" from the buttons. It should work.
Cheers!
That is a complex layout to create for every row...
Anyway, when you use the new keyword you are creating a different scope. In short your onClickListener does not see the 30 different extraListItemInfo references you expect it to see.
Try something like this:
#Override
public void onClick(View v) {
LinearLayout extraListItemInfo = v.getParent();
...
add android:onClick="onClick" to your button xml, it'll make it call the onClick() method
I had the same problem. Try taking the "android:clicable="true" " from the Relative Layout. When you do that the activity expects you to do make setOnClickListener to that RelativeLayout . It worked for me
you can fire click event on button from getView() method of ListViewAdapter Class.
See this question
Android : How to set onClick event for Button in List item of ListView.
I have extended my class with a dialog in which I have set content view and other button actionlisteners etc... what is happening is that when I create my dialog it shows background properly.
but opening it again and again is crating problems with content view, things are added haphazardly and most of the UI elements are repeating and background image is disappeared.
I know that dialog is created once and is used as a cache, I don't want that I know there is a method in Android Activity removeDialog(int dialogID) but I don't know who to use this, I don't give any id to my dialog I don't know how to give id to dialog.
#dialog code
public class OptionsDailog extends Dialog implements OnClickListener {
public OptionsDailog(Activity pContext) {
super(pContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
mContext = pContext;
setContentView(R.layout.option_menu);
setBasicContents();
}
#options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/rl_root_option_menu">
<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#drawable/bg_episode" android:id="#+id/rl_option_screen">
<LinearLayout android:id="#+id/ll_options" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true">
<Button android:text="Achievements" android:textSize="18sp" android:id="#+id/btn_achievements" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Tutorial" android:textSize="18sp" android:id="#+id/btn_tutorial" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Leaderboard" android:textSize="18sp" android:id="#+id/btn_leaderboard" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_music" android:text="Music" android:textSize="18sp" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_sound" android:textSize="18sp" android:text="Sound" android:layout_marginBottom="10dip" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
</LinearLayout>
</RelativeLayout>
<ImageButton android:background="#drawable/btn_back" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/ib_back_options_menu"></ImageButton>
</RelativeLayout>
#Dialog showing code
final OptionsDailog mDailog = new OptionsDailog(JungleCrashLand.this);
mDailog.show();
The Dialog class works with its own set of identifiers. If you create a dialog with createDialog(int x) you can pass this x as your own identifier. removing the dialog follows the same structure: removeDialog(int x) where x is, again, the identifier of your dialog type. It is done this way to be able to distinguish between different "types" of dialogs, defined by your own, without the need to extend the dialog class.
so basically:
static final int OPTIONS_DIALOG = 0;
[...]
createDialog(OPTIONS_DIALOG);
[...]
removeDialog(OPTIONS_DIALOG);
as you can have only one dialog per activity, this will remove only this dialog and every(!) reference to it. Read http://developer.android.com/guide/topics/ui/dialogs.html to fully understand what is going on here.