I try to display an own dialog in my android.support.v7.preference.PreferenceScreen - I created a layout file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.github.pinball83.maskededittext.MaskedEditText
android:id="#+id/licenseText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
app:mask="8 (***) *** **-**"
app:notMaskedSymbol="*"
app:maskIconColor="#color/colorPrimary"
/>
</android.support.constraint.ConstraintLayout>
and in my settings preference xml I did
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.codevscolor.materialpreference.widget.MaterialPreferenceCategory android:title="Allgemeines">
<com.sgi.smartcontrust.EditTextDialogPreference
android:key="key4"
android:title="Time Preference"
android:summary="Time Summary"
android:defaultValue="90"
/>
</com.codevscolor.materialpreference.widget.MaterialPreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
My appropriate class is
public class EditTextDialogPreference extends DialogPreference {
public EditTextDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.pref_dialog_license);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
setDialogIcon(null);
}
}
but I am getting the error
Tried to display dialog for unknown preference type.
Did you forget to override onDisplayPreferenceDialog()?
What am I doing wrong. Could anybody help me?
Related
What I am trying to achieve (which I do not know if it is even doable, since I'm new to android development) is to have:
A MainActivity class utilize/reference and XML layout (this i know is doable)
Within the MainActivity class also call a custom Layout programmatically (I am uncertain how to do this)
To elaborate on my question
I have the following XML layout which is compromised of two items:
A dropdown menu
A RelativeLayout view (*** this is where I would like to link to my custom class to)
Here is the XML code:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/weekDropdown"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:hint="#string/select_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:labelFor="#+id/weekSelected">
<AutoCompleteTextView
android:id="#+id/weekSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:layout_weight="1"
android:padding="14dp"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout <-- Here is where I would like to link my custom class
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
</LinearLayout>
In my custom class, I build my layout/view programmatically:
public class MainActivityLayout extends RelativeLayout {
Context context;
TableLayout tableA;
TableLayout tableB;
TableLayout tableC;
TableLayout tableD;
public MainActivityLayout(Context context) {
super(context);
this.initComponents();
this.setComponentsId();
etc...
}
}
Which I call it from my MainActivity class like so:
setContentView(new MainActivityLayout(this));
So is it possible to link the RelativeLayout in the XML to my custom class? If so can you let me know how to do so?
If you want to use your custom view, the easiest way is to include it in the xml. So this needs to be changed:
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
into this
<myapp.mypackage.MainActivityLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</myapp.mypackage.MainActivityLayout>
Change myapp.mypackage based on the real path of MainActitvityLayout.
Then just use it like any other usual layouts.
setContentView(R.layout.main_activity);
Also you must override these constructors:
public MainActivityLayout(Context context)
{
super(context);
}
public MainActivityLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MainActivityLayout(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
public MainActivityLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
}
However, if you insist on using it like this:
setContentView(new MainActivityLayout(this));
You can not use the main xml layout at all, but you can still inflate xml files onto your custom view. I won't write them here, you can easily search on this site on how implementing them.
I want to recyclerView on Bottom sheet can scroll but i cant . I try use OntouchListenner in RecyclerView but which can not scroll. Pls help me....
Main layout
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="#efefef"
tools:context=".MainActivity">
<include layout="#layout/conten_main" />
<include layout="#layout/layout_bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/air" />
</androidx.constraintlayout.widget.ConstraintLayout>
I want to know how to Scroll in RecyclerView
Try this custom view
package com.avatus.util
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.LinearLayout
class DisallowInterceptView : LinearLayout {
constructor(context: Context?) : super(context) {
requestDisallowInterceptTouchEvent(true)
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
requestDisallowInterceptTouchEvent(true)
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
requestDisallowInterceptTouchEvent(true)
}
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
parent.requestDisallowInterceptTouchEvent(true)
return super.dispatchTouchEvent(ev)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_MOVE -> requestDisallowInterceptTouchEvent(true)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> requestDisallowInterceptTouchEvent(
false
)
}
return super.onTouchEvent(event)
}
}
<com.avatus.util.DisallowInterceptView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:padding="#dimen/_16sdp"
android:background="#17141A"
android:layout_height="match_parent">
// Your view here
</androidx.constraintlayout.widget.ConstraintLayout>
</com.avatus.util.DisallowInterceptView>
I had the same requirement and solved it like below
Make BottomSheetDialog object attribute set cancelable false like below
viewIndentItemBottomSheetDialog.setCancelable(false)
This might be silly question but I didn't understand Design lib well. I am following this reference to create below layout. The Blue area should work as parallax when I scroll the GridView.
But when I scroll grid View nothing happens in AppBarLayout.
But This works with NestedScrollView and RecyclerView
Below is My Layout file-
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:background="#500403"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#122453"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<ImageView
android:id="#+id/backdrop1"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:fitsSystemWindows="true"
android:layout_gravity="center"
android:src="#drawable/bar_offline"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#982223"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<GridView
android:id="#+id/grid"
android:numColumns="4"
android:background="#367723"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/bar_offline"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
Any help would be appreciated.
With ListView/GridView, it works only on Lollipop by following code-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
}
I think for Now CoordinatorLayout works only with RecyclerView and NestedScrollView
EDIT :
use -
ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)
A simple solution was added to the support lib:
ViewCompat.setNestedScrollingEnabled(listView,true);
I've tested it with Android Support v4 Library 23.1 and it works well.
Currently the ListView and the GridView have the expected behavior with the CoordinatorLayout only with API>21.
To obtain this behavior you have to set:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setNestedScrollingEnabled(true);
}
It is not enough to implement the NestedScrollingChild.
The AbsListView isn't deployed with support libraries , then it depends by the SO running in the device.
You have to override some methods in the AbsListView. For example you can check the onInterceptTouchEvent method.
Inside this code you can see:
case MotionEvent.ACTION_DOWN: {
//......
startNestedScroll(SCROLL_AXIS_VERTICAL);
//....
}
case MotionEvent.ACTION_MOVE: {
//.....
if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, null)) {
//....
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
//..
stopNestedScroll();
break;
}
This code is only in the implementation of AbsListView v21+.
If you check the AbsListView with API 20 or lower, you will not find any nested scroll reference.
You have to put gridview inside NestedScrollview as usual then you have to add gridview height dynamically. then everything would work good.!!!
<android.support.v4.widget.NestedScrollView
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="match_parent"
android:layout_gravity="fill_vertical"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/camp_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar"
android:layout_margin="10dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:visibility="visible" >
</GridView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
This is working for me.
https://gist.github.com/sakurabird/6868765
I use GridView inside NestedScrollView
For convenience i've created a subclass with the new ViewCompat solution:
public class CoordinatedListView extends ListView {
public CoordinatedListView(Context context) {
super(context);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
ViewCompat.setNestedScrollingEnabled(this, true);
}
}
Enjoy :)
I customized some cardviews like this:
public class CustomCard extends CardView {
public CustomCard(Context context) {
this(context, null);
}
public CustomCard(Context context, AttributeSet attributeSet) {
this(context, attributeSet, 0);
}
public CustomCard(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
//R.layout.card_custom is the custom xml file
inflate(context, R.layout.card_custom, this);
}
}
Then I constructed and added them to ViewGroup like below:
CustomCard card = new CustomCard(this);
someLayout.addView(card);
The problem is I will see two layers of CardView border in the UI like below(it is apparent there was two layer of elevations at the border):
Anyone has an idea? Thanks
Edit:
One xml of the custom CardView:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp">
<!--- Some Details --->
</RelativeLayout>
</android.support.v7.widget.CardView>
Some Layout I mentioned above:
<ScrollView 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:background="#color/background_gray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--- Details --->
</LinearLayout>
</ScrollView>
Looks like you are inflating a CardView inside your custom card view, which is causing this issue.
To solve this, change your layout/custom_card.xml to have RelativeLayout as the parent instead of CardView
In my app I have a very basic looking compass which is rendered within my activity through a class. I am trying to display the compass with a layout. So rather than having just a circle with a line pointing north, I can include text box and buttons. How do I render this within a layout? Currently my activity sets the content view like so:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compassView = new CompassView(this);
setContentView(compassView);
I have tried setContentView(R.layout.activity_display_compass) which is my xml file however it only display "hello world" (the TextView), not the compass. See my xml file below.
xml
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"/>
<View
class = "com.example.gpsfinder.CompassView"
android:id="#+id/compassView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Any guidance would be greatly appreciated.
To use a custom View subclass in your xml layout file:
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"/>
<com.example.gpsfinder.CompassView
android:id="#+id/compassView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
And then you probably need to add some of these constructors in your Java code:
// you used this ctor when creating the view programmatically
public CompassView(Context context) {
this(context, null);
// add additional initialization here
}
// this constructor is needed for the class to be used in XML layout files!
public CompassView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
// add additional initialization here
}
// this constructor is needed for the class to be used in XML layout files,
// with a class-specific base style
public CompassView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
// add additional initialization here
}