I'm using com.google.android.material.textfield.TextInputLayout with style Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu and using also AutoCompleteTextView. These make the input as dropdown list. But the issue right now is long text of list won't show the complete item text. How can I show all the text and make it multiline?
<com.google.android.material.textfield.TextInputLayout
style="#style/CustomStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_input">
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
themes.xml
<style name="LayoutDropdown" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu">
<item name="boxBackgroundColor">#color/white</item>
<item name="android:textColor">#color/black</item>
<item name="colorControlNormal">#color/black</item>
<item name="colorControlActivated">#color/black</item>
<item name="colorControlHighlight">#color/black</item>
</style>
Java
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_menu_item, list);
AutoCompleteTextView dropdown = findViewById(R.id.autocomplete);
dropdown.setAdapter(adapter);
dropdown.setKeyListener(null);
dropdown_menu_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1"/>
Here is the result. It doesn't show full text on selection dropdown.
Change the layout used for each item.
Use something like:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_menu_item,R.id.item, list);
with:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
/>
</LinearLayout>
Also if you want to display the long text also in the AutoCompleteTextView use android:inputType="textMultiLine"
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
/>
Related
I tried following a tutorial (https://guides.codepath.com/android/using-the-app-toolbar#custom-title-view) on creating a custom TextView inside a Toolbar. What I want to do is have the ability to dynamically change the text inside the TextView using Java. However, the problem is that I can't and no error messages are being displayed.
Toolbar code in XML:
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/toolbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Test1"
android:textSize="16sp"
android:textColor="#color/textColor" />
</androidx.appcompat.widget.Toolbar>
Toolbar Java code in the Activity Class:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
TextView toolbarTitle = toolbar.findViewById(R.id.toolbarTitle);
toolbarTitle.setText("Test");
Styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorSecondary</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
A simple toolbarTitle.setText() should do the job but for some unknown reasons it doesn't. Even if I remove the android:text from the XML code and use setText() right after the Java code it still doesn't work.
Does this mean that TextViews inside Toolbars are immutable?
Thanks!
Edit 1:
So I managed to find the problem but haven't particularly found a solution. The problem is caused by the Data Binding in the activity_main.xml file. Once the all code related to Data Binding is removed, I'm able to use setText() the way I want. Why is this causing a problem?
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout
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"
tools:context=".activity.MainActivity">
<data>
<variable
name="VM"
type="com.tahmidu.app.view_model.IMainActivityNavViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/toolbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColor" />
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:id="#+id/audioMainFragment"
android:name="com.tahmidu.app.fragment.AudioFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorSecondary"
app:itemIconTint="#color/bottom_navigation_color"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_navigation_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
TextViews inside toolbars are definitely not immutable. Maybe the problem is where you are calling the textview.setText() method. It should be after after the view is created, so on onCreate() or onViewCreated()
Solution (Suggested by Nabil): Bind the text. Ensure you set the LifecycleOwner() method otherwise the UI would not update. setText() stops working if you use Data Binding from the looks of it.
My Android (Java) App uses Fragments to display products in a GridLayout.
Each product is a cardview with two TextViews as well as an ImageView.
I have all my products stored on a webserver in mariaDB. Fragment's OnCreate method gets my productList from server.
Now I want to dynamically create CardViews for all products returned from the server. For now, I have hardcoded the layout, but as the number of products can change, it's neccesary to create CardViews for the products dynamically.
How can I achieve this in Java?
This is how the Fragment looks:
Here's how I implemented the layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragments.DrinksFragment">
<androidx.gridlayout.widget.GridLayout
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="14dp"
app:alignmentMode="alignMargins"
app:columnCount="3"
app:columnOrderPreserved="false"
app:rowCount="3">
<androidx.cardview.widget.CardView
android:id="#+id/cV_water"
style="#style/ProductCardStyle">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iV_water"
style="#style/ImageStyle"
android:contentDescription="#string/ic_water"
app:layout_constraintBottom_toTopOf="#+id/tV_counterWater"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tV_descWater"
app:srcCompat="#drawable/ic_water_bottle" />
<TextView
android:id="#+id/tV_descWater"
style="#style/TextStyle"
android:text="#string/product_water"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tV_counterWater"
style="#style/TextStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.gridlayout.widget.GridLayout>
</FrameLayout>
Also, here's the ProductCardStyle:
<style name = "ProductCardStyle">
<item name = "cardBackgroundColor">#color/colorPrimary</item>
<item name = "android:layout_width">0dp</item>
<item name = "android:layout_height">0dp</item>
<item name = "android:layout_marginLeft">2dp</item>
<item name = "android:layout_marginRight">2dp</item>
<item name = "android:layout_marginBottom">2dp</item>
<item name = "android:layout_marginTop">2dp</item>
<item name = "cardCornerRadius">4dp</item>
<item name = "cardElevation">8dp</item>
<item name = "layout_columnWeight">1</item>
<item name = "layout_rowWeight">1</item>
</style>
Would appreciate any help!
You need to use RecyclerView with GridLayoutManager.
You can begin with simple RecyclerView like in this article
https://proandroiddev.com/how-to-implement-a-recyclerview-33fd4ff9988e
And apply GridLayoutManager after RecyclerView implemented.
For more info:
https://developer.android.com/guide/topics/ui/layout/recyclerview
The number of stars in the RatingBar seems to be more than I have set. Although I set it to 5, more stars appear on the screen. The screen shots are below. However, the normal number appears when choosing the stars.
My empty RatingBar's stars count are not coming true. Empty ratingbar is not showing, android default RatingBar is shown. When my RatingBar is filling, both of my filled RatingBar and android default RatingBar is showing.
My code is as follows:
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:id="#+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:orientation="horizontal"
android:padding="20dp">
<TextView
android:id="#+id/ratingInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/callRatingBar"
android:textColor="#color/white"
android:textSize="#dimen/topTitle" />
</LinearLayout>
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:isIndicator="false"
android:numStars="5"
android:progressBackgroundTint="#color/transparent_black_30"
android:stepSize="1"
android:theme="#style/"
tools:rating="1" />
<TextView
android:id="#+id/limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_horizontal"
android:contentDescription="#null"
android:gravity="end|center_horizontal"
android:paddingEnd="20dp"
android:paddingRight="20dp"
android:text="200"
android:textColor="#color/grey"
android:textSize="#dimen/infoTitle" />
<com.project.android.util.view.CustomEditText
android:id="#+id/commentText"
style="#style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:fadeScrollbars="true"
android:fastScrollAlwaysVisible="false"
android:fastScrollEnabled="true"
android:gravity="start"
android:hint="#string/leaveAComment"
android:maxLength="200"
android:minHeight="40dp"
android:nextFocusRight="#+id/textSendButton"
android:nextFocusForward="#+id/textSendButton"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="#drawable/edittext_scrollbar"
android:scrollbars="vertical"
android:scrollingCache="false"
android:scrollHorizontally="false"
android:smoothScrollbar="true"
android:textSize="#dimen/infoTitle"
app:emojiSize="#dimen/conversation_emojiSize" />
MyRatingBar style:
<style name="MyRatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">#color/lightTransparentBlack</item>
<item name="colorControlActivated">#color/pin_yellow</item>
<item name="android:numStars">5</item>
</style>
Have you tried setting the amount of stars via code?
ratingBar.setRating(int)
The ratingbar is known for having weird bugs like this
Also look at the various reasons for it not working here
I'm trying to use an xml selector to change the image of imagebuttons when they are pressed but I've been faceing a few problems. First of all, the images appear with different dimensions on the screen, and secondly the button changes color only when clicked, but not when it is active.
This is the xml of the layout with the imagebuttons:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/coupon_filter_btns"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#a7a7a7"
android:weightSum="7"
>
<ImageButton
android:id="#+id/filter_greece"
android:layout_width="0dp"
android:layout_height="40dp"
android:scaleType="fitCenter"
android:src="#drawable/greece"
android:layout_weight="1"
android:background="#drawable/greece"
android:paddingTop="4dp"
android:paddingBottom="4dp"
/>
<ImageButton/>
<ImageButton/>
</LinearLayout>
Below is the selector called greece.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/greece_active" android:state_selected="true"></item>
<item android:drawable="#drawable/greece_active" android:state_focused="true"></item>
<item android:drawable="#drawable/greece_active" android:state_pressed="true"></item>
<item android:drawable="#drawable/filter_white_greece"></item>
</selector>
And this is the java code where the button is used:
ImageView filterGreece = (ImageView) kati.findViewById(R.id.filter_greece);
filterGreece.setBackgroundResource(R.drawable.greece);
filterGreece.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
Ok first there Erro in your XML
you Just set drawable as background Not Src
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/coupon_filter_btns"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#a7a7a7"
android:weightSum="7"
>
<ImageButton
android:id="#+id/filter_greece"
android:layout_width="0dp"
android:layout_height="40dp"
android:scaleType="fitCenter"
android:layout_weight="1"
android:background="#drawable/greece"
android:paddingTop="4dp"
android:paddingBottom="4dp"
/>
</LinearLayout>
second selector will work fine without
filterGreece.setBackgroundResource(R.drawable.greece);
so remove this Line
I am making Spinner like below image. It's work fine as I want. But I want to change background color to #FFFFFF and textColor to #000000. But I got revise output...
java code:
Spinner staticSpinner = (Spinner)findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
xml code :
<Spinner
android:id="#+id/static_spinner"
android:layout_width="150dp"
android:layout_height="45dp"
android:layout_marginLeft="30dp"
style="#android:style/Widget.Holo.Light.DropDownItem" ></Spinner>
simple_spinner_dropdown_item.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#000000"
/>
simple_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textColor="#000000"
android:textAlignment="inherit"/>
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#0072BA</item>
<item name="colorPrimaryDark">#004F80</item>
<item name="colorAccent">#0072BA</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
<item name="android:actionBarSize">48dp</item>
<item name="actionBarSize">48dp</item>
<item name="android:windowActionBar">false</item>
</style>
</resources>
By using below code I can solve my problem.
<Spinner
android:id="#+id/static_spinner2"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:layout_marginLeft="30dp"
android:background="#drawable/apptheme_spinner_background_holo_light"
android:popupBackground="#ffffff" />
Spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:background="#FFFFFF"
android:textColor="#000000" />
spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#000000"/>
java code :
Spinner staticSpinner = (Spinner)findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter =
ArrayAdapter.createFromResource(this,R.array.request_role,R.layout.spinner_item);
staticAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
Use this spinner in your code
<Spinner
style="#style/edit_style"
android:id="#+id/spr_city"
android:background="#drawable/selector_spinner"
android:layout_width="0dp"
android:textColor="#android:color/white"
android:minHeight="#dimen/min_height"
android:textCursorDrawable="#null"
android:textColorHint="#android:color/white"
android:layout_height="fill_parent"
android:layout_weight="9"
/>
and make this style in style .xml
<style name="edit_style" >
<item name="android:textSize">14sp</item>
<item name="android:textColor">#ffffff</item>
</style>
and use this code in activity
ArrayAdapter<String> adp1=new ArrayAdapter<String> (activity,R.layout.spinner_item_selected,city_list);
adp1.setDropDownViewResource(R.layout.spinner_item);
s_city.setAdapter(adp1);
and create xml spinner_item_selected
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textViewSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/common_margin"
style="#style/edit_style"
android:textColor="#android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" />
and one more xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textViewSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/common_margin"
style="#style/edit_style"
android:textColor="#color/spinner_item_selector"
xmlns:android="http://schemas.android.com/apk/res/android" />
You can set the spinners background color in xml like this:
android:background="YOUR_HEX_COLOR_CODE"
and if you use the drop down menu with you spinner you can set its background color like this:
android:popupBackground="YOUR_HEX_COLOR_CODE"
change android.R to yourprojcet.R
android.R.layout.simple_spinner_dropdown_item
to
letmobility.com.itforte.R.layout.simple_spinner_dropdown_item;
it is taking android layout instead of your app specific layout.
Your call should be like this :
ArrayAdapter<CharSequence> staticAdapter =
ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
instead of :
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);