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>
Related
i have a custom_layout_dish
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:weightSum="10">
<ImageView
android:id="#+id/img_Dish"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="6"
android:src="#drawable/dau_phu_luot_van"/>
<LinearLayout
android:padding="10dp"
android:layout_weight="3.5"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_NameDish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pepsi"/>
<TextView
android:id="#+id/tv_Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30.000"/>
</LinearLayout>
<Button
android:id="#+id/btn_Order"
android:layout_gravity="center"
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_add"/>
</LinearLayout>
and activity_orderfood
<Button
android:id="#+id/btn_Cart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Xem Hóa Đơn" />
<ListView
android:id="#+id/lv_dish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/btn_Cart"
android:layout_alignParentTop="true" />
So, how to setText btn_Cart (activity) when I click btn_Order (custom_layout)
In OrderFoodActivity I create
public void ShowQuantityOrder(int quantity){
btnCart.setText("Xem Giỏ Hàng " + quantity);
}
and
holder.btnOrder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int tongsoluong = chiTietGoiMonDAO.getTongSoLuong(magoimon);
OrderFood orderFoodActivity = new OrderFoodActivity ();
orderFoodActivity .ShowQuantityOrder(tongsoluong);
}
});
but It's not working pls help me I want when click btn_Order change text btn_Cart like "Xem Giỏ Hàng " + quantity, please help me. I using adapter
I want a animation that first FadeIn the TextView with id Symbol_no from Bottom to Top Then FadeIn other View on the screen.
symbolnotextview.animate().setDuration(duration).
scaleX(1).scaleY(1).
translationX(0).translationY(0).
setInterpolator(sDecelerator).
withEndAction(new Runnable() {
public void run() {
// Animate the description in after the image animation
// is done. Slide and fade the text in from underneath
// the picture.
mTextView.setTranslationY(-mTextView.getHeight());
mTextView.animate().setDuration(duration/2).
translationY(0).alpha(1).
setInterpolator(sDecelerator);
}
});
But i think this is not what i mean to do .
Here is the XML of the design
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:id="#+id/toplevel">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="15"
android:background="#drawable/borderframe">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dp"
android:src="#drawable/abc_btn_radio_to_on_mtrl_015"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="21477"
android:padding="5dp"
android:gravity="center"
android:background="#drawable/border"
android:id="#+id/Symbol_no"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:text="Rohit Shrestha"
android:layout_weight="20"
android:textSize="30sp"
android:gravity="center"
android:id="#+id/name"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:id="#+id/parentblock">
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Block"
android:textSize="30sp"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/block_no"
android:background="#drawable/border"/>
<TextView
android:layout_width="120dp"
android:layout_height="fill_parent"
android:text="7"
android:gravity="center"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:textSize="40sp"
android:id="#+id/block_no"
android:background="#drawable/border"/>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:weightSum="100"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="30dp"
android:id="#+id/class_container"
>
<LinearLayout
android:layout_weight="40"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/border"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class"
android:textSize="30dp"
android:gravity="center"
android:id="#+id/class_text"
/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/room_no"
android:textSize="25dp"
android:gravity="center"
android:text="8"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="40"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/border"
android:gravity="center"
android:layout_marginLeft="15dp">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Seat"
android:gravity="center"
android:textSize="30dp"
android:id="#+id/Seat_text"
/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="8"
android:gravity="center"
android:id="#+id/Seat_No"
android:textSize="25dp"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="Good Luck"
android:gravity="center"
android:id="#+id/display_notification"
android:layout_weight="10"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/Time_Left"
android:text="5 hours to go"
android:gravity="center"
android:textSize="20dp"
android:layout_weight="10"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="15 july,2015"
android:gravity="center"
android:layout_weight="10"/>
This is the output :
Try this:
Button btn = (Button)findViewById(R.id.Symbol_no);
DisplayMetrics dis = getResources().getDisplayMetrics();
TranslateAnimation anim = new TranslateAnimation(0, 0, dis.heightPixels, 0);
anim.setDuration(1000);
anim.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationStart(Animation arg0)
{
}
#Override
public void onAnimationRepeat(Animation arg0)
{
}
#Override
public void onAnimationEnd(Animation arg0)
{
// show whatever you want to show after animation is finished.
}
});
btn.startAnimation(anim);
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);
}
Thanks in Advance. Help me out in this situation. Problem is first click on button does not work but after clicks works fine so I am in weird situation How it is made as first click view?. code is below
Main.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"
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=".MainActivity" >
<TableLayout
android:id="#+id/tableLayout1"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp"
android:background="#CDCDCD" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="user_account_creation"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#252525" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sign In"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#252525" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Menu"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#252525" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Settings"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#252525" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableLayout1"
android:layout_below="#+id/tableLayout1"
android:layout_marginTop="18dp"
android:text="More" />
</RelativeLayout>
Main class method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.tableLayout1);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (btn.getText()=="More") {
btn.setText("Less"); tl.setVisibility(v.VISIBLE);
}
else
{
btn.setText("More"); tl.setVisibility(v.GONE);
}
}
});
}
give me the solutions.
Try with equalsIgnoreCase Like this to be more precise
if (btn.getText().toString().equalsIgnoreCase("More")) {...}
Change this:
if (btn.getText()=="More") {...}
to this:
if (btn.getText().toString().equals("More")) {...}
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);
}
}
});
}
}