Android - force searchview to open programmatically - java

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.

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)
}

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

layout element defined globally

I'm trying to add a "You are offline" element to the bottom of each layout in my Android app. I would like to define it globally, not to paste the same element to each xml layout file.
I could probably create some ParentActivity and append it programmatically, but is it a good solution?
What is the best way?
Thanks.
Use custom BottomSheetDialog. You do not have to inclue it in layout file. Instead, you will call it programmatically.
BottomMessageDialog:
public class BottomMessageDialog extends BottomSheetDialog {
public BottomMessageDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.dialog_bottom_message);
}
}
dialog_bottom_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="You are offline"
android:textSize="18sp"
android:gravity="center"/>
</LinearLayout>
Call it:
BottomMessageDialog bottomMessageDialog = new BottomMessageDialog(MainActivity.this);
bottomMessageDialog.show();
Hope it will help.
best way is creating a custom xml file with any name you want and you can use it any number of times you want without any copy and paste.
step 1: creating custom layout named footer_message .
<TextView
android:layout_width="match_content"
android:layout_height="wrap_content"
android:text="You are Offline"/>
step 2: adding that custom layout in another xml which you want that msg.
<include
android:id="#+id/footer_message"
layout="#layout/footer_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
create an xml file
footer.xml
make your layout in it and write this code in the xml files where you want that footer
<include
android:layout_height="wrap_content"
android:layout_width="fill_parent"
layout="#layout/footer"
android:id="#+id/footer"/>

Autosize text in homescreen widget with support library

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

Android Watch fullscreen FAB like the DismissOverlay

There is the DismissOverlayView, which you can implement to give the user an alternative way to exit the application on an android watch. This View implements something, that looks like a fullscreen FAB. Now I would like to know, how I could implement the same View with another button icon/color/behaviour. Since you can't change the DismissOverlayView which code looks btw. like this :
<android.support.wearable.view.DismissOverlayView
android:id="#+id/dismiss_overlay"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
I guess you have to implement some custom FAB, but I can't use the FAB in my watch XML either because there is an dependency missing or because it's simply not supported by the watch os. I tried following code for testing :
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"/>
Edit: For better understanding, I want the button look like this :
Edit: I just found this solution, I was not aware of that one :
https://developer.android.com/training/wearables/ui/confirm.html
But I would still be curious, if you could implement a more customisable version of this buttons.
Okay I found a solution for that problem, actually you can simply create a new layout.xml and overwrite the default code, which is the following one :
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/dismiss_overlay_explain"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="32dp"
android:layout_width="wrap_content"
android:padding="12dp"
style="#style/DismissOverlayText"
/>
<android.support.wearable.view.ActionPage
android:id="#+id/dismiss_overlay_button"
android:layout_height="match_parent"
android:background="#color/dismiss_overlay_bg"
android:layout_width="match_parent"
android:src="#drawable/ic_full_cancel"
android:color="#color/dismiss_close"
app:buttonRippleColor="#color/dismiss_close_pressed"
android:text="#string/dismiss_overlay_button_label"
/>
</merge>
Now you can use your newly created xml customise it and use this one instead of the original dimissOverlayView. But you should be careful with doing so, because this is not really intended from the design guidelines to do.

Categories