Can't find the style for checkbox - java

I would like to programatically style my dynamically created checkboxes as radio buttons as I prefer the look.
To do this I have created a checkboxStyle.xml and put it in values section of the res folder.
checkboxStyle.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="checkboxStyle">
<item name="android:checkboxStyle">#android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton</item>
</style>
</resources>
I am then calling this in my fragment as I create my checkboxes but R.id.checkboxStyle is coming up as not found, not really sure where to put this id though or if I need to refer to it in another way
CheckBox checkBox;
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("this");
list.add("thing");
final ArrayList<CheckBox> checkBoxArray = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
checkBox.setId(i);
checkBox.setText(list.get(i));
checkBox.setTag(list.get(i));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.v("CheckBox - checked", buttonView.getTag().toString());
}else{
Log.v("CheckBox - unchecked", buttonView.getTag().toString());
}
}
});
checkBoxArray.add(checkBox);
checkboxContainer.addView(checkBox);
}
testing_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#color/colorWhite">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/checks"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="#+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
it's not an id, it's an style and you need to access it like this
R.style.checkboxStyle;

Related

Resizing linearlayout container doesn't update onClickListner position of views contained within

I have a linearLayout with some cardviews. When the Activity is created, these cardViews get an onClickListern. This Activity has a button that can shrink said linearLayout. However, once shrunk the location of the cardViews no longer match where you interact with it. For example, if you have a cardView on the middle, left side of the screen. Than shrink the linearLayout. You have to tap where the cardView would be, as if the linearLayout wasn't shrunk. Trying to tap where the actual cardView is, does nothing. How do I reposition the the location and size of the onClickListner to match the new location of the cardViews? I'm not against changing my approach on how to resize the linearLayout.
Before Shrinking:
After shrinking:
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter = "true">
<scale
xmlns:android = "http://schemas.android.com/apk/res/android"
android:duration = "1000"
android:fromXScale="1"
android:fromYScale = "1"
android:pivotX = "50%"
android:pivotY = "50%"
android:toXScale = "2"
android:toYScale = "2"/>
</set>
zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter = "true">
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration = "2500"
android:fromXScale = "1"
android:fromYScale = "1"
android:pivotX = "50%"
android:pivotY = "50%"
android:toXScale = ".2"
android:toYScale = ".2" />
</set>
TreeActivity.java
public class TaxaTreeViewActivity extends AppCompatActivity {
private TaxonomyNode taxonomyTree = TaxonomyNodeUtil.getInstance().getTaxonomyTree();
private BaseTreeAdapter adapter;
private Button zoomInButton, zoomOutButton;
private Animation zoomInAnim, zoomOutAnim;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tree_view);
...
adapter = new BaseTreeAdapter<NodeViewHolder>(this, R.layout.node) {...
//Tree stuff}
zoomInAnim = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
zoomOutAnim = AnimationUtils.loadAnimation(this, R.anim.zoom_out);
zoomInButton = findViewById(R.id.treeZoomInButton);
zoomOutButton = findViewById(R.id.treeZoomOutButton);
zoomOutButton.setOnClickListener(v -> {treeView.startAnimation(zoomOutAnim)});
zoomInButton.setOnClickListener(v -> {treeView.startAnimation(zoomInAnim);});
activity_tree_view.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="#+id/zoom_linear_layout"
tools:context=".TaxaTreeViewActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/treeZoomOutButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/treeviewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.blox.treeview.TreeView
android:id="#+id/treeview"
android:layout_width="match_parent"
android:layout_height="match_parent"></de.blox.treeview.TreeView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/treeZoomInButton"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:src="#drawable/ic_zoom_in"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/treeZoomOutButton"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:src="#drawable/ic_zoom_out"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Checkboxes of a ListView are changing theirs values when the List is scrolled

I'm using Android Studio to develop an application and i'm having a problem. In my app, there is a ListView that shows subjects added to a database using an arrayAdapter and the list gives you the option to select or not a subject (with checkboxes). Here is the XML file with the ListView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.cliente.matriadada.AddTurmaActivity">
<ListView
android:id="#+id/listaConteudosTurma"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:choiceMode="multipleChoice"
android:clickable="true"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
</android.support.constraint.ConstraintLayout>
Here is the Activity code (in Java):
public class AddTurmaActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_turma);
ListView lista = (ListView) findViewById(R.id.listaConteudosTurma);
MainActivity.BancoController crud = new MainActivity.BancoController(getBaseContext());
Cursor cursor = crud.carregaConteudos();
ArrayList<String> valores = new ArrayList<>();
if (cursor.moveToFirst()){
do {
valores.add(cursor.getString(1));
} while(cursor.moveToNext());
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,
R.layout.linha_com_check_layout, R.id.txtComCheck, valores);
lista.setAdapter(adaptador);
}
}
And finally the XML file that the adaptor uses:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
android:id="#+id/txtComCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxLista"
android:layout_toEndOf="#+id/checkBoxLista"
android:layout_toRightOf="#+id/checkBoxLista"
android:text="TextView"
android:textSize="18sp" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtComCheck" />
</android.support.constraint.ConstraintLayout>
When I launch my app and check some checkboxes, if I scroll the list other checkboxes get checked too. I think it's a problem with RecyclerView or something like this.
How can I fix this?
View Holder holds your views for a fixed position, and when this position is shown on the screen it will extract the view.
Please follow this link
Using ViewHolder
which helped me quite a while ago

How can I make an EditText in a ContextMenu

I have a ListView of values and I want to make each value editable by pressing on it and opening a context menu. How can I add an EditText field to the context menu that appears when clicking on an item in a ListView? If you have a better idea for said issue, feel free to suggest it. Thanks in advance to anyone who answers!
You don't have to use a context menu for that. You can use AlertDialog.
Check out this answer here.
https://stackoverflow.com/a/45352961/8200290
Just create a different layout with an edit view.
Here is you solution.
You are going to have to edit things to make it fit into your application!
MainActivity class
public class MainActivity extends AppCompatActivity {
List<String> list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("Android");
list.add("iPhone");
list.add("Windows");
list.add("Blackberry");
list.add("Mac");
list.add("Laptop");
list.add("LCD");
list.add("Dell");
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view_item, list);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked: " + list.get(position), Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.custom_alert, null);
//Set the view
alert.setView(alertView);
//Show alert
final AlertDialog alertDialog = alert.show();
//Can not close the alert by touching outside.
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Set the edit text for the item clicked
EditText editText = (EditText) alertView.findViewById(R.id.editText);
editText.setText(list.get(position));
ImageView closeButton = (ImageView) alertView.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
}
});
listView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.vzw.www.listviewalert.MainActivity">
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
custom_alert.xml
-> This will show your EDIT text. The box at the bottom closes the alert
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/alertContainer"
android:background="#drawable/custom_alert_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/rowOne">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<ImageView
android:id="#+id/closeButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_weight="0.33"
android:background="#cdcdcd" />
</RelativeLayout>
#drawable/custom_alert_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#ffffff"/>
<corners
android:radius="5dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
You get the following:

Using Espresso with Embedded Toolbar

I have the following list item that is used within a RecyclerView that has a vertical LinearLayout Manager. I am using espresso, and I would like to simulate clicking the id/action_save menu item. Below is my latest attempts at figuring this one out. I have tried using onData method combined with onChildView, but a MenuItem is not a View. The onView and withId methods works well if the menu item is located in a toolbar that is in the action bar, but while being embedded inside of a RecyclerView.
I have tried recording using the espresso recording feature in android studio, however, it inject code similar to this Espresso click menu item. When running the test case again causes the test to raise an exception.
List Item
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/MyDarkToolbarStyle"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ProgressBar
android:id="#+id/progress"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true" />
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imgPalette"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="250dp" />
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="-2dp"
android:elevation="4dp"
android:outlineProvider="bounds"
android:paddingTop="2dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_save"
android:title="#string/save"
app:showAsAction="never" />
</menu>
Main Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/banner" />
...
</RelativeLayout>
Java (Espresso / JUnit Test Case)
...
// mProducts is a list that is backed by the adapter and is of Type Product
for (int i = 0; i < mProducts.size(); i++) {
onView(allOf(withId(R.id.recyclerView), withParent(withId(R.id.container))))
.perform(actionOnItemAtPosition(i, clickOverflow(R.id.toolbar)));
// Add delay for system to respond to overflow being opened
SystemClock.sleep(1000);
onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(i, clickMenuItem(R.id.toolbar, R.id.action_save)));
}
...
public static ViewAction clickMenuItem(final int toolbarId, final int id) {
return new ViewAction() {
#Override
public Matcher<View> getConstraints() {
return null;
}
#Override
public String getDescription() {
return "Click on a child view with specified id.";
}
#Override
public void perform(UiController uiController, View view) {
Toolbar toolbar = (Toolbar) view.findViewById(toolbarId);
for (int i = 0; i < toolbar.getChildCount(); i++) {
if (toolbar.getChildAt(i) instanceof ActionMenuView) {
ViewGroup viewGroup = ((ViewGroup) toolbar.getChildAt(i));
for (int j = 0; j < viewGroup.getChildCount(); j++) {
if (viewGroup.getChildAt(j).getId() == id) {
viewGroup.getChildAt(j).performClick();
break;
}
}
}
}
}
};
}
public static ViewAction clickOverflow(final int toolbarId) {
return new ViewAction() {
#Override
public Matcher<View> getConstraints() {
return null;
}
#Override
public String getDescription() {
return "Click on a child view with specified id.";
}
#Override
public void perform(UiController uiController, View view) {
Toolbar toolbar = (Toolbar) view.findViewById(toolbarId);
((ViewGroup) toolbar.getChildAt(0)).getChildAt(0).performClick();
}
};
}
Error
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.variable.inspire:id/recyclerView
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.support.v7.widget.MenuPopupWindow$MenuDropDownListView{92d26ac VFED.VC.. .F...... 0,0-515,252}
Update #1
This does not solve my question, but provides a workaround by displaying the menu item directly and not inside the overflow menu. My question still is using espresso can you open the overflow menu and click an item in it.
onView(allOf(withId(R.id.recyclerView), withParent(withId(R.id.container)))).perform(actionOnItemAtPosition(i, clickMenuItem(R.id.toolbar, R.id.action_save)));

IllegalargumentException while making a custom toast in android

I am very new to android. I am making a custom toast but by app is crashing just after the toast appears in the screen.
This is my mainactivity.java -
public class MainActivity extends Activity {
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dosome(View V) {
Log.d("message","a message");
if(V.getId() == R.id.launchmap) {
Toast toast = new Toast(this);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
LayoutInflater inflater = getLayoutInflater();
View appear = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.root),true);
toast.setView(appear);
toast.show();
}
}
}
And these are my xml files.
toast_layout.xml -
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/images" />
activity_main.xml -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/root"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/launchmarket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dosome"
android:text="launchmarket" />
<Button
android:id="#+id/launchmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dosome"
android:text="launchmap" />
</LinearLayout>
However, in the inflater.inflate, if i pass the 3rd parameter as false, everything works. Why so?
Thing is toast is not supposed to be child of some layout in your view, its an independent layout. This is the layout of your toast (toast_layout.xml in your case).
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/images" />
You need to wrap this with a layout lets say a LinearLayout and give id toast_layout_root for that particular linearLayout. Like so
<?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="match_parent"
android:id="#+id/toast_layout_root">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/images" />
</LinearLayout>
Then do this while making your Toast
View appear = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout_root),true);
What you are doing currently is trying attach toast to the linearlayout that is in your activity. Toasts have to be independent.

Categories