Android Application force closes. (NullPointerException / ToggleButton) - android

My Code:
public class My_Application extends Activity {
public ToggleButton privBtn = (ToggleButton) findViewById(R.id.privacyShow);
public LinearLayout privLay = (LinearLayout) findViewById(R.id.privacyLayout);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
privBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(privBtn.isChecked()) {
privLay.setVisibility(LinearLayout.VISIBLE);
}
else {
privLay.setVisibility(LinearLayout.INVISIBLE);
}
}
});
}
}
When I launch the application it force closes. I debugged the application with the built-in Dev Tools application in the emulator, and see the following:
[2011-06-23 20:27:06 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:671)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
As well as a "Source not found." error at:
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417
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"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="My Application" android:gravity="center_horizontal"
android:textSize="25dp" android:background="#222" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text=" Server IP: " android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.7" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="eg: 0.0.0.0" android:layout_weight="0.5" android:textSize="20dp"
android:id="#+id/loginIP" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Server Port: " android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.7" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="eg: 1234" android:layout_weight="0.5" android:textSize="20dp"
android:id="#+id/loginPort" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/privacyLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Privacy Options" android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.4" />
<ToggleButton android:layout_width="fill_parent" android:layout_height="wrap_content"
android:checked="false" android:layout_weight="0.6" android:textOff="Show Options"
android:textOn="Hide Options" android:id="#+id/privacyShow" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Username: " android:textColor="#0F0" android:textSize="15dp"
android:layout_weight="0.5" />
<EditText android:layout_width="fill_parent" android:layout_height="30dp"
android:id="#+id/privacyUsername" android:textSize="15dp" android:layout_weight="0.2" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Password: " android:textColor="#0F0" android:textSize="15dp"
android:layout_weight="0.5" />
<EditText android:layout_width="fill_parent" android:layout_height="30dp"
android:id="#+id/privacyPassword" android:textSize="15dp" android:layout_weight="0.2" />
</LinearLayout>
</LinearLayout>

You can't set the privBtn and privLay before the setContentView() is called. Move those two statements into your onCreate().
public class My_Application extends Activity {
public ToggleButton privBtn;
public LinearLayout privLay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
privBtn = (ToggleButton) findViewById(R.id.privacyShow);
privLay = (LinearLayout) findViewById(R.id.privacyLayout);
privBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(privBtn.isChecked()) {
privLay.setVisibility(LinearLayout.VISIBLE);
}
else {
privLay.setVisibility(LinearLayout.INVISIBLE);
}
}
});
}
}

Related

Tab Content not showing on FragmentTabHost Android

I have tried using FragmentTabHost. all went well, and has managed to show the tab well. but my fragment not showing layout of that class. I've tried using the TextView, but still did not appear.
please help me.
My FragmentActivity
public class ViewDetailAnak extends FragmentActivity{
private static String idanak;
private FragmentTabHost tabHost;
private static final String TRANS_SPEC = "";
private static final String US_SPEC = "";
private static final String TABU_SPEC = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fro_single_item_anak);
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);
tabHost.addTab(
tabHost.newTabSpec(TRANS_SPEC)
.setIndicator(TRANS_SPEC, getResources().getDrawable(R.drawable.icon_trans)),
FROVDATransaksi.class, null);
tabHost.addTab(
tabHost.newTabSpec(US_SPEC)
.setIndicator(US_SPEC, getResources().getDrawable(R.drawable.icon_us)),
FROVDAUangsaku.class, null);
tabHost.addTab(
tabHost.newTabSpec(TABU_SPEC)
.setIndicator(TABU_SPEC, getResources().getDrawable(R.drawable.icon_tab)),
FROVDATabungan.class, null);
}
One of 3 fragment
public static class FROVDAUangsaku extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fro_vda_uangsaku, container, false);
return v;
}
}
and this is XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="14dp"
android:clipToPadding="true"
android:paddingTop="15dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/user1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="15.86"
android:orientation="vertical"
android:paddingLeft="10dp" >
<TextView
android:id="#+id/txtNama_FRO_Profil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/nama" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="10dp" >
<TextView
android:id="#+id/txNama_fra_profil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Uang Saku"
android:textColor="#666666"
android:textSize="9dp" />
<TextView
android:id="#+id/txtJumlahAnak_FRO_Profil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtnomuangsaku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Tabungan"
android:textColor="#666666"
android:textSize="9sp" />
<TextView
android:id="#+id/saldotabungan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnLogoutFRO_profilo"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="Edit Profile"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</TableRow>
<View
android:layout_height="2dip"
android:layout_marginTop="5dp"
android:background="#666666" />
</TableLayout>
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tableLayout1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="38dp" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
ignore inside TableLayout. The only design. and I make fragmenttab under TableLayout

Facebook like GridView to display images android

I am using a layout with Gridview by custom library Click here StaggeredGridView
But not achieved the result that I needed.
Th GridView should auto adjust its column and row depending upon the number of images (min 1 images & max 4 images ) like here
news_default.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"
xmlns:app="http://schemas.android.com/apk/lib/com.rb.nonstop.HomeActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout android:id="#+id/rel_sender_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<ImageView
android:id="#+id/img_news_sender_photo"
android:layout_width="70dip"
android:layout_height="70dip"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher" />
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/txt_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/img_news_sender_photo"
android:textColor="#343434"
android:textSize="15dip"
android:text="News"
android:paddingLeft="5dip"
android:paddingTop="10dip"
/>
<View
android:id="#+id/view1"
android:layout_width="100dp"
android:layout_height="2dp"
android:background="#color/border"
android:layout_toRightOf="#+id/img_news_sender_photo"
android:layout_below="#+id/txt_category"
android:paddingLeft="20dip"
android:paddingTop="5dip" />
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/txt_sender_name_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/img_news_sender_photo"
android:layout_below="#+id/view1"
android:textColor="#343434"
android:textSize="15dip"
android:text="Karthik Kolanji, Mumbai"
android:paddingLeft="5dip"
android:paddingTop="5dip"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/rel_news_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="#+id/rel_sender_photo">
<com.rb.lined.edittext.LinedEditText
android:id="#+id/edit_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:inputType="textMultiLine|textNoSuggestions"
android:padding="10dip"
android:singleLine="false"
android:imeOptions="actionNone"
android:textSize="10sp"
android:gravity="top|left"
android:minLines="10"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/rel_news_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="#+id/rel_news_content"
>
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/txt_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#343434"
android:textSize="10dip"
android:text="NewsFirst MobileApp"
android:paddingLeft="5dip"
android:paddingTop="5dip"
android:layout_alignParentLeft="true"
/>
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/txt_news_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#343434"
android:textSize="10dip"
android:text="September 10,2014 4:20 PM"
android:paddingLeft="5dip"
android:paddingTop="5dip"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/rel_news_grid_photos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="#+id/rel_news_date"
>
<com.jake.quiltview.QuiltView
android:id="#+id/quilt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dip"
app:scrollOrientation="horizontal|vertical" >
</com.jake.quiltview.QuiltView>
</RelativeLayout>
</RelativeLayout>
activity_home.xml
<?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/com.rb.nonstop"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include android:id="#+id/rel_news_default"
layout="#layout/news_default"/>
</RelativeLayout>
HomeActivity.java
public class HomeActivity extends Activity{
public QuiltView quiltView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
quiltView = (QuiltView) findViewById(R.id.quilt);
quiltView.setChildPadding(5);
addTestQuilts(200);
}
public void addTestQuilts(int num){
ArrayList<ImageView> images = new ArrayList<ImageView>();
for(int i = 0; i < num; i++){
ImageView image = new ImageView(this.getApplicationContext());
image.setScaleType(ScaleType.CENTER_CROP);
if(i % 2 == 0)
image.setImageResource(R.drawable.app_logo);
else
image.setImageResource(R.drawable.app_logo1);
images.add(image);
}
quiltView.addPatchImages(images);
}
}
I would suggest you to use RecyclerView with StaggeredGridLayoutManager.
There is a great tutorial on the official documentation.

Custom Alignment of RadioButtons in Android

I have 4 RadioButtons (part of same RadioGroup) and I want to align them like this :
Code I am using is :
<RadioGroup
android:id="#+id/add_reminder_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="#dimen/zero"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RadioButton
android:id="#+id/add_reminder_daily"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_reminder_daily" />
<RadioButton
android:id="#+id/add_reminder_weekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_reminder_weekly" />
</LinearLayout>
<LinearLayout
android:layout_width="#dimen/zero"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RadioButton
android:id="#+id/add_reminder_monthly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_reminder_monthly" />
<RadioButton
android:id="#+id/add_reminder_yearly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_reminder_yearly" />
</LinearLayout>
</RadioGroup>
But by using this code, RadioGroup loses its property and all the RadioButtons can be checked at same time.
Any idea how RadioGroup can retain its property using this type of alignment ?
You can Try This....
first declare all radio button
r1=(RadioButton) findViewById(R.id.add_reminder_daily);
r2=(RadioButton) findViewById(R.id.add_reminder_weekly);
r3=(RadioButton) findViewById(R.id.add_reminder_monthly);
r4=(RadioButton) findViewById(R.id.add_reminder_yearly);
now on - onclick listner do
r1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
r2.setChecked(false);
r3.setChecked(false);
r4.setChecked(false);
}
});
r2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
r1.setChecked(false);
r3.setChecked(false);
r4.setChecked(false);
}
});
r3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
r2.setChecked(false);
r1.setChecked(false);
r4.setChecked(false);
}
});
r4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
r2.setChecked(false);
r3.setChecked(false);
r1.setChecked(false);
}
});
try below code works fine at my end:-
<RadioGroup
android:id="#+id/add_reminder_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RadioButton
android:id="#+id/add_reminder_daily"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Daily" />
<RadioButton
android:id="#+id/add_reminder_weekly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Monthly" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RadioButton
android:id="#+id/add_reminder_monthly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Weekly" />
<RadioButton
android:id="#+id/add_reminder_yearly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yearly" />
</LinearLayout>
</RadioGroup>
use this code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RadioGroup
android:id="#+id/add_reminder_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="#dimen/zero"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RadioButton
android:id="#+id/add_reminder_daily"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="daily" />
<RadioButton
android:id="#+id/add_reminder_weekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="weekly" />
</LinearLayout>
<LinearLayout
android:layout_width="#dimen/zero"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RadioButton
android:id="#+id/add_reminder_monthly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="monthly" />
<RadioButton
android:id="#+id/add_reminder_yearly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yearly" />
</LinearLayout>
</RadioGroup>
</LinearLayout>

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 inflate a list view to a fragment

i am using fragment in my application.there are 5 tabs.in 1 tab i need to inflate a list view on a button click.given below is my code for that tab.
public class TabStoreLocatorFragment extends Fragment implements OnClickListener {
private Button mapSwitcher;
private LinearLayout lastSelectedStoreButton;
private LinearLayout noSelectedStoreSection;
private TextView storeInfoName;
private TextView storeInfoAddress;
private TextView storeInfoCity;
private RelativeLayout phoneLocationButton;
private EditText searchStoreLocation;
private ListView storeList;
private ViewFlipper storeFlipper;
LinearLayout theLayout;
Store store;
#SuppressWarnings("static-access")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
}
theLayout = (LinearLayout) inflater.inflate(
R.layout.tab_store_locator_layout, container, false);
phoneLocationButton = (RelativeLayout) theLayout.findViewById(R.id.phone_location_button);
mapSwitcher=(Button)theLayout.findViewById(R.id.switch_state_button);
lastSelectedStoreButton=(LinearLayout)theLayout.findViewById(R.id.last_selected_store_button);
noSelectedStoreSection=(LinearLayout)theLayout.findViewById(R.id.no_selected_store);
storeInfoName=(TextView)theLayout.findViewById(R.id.store_name);
storeInfoAddress=(TextView)theLayout.findViewById(R.id.store_address);
storeInfoCity=(TextView)theLayout.findViewById(R.id.store_city);
searchStoreLocation=(EditText)theLayout.findViewById(R.id.search_store);
storeFlipper = (ViewFlipper)theLayout.findViewById(R.id.store_flipper);
store=new Store();
store.setName("store1");
store.setAddress("california");
store.setCity("newyork");
store.setZipCode("23");
store.setState("california");
phoneLocationButton.setOnClickListener(this);
mapSwitcher.setOnClickListener(this);
lastSelectedStoreButton.setOnClickListener(this);
return theLayout;
}
#Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.phone_location_button) {
storeList = (ListView)storeFlipper.inflate(getActivity(), R.layout.store_list, storeFlipper);
storeFlipper.addView(storeList);
StoreAdapter adapter = new StoreAdapter(getActivity(), R.layout.store_list_item);
storeList.setAdapter(adapter);
}
else if(id == R.id.switch_state_button){
}
else{
}
}
}
i want to inflate storelist to storeFlipper.i tried to do that.but it is not working.given below is the corresponding layout for that tab.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/find_a_store_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:text="#string/tab_find_a_store_title"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<Button android:id="#+id/switch_state_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/switch_selector" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E8E2D3" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#C5BFAD" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/last_selected_store_button" android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#android:style/Widget.Button"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#8E8672"
android:text="#string/last_selected_store_label"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<TextView android:id="#+id/store_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/arrow" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:id="#+id/no_selected_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#8E8672"
android:text="#string/no_selected_store_label"
android:textSize="12sp" />
</LinearLayout>
</FrameLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<EditText android:id="#+id/search_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:singleLine="true"
android:textSize="14sp"
android:hint="#string/search_store_placeholder"
android:layout_toLeftOf="#+id/phone_location_button"
android:background="#drawable/search_store_background" />
<RelativeLayout android:id="#id/phone_location_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#drawable/get_location_background" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/get_location" />
</RelativeLayout>
</RelativeLayout>
<ViewFlipper android:id="#+id/store_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
can anybody help me to inflate the list to flipper.
it is simple.just put this code.
storeList = (ListView) inflater.inflate(R.layout.store_list, null);
it is working

Categories

Resources