Material toggle group component not working on Android - java

I've been trying to use MaterialToggleGroup in an Android project, but it is not working properly. The problem is that it always seems to be empty, no matter what you put inside it.
I'm using a very simple layout, with a LinearLayout that includes the group and a ListView. The group itself contains two buttons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="#+id/toggle_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
android:id="#+id/button1"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<com.google.android.material.button.MaterialButton
android:id="#+id/button2"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</com.google.android.material.button.MaterialButtonToggleGroup>
<ListView
android:id="#+id/listado"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
/>
</LinearLayout>
In the editor it looks like this:
toggle group with wrap_content
If I change the hight and set it, for example, to 30dp, it shows up but as if it was empty, proving that it is being inserted but it's no working properly:
toggle group with 30dp
Project is in Java and its dependencies are pretty much not updated. The problem can be seen on editor but also when app is running on emulator and physical device.
What I've tried so far:
Invalidating caches/restarting.
Clean and rebuild project.
Upgrading all dependencies, including Gradle build tools, material and app compat libraries.
Test it in another layout, same result.
Test some other Material components, they work fine.
Create a new project and use the component, no problems found, seems that the problem is in this project.
I'm running out of ideas, but I'd like to avoid creating my own component to achieve the same result.
Thanks for your help.

Related

How to multiply layouts dynamically and know witch one is clicked by user?

Fristly, sorry for my english.
Then, I'm trying to develop an app with android studio. I'm doing well, thank to stackoverflow !
But now I have a problem. In my app, I need to create new layouts (from an original one) when the user wants a new one in the main layout.
The layout I want to duplicate :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"> >
<TextView android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text1">
<TextView android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="text2"
</LinearLayout>
After adding it and setting Textviews with data, the user can click on it to see more information (by maybe using another layout).
But the problem is, I don't know how to put a global Click Listener AND know witch layout added is clicked. I try a lot of things because it didn't work, can someone help me please ?

Right to left in android studio

I'm using Android Studio for application development and I have a problem with placing elements in the screen.
For example: I'm trying to place a button to the right of the screen and in the studio its shown to the right of the screen but when I install the app in my smartphone i see it in the left of the screen (my smartphone is right to left configured).
How can i resolve it? (I use RelativeLayout if that matters).
Thanks!
illustration image
It would be easier to answer your question if you included some code. However, assuming your button is inside a relative parent layout which fills the screen, adding this to your button's xml code will make it align to the right:
android:layout_alignParentRight="true"
ie.
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp" />
Or to make it really easy, replace your entire XML with:
<?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"
android:weightSum="1">
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
The code for those who requested:
<?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"
android:weightSum="1"><![CDATA[
android:splitMotionEvents="true"
tools:context="tomer.newapp.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
]]>
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_column="2"
android:layout_row="2"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Note that the absolute values that you are using are in the tools namespace - this means they are not compiled into your app.
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
That is probably why you are getting this kind of behavior.
Remove them and then see if the button moves to the left of the screen.
More about tools namespace - here
You mentioned "(my smartphone is right to left configured)".
This could be a dud answer, but...
With the layout_editor, for either the "gravity" or "layout_gravity" properties, there is a "right", "left", or "start" and "end". In English, one starts writing at the left, and ends at the right. In Arabic, for example, one starts at the right and ends left. Any chance your property says "end", instead of "right"?

How to have an expandable CardView?

I'm trying to develop an app right now that uses CardViews, and I'd like to have them be expandable; that is to say that upon someone pressing the CardView, it should expand to show more details. How would I go about doing this?
Here's a visual explanation of what i'm trying to do; when I press the highlighted CardView, I want it to expand to show more information, and be able to contract upon another press; I'm not sure how to do this, and searching the internet has led me to no answer as of yet.
Best Solution is,use Expandable Layout and add it inside your CardView.
<com.kyo.expandablelayout.ExpandableLayout
android:id="#+id/expandlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp" >
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/child"
app:canExpand="true" />
</com.kyo.expandable.ExpandableLayout>

Why R.java disappears (R cannot be resolved error)? [duplicate]

This question already has answers here:
R cannot be resolved - Android error
(108 answers)
Closed 8 years ago.
I know this is a very common problem and I read the previous posts
But my project was just working fine and after I added a radio button it ruined the interface
Then I deleted the radio button and some interface contents (like edit-texts and button) and added them again. Now the interface is fine.
The MainActivity.java did not recognize the radio button (although import android.widget.RadioGroup;
import android.widget.RadioButton; are there) so I cleaned the project because I wanted R.java to be updated
Then, R.java DISAPPEARS and the console shows this image
Things I have tried :
Re-clean project
Fix project portieres
This is my main_activity.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.reg.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/radioGroup1"
android:layout_marginTop="24dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textEmailAddress" >
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText4"
android:layout_alignLeft="#+id/editText2"
android:layout_marginBottom="18dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/editText4"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="date" />
<EditText
android:id="#+id/editText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText5"
android:layout_below="#+id/editText5"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textEmailAddress"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
/>
</RelativeLayout>
please justify your answer, I am just a beginner
UPDATE
it works when I change the target to 21 but I want it to be 20
I think you are confused about your issue, Your logcat says that you are getting some issues while importing support-v7-appcompat library and you are saying , there is no R.java
I think you are getting issues while adding support-v7-appcompat library and thus it might be affecting R.java
Please follow below steps to add lib
ollow the instructions in the Support Library Setup - Adding libraries with resources.
One thing that can bring some doubt is add the library to your aplicattion project:
(1) In the Project Explorer, right-click your project and select Properties.
(2) At the left pane select Android.
(3) At th bottom of the right pane click Add
(4) Select the library project android-support-v7-appcompat and click OK. Click OK to close the properties window.
If you not get any of the errors I mentioned it´s all done.
However if you are like me, you dont´t followed the advice of Android team that suggests you always compile your source using the latest SDK, you get the error.
The error ocurrs because my Project Build Target was Android 2.3.3 and need to be at lest 3.0.
It was not immediately clear to me because of the fact that I'm specifically developing for Android 2.3.3 and I'm using support libraries.
OK, I hope this helps.
R.java is not being generated as there is error in your xml files. If I were you, I would check the xml files (especially the layout files) for errors, resolve them and then clean the project.
Try changing your XML file to : (defining the android namespace first, and then the tools)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
.....
Just go to gen folder>R.java & delete R.java and clean your project.
R.java will be automatically generated and your error will be removed .
Now I have seen the image post by you :-
So I think you should check this also,also right click your project->build path->android
check whether the appcompact-v7 library is refered properly.check whether there is green tick for lib reference
Case 1 = i think u delete radio button manually from xml so when we delete some button on textview etc. from xlm then some there other edit text or text view contain there id's like in below or above option in xml so R.java file have also these id's so just go in R.java find the error like and delete that id from R.java and xml also like
android:layout_below="#+id/radioGroup1"
in your case you delete a radio group but u did not delete id from edit text 2 so may b its give you that error
Case 2 = may be you are using android-support-v7-appcompat.jar file in your project so simple go in lib folder and delete android-support-v4.jar from lib and clear and re build your project
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/radioGroup1"
android:layout_marginTop="24dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textEmailAddress" >
</EditText>
Hope its works
According to the Docs and Your error message that You´ve showed in the picture, it seems that one of the error is, that Your V7 support library is not included the right way. But this is vague and Your errors can have multiple causes. But for the first, try something:
be sure You have v4 support and v7 appcompat libraries included (both)
be sure You have downloaded both libraries to Your Android SDK
You have to set Your build versions like specified in the doc here:
https://developer.android.com/tools/support-library/features.html#v7
if all this is done, usually the error messages that You have showed in the image should be gone. Then You can go on, with the other error messages, if there are still some. Then come back if You need more help and show updatet question.

Crouton centering custom view and issues with disappearing

Today I changed my Toast to Crouton.
But I had these problems :
1)Showing Crouton in center
2)Whenever I want to display new Crouton it is displaying previous Crouton again, though previous one hided long ago
I want to know if there is better way to solve above problems?? Cause I looked at sample and library but could not find"
Currently I'm solving these ways
activity layout:
<RelativeLayout
android:id="#+id/bigcontainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DilmancSRActivity"
tools:ignore="MergeRootFrame" />
<RelativeLayout
android:id="#+id/alternate_view_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Creating my custom toast view and centering it horizontally:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/kdrw"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="8dp"
android:contentDescription="#string/strnull"
android:src="#drawable/logo" />
<TextView
android:id="#+id/toasttextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
Showing Crouton:
LayoutInflater inflater = localdsr.getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, null);
TextView text = (TextView) layout.findViewById(R.id.toasttextView1);
text.setText(info);
if (toast != null)
toast.cancel();
Configuration croutonConfiguration=new Configuration.Builder().setDuration(1500).build();
toast=Crouton.make(localdsr, layout ,R.id.alternate_view_group ,croutonConfiguration);
toast.show();
For solving the Second one I had to download library source and use it. .As I do not want to use animations firstly, I disabled animations
//croutonView.startAnimation(crouton.getInAnimation());
issue on removing function that was making things appear again inside
protected void removeCrouton(Crouton crouton)
//sendMessageDelayed(crouton, Messages.DISPLAY_CROUTON, crouton.getOutAnimation().getDuration());
//I changed it with below .plus removed crouton.getOutAnimation().getDuration() so it does not throw exception
sendMessage(crouton, Messages.DISPLAY_CROUTON);
I believe I am missing something. Hope there is already right way to do those .
Making changes within the library itself is not encouraged as it might create issues that only appear within your version of Crouton.
You can change the Animation via Configuration.Builder.setInAnimation(...) and .setOutAnimation(...).
The Configuration can be re-used and does not have to be created every time you want to show the Crouton. The same is valid for your View. It does not have to be inflated every time.
Regarding the duplicate display of a Crouton: This behavior is not intended.
The code above looks like it should display a Crouton with the info text set. If your info text has changed during the display of Croutons it might be a bug within the library. If that is the case, feel free to report it.

Categories