android view is inflated, found but has no height - java

I have a view alertpopup.xml which contains in it:
<LinearLayout
android:id="#+id/navBarTop"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:background="#drawable/alert_nav_bar"
android:clickable="true"
android:orientation="horizontal"
android:padding="0dp"
android:visibility="visible">
<RelativeLayout
android:id="#+id/navBarBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<com.w.view.text.WTextView
android:id="#+id/navBarDirection"
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#drawable/big_direction_right"
android:gravity="center"
android:textColor="#color/solid_white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
..
this line is executed:
inflater.inflate(R.layout.alert_nav_bar, this);
and later this one:
public NavBar(View view, BottomBar bottomBar) {
this.view = view;
this.bottomBar = bottomBar;
bottomBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
NavBarManager navBarNativeManager = NativeManager.getInstance().getNavBarManager();
navBarNativeManager.showNavigationResult();
}
});
instImages = instImagesRight;
topView = view.findViewById(R.id.navBarTop);
nextView = view.findViewById(R.id.navBarThen);
boxView = view.findViewById(R.id.navBarBox);
how come all heights are 0 only?
especially topview which has fixed height?
<LinearLayout
android:id="#+id/navBarTop"
android:layout_width="match_parent"
android:layout_height="60dp"

it looks like you have navBarBox set to wrap content (height) and navBarDirection height to match parent, which means it won't try and calculate how high it should be and just match it's parent which starts at 0 and increases as it's children does, so it stays at 0.
try making navBarDirection wrap content also.
edit: you could also make navBarBox match parent, but I'm not sure what else lives in navBarTop

Related

Relative Layout adding views programmatically one below other not aligning correctly

I am trying to add Text views programmatically to Relative layout parent one below other. However, the first view is not aligning correctly while the rest of the view got aligned correctly. See attached image.
My code:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
Relative layout xml where I add my views to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Back"
android:textColor="#android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Next"
android:textColor="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I read some posts related to this issue and followed the guidelines from them but that did not help.
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.
I changed the line
textView.setId(i)
to
textView.setId(ViewGroup.generateViewId())
Thanks.

pushed off by the recycler view button and notifyItemRemoved

So my problem is: when I add many items to the Recycler View, the button below which has to add more items, dissapear because he is pusshed off from the screen by the recycler. before add and after. There is no button :/
here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".AddActivityFormEvent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="14"
android:id="#+id/dateField"
android:singleLine="true"
android:hint="#string/data"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:drawableLeft="#drawable/ic_today"
android:onClick="showDatePickerDialog"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="time"
android:singleLine="true"
android:hint="#string/czas"
android:id="#+id/timeField"
android:layout_alignTop="#+id/dateField"
android:layout_toRightOf="#+id/dateField"
android:layout_toEndOf="#+id/dateField"
android:drawableLeft="#drawable/ic_time"
android:onClick="showTimePickerDialog"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/relativeLayout"
android:layout_below="#+id/dateField">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/activityRecyclerView"
android:elevation="15dp"
android:layout_below="#+id/relativeLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addActivityEvent"
android:text="Dodaj aktywność"
android:layout_gravity="bottom"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/testButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
By the way, how to get actual position of the view holder? I ask because when I delete item first time, it return correct index, but each next time it is one more, for example when I delete first item(index 0) it returns 1
here is an adapter with view holder:
public class ActivityFormEventAdapter extends RecyclerView.Adapter<ActivityFormEventAdapter.ActivityFormEventViewHolder>{
ArrayList<ActivityFormEvent> adapter_list = new ArrayList<>();
AddActivityFormEvent addActivityFormEvent;
private static Button button;
Context ctx;
public ActivityFormEventAdapter(ArrayList<ActivityFormEvent> adapter_list,Context ctx){
this.adapter_list=adapter_list;
this.ctx=ctx;
addActivityFormEvent= (AddActivityFormEvent) ctx;
button= (Button) addActivityFormEvent.findViewById(R.id.testButton);
}
public ActivityFormEventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_form_event_card,parent,false);
ActivityFormEventViewHolder activityFormEventViewHolder = new ActivityFormEventViewHolder(view,addActivityFormEvent);
return activityFormEventViewHolder;
}
#Override
public void onBindViewHolder(ActivityFormEventViewHolder holder, final int position) {
holder.activityName.setText(adapter_list.get(position).getActivityForm().getName());
holder.duration.setText(adapter_list.get(position).getTime());
holder.burnedKcalPerHour.setText(adapter_list.get(position).getActivityForm().getBurnedKcalPerHour());
holder.burnedKcal.setText(adapter_list.get(position).getBuriedKcal());
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
adapter_list.remove(position);
notifyItemRemoved(position);
}
});
}
#Override
public int getItemCount() {
return adapter_list.size();
}
public static class ActivityFormEventViewHolder extends RecyclerView.ViewHolder{
AddActivityFormEvent addActivityFormEvent;
EditText activityName;
EditText duration;
EditText burnedKcalPerHour;
EditText burnedKcal;
ImageButton deleteButton;
public ActivityFormEventViewHolder(View itemView,AddActivityFormEvent addActivityFormEvent) {
super(itemView);
activityName = (EditText) itemView.findViewById(R.id.activityName);
duration = (EditText) itemView.findViewById(R.id.duration);
burnedKcalPerHour = (EditText) itemView.findViewById(R.id.burnedKcalPerHour);
burnedKcal = (EditText) itemView.findViewById(R.id.kcalExercise);
deleteButton = (ImageButton) itemView.findViewById(R.id.deleteButton);
this.addActivityFormEvent = addActivityFormEvent;
}
}
}
Okay try this, here bottom view must have minimum height = height of Buttom that attached and it's height must be programmatically set to height of all view - recyclerview content height. Try to change height of bottom view to see the difference in preview.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout"
android:layout_below="#+id/dateField">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/linearLayout"
android:layout_above="#+id/bottom_space_holder"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activityRecyclerView"
android:elevation="15dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addActivityEvent"
android:text="Dodaj aktywność"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout"
android:layout_alignParentStart="true" />
<View
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="#+id/bottom_space_holder"
android:text="Dodaj aktywność"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
This happens because you place the Button below the RecyclerView using layout_below and the RecyclerView's height is set to wrap_content. This means that once the RecyclerView's content exceeds the screens height, the Button will fall off the screen, because it is placed below the RecyclerView.
To fix this issue, you should align your Button to the parent's bottom (using layout_alignParentBottom) and layout your RecyclerView above the button (using layout_above and layout_alignParentTop), this way the Button will always be placed and visible at the bottom of your screen.

Can't Get TableLayout to Programatically Be Created

I've created what I want in xml but I need to do it programatically and I can't get it to work. It's a table layout with one row. In that row there is two more table layouts. The first of which has one row containing a picture and the second table layout has two rows containing two textviews and a picture. Is there some kind of xml to java converter? I've tried to write the code now for a couple of hours and can't get the layouts to work. I'd post a picture but I need 10 rep points first. The whole xml file is in a linear layout if it helps.
<TableLayout
android:id="#+id/tablemain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal" >
<TableRow
android:id="#+id/TableRow1"
android:background="#drawable/border"
android:weightSum="100" >
<TableLayout
android:id="#+id/Tableout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#drawable/border"
android:gravity="center_horizontal" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:padding="3dp"
android:src="#drawable/unknown" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/Tableout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:gravity="center_horizontal"
android:weightSum="100" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border" >
<TextView
android:id="#+id/Judgename"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="7dp"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tablerowctroom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border"
android:weightSum="100" >
<TextView
android:id="#+id/Courtroom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="70"
android:padding="7dp"
android:text="Room"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:src="#drawable/umid" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
This is the new code I'm trying to use based on Nathan Walter's advice. However, It'll set the first set text but the rest are just the default value. Although the layout looks right. How can I set the textfields for each individual view when the method is called.
public void Display(String name, String ctroom) {
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
TextView jn = (TextView) findViewById(R.id.JudgenameCourts);
jn.setText(name);
TextView ct = (TextView) findViewById(R.id.Courtroomnew);
ct.setText(ctroom);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
jn.setText("Name1");
ct.setText("Courtroom1");
}
I think the code you want looks something like this. Let me know if it makes sense.
// Create an array of things to create views from.
// You will probably be getting your array from somewhere else
YourObjectHere[] things = new YourObjectHere[2];
things[0] = new YourObjectHere();
things[1] = new YourObjectHere();
// Get a reference to a LinearLayout to hold your views
LinearLayout list = (LinearLayout) findViewById(R.id.list);
// Loop through your list of objects
for(int i = 0; i < things.length; i++) {
// Inflate an instance of your layout
View listItem = getLayoutInflater().inflate(R.layout.child, null);
// Set the TextViews to the appropriate things
((TextView) listItem.findViewById(R.id.name)).setText(things[i].getName());
((TextView) listItem.findViewById(R.id.type)).setText(things[i].getType());
// Add the inflated view to the list
item.addView(child);
}

Unable to dynamically add EditText to LinearLayout

I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.

Android List onListItemClick not working

I've got a new List of things that needs to be clicked but this one isn't working. onListItemClick is never called. I have another one in my app that has been working as expected and I can't figure out what the difference is. I've seen the people saying to change focusable but I've tried that a bunch of different ways with no effect. So here's some code.
Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
v_ProjectInvestigatorSiteContact project = (v_ProjectInvestigatorSiteContact) this.getListAdapter().getItem(
position);
Intent myIntent = new Intent(this, Details.class);
myIntent.putExtra(res.getString(R.string.project), project);
startActivity(myIntent);
}// onListItemClick
Not Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
final v_SitePeople vSitePeople = (v_SitePeople) this.getListAdapter().getItem(
position);
AlertDialog.Builder builder = new AlertDialog.Builder(Share.this);
builder.setTitle(res.getString(R.string.forgot_password_check_dialog_title))
.setMessage(res.getString(R.string.share_check_dialog_text))
.setPositiveButton(res.getString(R.string.send), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
sendShareEmail(vSitePeople);
}
}).setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// cancelled, so do nothing
}
});
AlertDialog msgBox = builder.create();
msgBox.show();
}// onListItemClick
Working XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/app_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/search_gradient" >
<ImageView
android:id="#+id/searchBoxIcon"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerCrop"
android:src="#drawable/action_search" >
</ImageView>
<EditText
android:id="#+id/searchBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="#+id/searchBoxIcon"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="#+id/searchBoxIcon"
android:background="#drawable/search_box"
android:hint="#string/search_hint"
android:inputType="text"
android:maxLines="1"
android:minHeight="30sp"
android:paddingBottom="2dp"
android:paddingLeft="25sp"
android:paddingTop="2dp"
android:textColor="#ff000000" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Not Working XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/share_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Here is more on how the broken one works, just in case you want more code.
Not Working Row XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical"
android:padding="6dp"
android:background="#ffffffff"
android:layout_margin="10dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/moreInfo"
android:gravity="center_vertical"
android:minHeight="20sp"
android:textColor="#ff000000"
android:textStyle="bold" />
</RelativeLayout>
Not Working View Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.share_row, null);
convertView.setTag(holder);
}// if
else
{
holder = (ViewHolder) convertView.getTag();
}
v_SitePeople i = items.get(position);
if (i != null)
{
TextView topText = (TextView) convertView.findViewById(R.id.toptext);
topText.setGravity(Gravity.CENTER_VERTICAL);
topText.setMinHeight(40);
if (topText != null)
{
if (i.SitePerson != null)
{
if (i.PersonTitle != null)
{
topText.setText(String.format(i.SitePerson + ", " + i.PersonTitle));
}
else
{
topText.setText(i.SitePerson);
}
}// if has ProtocolNumber
else
{
if (i.Nickname != null)
{
topText.setText(i.Nickname);
}
}// if does not have ProtocolNumber
}// if
}// if
return convertView;
}// getView
Thank you so much for your help.
I figured it out. I never posted the relavant code, or I'm sure you guys would have found it for me.
My not working adapter had a isEnabled method copied from my functioning adapter that disabled items of type 0. But since my new list is all just one type everything was disabled.
Sorry for the trouble and thank you for your efforts.
You haven't shown where you initialise your list and adapter, but you need to make sure the onClickListner is set. Something like:
myListView.setOnItemClickListener(this);
edit: 'this' assuming initialization is done in the same class as the click listeners appear.
When you're using a ListView, you have mainly two layouts:
1)The layout where you have your listView
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize"
android:clickable="false"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:clipToPadding="false"
android:paddingLeft="#dimen/list_left_padding"
android:paddingRight="#dimen/list_right_padding"
android:paddingTop="#dimen/list_separator"
android:dividerHeight="#dimen/list_separator"
android:paddingBottom="#dimen/list_separator" />
</RelativeLayout>
2)The layout which contains each item for your ListView.
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_responsive"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:id="#+id/sampleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:clipToPadding="false">
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/textViewSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="5dp"
android:text="Sample Text"
android:textSize="17sp"
app:typeface="roboto_regular" />
</RelativeLayout>
</RelativeLayout>
If you search on StackOverFlow about this problem, you will got the following instructions to solve it:
1) Add
android:descendantFocusability="blocksDescendants"
to the root of your layout
2) Add
android:clickable="false"
android:focusable="false"
To your item.
You can use these two options separately. The first one have helped me. But, you need to pay attention on where you need to put these attributes in your XML File. Put them in your ITEM layout, not in your ListView Layout. I'm saying this, because I was using that solution, but i was putting on the wrong xml. So, it just pay attention and it will work very well.

Categories

Resources