Custom button that extends AppCompatButton - java

I'm trying to create custom button that will contain multiple TextViews. I read that I should have it extend android.support.v7.widget.AppCompatButton rather than android.widget.Button
Here is the code for the custom button class:
public class CustomButton extends AppCompatButton {
public CustomButton(Context context) {
this(context, null);
}
public CustomButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
This works as a simple button, but as soon as I add a textview in the activity xml I get the following error, where the error line is the line of the text view
java.lang.RuntimeException: Unable to start activity ComponentInfo{me.app/me.app.MainActivity}: android.view.InflateException: Binary XML file line #21: me.app.CustomButton cannot be cast to android.view.ViewGroup
activity_main.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"
tools:context=".MainActivity">
<me.app.CustomButton
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="100dp"
android:text="Button"
android:theme="#style/Widget.AppCompat.Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/increment_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="00:00:00"
android:textColor="#android:color/black"
android:textSize="22sp" />
</me.app.CustomButton>
</android.support.constraint.ConstraintLayout>
Do I need to write the activity.xml file in a different way if I extend AppCompatButton instead of Button ?

You can use a CardView for the same effect...
<android.support.v7.widget.CardView
android:id="#+id/card0"
android:focusable="true"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:foreground="?android:selectableItemBackground"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="false">
<LinearLayout
android:id="#+id/ll1"
android:padding="10.0dip"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="How to Refresh!?"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif"
android:gravity="center"/>
<TextView
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="How to Refresh!?"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif"
android:gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>
This way you can add as many TextView you want!

Related

Android (Java): Programatically adding a custom TableRow View with cells of equal width does not result in cells of equal width

In my Android application, I am attempting to programmatically add a custom TableRow view (CustomTableRow) to a TableLayout. CustomTableRow has cells of equal length (the first two cells have text, and the third one has a edit and delete button, as shown in the diagram below):
However, when I programmatically add a CustomTableRow to my TableLayout, this is the result I get (the cells are all jumbled together instead of occupying equal width):
This seems strange, considering that I have specified that all cells are to have weight 1 in the XML layout that CustomTableRow inflates. Furthermore, I even programatically set the cells in CustomTableRow to have weight 1 as an extra measure. And yet, the cells are still jumbled together.
Below is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/housemate_data_table"
layout="#layout/table" />
</LinearLayout>
table.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rent_form"
android:layout_marginTop="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:elevation="4dp"
android:padding="8dp"
android:background="#ffffff"
android:orientation="vertical">
<TableLayout
android:id="#+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TableRow android:background="#d6d7d7" android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Area"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textColor="#000000" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
custom_table_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/col1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="#000000" />
<TextView
android:id="#+id/col2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Area"
android:textColor="#000000" />
<LinearLayout
android:id="#+id/col3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:text="E" />
<Button
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:text="D" />
</LinearLayout>
</TableRow>
MainActivity.java:
package com.example.programaticallyaddtablerowtotablelayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve of the table layout.
TableLayout table = (TableLayout) findViewById(R.id.table_layout);
// Create the housemate's custom TableRow.
CustomTableRow customTableRow = new CustomTableRow(getApplicationContext(), "Name", "Area");
customTableRow.findViewById(R.id.col1).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.findViewById(R.id.col2).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.findViewById(R.id.col3).setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
customTableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
table.addView(customTableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
CustomTableRow.java:
package com.example.programaticallyaddtablerowtotablelayout;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TableRow;
import androidx.annotation.Nullable;
public class CustomTableRow extends TableRow {
public CustomTableRow(Context context) {
super(context);
initUI();
}
public CustomTableRow(Context context, String name, String areaText) {
super(context);
initUI();
}
public CustomTableRow(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
initUI();
}
// Initiate the custom TableRow's UI by inflating the template XML layout.
private void initUI() {
inflate(getContext(), R.layout.custom_table_row, this);
}
}
In custom_table_row.xml, changing TableRow to the merge tag resolved the problem.

Why is my layout not filling the screen in landscape mode?

I have the following layout for a video player:
video_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout
android:id="#+id/aspectRatioLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/posterImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/poster_image_description" />
<com.google.android.exoplayer2.ui.SubtitleView
android:id="#+id/subtitleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.exoplayer2.ui.PlayerControlView
android:id="#+id/nativeControls"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.exoplayer2.ui.PlayerControlView>
<com.sdk.adapters.controls.ControlBar
android:id="#+id/controls"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.sdk.adapters.controls.ControlBar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/messageOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#80000000">
<TextView
android:id="#+id/messageOverlayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/message_overlay"
android:textColor="#android:color/white"
android:textSize="30sp" />
</FrameLayout>
</FrameLayout>
</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
</FrameLayout>
The ControlBar is defined as:
control_bar_view.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"
android:background="#80000000">
<!-- BEGIN: Strut -->
<View
android:id="#+id/exitPlaceholder"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.1" />
<!-- END: Strut -->
<TextView
android:id="#+id/broadcastName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="#string/broadcast_name"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/castButton"
app:layout_constraintStart_toEndOf="#id/exitPlaceholder"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/siteName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="#string/site_name"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/castButton"
app:layout_constraintStart_toEndOf="#id/exitPlaceholder"
app:layout_constraintTop_toBottomOf="#id/broadcastName" />
<ImageButton
android:id="#+id/castButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:contentDescription="#string/send_to_google_chromecast"
android:visibility="invisible"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/airplayButton"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/airplayButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:contentDescription="#string/send_to_apple_airplay"
android:visibility="invisible"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/optionsButton"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/optionsButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:contentDescription="#string/fullscreen"
android:scaleType="fitStart"
android:src="#drawable/closed_caption_gray"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/skipBackButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:contentDescription="#string/skip_back_button"
android:scaleType="fitStart"
android:src="#drawable/back_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/playPauseToggle"
app:layout_constraintHeight_percent="0.20"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playPauseToggle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:contentDescription="#string/play_button"
android:scaleType="fitStart"
android:src="#drawable/play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/skipForwardButton"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintStart_toEndOf="#id/skipBackButton"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/skipForwardButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:contentDescription="#string/skip_forward_button"
android:scaleType="fitStart"
android:src="#drawable/forward_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.20"
app:layout_constraintStart_toEndOf="#id/playPauseToggle"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/currentTime"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="10dp"
android:text="#string/null_time"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#id/seekBar"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/remainingTime"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="10dp"
android:text="#string/null_time"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#id/seekBar"
app:layout_constraintEnd_toEndOf="parent" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="0dp"
android:layout_height="0dp"
android:progress="50"
android:progressDrawable="#drawable/seek_bar"
android:thumb="#drawable/seek_thumb"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/fullscreenButton"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/fullscreenButton"
app:layout_constraintVertical_bias="0.0" />
<ImageButton
android:id="#+id/fullscreenButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:contentDescription="#string/fullscreen"
android:scaleType="fitStart"
android:src="#drawable/fullscreen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1" />
</androidx.constraintlayout.widget.ConstraintLayout>
In the layout editor in Android Studio, this looks good:
However when I run an actual app, the ControlBar (and only the ControlBar) isn't filling the screen! Everything else stretches to fill the screen perfectly:
What am I doing wrong?
If it helps, the ControlBar is a custom View which extends ConstraintLayout. Here's what the initialization code looks like:
ControlBar.java
public class ControlBar extends ConstraintLayout
{
private BFPlayer player;
private TextView broadcastName;
// ... references for the various subviews ...
private ImageButton fullscreenButton;
public ControlBar ( Context context )
{
super(context);
init(context);
}
public ControlBar ( Context context, AttributeSet attrs )
{
super(context, attrs);
init(context);
}
public ControlBar ( Context context, AttributeSet attrs, int defStyleAttr )
{
super(context, attrs, defStyleAttr);
init(context);
}
private void init ( Context context )
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.bf_control_bar_view, null);
addView(view);
broadcastName = findViewById(R.id.broadcastName);
// ... grab references to the various buttons ...
fullscreenButton = findViewById(R.id.fullscreenButton);
// ... set up listeners for the various buttons ...
}
// ... The rest of the class ...
}
What am I doing wrong? I'm sure it's something simple, but I've been staring for a minute and can't figure it out. (Note: I'm pretty new to Android development, and not very experienced. My job has me changing platforms and programming languages very often)
You're not telling it what the parent is when you inflate your View, so it doesn't apply the LayoutParams correctly:
View view = inflater.inflate(R.layout.bf_control_bar_view, null);
addView(view);
Instead, tell it to inflate directly into the parent:
View view = inflater.inflate(R.layout.bf_control_bar_view, this);

How to manage width of a custom view with layout using ConstraintLayout?

I have a fragment and I'm using an accordion view in it, which I downloaded form GitHub - https://github.com/riyagayasen/Android_accordion_view, and in its body (which is a RelativeLayout) I need a custom view named AccordionItem like the second one on the pic, which I simply made in fragment's xml:
https://pp.userapi.com/c846123/v846123865/1d925d/CaEhr8uSLFA.jpg
But my custom view (the first one) has a wrap_content width behavior for some reason.
The code follows:
Here is the body of an AccordionView in my fragment's xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This is my custom view -->
<!-- Notice that the width is set to match_constraint -->
<com.app.myapp.views.AccordionItem
android:id="#+id/item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:isStarred="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Another title" />
<!-- This is an example of how my view should look like -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/accordion_item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item">
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:button="#drawable/star_checkbox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_t" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
The xml of the custom view:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/accordion_item">
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:button="#drawable/star_checkbox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/title"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_t" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/item_icon"
app:layout_constraintStart_toEndOf="#+id/item_icon"
app:layout_constraintTop_toTopOf="#+id/item_icon" />
</android.support.constraint.ConstraintLayout>
Java class of the custom view:
public class AccordionItem extends ConstraintLayout {
private String titleString;
private TextView title;
private boolean isStarred;
public AccordionItem(Context context) {
super(context);
initView();
}
public AccordionItem(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.AccordionItem,
0, 0);
try {
titleString = a.getString(R.styleable.AccordionItem_title);
isStarred = a.getBoolean(R.styleable.AccordionItem_isStarred, false);
} finally {
a.recycle();
}
initView();
}
private void initView() {
View view = inflate(getContext(), R.layout.accordion_item, null);
addView(view);
}
public void setTitleString(String titleString) {
this.titleString = titleString;
invalidate();
requestLayout();
}
public String getTitleString() {
return titleString;
}
public void setStarred(boolean starred) {
this.isStarred = starred;
invalidate();
requestLayout();
}
public boolean isStarred() {
return isStarred;
}
}
And finally the attributes of my view:
<declare-styleable name="AccordionItem">
<attr name="title" format="string"/>
<attr name="isStarred" format="boolean"/>
<attr name="background_drawable" format="reference"/>
</declare-styleable>
I expect my view to take the whole width it is given, but it shrinks instead.
Am I using the wrong way of creating views, or the wrong layout that my view extends?
Thank you.
inflate method with no parent will lost layoutParams(height,width)
private void initView() {
inflate(getContext(), R.layout.accordion_item, this);
// or
// View view = LayoutInflater.from(getContext()).inflate(R.layout.accordion_item, this, false);
// addView(view);
}
You don't really need library for this simple layout, have a look at this example :
<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=".Fragments.CheckoutScreen">
<!-- This is an example of how my view should look like -->
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView4" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="T"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/textView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView4" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.383" />

TextInputEditText don't work on Android 5.1

I'm hoping you would help me to get to the problem I'm facing right now.
I created a custom search view using a TextInputEditText, it's really simple actually and works fine on all the Android version with the exception of 5.1(Lollipop), the EditText never hide the hint when write or even show the cursor, also the text is not visible.
I t I have tried everything I know, but I can't figure what could it be.
Thank you so much for your time.
This is my custom class view :
public class CustomSearchView extends RelativeLayout {
#BindView(R.id.csvIcon) ImageView csvIcon;
public #BindView(R.id.csvEditText) TextInputEditText editText;
public CustomSearchView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_search_view, this, true);
ButterKnife.bind(this);
}
public CustomSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_search_view, this, true);
ButterKnife.bind(this);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.CustomSearchView,
0, 0);
String hintText = typedArray.getString(R.styleable.CustomSearchView_hintText);
try {
int max = typedArray.getInteger(R.styleable.CustomSearchView_csv_length, 17);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
editText.setInputType(typedArray.getInt(R.styleable.CustomSearchView_android_inputType, InputType.TYPE_CLASS_TEXT));
editText.setHint(hintText);
csvIcon.setImageDrawable(typedArray.getDrawable(R.styleable.CustomSearchView_csv_icon));
} catch (Exception e) {
e.printStackTrace();
}
}
public CustomSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
This are the attribute specification :
<declare-styleable name="CustomSearchView">
<attr name="csv_icon" format="reference"/>
<attr name="hintText" format="string"/>
<attr name="csv_length" format="integer"/>
<attr name="android:inputType"/>
<attr name="android:digits"/>
</declare-styleable>
This is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/charcoal_grey_two">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/csvIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:src="#drawable/icon_search"/>
<android.support.design.widget.TextInputEditText
android:id="#+id/csvEditText"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:layout_marginLeft="10dp"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete"
android:textSize="20dp"
android:textColorHint="#color/slate_grey"
android:textColor="#color/slate_grey"
android:background="#android:color/transparent"/>
</LinearLayout>
</RelativeLayout>
And this is my implementation in a fragment :
<?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:clickable="true"
android:background="#color/gunmetal">
<mx.com.segurosbancomer.gpsajustadores.view.custom.TopBarLayout
android:id="#+id/topBar"
android:layout_width="match_parent"
android:layout_height="45dp"
app:tb_color="#color/gunmetal"
app:tb_text="#string/bf_title"
app:tb_text_color="#color/white"
app:tb_btnleft_visibility="0"
app:tb_btnright_visibility="8"
app:tb_btnleft="#drawable/icon_prev_white"
app:tb_btnright="#drawable/icon_dot_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
android:id="#+id/tituloPoliza"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/bf_search"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_margin="10dp"
app:ctv_customFont="stagsansbook.otf" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchPoliza"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="30"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_poliza" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchInciso"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="30"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_inciso" />
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/charcoal_grey_two"/>
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
android:id="#+id/tituloSerie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/bf_search_serie"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_margin="10dp"
app:ctv_customFont="stagsansbook.otf" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchSerie"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="17"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_serie"/>
</LinearLayout>
</LinearLayout>
Finally I find the solution. If some else have a similar problem just like me, the issue was resolved by removing this line from the AndroidManifest.xml
android:hardwareAccelerated="false"
This line allow to the fragment and all the components to work perfectly.
You should use TextInputEditText with TextInputLayout wrapped. Just use EditText instead.
See this: https://stackoverflow.com/a/37834022/5558150

How do I show a fragment from an Android custom component?

I've got a custom component declared using merge tags that is used in several locations in my application. Is it possible to call getFragmentManager() or getSupportFragmentManager() from this class? This class extends the LinearLayout instead of an activity. What I'm trying to do is show a new fragment from this custom component, but neither of these functions can be accessed. How do I show the new fragment from this custom component?
Here are the basics of my code:
public class SpecialTextBox extends LinearLayout implements View.OnClickListener {
//......constructors
public void initialize(final Context context, AttributeSet attrs) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.special_box, this);
}
#Override
public void onClick(View view) {
super.callOnClick();
switch (view.getId()) {
case R.id.searchButton:
SearchFragment searchFragment = new SearchFragment(new DialogFragmentDismissHandler(), specialBox.getEditText().getText().toString());
searchFragment.show(dashboardActivity.getFragmentManager(), SearchFragment.class.toString());
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customAttribute="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_gravity="left">
<com.mobile.layout.SpecialEditText
android:id="#+id/specialBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
style="#style/value"
android:layout_gravity="left"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="3dp"
android:layout_gravity="bottom">
<ImageButton
android:id="#+id/searchButton"
style="#style/searchImageButton"
android:clickable="true"
android:longClickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/myButton"
style="#style/reportButton"
android:clickable="true"
android:longClickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_gravity="left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nameTextView"
android:text=""
android:textColor="#color/black"
android:layout_marginLeft="1dp"
style="#style/value"
android:layout_gravity="bottom|left" />
</LinearLayout>
</LinearLayout>
</merge>
You can call getFragmentManager() or getSupportFragmentManager() only from FragmentActivity. If your custom view is inside FragmentActivity, you can this
FragmentActivity a = (FragmentActivity) getContext();
a.getFragmentManager()
I was able to access getFragmentManager() by the following:
Activity a = (Activity)(((ContextWrapper) getContext()).getBaseContext());
a.getFragmentManager();

Categories

Resources