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.
Related
this below code for my login_activity xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:background="#color/colorBackgroundLogin"
android:clipToPadding="false"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/login"
tools:ignore="ContentDescription"/>
</RelativeLayout>
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email_edit_text"
android:hint="#string/email_address_string" />
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:id="#+id/password_edit_text"
android:hint="#string/password_string" />
<Button
android:layout_width="match_parent"
android:textAllCaps="true"
android:text="#string/login_string"
android:background="#drawable/button_style"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/login_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
</LinearLayout>
</ScrollView>
I want to click on TextView id=need_an_account that like below:
public class LoginActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
}
Note that i don't want to use findViewById() method
But onClick not Working at all how can I solve this problem?
If you don't want to use OnClickListener, Here is an alternative -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:onClick="myTextViewMethod"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
Now write myTextViewMethodin you Activity
public void myTextViewMethod(View v) {
// Do your stuff here...
}
Plus point, No need to implement OnClickListener, Android will do it in background.
And if you wanna use an Interface View.OnClickListener
Simple, set need_an_account.setOnClickListener(this);
In oncreate add this,
TextView need_an_account = (TextView) findViewById(R.id.need_an_account);
need_an_account.setOnClickListener(this);
You can add OnClick to your xml to avoid using findViewById:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:clickable="true"
android:textStyle="italic"
android:onClick="textViewClick"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
and then write your function in the activity as:
public void textViewClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
no need to implement an OnClickListener if you dont want to define the TextView programatically. Remember to set TextView to clickable and for authenticity i also set background to ?selectableItemBackground so the user knows that the TextView can be clicked
I am facing a strange issue working with SearchView and BottomSheet.
BottomSheet gets an unexpected margin when used after using SearchView. They video explains the problem.
Video
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
<xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundcolor"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/trip_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundcolor"
android:clipToPadding="false"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="32dp"
android:orientation="vertical"
android:padding="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Sort & Filter"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/sheet_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:drawablePadding="8dp"
android:drawableStart="#drawable/ic_done"
android:text="Done"
android:textSize="18sp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Sort By" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/sheet_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/sort_by"
android:padding="8dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Price Range" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:id="#+id/sheet_price_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/sheet_price_max"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end" />
</LinearLayout>
<SeekBar
android:id="#+id/sheet_bar_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="?android:attr/textSelectHandle" />
<TextView
android:id="#+id/sheet_price_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Rs 1,115" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Ratings" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:gravity="start"
android:text="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_weight="1"
android:gravity="end"
android:text="4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:text="5" />
</LinearLayout>
<SeekBar
android:id="#+id/sheet_bar_rating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="4"
android:thumb="?android:attr/textSelectHandle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Packages" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/sheet_package_gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Gold" />
<CheckBox
android:id="#+id/sheet_package_diamond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Diamond" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/sheet_package_silver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Silver" />
<CheckBox
android:id="#+id/sheet_package_platinum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Platinum" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Activity
public class TripsActivity extends AppCompatActivity implements
SearchView.OnQueryTextListener, SeekBar.OnSeekBarChangeListener, View.OnClickListener {
public String MAX = "150000";
public String MIN = "0";
#BindView(R.id.trip_recyclerView)
RecyclerView recyclerView;
#BindView(R.id.bottom_sheet)
View view;
#BindView(R.id.sheet_spinner)
Spinner spinner;
#BindView(R.id.sheet_price_min)
TextView minPrice;
#BindView(R.id.sheet_price_max)
TextView maxPrice;
#BindView(R.id.sheet_price_text)
TextView currentPrice;
#BindView(R.id.sheet_bar_price)
SeekBar priceBar;
#BindView(R.id.sheet_bar_rating)
SeekBar ratingBar;
#BindView(R.id.sheet_package_gold)
CheckBox packageGold;
#BindView(R.id.sheet_package_silver)
CheckBox packageSilver;
#BindView(R.id.sheet_package_platinum)
CheckBox packagePlatinum;
#BindView(R.id.sheet_package_diamond)
CheckBox packageDiamond;
BottomSheetBehavior.BottomSheetCallback callback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
if (slideOffset < 0.4)
getSupportActionBar().show();
else
getSupportActionBar().hide();
}
};
private SearchView searchView;
private TripsAdapter adapter;
private BottomSheetBehavior mBottomSheetBehavior;
#OnClick(R.id.sheet_done)
public void onClick(View view) {
switch (view.getId()) {
case R.id.sheet_done:
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
}
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_trips_activity);
ButterKnife.bind(this);
mBottomSheetBehavior = BottomSheetBehavior.from(view);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mBottomSheetBehavior.setPeekHeight(0);
mBottomSheetBehavior.setBottomSheetCallback(callback);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
else
this.finish();
} else if (item.getItemId() == R.id.filterOptions)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.seach_meanu, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
adapter.filter(query);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return true;
}
#Override
public void onBackPressed() {
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
else
super.onBackPressed();
}
}
Someone can help me identify the problem. Thanks
Add this to your manifest.This issue trigered by the softkeyboard.Sadly I cant find the real source of the problem.
<activity>
android:name=..
android:windowSoftInputMode="adjustPan">
I get this from : https://stackoverflow.com/a/12956520/7202257
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(...);
I didn't have this problem until now. I don't know what is the problem here because in the same layout i have several buttons and only buttons which are in LinearLayout won't respond.
This is the layout where my buttons won't respond onClick (EDITED):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:background="#drawable/texture"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/input_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size">
<requestFocus />
</EditText>
<EditText
android:id="#+id/input_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_first_name"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size" />
<EditText
android:id="#+id/input_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_last_name"
android:ems="10"
android:inputType="number"
android:textSize="#dimen/text_size" />
<ImageView
android:id="#+id/profile_image"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_below="#+id/input_age"
android:layout_centerHorizontal="true"
android:src="#drawable/add" />
<Button
android:id="#+id/save_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/edit_button"
android:onClick="run"
android:text="#string/save_button" />
<Button
android:id="#+id/edit_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/slider"
android:text="#string/edit_button" />
<Button
android:id="#+id/delete_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/save_button"
android:layout_toStartOf="#+id/save_button"
android:text="#string/delete_button" />
<Button
android:id="#+id/edit_birthday_date"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/button_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cake_image"
android:layout_marginTop="10dp"
android:clickable="true"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:id="#+id/button_movie"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_books"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_tech"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container1"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="3"
android:clickable="true">
<Button
android:id="#+id/button_body_care"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_clothes"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_accessories"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container2"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button_games"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/edit_button"
android:layout_alignTop="#+id/slider"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<ImageView
android:id="#+id/cake_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/edit_birthday_date"
android:layout_below="#+id/edit_birthday_date"
android:layout_marginTop="20dp"
android:src="#drawable/birthday_cake" />
<TextView
android:id="#+id/turning_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cake_image"
android:layout_toEndOf="#+id/cake_image"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/cake_image" />
</RelativeLayout>
</ScrollView>
I have set for every linear layout visibility when one button which is working is clicked to visible, so i think that is not the problem.
This is the code in java:
if (getMovieCategory.equals("movies")) {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
}
});
} else {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbh.deleteMovies(getMovieCategory);
}
});
}
Here everything is working except onClick. I'm getting no errors. My button doesn't respond on click.
try by handling setOnClickListener out side the if condition.
.You're coding with a lot of redundancy and risking introducing errors, some of which may not be immediately obvious. Here's a leaner and clearer version of your posted code sample, using one onClick method to handle your if(){else} conditions.
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(
getMovieCategory.equals("movies") ? R.drawable.ic_cancelar : R.drawable.ic_add, 0,0,0
);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
dbh.deleteMovies(getMovieCategory);
}
}
});
EDIT 1: I mistakenly left out your String movies = "movies" and have added it back in. If you're only going to use this String once, to pass it as a parameter for dbh.updateCategoryMovies(birthdayId, movies), then you could simply call dbh.updateCategoryMovies(birthdayId, "movies") and delete String movies = "movies"
EDIT 2: Without seeing your entire source code, I don't know if you're at all changing the value of getMovieCategory somewhere. If not, then the below sample will change it on every click of the button (once you launch the app to test the sample, your buttonCategoryMovie will be a generic Button; once you start clicking the button, it should toggle between showing your R.drawable.ic_cancelar and R.drawable.ic_add resources. If the button toggles between these two drawables, then you at least know that the button is in fact receiving the onClick.
Remember that, if the value of getMovieCategory doesn't change on each click, your button will always perform only one of the sets of onClick actions and it'll look like nothing's happening.
If it works for you as I've described, then you're on your way ;)
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
getMoviesCategory = "not movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
getMoviesCategory = "movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
dbh.deleteMovies(getMovieCategory);
}
}
});
Try changing android:visibility="gone" to android:visibility="visible" in your LinearLayout.
And again add android:clickable="true" to your LinearLayout
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);
}