LinearLayout is null inside button's onClick - android

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.

Related

how to expand and collapse the textView which is having EditText as its child

i am new to android..In my xml file i have used three textviews,
inside that i have used three edittext for every textview..
what i want is when i press a particular textview it has to expand to get their
child(edittexts)..and when i again press the textview should collapse back.
Now in my code when i press textview it expands,but when i press again its not collapsing.
Below is my code..any help would be appreciated...thanks in advance..
Java file
CategoryPage.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class CategoryPage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_page);
TextView btnProfile = (TextView) findViewById(R.id.btnprofile);
TextView btnSettings = (TextView) findViewById(R.id.btncitizen);
TextView btnPrivacy = (TextView) findViewById(R.id.btnprivacy);
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
btnProfile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.VISIBLE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.VISIBLE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnPrivacy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.VISIBLE);
}
});
}
}
XML File
activity_category_page.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#fff">
<LinearLayout android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:src="#drawable/miiskylogo" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/header" >
<LinearLayout
android:id="#+id/root_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:orientation="vertical">
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:text="#string/Text"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textview1"
android:text="#string/Text2"
android:layout_marginBottom="10dp"
android:textSize="13dp"
/>
<TextView
android:id="#+id/btnprofile"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:background="#drawable/verifyedit"
android:text="Profile Vault"
android:textColor="#000"
android:textSize="15dp" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/salutation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Salutation" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/firstname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="First Name" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/middlename"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Middle Name" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile4"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Last Name" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/btncitizen"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/verifyedit"
android:text="Citizenship/Registration"
android:textColor="#000"
android:textSize="15dp" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelSettings1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/currentaddress"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Current address" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/houseno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="House no" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/roadno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Road no" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings4"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Location" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/btnprivacy"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/verifyedit"
android:text="Banking"
android:textColor="#000"
android:textSize="15dp" />
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelPrivacy1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/bankacc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Bank account no" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/panelPrivacy2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/acctype"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Account Type" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelPrivacy3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/bankcredit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="#drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Bank Credit Card No" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
check if view is visible or not onClick of TextView:
if(panelProfile.getVisibility() == View.VISIBLE) {
panelProfile.setVisiblity(View.GONE);
} else {
panelProfile.setVisiblity(View.VISIBLE);
}
Note: You are declaring below code multiple times, instead just do it only once.
View panelProfile = findViewById(R.id.panelProfile);
same with,
View panelSettings = findViewById(R.id.panelSettings);
View panelPrivacy = findViewById(R.id.panelPrivacy);

How to remove list view item in list view android?

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....

getChildAt() InvocationTargetException

In xml of row_layout I've set a delete button as follows
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:background="#ffffff"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingLeft="10sp"
android:weightSum="100" >
<LinearLayout
android:id="#+id/Layout"
android:layout_width="150sp"
android:layout_height="50dp"
android:layout_weight="90"
android:orientation="vertical" >
<TextView
android:id="#+id/firstTextView"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10sp"
android:gravity="top"
android:text="Activity"
android:textColor="#000" />
<TextView
android:id="#+id/firstTextView2"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="top"
android:text="Sub-Activity"
android:textColor="#000" />
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/edit" />
<ImageView
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onClickDelete"
android:src="#drawable/neg" />
</LinearLayout>
here is onClickDelete method
public void onClickDelete(View v)
{
ListView lvItems = getListView();
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
TextView child = (TextView) vwParentRow.getChildAt(0);
child.setText("Delete Clicked");
}
When i press delete, it shows invocationtargetexception at (TextView child = (TextView) vwParentRow.getChildAt(0);)
Sorry if this is very basic mistake. I am new to android.
Thank you for your reply.
It's better
TextView child = (TextView)findViewById("yourtextfieldIDHere");
child.setText("Delete Clicked");

How to populate the same view with layout inflater?

I have a layout and every time my button is pressed I want to add a group of views stored in another layout. This is the way I tried it so far. The error in logcat is:
"E/AndroidRuntime(7900): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
Code:
material_cost.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/materialCostText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Κόστος Υλικών"
android:textColor="#FF0000"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center_horizontal"
android:layout_margin="5dp"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="4dp"
android:background="#33B5E5" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/materialWrapper">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:id="#+id/btnMaterialSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Αποθήκευση"
android:layout_weight="50"/>
<Button
android:id="#+id/btnMaterialAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Προσθήκη πεδίου"
android:layout_weight="50"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
material_cost_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/materialItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Υλικό"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Τιμή μονάδας"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Ποσότητα"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="4dp"
android:background="#33B5D8" />
</LinearLayout>
</LinearLayout>
And finally my class:
public class MaterialActivity extends Activity implements OnClickListener {
Button btnAdd;
LinearLayout rootLayout;
View layoutItem;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.material_cost);
btnAdd = (Button) findViewById(R.id.btnMaterialAdd);
rootLayout = (LinearLayout) findViewById(R.id.materialWrapper);
layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, rootLayout,false);
rootLayout.addView(layoutItem);
btnAdd.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.btnMaterialAdd:{
inflateEntry();
break;
}
}
}
private void inflateEntry() {
// TODO Auto-generated method stub
Log.v("debug","pressed");
rootLayout.addView(layoutItem);
}
}
you need to inflate the view again every time you click the button:
private void inflateEntry()
{
// TODO Auto-generated method stub
Log.v("debug","pressed");
View layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, null);
rootLayout.addView(layoutItem);
}

how to create expendable layout on each node

im creating and application which have thre nodes (Description, Contents, Nutrition) like this image http://imgur.com/TmJT6GI when user click any node it will expend and show another screen inside it how i will do that? i will see there is expendable listview available but its show single list on each node like this example http://ranfeng0610.blog.163.com/blog/static/1857082842011727111359969/ is expend listview in each node i lie to expend layout on reach node al node show difrent layout User interface) how i willl make screenlike this? help me please u have any idea????
1、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 禁用系统自带图标android:groupIndicator="#null" -->
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:groupIndicator="#null"
android:id="#+id/mExpandableListView"
/>
</LinearLayout>
2、group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation = "horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width = "25dip"
android:layout_height = "25dip"
android:layout_marginTop="10dip"
android:id="#+id/mImageView"
/>
<TextView
android:id = "#+id/group_tv"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:paddingLeft = "30px"
android:paddingBottom = "10px"
android:textSize = "26sp"/>
</LinearLayout>
3、child.xml
<?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">
<ImageView
android:layout_gravity = "center_vertical"
android:id = "#+id/child_iv"
android:layout_width = "70px"
android:layout_height = "70px"
android:paddingLeft = "30px"
android:paddingTop = "2px"
android:paddingBottom = "5px"
android:src = "#drawable/icon"/>
<TextView
android:layout_gravity = "center_vertical"
android:id = "#+id/child_tv"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:paddingLeft = "30px"
android:paddingTop = "10px"
android:paddingBottom = "5px"
android:textSize = "30sp"/>
</LinearLayout>
u can make it without using expandable list-view..u have to just control visibility of your views.
MainActivity
boolean flag;
boolean flag2;
LinearLayout llContains;
LinearLayout llContents;
LinearLayout llContains2;
LinearLayout llContents2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llContains = (LinearLayout)findViewById(R.id.llContains);
llContents = (LinearLayout)findViewById(R.id.llContents);
llContains2 = (LinearLayout)findViewById(R.id.llContains2);
llContents2 = (LinearLayout)findViewById(R.id.llContents2);
llContains.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(!flag)
{
llContents.setVisibility(View.VISIBLE);
flag = true;
}
else
{
llContents.setVisibility(View.GONE);
flag = false;
}
}
});
llContains2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(!flag2)
{
llContents2.setVisibility(View.VISIBLE);
flag2 = true;
}
else
{
llContents2.setVisibility(View.GONE);
flag2 = false;
}
}
});
}
activity_main.xml
<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"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/llContains"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000000"
>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Contains"
android:textSize="20dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
/>
<LinearLayout
android:id="#+id/llContents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:padding="10dp"
android:visibility="gone"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Cheese"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Tomatos"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Wheat"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Salt"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/llContains2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000000"
>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Contains2"
android:textSize="20dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
/>
<LinearLayout
android:id="#+id/llContents2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:padding="10dp"
android:visibility="gone"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Cheese"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Tomatos"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Wheat"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Salt"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
if it helps then let me know.

Categories

Resources