I am trying to add textview dynamically in alertdialog. But end up with null pointer error.
My code...
att_layout = (LinearLayout)findViewById(R.id.att_layout);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);
TextView con = (TextView) dialogView.findViewById(R.id.textView29);
con.setText("Consultation on " + item.getString(4));
TextView des = (TextView) dialogView.findViewById(R.id.textView28);
des.setText(item.getString(2));
TextView pre = (TextView) dialogView.findViewById(R.id.textView31);
pre.setText(item.getString(3));
TextView fee = (TextView) dialogView.findViewById(R.id.textView32);
fee.setText(item.getString(5));
if (att.getCount() == 0) {
att.close();
} else {
//here is the part where error occurs
View addView = inflater.inflate(R.layout.att_values, null);
TextView att_name=(TextView)addView.findViewById(R.id.textView34);
att_name.setText(att.getString(3));
TextView att_value=(TextView)addView.findViewById(R.id.textView26);
att_value.setText(att.getString(4));
att_layout.addView(addView);
}
adapter1 = new GridViewAdapter(FilePathStrings, getApplicationContext());
grid = (GridView) dialogView.findViewById(R.id.gridview);
grid.setAdapter(adapter1);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + FilePathStrings[position]), "image/*");
startActivity(intent);
}
});
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
alert_label_editor.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="20dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView29"
android:layout_weight="1"
android:gravity="center"
android:textSize="24sp"
android:layout_marginBottom="10dp"
android:textStyle="normal|bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView27"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView28"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Prescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView30"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView31"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="fee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView33"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView32"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/att_layout"></LinearLayout>
</LinearLayout>
</ScrollView>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
att_values.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView34"
android:layout_weight="1"/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView26"
android:layout_weight="1"/>
When i add those dynamically adding code.. its stops worling... The whole other code is working fine...
Logcat...
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
How can i resolve this issue.?
Your alert_label_editor.xml file which contains the R.id.att_layout LinearLayout is not inflated until you run this line:
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
So when you try to find it in the current view, it is not found and att_layout is null
Move this line after you actually inflate the layout, and use dialogView:
att_layout = (LinearLayout) dialogView.findViewById(R.id.att_layout);
Related
I have a ListView, where i changed appearence of row, but listview have size of one row, instead of fullscreen.
and my scrollview is working but listview is not working.
activity_graph_view.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_graph_table_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="00dp"
android:background="#color/background"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appbar"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layoutTableOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Table View"
android:textSize="#dimen/subheading"
android:textColor="#color/subheading"
android:textAllCaps="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lv_spirometer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/mdtp_button_selected"
android:dividerHeight="1dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:padding="10dp"
android:text="Graph View"
android:textAllCaps="false"
android:textColor="#color/subheading"
android:textSize="#dimen/subheading" />
</LinearLayout>
<com.jjoe64.graphview.GraphView
android:id="#+id/grapfinal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:id="#+id/grphtextMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/grphtextMain2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor2"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:background="#color/red_btn_bg_color"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:padding="10dp"
android:text="Graph View"
android:textAllCaps="false"
android:textColor="#color/subheading"
android:textSize="#dimen/subheading" />
</LinearLayout>
<com.jjoe64.graphview.GraphView
android:id="#+id/grapfinal1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:id="#+id/grphtextMain1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor1"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:background="#color/red_btn_bg_color"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
spirometer_item.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:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardBackgroundColor="#fff4f4f3"
app:cardElevation="10dp"
app:cardPreventCornerOverlap="true"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_fvc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FVC 3.15 L" />
<TextView
android:id="#+id/tv_fev1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FEV1 2.44 L" />
<TextView
android:id="#+id/tv_pef"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PEF 3.74 L/s" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
CustomAdapter1.java
public class CustomAdapter1 extends BaseAdapter {
ArrayList<GetUserSpirometer> arrayList;
public CustomAdapter1(ArrayList<GetUserSpirometer> arrayList){
this.arrayList=arrayList;
Log.e("arraylist length",""+arrayList.size());
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= getLayoutInflater();
ViewHolder1 holder=new ViewHolder1();
convertView = inflater.inflate(R.layout.spirometer_item, parent, false);
holder.tv_date=(TextView)convertView.findViewById(R.id.tv_date);
holder.tv_fvc=(TextView)convertView.findViewById(R.id.tv_fvc);
holder.tv_fev1=(TextView)convertView.findViewById(R.id.tv_fev1);
holder.tv_pef=(TextView)convertView.findViewById(R.id.tv_pef);
String[] arr = getDate(Long.parseLong(arrayList.get(position).get_date()), "MMM dd, yyy/hh:mm a").split("/");
holder.tv_date.setText(arr[0] + "\n" + arr[1]);
holder.tv_fvc.setText(arrayList.get(position).get_userfvc());
holder.tv_fev1.setText(arrayList.get(position).get_userfev1());
holder.tv_pef.setText(arrayList.get(position).get_userpef());
convertView.setTag(holder);
return convertView;
}
}
Items are not scrolling.
how to solve this. please help.
thanks in advance.
Then you need to change itemView's xml file ,
android:layout_height="match_parent"
instead of like ,
android:layout_height="wrap_content"
Simple change in spirometer_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
....Your design code
</LinearLayout>
I was having problem with the same from such a long time. Then I found a solution that worked for me.
Add a ListViewHelper java class. Here below is code for ListViewHelper.java
package com.molescope;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ListViewHelper {
public static void getListViewSize(ListView listView){
ListAdapter adapter = listView.getAdapter();
if(adapter!=null){
int totalHeight = 0;
//setting list adapter in loop tp get final size
for (int i=0; i<adapter.getCount(); i++){
View listItem = adapter.getView(i, null, listView);
listItem.measure(0,0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview items in adapter
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() *
(adapter.getCount()-1));
listView.setLayoutParams(params);
}else{
return;
}
}
}
And after adding this java file, in your code wherever you are setting adapter to listview, right after that line add the code below:
ListView myList=(ListView) findViewById(R.id.listView);
myList.setAdapter(new ArrayAdapter<String>.
(this,android.R.layout.simple_list_item_1, listview_array));
ListViewHelper.getListViewSize(myList);
I'm trying to hide a linearlayout on a button click and display another linearlayout which is in the same layout file. But the linearlayouts are null inside the onclick of the button.
The displayLayout and editLayout are null.
displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
editLayout = (LinearLayout) findViewById(R.id.editLayout);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
editLayout = (LinearLayout) findViewById(R.id.editLayout);
displayLayout.setVisibility(View.GONE); //error occurs here
editLayout.setVisibility(View.VISIBLE);
}
});
}
Here's the layout file:
<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"
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="com.prematixsofs.taxiapp.EditUserDetails">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
<!--Display UserDetails Layout-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/displayLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<TextView
android:id="#+id/phNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<TextView
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/custom_edittext"
android:gravity="center"
android:inputType="textEmailAddress"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<Button
android:id="#+id/edit"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#drawable/button_custom"
android:text="Edit"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<!--Edit Layout -->
<LinearLayout
android:id="#+id/editLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="#+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<EditText
android:id="#+id/editPhNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<EditText
android:id="#+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<EditText
android:id="#+id/editConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
</LinearLayout>
Just fix your view ID in your code
displayLayout = (LinearLayout) findViewById(R.id.displayLayout);
editLayout = (LinearLayout) findViewById(R.id.editLayout);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
displayLayout.setVisibility(View.GONE);
editLayout.setVisibility(View.VISIBLE);
}
});
}
error code displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
right code displayLayout = (LinearLayout) findViewById(R.id.displayLayout);
you set a not exist id.
In my application i have one custom list view. whenever i click the close image list item, the list item layout must be removed with space. i used visibility:gone attribute, the list item layout deleted but the space is visible. I am using listview in scroll view ,,that was also not working....please help me out.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.recharge_listitem, null);
TextView accountnumber = (TextView) convertView
.findViewById(R.id.accountnumber);
TextView servicename = (TextView) convertView
.findViewById(R.id.servicetypname);
TextView planamount = (TextView) convertView
.findViewById(R.id.rechargeamount);
final ImageView close = (ImageView) convertView
.findViewById(R.id.crosse_close_img);
final LinearLayout layoutrecharge=(LinearLayout)convertView.findViewById(R.id.rechargelay);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layoutrecharge.setVisibility(View.GONE);
}
});
accountnumber.setText("Rs."+bpData.get(position).getAccountNumber());
servicename.setText(bpData.get(position).getServicename());
planamount.setText(bpData.get(position).getRechargeamount());
return convertView;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e365a">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:text="PAYMENT"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:text="Your Recharges"
android:textColor="#1e365a"
android:textSize="20dp" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="4dp"
android:background="#drawable/dottedline_view" />
<ListView
android:id="#+id/recharge_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</ListView>
<View
android:layout_width="fill_parent"
android:layout_height="0.8dp"
android:layout_marginTop="12dp"
android:background="#1e365a" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Amount to be Paid"
android:textColor="#1e365a"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Rs.130"
android:textColor="#1e365a"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="100" >
<EditText
android:id="#+id/ev_b_dob"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="50"
android:hint="Enter coupn code"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/bt_apply"
style="#style/payment_button_style"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="-5dp"
android:layout_weight="20"
android:gravity="center"
android:text="Apply" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp">
<CheckBox
android:id="#+id/cb_reload_cash"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pay using Reload Cash"
android:textColor="#1e365a"
android:textSize="20dp"
android:layout_marginLeft="12dp"/>
</LinearLayout>
<Button
android:id="#+id/atmdebit_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="#F93249"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="ATM/DEBIT/CREDIT"
android:textColor="#ffffff"
android:textSize="20dp"/>
<Button
android:id="#+id/netbankng_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="#F93249"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="NET BANKING"
android:textColor="#ffffff"
android:textSize="20dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
please help me.
custom listview item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rechargelay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/accountnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8008505106" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="(" />
<TextView
android:id="#+id/servicetypname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vodafone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")" />
</LinearLayout>
<TextView
android:id="#+id/rechargeamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RS.30"
android:layout_weight="1" />
<ImageView
android:id="#+id/crosse_close_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Set View.GONE for View is not right way to remove row from ListView. to remove row remove select row item from Adapter data-source and call notify data change method of adapter to populate changes:
close.setTag(position);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int deletePos=Integer.parseInt(v.getTag().toString());
bpData.remove(deletePos);
notifyDataSetChanged();
}
});
Set visibility View.GONE of SCROLL VIEW or other parent of list view....
I have a list view and want to perform clickable event on it. I did it before. but now its not working. I have added my XML as well. I just need to move from one actvity to other on click in list view.
CustomFinalSubmit_ItemDetail item = new CustomFinalSubmit_ItemDetail(Final_Submit.this , R.layout.customview_finalsubmit_itemdetails, itemInfo);
itemList.setAdapter(item);
itemList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
FinalSubmitItem pos = itemInfo.get(position);
String itemType = pos.getItemType();
String itemCountry = pos.getItemCountry();
String itemSerial = pos.getItemNo();
pos.setChecked(true);
Intent inn = new Intent(getApplicationContext(), FinalSubmitDetails.class);
inn.putExtra("itemType", itemType);
inn.putExtra("itemCountry", itemCountry);
inn.putExtra("itemSerial", itemSerial);
startActivity(inn);
}
});
Here is my main Xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/green">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:layout_marginTop="10dip"
android:orientation="vertical"
android:background="#0B3B0B">
<ListView
android:id="#+id/CustomerDetailList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/yellow"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:id="#+id/Itemtype"
android:paddingTop="5dp"
android:background="#color/green"
android:textColor="#color/yellow"
android:layout_marginLeft="5dip"
android:text="Item Type"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingTop="5dp"
android:id="#+id/txtcountry"
android:background="#color/green"
android:textColor="#color/yellow"
android:text="Country"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingTop="5dp"
android:id="#+id/txtItemNumber"
android:background="#color/green"
android:textColor="#color/yellow"
android:text="Item No."/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:id="#+id/itemChck"
android:button="#drawable/custom_checkbox"
android:paddingTop="5dp"
android:padding="5dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_margin="5dp"
android:layout_height="1dp"
android:background="#color/white"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/itemList"
android:background="#088A08"
android:divider="#color/white"
android:dividerHeight="1dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I did it this way:
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
your code
}});
Just add this much code to my customview
Recently I had the same problem after adding a ToggleButton to my list items. The solution for me was to add android:descendantFocusability="blocksDescendants" to the layout of the item.
Here is the whole XML:
<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="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:paddingLeft="15dp"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="#+id/watch_roadName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Addresse"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<ToggleButton
android:id="#+id/watch_button_arrived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/watch_roadName"
android:layout_alignParentRight="true"
android:textOn="#string/arrived"
android:textOff="#string/arrived"
android:text="#string/arrived" />
</RelativeLayout>
Similar answers to the same issue can be found here:
How to fire onListItemClick in Listactivity with buttons in list?
and here
Android custom ListView with ImageButton is not getting focus
in the widgets(Textview or button whatever it is) of your inflator layout just add:
android:focusable="false"
android:focusableInTouchMode="false"
and in parent layout of your inflator add:
android:descendantFocusability="blocksDescendants"
I am trying to implement a Listview with alphabet indexing . I succeed in displaying it but the problem is the text in Listview goes inside the alphabets rather than displaying horizontally. Please help . Song name went inside the alphabet indexer. Pasting below the xml file and code:
List item xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="songPicked"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/sort_key_layout"
android:layout_width="fill_parent"
android:layout_height="18dip"
android:background="#EAEAEA" >
<TextView
android:id="#+id/sort_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textColor="#000000"
android:textSize="13sp" />
</LinearLayout>
<TextView
android:id="#+id/tv_song_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
Listview xml:
<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" >
<FrameLayout
android:id="#+id/title_layout"
android:layout_width="fill_parent"
android:layout_height="18dip"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:background="#EAEAEA" >
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textColor="#000000"
android:textSize="13sp" />
</FrameLayout>
<LinearLayout
android:id="#+id/indexer_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#+id/lv_song_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#null"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
</ListView>
</LinearLayout>
</RelativeLayout>
Adapterclass:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.d(this.getClass().getName(), "inside getview");
if(convertView == null) {
Log.d(this.getClass().getName(), "inside if getview & position is"+position);
convertView = songInf.inflate(R.layout.song, null);
holder = new ViewHolder();
holder.sortKeyLayout = (LinearLayout) convertView.findViewById(R.id.sort_key_layout);
holder.sortKey = (TextView) convertView.findViewById(R.id.sort_key);
holder.songName = (TextView) convertView.findViewById(R.id.tv_song_title);
holder.songArtist = (TextView)convertView.findViewById(R.id.tv_song_artist);
convertView.setTag(holder);
} else {
Log.d(this.getClass().getName(), "inside else getview & position is"+position);
holder = (ViewHolder) convertView.getTag();
}
Song currSong = songs.get(position);
holder.songName.setText(currSong.getTitle());
holder.songArtist.setText(currSong.getArtist());
int section = mIndexer.getSectionForPosition(position);
if (position == mIndexer.getPositionForSection(section)) {
holder.sortKey.setText(currSong.getSortKey());
holder.sortKeyLayout.setVisibility(View.VISIBLE);
} else {
holder.sortKeyLayout.setVisibility(View.GONE);
}
return convertView;}![enter image description here][1]
I guess the problem is with your layout Listview xml you are using relative layout they will obviously overlap each other until or unless you place them relative to each other.
You have to take out your lv_song_list from indexer_layout
You can refer
<FrameLayout
android:id="#+id/title_layout"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:background="#a86bf0"
android:visibility="gone" >
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textColor="#000000"
android:textSize="13sp" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="10" >
<ListView
android:id="#+id/indexed_listView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:cacheColorHint="#null"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
<LinearLayout
android:id="#+id/indexer_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" />
</LinearLayout>
<RelativeLayout
android:id="#+id/section_toast_layout"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_centerInParent="true"
android:background="#drawable/black_base_rounded_transparent_50_rad2"
android:visibility="gone" >
<TextView
android:id="#+id/section_toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#style/text_white_medium_franklin_bkcd"
android:textSize="30sp" />
</RelativeLayout>