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")) {...}
Related
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);
when I am selected multiple spinner item that all data item display on one separated edit box. this is one part of code and xml.
public void addItemsOnSpinner(){
sp1=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.facility_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> spinner, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch(view.getId()){
//Spinner spinner1,spinner2,spinner3;
case R.id.spinner1:
Toast.makeText(myetms.this,"spinner1",Toast.LENGTH_LONG).show();
//break
case R.id.spinner2:
Toast.makeText(myetms.this,"spinner2",Toast.LENGTH_LONG).show();
//break;
case R.id.spinner3:
Toast.makeText(myetms.this,"spinner3",Toast.LENGTH_LONG).show();
break;
}
//sendSMS("9819861968","+location[index]");
//sc.setVisibility(view.VISIBLE); }
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
public void onItemSelected(AdapterView<?> sp1,View view,int pos,long id){
//sc.setText((CharSequence) sp1.getSelectedItem());
sc.setVisibility(view.VISIBLE);
}
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="132dp"
android:src="#drawable/myshedule" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView04"
android:layout_width="25dp"
android:layout_height="65dp"
android:layout_weight="1"
android:background="#dcdcdc"
android:gravity="center"
android:padding="20dip"
android:text="From"
android:textColor="#000000" />
<EditText
android:id="#+id/editText"
android:layout_width="55dp"
android:layout_weight="1"
android:background="#d3d3d3"
android:gravity="center"
android:padding="20dip"
android:text="Select Date"
android:textColor="#000000" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_height="65dp"
android:contentDescription="#string/selectdate"
android:gravity="center"
android:padding="20dip"
android:src="#drawable/calendar_icon" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="35dp"
android:layout_weight="1"
android:background="#dcdcdc"
android:gravity="center"
android:padding="20dip"
android:text="Emplyee Name"
android:textColor="#000000" >
</TextView>
<EditText
android:id="#+id/screenName"
android:layout_width="45dp"
android:layout_height="50dp"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_weight="1" >
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_height="65dp"
android:layout_weight="1"
android:background="#b0b0b0"
android:gravity="center"
android:padding="18dip"
android:text="Facility Type"
android:textColor="#000000" />
<Spinner
android:id="#+id/spinner1"
android:layout_height="65dp"
android:layout_weight="1"
android:background="#a09f9f"
android:entries="#array/facility_array"
android:gravity="center"
android:padding="18dip"
android:prompt="#string/facility_prompt"
android:textColor="#000000" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_height="65dp"
android:layout_weight="1"
android:background="#b0b0b0"
android:gravity="center"
android:padding="18dip"
android:text="Trip Type"
android:textColor="#000000" />
<Spinner
android:id="#+id/spinner2"
android:layout_height="65dp"
android:layout_weight="1"
android:background="#a09f9f"
android:entries="#array/trip_array"
android:gravity="center"
android:padding="18dip"
android:prompt="#string/trip_prompt"
android:textColor="#000000" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_height="65dp"
android:layout_weight="1"
android:background="#b0b0b0"
android:gravity="center"
android:padding="18dip"
android:text="Shift Type"
android:textColor="#000000" />
<Spinner
android:id="#+id/spinner3"
android:layout_height="65dp"
android:layout_weight="1"
android:background="#a09f9f"
android:entries="#array/shift_array"
android:gravity="center"
android:padding="18dip"
android:prompt="#string/shift_prompt"
android:textColor="#000000" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/btnSubmit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</TableRow>
</TableLayout>
<EditText
android:id="#+id/searchBox"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:inputType="textVisiblePassword"
android:paddingLeft="10dp" >
</EditText>
</LinearLayout>
</ScrollView>
Try this:
Globally define String Variable in which you need to store your different Spinner Selected Values.
Suppose you have 3 Spinners:
Then Define like:
String Spinner1Value,Spinner2Value,Spinner3Value;
before your
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
}
Now on Every Spinner setOnItemSelectedListener():
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
try
{
Spinner1Value= spinner1.getSelectedItem().toString();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Error::"+e.toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
and after that you can get the selected values from Spinner1Value,Spinner2Value,Spinner3Value.
Like:
edittext1.setText(Spinner1Value+Spinner2Value+Spinner3Value);
Hope this may help you!
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>
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);
}
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);
}
}
});
}
}