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(...);
Related
There are four ImageVIews with colors above and four below.
And there are buttons connected to each color.
If you press the button, the visibility of the image of the same color is gone.
If orange is gone, and pink, skyblue, and emerald is gone, I would like to have purple next to blue.
Should I use another layout or is there any other way?
I don't know what to do.
It's activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/redImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#android:color/holo_red_light" />
<ImageView
android:id="#+id/greenImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#android:color/holo_green_light" />
<ImageView
android:id="#+id/blueImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#android:color/holo_blue_light" />
<ImageView
android:id="#+id/orangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#android:color/holo_orange_light" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/purpleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#android:color/holo_purple" />
<ImageView
android:id="#+id/pinkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#ffccff" />
<ImageView
android:id="#+id/skyblueImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#66ffff" />
<ImageView
android:id="#+id/emeraldImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher_foreground"
android:visibility="visible"
app:tint="#99ff99" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/redButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="Red" />
<Button
android:id="#+id/greenButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_green_light"
android:onClick="onClick"
android:text="Green" />
<Button
android:id="#+id/blueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_blue_light"
android:onClick="onClick"
android:text="Blue" />
<Button
android:id="#+id/orangeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_orange_light"
android:onClick="onClick"
android:text="Orange" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3">
<Button
android:id="#+id/purpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_purple"
android:onClick="onClick"
android:text="Purple" />
<Button
android:id="#+id/pinkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffccff"
android:onClick="onClick"
android:text="Pink" />
<Button
android:id="#+id/skyblueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#66ffff"
android:onClick="onClick"
android:text="SkyBlue" />
<Button
android:id="#+id/emeraldButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#99ff99"
android:onClick="onClick"
android:text="Emerald" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
It's MainActivity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView redImage, greenImage, blueImage, orangeImage, purpleImage, pinkImage, skyblueImage, emeraldImage;
Button redButton, greenButton, blueButton, orangeButton, purpleButton, pinkButton, skyblueButton, emeraldButton;
boolean b1 = true, b2 = true, b3 = true, b4 = true, b5 = true, b6 = true, b7 = true, b8 = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redImage = findViewById(R.id.redImage);
greenImage = findViewById(R.id.greenImage);
blueImage = findViewById(R.id.blueImage);
orangeImage = findViewById(R.id.orangeImage);
purpleImage = findViewById(R.id.purpleImage);
pinkImage = findViewById(R.id.pinkImage);
skyblueImage = findViewById(R.id.skyblueImage);
emeraldImage = findViewById(R.id.emeraldImage);
redButton = findViewById(R.id.redButton);
greenButton = findViewById(R.id.greenButton);
blueButton = findViewById(R.id.blueButton);
orangeButton = findViewById(R.id.orangeButton);
purpleButton = findViewById(R.id.purpleButton);
pinkButton = findViewById(R.id.pinkButton);
skyblueButton = findViewById(R.id.skyblueButton);
emeraldButton = findViewById(R.id.emeraldButton);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.redButton:
b1 = !b1;
if(!b1) {
redImage.setVisibility(View.GONE);
} else {
redImage.setVisibility(View.VISIBLE);
}
break;
case R.id.greenButton:
b2 = !b2;
if(!b2) {
greenImage.setVisibility(View.GONE);
} else {
greenImage.setVisibility(View.VISIBLE);
}
break;
case R.id.blueButton:
b3 = !b3;
if(!b3) {
blueImage.setVisibility(View.GONE);
} else {
blueImage.setVisibility(View.VISIBLE);
}
break;
case R.id.orangeButton:
b4 = !b4;
if(!b4) {
orangeImage.setVisibility(View.GONE);
} else {
orangeImage.setVisibility(View.VISIBLE);
}
break;
case R.id.purpleButton:
b5 = !b5;
if(!b5) {
purpleImage.setVisibility(View.GONE);
} else {
purpleImage.setVisibility(View.VISIBLE);
}
break;
case R.id.pinkButton:
b6 = !b6;
if(!b6) {
pinkImage.setVisibility(View.GONE);
} else {
pinkImage.setVisibility(View.VISIBLE);
}
break;
case R.id.skyblueButton:
b7 = !b7;
if(!b7) {
skyblueImage.setVisibility(View.GONE);
} else {
skyblueImage.setVisibility(View.VISIBLE);
}
break;
case R.id.emeraldButton:
b8 = !b8;
if(!b8) {
emeraldImage.setVisibility(View.GONE);
} else {
emeraldImage.setVisibility(View.VISIBLE);
}
break;
}
}
}
Plz help me...
You should use GridView or RecyclerView instead. An other way when you want hide an ImageView you set gone the last ImageView and change color of others
I am developing app having a popup window showing successfully but button inside is not working. When the button is click the event is not listening. The code is shown below.
private void showPopup() {
//LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup_layout,null);
mPopupWindow = new PopupWindow(
customView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
Button btn_popup_submit = (Button)customView.findViewById(R.id.btn_popup_submit);
Button btn_popup_cancel = (Button)customView.findViewById(R.id.btn_popup_cancel);
btn_popup_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "aaa", Toast.LENGTH_SHORT).show();
Log.d("LOG","aaaa");
}
});
btn_popup_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "bbbb", Toast.LENGTH_SHORT).show();
Log.d("LOG","bbbb");
}
});
if(Build.VERSION.SDK_INT>=21){
mPopupWindow.setElevation(5.0f);//5.0f
}
mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(mContext, android.R.color.transparent)));
mPopupWindow.setOutsideTouchable(false);
mPopupWindow.setTouchable(false);
mPopupWindow.update();
}
The pop up layout code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:background="#5b5e93"
>
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email Sent."
android:textSize="20sp"
android:textColor="#color/colorWhite"
android:padding="25sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_above="#+id/editText"
android:layout_marginBottom="50dp"
android:background="#color/colorGray"
android:orientation="horizontal"></LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:textSize="16sp"
android:textColor="#color/colorWhite"
android:text="#string/email_sent" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:ems="4"
android:maxLength="4"
android:minLines="4"
android:inputType="number" />
<LinearLayout
android:layout_width="match_parent"
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/editText"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btn_popup_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/buttoncolor"
android:text="Submit"
android:textAllCaps="false" />
<Button
android:id="#+id/btn_popup_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/buttoncolor"
android:text="Cancel"
android:textAllCaps="false" />
</LinearLayout>
</RelativeLayout>
The button names are btn_popup_submit and btn_popup_cancel.
I tried different methods but the problem. I don't know where is the problem facing. Please help me thanks.
Replace this line of code.Allow popWindow Touch.
mPopupWindow.setTouchable(false);
to
mPopupWindow.setTouchable(true);
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
This is the getChildView inside the Adapter class...
public View getChildView(int parent, int child, boolean lastChild, View view, ViewGroup viewGroup) {
ArrayList<String> itemDetails= (ArrayList<String>)getChild(parent,child);
if(view==null)
{
LayoutInflater inflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.child_layout,viewGroup,false);
}
TextView textView =(TextView)view.findViewById(R.id.textView2);
textView.setText(itemDetails.get(0));
TextView textView1(TextView)view.findViewById(R.id.itemdescription);
textView1.setText(itemDetails.get(1));
TextView textView3= (TextView)view.findViewById(R.id.price);
textView3.setText(itemDetails.get(2));
final TextView textView2 = (TextView)view.findViewById(R.id.counterTextView);
ImageButton add = (ImageButton)view.findViewById(R.id.imageButton);
final ImageButton remove = (ImageButton)view.findViewById(R.id.imageButton1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString())+1));
remove.setVisibility(View.VISIBLE);
}
});
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Integer.parseInt(textView2.getText().toString())==0)
{
remove.setVisibility(View.INVISIBLE);
}
else {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString()) - 1));
}
}
});
return view;
}
This is the ChildLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:background="#FFFFFF">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/k_meals_rs"
android:id="#+id/imageView6" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="10dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_remove_black"
android:id="#+id/imageButton1"
android:background="#drawable/round_button"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#000000"
android:id="#+id/counterTextView" />
<ImageButton
app:srcCompat="#drawable/ic_add_black"
android:id="#+id/imageButton"
android:background="#drawable/round_button"
android:layout_width="29dp"
android:layout_height="29dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp">
<TextView
android:id="#+id/itemdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxEms="15"
android:textColor="#000000"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="₹150"
android:textColor="#000000"/>
</RelativeLayout>
</LinearLayout>
i want to increment & decrements the value of counterTextView as the the + and - buttons are pressed.. but as i do so it increment and decrements the value of counterTextView in other child in other parents with same index value...any idea how to overcome this problem.. thnx
I am facing a really strange behaviour where I set an onClickListener on checkboxes and their labels, within a custom adapter.
The problem is when I check an answer on the first question, the same item on the 3rd question is checked too. The checkboxes seems to be synchronised, but i don't understand why.
Here is some screens to understand.
1rst question
3rd question
Here is the getView() from my adapter:
public View getView(final int position, View convertView, ViewGroup parent)
{
View vi = convertView;
if(convertView == null)
{
vi = inflater.inflate(R.layout.question_item, null);
}
Qcm qcm = qcms.get(position);
List<Proposition> propositions = qcm.getPropositionsList();
TextView numQuestion = (TextView)vi.findViewById(R.id.question_number_item);
TextView question = (TextView)vi.findViewById(R.id.question_text);
LinearLayout[] labels = new LinearLayout[5];
labels[0] = (LinearLayout)vi.findViewById(R.id.question_label1);
labels[1] = (LinearLayout)vi.findViewById(R.id.question_label2);
labels[2] = (LinearLayout)vi.findViewById(R.id.question_label3);
labels[3] = (LinearLayout)vi.findViewById(R.id.question_label4);
labels[4] = (LinearLayout)vi.findViewById(R.id.question_label5);
final CheckBox[] checkBoxes = new CheckBox[5];
checkBoxes[0] = (CheckBox)vi.findViewById(R.id.question_check1);
checkBoxes[1] = (CheckBox)vi.findViewById(R.id.question_check2);
checkBoxes[2] = (CheckBox)vi.findViewById(R.id.question_check3);
checkBoxes[3] = (CheckBox)vi.findViewById(R.id.question_check4);
checkBoxes[4] = (CheckBox)vi.findViewById(R.id.question_check5);
numQuestion.setText(String.valueOf(position+1));
question.setText(Html.fromHtml(qcm.getQuestion()));
for(int i=0; i<5; i++)
{
final int cpt = i;
checkBoxes[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Checked[i] -> !Checked[i]
QcmListAdapter.this.checked[position][cpt] = !QcmListAdapter.this.checked[position][cpt];
APIHelper.log('d', "position="+position);
// TODO : Faire la methode PlaySerieFragment.saveSession();
//QcmListAdapter.this.activity.getFragmentManager().getFragment(QcmListAdapter.this.);
}
});
labels[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBoxes[cpt].performClick();
APIHelper.log('d', "onClick:cpt="+cpt);
}
});
Proposition p = propositions.get(i);
p.setFormattedView(this.activity, labels[i], (i + 1));
}
return vi;
}
and here is the view for an item :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/question_block"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<RelativeLayout android:id="#+id/question" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="#+id/question_number_item"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:text="X"
android:textSize="30sp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#drawable/question_number" />
<TextView
android:id="#+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Question 1 : Texte descriptif de la question tellement long"
android:textSize="16sp"
android:textColor="#color/MPQ_blue"
android:layout_toRightOf="#id/question_number_item"/>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question" android:layout_marginTop="16dp">
<CheckBox android:id="#+id/question_check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check1"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item1" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check2"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item2" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check3"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item3" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check4"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item4" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check5"></LinearLayout>
</RelativeLayout>
</RelativeLayout>
I suspect this is because the view is getting re-used. Android re-uses views in a listview for performance, as the view is re-used when you click the same view displays the checked state.
You can either remove the if statement around convertView
if(convertView == null)
..
But this will be determental to performance if you have a lot of items.
Otherwise you should use the common viewHolder method:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Hope this helps.