Autosize text in homescreen widget with support library - java

I am not able to use support library 26 app:autoSizeTextType attribute on textview it forces me to use AppCompatTextView class.Regular TextView doesn't recognize app prefix like official docs. When i use AppCompatTextView on widget it brings rendering issuses.Is there a way to achieve this ?

I believe this is an issue with Android's linter. Try using the tools:ignore="MissingPrefix" attribute.
<TextView
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="350dp"
android:layout_height="48dp"
android:maxLines="1"
app:autoSizeMaxTextSize="13sp"
app:autoSizeMinTextSize="8sp"
app:autoSizeTextType="uniform"
tools:ignore="MissingPrefix"/>
EDIT: Android has an official response into using AppCompat Views in a RemoteViews way (hint: don') https://issuetracker.google.com/issues/37071559

Related

Get Ids Dynamically using findViewById() in Android using Java

In my XML layout I have some TextView with ids like slot0, slot1...slot15.
Is there any way to generate the corresponding Id dynamically in java like the following?
findViewById(R.id.*customStringForId*)
then access each of TextView using a for loop?
I am currently unable to use findViewById(R.id.*customStringForId*) because I can't find it in the XML.
Thats a bad practice for access component from your xml
You need set manual for id with findViewById for tell java class if in your xml there existing textview with id which already you set and give you access for do whatever like implement onclick event, settext, etc.
If you cant find your id, you need check if setContentView in your java point to your xml.
there are some ways to solve your problem but you should write your layout in the question to let us know how you design the layout.
But for example if you have a list of TextViews inside layout like the following:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/slot0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
</LinearLayout>
you can access the TextView Dynamically via the layout like the following:
public TextView getTextView(int index){
return ((LinearLayout) findViewById(R.id.layout)).getChildAt(index)
}

Anchored adaptive banners in LinearLayout structure

Good afternoon!
I am updating the normal(smart) admob banners for admob Anchored adaptive banners, I am following the example of google but I have several doubts.
I use LinearLayout as a general structure and not RelativeLayout like the google example.
When using the code to display the banner, it underlines two lines.
Example new code banner admob:
<FrameLayout
android:id="#+id/ad_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true" />
Yellow underline in android studios (in my code):
android:layout_centerInParent="true"
Message or warning:
Invalid layout param in a LinearLayout: layout_centerInParent
android:layout_alignParentBottom="true"
Message or warning:
Invalid layout param in a LinearLayout: layout_alignParentBottom
Example complet code banner google-admob:
https://github.com/googleads/googleads-mobile-android-examples/blob/master/java/admob/AdaptiveBannerExample/app/src/main/res/layout/activity_my.xml
In the application the test ads are displayed well and work. But I don't like the messages of the underlined code (to avoid problems)
Before I used this code for my SMART banners:
<LinearLayout
android:id="#+id/ad_view_container"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
Could I use the same code for Anchored adaptive banners? or does it have to be FrameLayout..?
thank you for answers
Try This
android:layout_gravity="center_horizontal|bottom"

How to fix this > attribute app:constraintBottom_toBottomOf not found

Android newbie here.
I am building and AR app and now I'm planning to add a User Interface. I am trying to add a constraint layout to app:layout_constraintBottom_toBottomOf="parent", however Im getting the error that this constraint was not found
These are my codes and error messages:
All the lines of code I add in my activity_ux.xml file that begin with "app"
e.g. app:layout_constraintBottom_toTopOf do not seem to work, I just showed you an example here with one of them just to make it easier to debug I think.
I tried searching online, one of the solutions was that I should install "ConstraintLayout for Android" and "Solver for ConstraintLayout" in my SDK Tools, but I already had this installed.
<FrameLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.google.ar.sceneform.Overlei.Overlei">
<fragment android:name="com.google.ar.sceneform.ux.ArFragment"
android:id="#+id/ux_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/gallery_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
>
</LinearLayout>
</FrameLayout>
I expected this to just work, I don't know where I can start looking if an in-built function(such as this) does not work.
Thanks for the suggestions guys. I have managed to deal with the error by adding this line
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
into my build.gradle(module.app) under dependencies
Again, thanks for everything
You should use app:layout_constraintBottom_toBottomOf only in ConstraintLayout. You can try to use inside of FrameLayout
then android:gravity = "bottom" in your LinearLayout.
Wrap you FrameLayout in a ConstraintLayout or change the FrameLayout to ConstraintLayout

Android - force searchview to open programmatically

In my application am using the library for the autoComplete searchview
Here is what the library am using
compile 'com.miguelcatalan:materialsearchview:1.4.0'
Here is my xml
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/theme_primary" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
What I want is I want to open the searchView programmatically to open searchbar without using pressing the search icon in actionbar.
How can i achieve this.any help.
you can use inbuilt functionality of autocompletetextview which this library must be extending
search_view.setFocusedByDefault(true);
I observed the library and MaterialSearchView class i see there one method it is showSearch(). may be it is for show SearchView.
method is in MaterialSearchView.class
public void showSearch(boolean animate) {
if (isSearchOpen()) {
return;
}
So you can call this method like this :
materialSearchView.showSearch(true/false);
May be work for your requirement. Hope this help you.

Background drawable is not displaying in real device

Please have a look at the following XML
<RelativeLayout 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"
tools:context=".MainActivity"
android:background="#drawable/background_main" >
</RelativeLayout>
When I run this, all the tings are perfect and the background image of the main layout also get displayed in Emulator. But, when I run this code in Phone, the background image is missing! What is wrong?
I found the issue by my self. My app is multi screen supporting so I had to put those images (with different sizes) into all the related drawable folders.
<RelativeLayout 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"
tools:context=".MainActivity"
android:background="#drawable/background_main" >
xmlns:tools are most probably used for the activity the tools UI editor uses to render your layout (it will find the right theme based on the activity).
So if you can just remove tools
OR in .MainActivity java class. define your background source.
//outside your onCreate
RelativeLayout relativeLayout;
Context ctx;
//inside your onCreate
relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.background_main);
setContentView(relativeLayout);

Categories