I have a view. The view might become visible at some time in the future. When this view is visible I want to call a method. How to do this?
val editText = findViewById<EditText>(R.id.editText)
// editText might become invisible in some time in future
// and in some in future it might visible
if(editText.isVisible(){
// code to be executed
}
Code for View.isVisible() :
fun View.isVisible() = this.visibility == View.VISIBLE // check if view is visible
Is there anything like View.setOnClickListener which could be applied and triggered when the view is visible-
editText.setOnClickListener { view ->
}
click listener is callback when the view is being clicked. it has no concern with its visibility. There is no method like isVisible(). to check Visibility
if(yourView.getVisiblity()==View.VISIBLE){ //your task}
for kotlin:
if(youView.visibility==View.VISIBLE){//your task}
I might initialize a variable int status of visibility and set it to 0 with the view invisible.
Now I would create a function instead directly setting the visibility of the view.
For example a function named onVisibilityChanged();
In that function add the set visibility code followed by setting the int to 0 or 1 as per the visibility an if-else block.
If you just set the view to visible, set the int to 1.
The reason for adding if-else block is to configure your actions based on the visibility status.
So that gives you the freedom to do whatever you want bases on the visibility.
Make sure you add this code in such a way that it is executed anytime you want.
Use the performClick() function to click any button or any other view.
I hope you understand. Or comment any query. I would have posted the code for the same but it looks like you are using Kotlin. So I'll try to post it in Kotlin if possible.
The main intention of doing such a thing is when the value of int changes, the app knows what to do and also knows that visibility has changed.
Just call the function wherever you want. It will be easy.
So this is what I am trying to do:
int visibilityStatus;
textview = findViewById(R.id.textview);
getInitialVisibility();
funcOnVisibilityChange();
}
private void getCurrentVisibility() {
if (textview.getVisibility() == View.VISIBLE) {
visibilityStatus = 1;
} else {
visibilityStatus = 0;
}
}
private void funcOnVisibilityChange() {
//Now change the visibility in thia function
textview.setVisibility(View.VISIBLE);
int currentVisibilityStatus;
if (textview.getVisibility() == View.VISIBLE) {
currentVisibilityStatus = 1;
} else {
currentVisibilityStatus = 0;
}
if (visibilityStatus != currentVisibilityStatus) {
//Visibility status has changed. Do your task
else {
//visibility status not changed
}
}
}
So all that we are doing is getting visibility of the view when the app is started and when you somehow change its visibility. Here in the example, I've directly changed the visibility. So wherever you know that visibility is going to change just put the funcOnVisibilityChange() and it will do your job... hope it helps. Let me know if you need more clarification.
Related
In particular class, from many places call back is coming, i just want to whether it's coming text or button for example, so that I can set the data accordingly.
NOTE: I'm not talking about parent layout, I want to know exact name where the event click is happened!
If I'm doing this: Log.d("Hello", "Clicked finally: "+ view?.id)
This is coming:
D: Clicked finally: 2131296625
If you are using Kotlin, You can simply check if the View is Button or Image using is operator like:
when(view) {
is Button -> {
// a Button is clicked.
}
is AppCompatImageView -> {
// an Image is clicked.
}
else -> {
// any other view is clicked.
}
}
You can do something like this:
if (view.javaClass.simpleName == Button::class.java.simpleName) {
// it is a button
} else if (view.javaClass.simpleName == TextView::class.java.simpleName) {
// it is a text view
}
Please note that this won't work if you are using any subclass of Button or TextView. You will need to explicitly specify the class you want to check for.
In my app, I need to show and hide widgets like button and textview at a certain time.
and how I am doing is as the following:
private void hideviews() {
image.setVisibility(View.GONE); ///ImageView
title1.setVisibility(View.GONE);///TextView
title2.setVisibility(View.GONE);///TextView
title3.setVisibility(View.GONE);///TextView
title4.setVisibility(View.GONE);///TextView
title5.setVisibility(View.GONE);///TextView
}
private void showviews() {
image.setVisibility(View.VISIBLE);
title1.setVisibility(View.VISIBLE);///TextView
title2.setVisibility(View.VISIBLE);///TextView
title3.setVisibility(View.VISIBLE);///TextView
title4.setVisibility(View.VISIBLE);///TextView
title5.setVisibility(View.VISIBLE);///TextView
}
I don't think this is the correct way to do this.
Because I don't know how many widgets there will be.
Any guidance on how to correctly show widgets is really appreciated.
Get the reference to root layout, iterate through the childs, check if the view at certain index is instance of EditText(or View that you dont need to hide), if not hide it
RelativeLayout root = findViewById(R.id.root)
for(i=0,i<root.getChildCount()-1,i++){
if(! (root.getChildAt(i) instance of EditText)){
root.getChildAt(i).setVisibility(View.GONE)
}
}
Since you don't know how many testviews will be attached, then I believe that the best approach will be to:
get the reference of the parent view group (that contains all the
textviews),
loop through all the childs using getChildAt,
verify whether the object is an instance of TextView/ImageView and if so set its visibility according to your logic
Instead of hiding every widget separately hide the root layout.
RelativeLayout rootLayout;
rootLayout= (RelativeLayout) findViewById(R.id.root_layout);
and use something like this to control the visibility.
public void setLayoutInvisible() {
if (rootLayout.getVisibility() == View.VISIBLE) {
rootLayout.setVisibility(View.GONE);
}
}
public void setLayoutVisible() {
if (rootLayout.getVisibility() == View.GONE) {
rootLayout.setVisibility(View.VISIBLE);
}
}
Make an array of all the views that you want to show/hide:
View[] views = {image, title1, title2, title3, title4, title5};
and then use this to hide them:
for (View view : views) {
view.setVisibility(View.GONE);
}
and use this to show them:
for (View view : views) {
view.setVisibility(View.VISIBLE);
}
although you can combine the 2 code parts in a single procedure:
void fixViews(int state) {
for (View view : views) {
view.setVisibility(state);
}
}
and call it:
fixViews(View.GONE); or fixViews(View.VISIBLE);
The way app works is the following: App prompts 30 buttons to user and user may guess the right ones by tapping. When user taps some button all the buttons (say a view containing these buttons) should be locked while corresponding (right or wrong guess) animation is playing. Tapped button by itself should be disabled till the next round. After animation is finished all not tapped previously buttons (say a view containing these buttons) should be available again.
So I have a Layout which includes another layout with these 30 buttons:
...
<RelativeLayout
android:id="#+id/alphabetContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="#layout/alphabet" />
</RelativeLayout>
...
Now I need to lock the buttons from being clicked and then unlock. So I tried:
...
private RelativeLayout alphabetPanel;
...
public void onCreate(){
...
alphabetPanel = (RelativeLayout) findViewById(R.id.alphabetContainer);
...
}
...
private void lockButtons(){
alphabetPanel.setEnabled(false);
}
but this doesn't lock buttons. I also tried:
alphabetPanel.setFocusable(false);
alphabetPanel.setClickable(false);
Doesn't help either. Seems like it all relies only to a layout by itself but not the views it contains.
Also I tried to add a fake layout to place it over layout with buttons by bringing it to the front. This is a workaround and its tricky cuz both layouts must be placed inside a RelativeLayout only:
...
blockingLayout = new RelativeLayout(this);
blockingLayout.setLayoutParams(alphabetPanel.getLayoutParams());
...
but this works very strange: somehow both layouts in this case appears and disappears every second or so or doesn't appear at all - I cant understand that at all cuz there is no setVisibility() method used in code!
The only one way left is to iterate every view (button) to make it disabled and than back.
Is there any other way?
UPDATE
Finally I had to add a "wall"-layout into the xml. Now by making it clickable and focusable it becomes a solution.
Try setting for each Button's xml definition
android:duplicateParentState="true"
I'm not sure, but I think it should make them not only to seem disabled, but also to act accordingly.
Hmm it surprises me that disabling the parent-layout doesn't work.. as far as i know it should.
Try fetching your included layout instead, and disable that.
Anyway, if all else fails you can always loop through the buttons themselves.
for(int i=0;i<relativeLayout.getChildCount();i++){
View child=relativeLayout.getChildAt(i);
//your processing....
child.setEnabled(false);
}
I used extension to lock and unlock the view
//lock
fun View.lock() {
isEnabled = false
isClickable = false}
//unlock
fun View.unlock() {
isEnabled = true
isClickable = true}
if you want to lock all children of the view group
//lock children of the view group
fun ViewGroup.lockAllChildren() {
views().forEach { it.lock() }}
//unlock children of the view group
fun ViewGroup.unlockAllChildren() {
views().forEach { it.unlock() }}
firstly define your button
Button bit = (Button)findViewById(R.id.but);
bit.setEnabled(false);
and set enabled false;
Java:-
public void disableButtons(Layout layout) {
// Get all touchable views
ArrayList<View> layoutButtons = layout.getTouchables();
// loop through them, if they are instances of Button, disable them.
for(View v : layoutButtons){
if( v instanceof Button ) {
((Button)v).setEnabled(false);
}
}
}
Kotlin:-
fun disableButtons(layout: Layout) {
// Get all touchable views
val layoutButtons: ArrayList<View> = layout.getTouchables()
// loop through them, if they are instances of Button, disable them.
for (v in layoutButtons) {
if (v is Button) {
(v as Button).setEnabled(false)
}
}
}
Retrieve all touchables views into an ArrayList, then loop through them and check if it is an instance of the Button or TextView or which ever you want, then disable it!
In case data binding is needed
import android.view.ViewGroup
import android.widget.Button
import androidx.core.view.children
import androidx.databinding.BindingAdapter
#BindingAdapter("disableButtons")
fun ViewGroup.setDisableButtons(disableButtons: Boolean) {
children.forEach {
(it as? Button)?.isEnabled = !disableButtons
}
}
Usage:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/guideline"
app:disableButtons="#{vm.busy}">
....
</androidx.constraintlayout.widget.ConstraintLayout>
Might work in constraint layout . Use group widget and add all the button ids.
In the java code set enabled false for the group.
For disable all buttons in any nested layouts.
void DisableAllButtons( ViewGroup viewGroup ){
for( int i = 0; i < viewGroup.getChildCount(); i++ ){
if( viewGroup.getChildAt(i) instanceof ViewGroup ){
DisableAllButtons( (ViewGroup) viewGroup.getChildAt(i) );
}else if( viewGroup.getChildAt(i) instanceof Button ){
viewGroup.getChildAt(i).setEnabled( false );
}
}
}
write these two lines on your button declartion in XML
android:setEnabled="false"
android:clickable="false"
Ok, I have a custom AdapterView. Whenever I detect a long click, I call a method to change a custom editable status.
public void setEditing(boolean editing) {
this.editing = editing;
//Set editing to children
for (int i=0; i < getChildCount(); i++){
((PresentationPickerGalleryCellView)getChildAt(i)).setEditing(editing);
if (editing == true)
getChildAt(i).setVisibility(View.INVISIBLE);
//((PresentationPickerGalleryCellView)getChildAt(i)).deleteImageButton.setVisibility(View.VISIBLE);
}
}
It's executed in the main thread as far as I am concerned.
Now, if I call:
getChildAt(i).setVisibility(View.INVISIBLE);
It hides the whole view correctly when editing==true. But if I call:
((PresentationPickerGalleryCellView)getChildAt(i)).deleteImageButton.setVisibility(View.VISIBLE);
The deleteImageButton is a button inside the cell. It won't show the deleteImageButton at all. I tried invalidate, postInvalidate, layout, requestLayout, refreshDrawableState, but nothing...
Any ideas?
Ok...
It seems if I set View.GONE at the beginning, and then I try to set View.VISIBLE, it won't show the button...
I have to work only with View.INVISIBLE and View.VISIBLE :/
I have a View object on my Activity and I'd like to change the background resource of the view. More specifically, I'd like to toggle it.
So I'll need some logic like this:
if (myView.getBackgroundResource() == R.drawable.first) {
myView.setBackgroundResource(R.drawable.second);
}
else {
myView.setBackgroundResource(R.drawable.first);
}
The issue here being that there is no getBackgroundResource() method.
How can I obtain the resource a View is using for its background?
I don't think the View remembers what resource it is using after it gets the Drawable from the resource.
Why not use an instance variable in your Activity, or subclass the View and add an instance variable to it?
Wouldn't it be easier to just have a control variable that maintains the state? Lets you be flexible and allows you any number of drawables.
int[] backgrounds = {
R.drawable.first,
R.drawable.second,
R.drawable.third
};
int currentBg;
void switch() {
currentBg++;
currentBg %= backgrounds.length;
myView.setBackgroundResource(backgrounds[currentBg]);
}
You could use a flag to keep track of which was last set
private static final int FIRST_BG = 0;
private static final int SECOND_BG = 1;
private int mCurrentBg;
...
if (mCurrentBg == FIRST_BG) {
myView.setBackgroundResource(R.drawable.second);
mCurrentBg = SECOND_BG;
}
else {
myView.setBackgroundResource(R.drawable.first);
mCurrentBg = FIRST_BG;
}
You would have to initialize mCurrentBg wherever the background is initially set though.
You can get the ID of a resource via the getResources().getIdentifier("filename", "drawable", "com.example.android.project"); function. As you can see you will need the filename, the type of resource (drawable, layout or whatever) and the package it is in.
EDIT: Updated my logic fail.
I think you might be able to put the setTag() and getTag() methods to use here:
//set the background and tag initially
View v = (View)findViewById(R.id.view);
v.setBackgroundResource(R.drawable.first);
v.setTag(R.drawable.first);
if(v.getTag().equals(R.drawable.first)) {
v.setBackgroundResource(R.drawable.second);
v.setTag(R.drawable.second);
} else {
v.setBackgroundResource(R.drawable.first);
v.setTag(R.drawable.first);
}
I have not tested this, but I think it should work, in theory. The downside is that you add a little overhead by having to manually tag it the first time, but after the initial tagging, you shouldn't have to worry about keeping track of flags.