In Android My requirement is :-
I have multiple checkboxes and one button..after selection when You click the button it will display as text which boxes you have selected.
So I have activity_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/check"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay1">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apple"
android:tag="apple"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="banana"
android:tag="banana"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="water_milon"
android:tag="water_milon"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay2">
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="guava"
android:tag="guava"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="panir"
android:tag="panir"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chatni"
android:tag="chatni"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lay3">
<CheckBox
android:id="#+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="college"
android:tag="college"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="#+id/checkBox8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="school"
android:tag="school"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="#+id/checkBox9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="primary"
android:tag="primary"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
and the java class file is:--
public class MainActivity_test extends Activity {
CheckBox chk1,chk2,chk3,chk4,chk5,chk6,chk7,chk8,chk9;
Button btn;
TextView txt;
ArrayList<String> list ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
list = new ArrayList<String>();
chk1=(CheckBox)findViewById(R.id.checkBox1);
chk2=(CheckBox)findViewById(R.id.checkBox2);
chk3=(CheckBox)findViewById(R.id.checkBox3);
chk4=(CheckBox)findViewById(R.id.checkBox4);
chk5=(CheckBox)findViewById(R.id.checkBox5);
chk6=(CheckBox)findViewById(R.id.checkBox6);
chk7=(CheckBox)findViewById(R.id.checkBox7);
chk8=(CheckBox)findViewById(R.id.checkBox8);
chk9=(CheckBox)findViewById(R.id.checkBox9);
txt = (TextView)findViewById(R.id.textView1);
btn =(Button)findViewById(R.id.button1);
addListenerOnButton();
}
public void addListenerOnButton() {
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (String str : list) {
txt.setText("you have selected:--"+str);
}
}
});
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()) {
case R.id.checkBox1:
list.add(chk1.getTag().toString());
break;
case R.id.checkBox2:
list.add(chk2.getTag().toString());
break;
case R.id.checkBox3:
list.add(chk3.getTag().toString());
break;
case R.id.checkBox4:
list.add(chk4.getTag().toString());
break;
case R.id.checkBox5:
list.add(chk5.getTag().toString());
break;
case R.id.checkBox6:
list.add(chk6.getTag().toString());
break;
case R.id.checkBox7:
list.add(chk7.getTag().toString());
break;
case R.id.checkBox8:
list.add(chk8.getTag().toString());
break;
case R.id.checkBox9:
list.add(chk9.getTag().toString());
break;
}
}
}
But here the problem is when I click the button it is displaying only the last selected item..Not the all selected items...where is the problem??
Change txt.setText("you have selected:--"); in your for loop implement as
for (String str : list) {
txt.setText(txt.getText().toString() + " , " + str);
}
Related
I made a stopwatch using chronometer with four buttons but when i use the visibility modes to make stop and pause button appear they overlap.... Pls explain why...Below is the code.....
Assume the layout file with buttons in relative layout...
public class StopWatchFragment extends Fragment {
Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;
public StopWatchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);
//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());
startStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startStopWatch.setVisibility(View.GONE);
pauseStopWatch.setVisibility(View.VISIBLE);
if(check == 1){
resumeStopWatch.setClickable(true);
}
else{
resumeStopWatch.setClickable(false);
}
//params.setMargins(16,16,16,16);
//pauseStopWatch.setLayoutParams(params);
}
});
pauseStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
check = 1;
lastPause = SystemClock.elapsedRealtime();
chronometer.stop();
}
});
resumeStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
chronometer.start();
resumeStopWatch.setClickable(false);
}
});
stopStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
resumeStopWatch.setClickable(false);
}
});
resetStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
resumeStopWatch.setClickable(false);
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
}
});
return rootView;
}
}
This is the layout file pls refer for this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the way buttons work when click on start button
You are hiding the button, but you are still listening with the OnClickListener. Try adding:
pauseStopWatch.setOnClickListener(null);
Then, when you make your button visible:
pauseStopWatch.setOnClickListener(whatever);
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:visibility="gone"
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</LinearLayout>
</LinearLayout>
In the above, the pause button will be set to Gone. You make it visible later when user clicks start button
I am creating a setting tab for user and I use CheckBox to check. This is the code:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.Lang_Vie:
if (checked){
mCBLangEng.setChecked(false);
Constants.STR_LANG = "vi";
setLocale("vi");
}
break;
case R.id.Lang_Eng:
if (checked){
mCBLangViet.setChecked(false);
Constants.STR_LANG = "en";
setLocale("en");
}
case R.id.locHanoi:
if (checked){
mCBLocDN.setChecked(false);
Constants.STR_LOC = "hn";
Constants.API_TYPE_AQI = 1;
Constants.API_TYPE_TMP = 2;
Constants.API_TYPE_HUM = 3;
}
case R.id.locDanang:
if (checked){
mCBLocHN.setChecked(false);
Constants.STR_LOC = "dn";
Constants.API_TYPE_AQI = 7;
Constants.API_TYPE_TMP = 8;
Constants.API_TYPE_HUM = 9;
}
break;
case R.id.SensCheck:
if (checked){
Constants.STR_SENS = "yes";
Constants.USER_LIMITATION = 200;
}
else{
Constants.STR_SENS = "no";
Constants.USER_LIMITATION = 150;
}
break;
}
}
And I use this code in my SettingTab:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_setting, container, false);
Sens = (CheckBox) view.findViewById(R.id.SensCheck);
mCBLangEng = (CheckBox) view.findViewById(R.id.Lang_Eng);
mCBLangViet = (CheckBox) view.findViewById(R.id.Lang_Vie);
mCBLocHN = (CheckBox) view.findViewById(R.id.locHanoi);
mCBLocDN = (CheckBox) view.findViewById(R.id.locDanang);
Save = (Button) view.findViewById(R.id.save);
onCheckboxClicked(view);
return view;
But I have this error :
11-14 15:50:24.574 16794-16794/com.journaldev.viewpager E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.CheckBox
at com.journaldev.viewpager.MyApplication.MyApp.TabSetting.onCheckboxClicked(TabSetting.java:155)
at com.journaldev.viewpager.MyApplication.MyApp.TabSetting.onCreateView(TabSetting.java:140)
Maybe there's something wrong? I tried to replace view with this.getActivity() but it won't work.
Can you help me please? Thanks !.
This is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Setting"
android:textSize="35dp"
android:textColor="#faf7f7"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Language"
android:textSize="30sp"
android:textColor="#faf7f7"
android:layout_marginTop="20dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="English"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="false"
android:id="#+id/Lang_Eng"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Tiếng Việt"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="true"
android:id="#+id/Lang_Vie"
android:onClick="onCheckboxClicked"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Location"
android:textSize="30sp"
android:textColor="#faf7f7"
android:layout_marginTop="20dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/Danang"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="false"
android:id="#+id/locDanang"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/Hanoi"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="true"
android:id="#+id/locHanoi"
android:onClick="onCheckboxClicked"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/User"
android:textSize="30sp"
android:textColor="#faf7f7"
android:layout_marginTop="20dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:textSize="20sp"
android:textColor="#faf7f7"
android:text="#string/Sensitivegroup"
android:id="#+id/SensCheck"
android:onClick="onCheckboxClicked"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textColor="#faf7f7"
android:text="#string/Note"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textColor="#faf7f7"
android:text="#string/Example"/>
</LinearLayout>
</ScrollView>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#bfbebe"
android:text="SAVE SETTING"
android:textColor="#000000"
android:id="#+id/save"/>
</LinearLayout>
You call onCheckboxClicked(view); on your Layout not only on CheckBox. Try replace
boolean checked = ((CheckBox) view).isChecked();
with
if (view instanceof CheckBox) {
boolean checked = ((CheckBox) view).isChecked();
...
}
Or bettter add setOnCheckedChangeListener() to your CheckBoxes
mCBLangEng.setOnCheckedChangeListener(...);
My doubt is the following, I have a layout with some checkbox and a text field, how do I get the selected items in checkboxes, written text and add a marker, which when clicked on it to appear in your info window?
My layout and class to add marker:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="br.selectv.CadMarkerActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbottom"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<EditText
android:id="#+id/txDescricao"
android:inputType="textMultiLine"
android:layout_width="333dp"
android:layout_height="wrap_content"
android:hint="#string/descricao"
android:layout_marginTop="300dp"
android:layout_marginEnd="25dp"
android:maxLength="109"
android:textColorHint="#color/secondary_text"
android:textColor="#color/primary_text"
android:layout_marginStart="25dp" />
<CheckBox android:id="#+id/papel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Papel"
android:layout_marginTop="150dp"
android:textColor="#color/primary_text"
android:onClick="onCheckboxClicked"
android:layout_marginStart="25dp"/>
<CheckBox android:id="#+id/vidro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vidro"
android:textColor="#color/primary_text"
android:onClick="onCheckboxClicked"
android:layout_alignTop="#+id/papel"
android:layout_alignStart="#+id/organico" />
<CheckBox android:id="#+id/plastico"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Plástico"
android:textColor="#color/primary_text"
android:onClick="onCheckboxClicked"
android:layout_alignTop="#+id/vidro"
android:layout_alignEnd="#+id/txDescricao" />
<CheckBox android:id="#+id/metal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="200dp"
android:text="Metal"
android:textColor="#color/primary_text"
android:onClick="onCheckboxClicked"/>
<CheckBox android:id="#+id/organico"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orgânico"
android:textColor="#color/primary_text"
android:onClick="onCheckboxClicked"
android:layout_alignTop="#+id/metal"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/txTipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tipo"
android:textColor="#color/primary_text"
android:layout_marginStart="25dp"
android:layout_marginTop="100dp" />
<Button
android:id="#+id/btOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:background="#color/colorAccent"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/txDescricao"
android:layout_marginBottom="100dp" />
<Button
android:id="#+id/btCancelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancelar"
android:background="#color/colorAccent"
android:layout_alignTop="#+id/btOK"
android:layout_alignStart="#+id/organico"
android:onClick="cancelar" />
class add marker
public class CadMarkerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cad_marker);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void cancelar(View view) {
finish();
}
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch (view.getId()) {
case R.id.papel:
if (checked) {
} else {
}
break;
case R.id.vidro:
if (checked) {
} else {
}
break;
case R.id.plastico:
if (checked) {
} else {
}
break;
case R.id.metal:
if (checked) {
} else {
}
break;
case R.id.organico:
if (checked) {
} else {
}
break;
// TODO: Veggie sandwich
}
}
}
Yeah you will be able to get the selected items from the checkbox by checking if it's checked and for texts you should combine the code for if checkbox is checked then get the text of your checked box.
for show it in info window you should have to use AlertDialogBox and onCheckedListener on Your Check box.
i hope my idea will be work for you.
I am new user in android. I am trying to change the font size of the image button. But my java file shows error. Can anybody tell me how to show text in image button and also to change typeface in an image button. My coding in java file is as follows.
public class AyurvedaMenuActivity extends Activity implements OnClickListener{
ImageButton btnAyurveda, btnDiseases, btnHerbs, btnProducts, btnHospitals, btnResearchCenters, btnDoctors, btnInstitutes, btnContactUs;
TextView tvMainTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ayurveda_menu);
btnAyurveda = (ImageButton)findViewById(R.id.bAMAyurveda);
btnContactUs = (ImageButton)findViewById(R.id.bAMContactUs);
btnDiseases = (ImageButton)findViewById(R.id.bAMDiseases);
btnDoctors = (ImageButton)findViewById(R.id.bAMDoctors);
btnHerbs = (ImageButton)findViewById(R.id.bAMHerbs);
btnHospitals = (ImageButton)findViewById(R.id.bAMHospitals);
btnInstitutes = (ImageButton)findViewById(R.id.bAMInstitutes);
btnProducts = (ImageButton)findViewById(R.id.bAMProducts);
btnResearchCenters = (ImageButton)findViewById(R.id.bAMResearchCenters);
tvMainTitle=(TextView)findViewById(R.id.tvMainTitle);
String fontpath = "fonts/magneto_bold.ttf";
/*String fontpath = "fonts/forte.ttf";*/
Typeface tf = Typeface.createFromAsset(getAssets(),fontpath);
btnAyurveda.setTypeface(tf);
tvMainTitle.setTypeface(tf);
btnAyurveda.setTypeface(tf);
btnDiseases.setTypeface(tf);
btnContactUs.setTypeface(tf);
btnDoctors.setTypeface(tf);
btnHerbs.setTypeface(tf);
btnHospitals.setTypeface(tf);
btnInstitutes.setTypeface(tf);
btnResearchCenters.setTypeface(tf);
btnContactUs.setTypeface(tf);
btnProducts.setTypeface(tf);
btnAyurveda.setOnClickListener(this);
btnContactUs.setOnClickListener(this);
btnDiseases.setOnClickListener(this);
btnDoctors.setOnClickListener(this);
btnHerbs.setOnClickListener(this);
btnHospitals.setOnClickListener(this);
btnInstitutes.setOnClickListener(this);
btnProducts.setOnClickListener(this);
btnResearchCenters.setOnClickListener(this);
//databaseEntry();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ayurveda_menu, menu);
return true;
}
#Override
public void onClick(View v) {
System.out.println("V id"+v.getId());
switch (v.getId()) {
case R.id.bAMAyurveda:
startActivity(new Intent(AyurvedaMenuActivity.this,AyurvedaActivity.class).putExtra("AyurdevaActivity", 1));
break;
case R.id.bAMContactUs:
startActivity(new Intent(AyurvedaMenuActivity.this,ContactUsActivity.class).putExtra("ContactUsActivity", 9));
break;
case R.id.bAMDiseases:
startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 2));
break;
case R.id.bAMDoctors:
startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 7));
break;
case R.id.bAMHerbs:
startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 3));
break;
case R.id.bAMHospitals:
startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 5));
break;
case R.id.bAMInstitutes:
startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 8));
break;
case R.id.bAMProducts:
startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 4));
break;
case R.id.bAMResearchCenters:
startActivity(new Intent(AyurvedaMenuActivity.this,MenuListActivity.class).putExtra("MenuListActivity", 6));
break;
default:
break;
}
}
and my xml file is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/b_image"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".AyurvedaMenuActivity" >
<TextView
android:id="#+id/tvMainTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_image"
android:gravity="center_horizontal"
android:text="#string/ayurveda_title"
android:textColor="#3D0303"
android:textSize="30sp" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="385dp"
android:layout_marginTop="5dp"
android:layout_weight="1.27" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:id="#+id/bAMAyurveda"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_image"
android:textColor="#3D0303"
android:text="#string/ayurveda" />
<ImageButton
android:id="#+id/bAMDiseases"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#3D0303"
android:background="#drawable/button_image"
android:text="#string/disease" />
<ImageButton
android:id="#+id/bAMHerbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#3D0303"
android:background="#drawable/button_image"
android:text="#string/herbs" />
<ImageButton
android:id="#+id/bAMProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#3D0303"
android:background="#drawable/button_image"
android:text="#string/products" />
<ImageButton
android:id="#+id/bAMHospitals"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_image"
android:textColor="#3D0303"
android:text="#string/hospitals" />
<ImageButton
android:id="#+id/bAMResearchCenters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#3D0303"
android:background="#drawable/button_image"
android:text="#string/research_centers" />
<ImageButton
android:id="#+id/bAMDoctors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#3D0303"
android:background="#drawable/button_image"
android:text="#string/doctors" />
<ImageButton
android:id="#+id/bAMInstitutes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#3D0303"
android:background="#drawable/button_image"
android:text="#string/institutes" />
<ImageButton
android:id="#+id/bAMContactUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#3D0303"
android:background="#drawable/button_image"
android:text="#string/contact_us" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:background="#drawable/title_image"
android:orientation="vertical" >
<EditText
android:id="#+id/addView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Try this..
you can't use android:text in ImageButton
I recommend you to use a normal button like below
<Button
android:id="#+id/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/button_image"
android:text="#string/ayurveda"/>
You can put the drawable wherever you want
android:drawableRight="#drawable/button_image"
android:drawableTop="#drawable/button_image"
android:drawableBottom="#drawable/button_image"
then you can add your Typeface like below
Button btnAyurveda = (Button) findViewById(R.id.bAMAyurveda);
Typeface tf = Typeface.createFromAsset(getAssets(),fontpath);
btnAyurveda.setTypeface(tf);
use like this:
Button n=(Button) findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
n.setText("show");
n.setTypeface(typeface);
i've a radio group radiogoup1 that hat two radiobutton rbtn1, rbtn2. say i want to store Male for rbtn1 and Female for rbtn2 in database. How to do it? I am mentioning the .xml and .java file.
sqlliteexample.xml :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="#string/age" >
</TextView>
<EditText
android:id="#+id/editAge"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textContcat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="#string/contact" />
<EditText
android:id="#+id/editContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textMultiLine" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/sLabel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left|center"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:text="#string/sx" />
<RadioGroup
android:id="#+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/malebutton"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:text="#string/mal"
android:textSize="15sp" />
<RadioButton
android:id="#+id/femalebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/femal"
android:textSize="15sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/savebutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:gravity="left|center"
android:text="#string/save"
android:textSize="13sp" />
<Button
android:id="#+id/viewbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center"
android:gravity="center|left"
android:text="#string/view"
android:textSize="13sp" />
</LinearLayout>
SqlLiteExample.java :
public class SqlLiteExample extends Activity implements OnClickListener, OnCheckedChangeListener {
Button sqlUpdate, sqlView;
EditText etName,etAge,etContact;
RadioGroup rdgrp;
RadioButton selectedRadioButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlliteexample);
sqlUpdate = (Button) findViewById(R.id.savebutton);
etName = (EditText) findViewById(R.id.editName);
etAge = (EditText) findViewById(R.id.editAge);
etContact = (EditText) findViewById(R.id.editContact);
rdgrp.setOnCheckedChangeListener(this);
sqlView = (Button) findViewById(R.id.viewbutton);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.savebutton:
boolean didWork = true;
try{
String name = etName.getText().toString();
String age = etAge.getText().toString();
String contact = etContact.getText().toString();
MyDB entry = new MyDB(SqlLiteExample.this);
entry.open();
entry.createEntry(name,age,contact);
entry.close();
}catch(Exception e){
didWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(didWork){
Dialog d = new Dialog(this);
d.setTitle("Updated");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.viewbutton:
Intent i = new Intent("com.bysakiralam.mydatabase.DISPLAYRECORDS");
startActivity(i);
break;
}
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch(arg1){
case R.id.malebutton:
break;
case R.id.femalebutton:
break;
}
} }
There is no need to override onCheckedChanged() use following in your on savebutton click
rdgrp=(RadioGroup)findViewById(R.id.RadioGroup01);
String radiovalue= (RadioButton)this.findViewById(rdgrp.getCheckedRadioButtonId())).getText().toString();
and now use radiovalue to store in database
Edit: forget (
rdgrp=(RadioGroup)findViewById(R.id.RadioGroup01);
String radiovalue= ((RadioButton)this.findViewById(rdgrp.getCheckedRadioButtonId())).getText().toString();
try this
myRadioGrp=(RadioGroup)findViewById(R.id.RadioGroup01);
String radiovalue= ((RadioButton)this.findViewById(myRadioGrp.getCheckedRadioButtonId())).getText().toString();