Android button not clickable when turning visible - java

Hi Im posting this after not finding any possible answer here.
I have an invisible LinearLayout LL1 in a fragment that I turn visible when a recylerView data is empty, that LL1 has a button on it. The problem is simply that the button is not clickable ! I tried setting the listener in different ways but still not working. Here's the code below:
Here's the xml file:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.main.MainActivity"
android:layout_marginTop="3dp">
<LinearLayout
android:id="#+id/msg_emty_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_empty_box" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aucune vente n'a été trouvée !"
android:textSize="15dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/greydark"/>
<Button
android:id="#+id/btnAddSale"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Créer une vente"
android:textColor="#color/whiteTextColor"
android:background="#drawable/centre_button">
</Button>
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_db"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_sale_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_add_db"
android:layout_margin="20dp"
android:layout_gravity="bottom|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorAccesGreen"
android:src="#drawable/ic_edit"/>
</android.support.design.widget.CoordinatorLayout>
And here's the java:
private void generateSaleList(ArrayList<Sale> saleArrayList, View view) {
if(saleArrayList.isEmpty()){
getActivity().setTitle("Ventes sur portable");
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
}
});
creatSaleBtn.setClickable(true);
}else{
msgLayout.setVisibility(View.GONE);
recyclerView_db = view.findViewById(R.id.recycler_view_sale_list);
adapter_db = new SaleAdapter((ArrayList<Sale>) Utils.sortArray(saleArrayList),this,1,getContext(),this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView_db.setLayoutManager(layoutManager);
recyclerView_db.setAdapter(adapter_db);
adapter_db.notifyDataSetChanged();
}
}
Do you guys see what's causing this weird problem ?

You can try with requestFocusFromTouch()
Call this to try to give focus to a specific view or to one of its
descendants.
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.requestFocusFromTouch();

Hide your RecyclerView when there's no items (set visibility to gone). The problem is that it's capturing the clicks, since it's placed on top of the button.

as u said in comments you not using creatSaleBtn.setClickable(false); anywhere, then you have to hide the recyclerView using recyclerView_db.setVisibility(View.GONE);
why? because it by default fits the full-screen due too match_parent for width and height.
please add recyclerView_db.setVisibility(View.GONE); in the saleArrayList.isEmpty() check
so your new code will be :
if(saleArrayList.isEmpty()){
recyclerView_db.setVisibility(View.GONE);
getActivity().setTitle("Ventes sur portable");
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
}
});
creatSaleBtn.setClickable(true);
}

SwipeRefreshLayout's visibility should be GONE after recyclerview is empty.
of coarse recyclcerview is empty but it is still visible so your click events are caught by SwipeRefreshLayout witch is defined on top of LL1.
EDIT
instead of recyclerview, set swipe_refresh_db visibility to gone cos I doubt recyclerview is a child of swipe_refresh_dd so if you set recyclerview's visibility to gone, swipe_refresh_db is still visible and gets click events.
give it a try

Related

How can I add different ids to buttons recyclerview

My project is based on a recycler view. The layout of each elements is this :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imgview"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="2dp" />
<TextView
android:id="#+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="51dp"
android:layout_marginTop="1dp"
android:text="Line 1"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="52dp"
android:layout_marginTop="29dp"
android:layout_marginBottom="1dp"
android:text="Line 2"
android:textSize="15sp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="3dp"
android:layout_marginEnd="3dp"
android:text="Voir plus" />
</RelativeLayout>
And I edit it with this :
ArrayList<exempleitem> ExampleList = new ArrayList<>();
ExampleList.add(new exempleitem(R.drawable.ic_android, "Line 1", "Line 2 ", button2.setId(R.id.hydro) ));
ExampleList.add(new exempleitem(R.drawable.ic_baseline_airplanemode_active_24, "Line 3", "Line 4 ",button3.setId(R.id.hel)));
ExampleList.add(new exempleitem(R.drawable.ic_baseline_beach_access_24, "Line 5", "Line 6 ",button4.setId(R.id.lit)));
My problem is that I would like to change the ID of each button, and it doesnt work.
I can't think of anything else right now, and I'm quite a begginner.
I think you don't know how a recyclerview works. It doesn't matter if a button have the same id as another as long as they live in different "cells". Thats the purpose of recyclerviews.
So you have to create a recyclerview adapter and put all the logic in there, the logic will be based on a dataset which is an array probably and your clicks will be based on the position of that array.
You can read more from Google's official guide
Hope it helps!
You shouldn't change the ids and if I am not mistaken, you are not even allowed to do that.
I read in the comments that you want to change the event of a button. Try encapsulating the actions of each button click into methods and then setOnClickListener again on the buttons to change their on click action.
Initial Code before changing action
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dosmthA();
}
});
To change action simply:
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dosmthB();
}
});
You can even create View.OnClickListener() objects and interchange them in the setOnClickListener() method like so:
View.OnClickListener action = new View.OnClickListener() {
#Override
public void onClick(View view) {
smth();
}
}
view.findViewById(R.id.button_first).setOnClickListener(action);
You can use a ViewHolder and RecyclerView Adapter to set Click listener for each view...
If you share me the adapter code snippet I can give you the code...
😁

How to change a layout in same activity by clicking a Button?

Change a layout in same Activity when click on play button then layout show pause Button at the same place in Android Studio music player app.
How it is possible?
I'm a beginner so I don't know how to use method about it?
I cannot get an answer.
I understand from your question that when your user taps pause button, you should hide pause button and show play button and vice-versa. This is relatively simple to achieve. Just create a layout with a linear layout with two buttons like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:padding="5dp">
<Button
android:id="#+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:text="Play"
android:padding="5dp"/>
<Button
android:id="#+id/btnPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Pause"/>
</LinearLayout>
Now set on click listner to both of the buttons and handle button visibility within that like this :
Button btnPlay = barcodeDialog.findViewById(R.id.btnPlay);
Button btnPause = barcodeDialog.findViewById(R.id.btnPause);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.VISIBLE);
btnPause.setVisibility(View.GONE);
}
});
This way you can handle the visibility of your Play and Pause buttons. Reply if you need more information.

New Value EditText not being displayed , no errors whatsoever

EDIT : By the way , if i hard code edt4.setText("any text") IN THE SCOPE , it shows the value
EDIT2: tried rebuilding/cleaning project , could this be a bug in settext method, it looks like im doing it right . When i look at other code with settext
EDIT3: startactivityforresult might be my answer ? , i openend another question related to what i wanna try out , i still havent found another solution , stuck with this for a week now : (
this is the link to my question Can i use startActivityForResult , with one activity?
EDIT4 : now trying it with making views visible and invisible
I have read every single article about anything related on stackoverflow , dreamincode etc, and i cant find anybody that knows the answer for my problem.
I have a qr scanner and after a successful scan the result needs to be put in a EDIT TEXT field with name editText4 , my code throws no errors but it is NOT displaying any value .
I've posted on different forums but to no avail (https://www.dreamincode.net/forums/topic/412000-settext-is-not-showing-set-value-in-edittext-in-gui/page__st__15__gopid__2372214&#entry2372214)
, as you can see the commented code . That's pretty much also what I've tried , I think I somehow got to get my handle result method inside of the scope.
NOTE : Log.v outputs the result very well , but when i try anything else with the result it just don't works or being displayed
ps: I m beginner at java
Thanks for your help
here's my mainactivity
` public class MainActivity extends Activity implements
ZXingScannerView.ResultHandler {
Button sendButton;
EditText edt4;
EditText edt2;
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt4 = findViewById(R.id.editText4);
mScannerView = findViewById(xmlScannerView);
mScannerView.setFormats(ZXingScannerView.ALL_FORMATS);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
EditText delete2;
Button button3;
button3 = findViewById(R.id.button3);
delete2 = findViewById(R.id.editText2);
final EditText finalEdittext = delete2;
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear EditText
finalEdittext.getText().clear();
}
});
EditText delete4;
Button button4;
delete4 = findViewById(editText4);
button4 = findViewById(R.id.button4);
final EditText finalEdittext1 = delete4;
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear EditText
finalEdittext1.getText().clear();
}
});
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
public void onClick(View v){
RelativeLayout someid = findViewById(R.id.someId);
mScannerView.setVisibility(View.VISIBLE);
someid.setVisibility(View.INVISIBLE);
}
// EditText edt4;
#Override
public void handleResult(final Result result) {
//handle result
Log.v("handleResult", result.getText());
edt4 = findViewById(editText4);
edt4.setText(result.getText());
//edt4.setText(String.valueOf(result.getText()));
// edt4.setText(new
StringBuilder().append("Resultaat:").append(result.getText()).toString());
}
`
this is xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#mipmap/ic_launcher_foreground">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/xmlScannerView"
android:visibility="gone"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<RelativeLayout
android:id="#+id/someId"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="#string/scan_locatie"
android:inputType="text"
android:text=""
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText4"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="61dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="197dp"
android:ems="10"
android:hint="#string/scan_order"
android:inputType=""
android:visibility="visible"
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<Button
android:id="#+id/sendButton"
android:layout_width="157dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:background="#android:color/holo_red_light"
android:text="#string/button"
tools:text="Versturen.." />
<Button
android:id="#+id/button3"
android:layout_width="40dp"
android:layout_height="38dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
<Button
android:id="#+id/button4"
android:layout_width="39dp"
android:layout_height="37dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
</RelativeLayout>
EDIT:5 also changed XML , and main , still not working :(
try removing recreate();
you are recreating the activity after setting the value, so the previous value is lost
I think result.getText() is a String anyway, so you could change the following line:
edt4.setText(String.valueOf(result.getText()));
To:
edt4.setText(result.getText());
After that you call updateScannerData, which is also writing to the EditText once again, but from the UI-Thread. I would recommend to remove the UI-Thread code as well since I assume the code runs on that Thread anyway.
You can either use
edt4.clear();
or
edt4.setText("");

Convert Edittext to readonly and Make it editable on Button Press

i got this nw. Before clicking on the button, my EditText is just viewable:
After button press, it should convert to an editable field:
I want the EditText field line to disappear and become read only and on button click make it editable. But this just fades the line and also the text inside it loses it visual appearance.
Here's my code:
et1.setEnabled(false);
et1.setFocusable(true);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
et1.setEnabled(true);
et1.requestFocus();
}
});
Your XML should look like this,
<?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:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Now, add the following code to your activity class.
//init the views.
et1.setVisiblity(GONE);
tv1.setVisiblity(VISIBLE);
//if the textView is shown, the btn click will hide it and show editText
//instead, and same happens for editText
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(){
if(et1.getVisiblity() != VISIBLE) {
et1.setVisbility(VISIBLE);
tv1.setVisibility(GONE);
} else {
et1.setVisbility(GONE);
tv1.setVisibility(VISIBLE);
}
} });
P.S: This isn't executable code, although, hopefully it will get you the idea.

Button setOnClickListener is not working only for one button

I have two different buttons in my activity, and an if statement to show them...
My if statement is working well and show the right button, but one of those buttons is not doing anything when I click on it! (Actually, it's not clickable at all!)
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("LOG", "Clicked!"); // Even this log won't work!
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutImportant.setVisibility(View.VISIBLE); // This line is working
}
if (!isNeeded)
{
// buttonNormal is working well!
buttonNormal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutNormal.setVisibility(View.VISIBLE); // And this one is not working too!
}
The problem is, it won't return any error or log, to know what's the problem! It's log I disabled the button... (which I didn't)
I checked if it's for declarations, but everything is declared well...
I checked the XML, there's no problem there either:
<LinearLayout
android:id="#+id/linearLayoutImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="#+id/textViewImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="#string/important"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="#+id/buttonImportant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/details" />
</LinearLayout>
...
...
<LinearLayout
android:id="#+id/linearLayoutNormal"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="gone" >
<Button
android:id="#+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="end"
android:background="#android:drawable/ic_menu_close_clear_cancel" />
<TextView
android:id="#+id/textViewNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="#string/normal"
android:textStyle="bold" />
<Button
android:id="#+id/buttonNormal"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="2dp"
android:layout_gravity="center_horizontal"
android:text="#string/details" />
</LinearLayout>
EDIT:
Here's the declarations:
LinearLayout linearLayoutImportant, linearLayoutNormal;
Button buttonImportant, buttonClose, buttonNormal;
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutImportant = (LinearLayout) findViewById(R.id.linearLayoutImportant );
buttonImportant = (Button) findViewById(R.id.buttonImportant);
buttonClose = (Button) findViewById(R.id.buttonClose);
buttonClose .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
}
});
buttonNormal = (Button) findViewById(R.id.buttonNormal);
linearLayoutNormal = (LinearLayout) findViewById(R.id.linearLayoutNormal);
}
EDIT 2:
Alright guys, maybe I didn't made it clear...
That isNeeded is a variable and I control it from another place...
When I make it false, the second if will get executed very well, and when I make it true, the first if will execute too, but only the button is not clickable!
EDIT 3:
I also checked .setEnabled(true); or .setClickable(true);, still not working... :/
SOLVED:
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
Try without the if statement in order to know if is a validation problem and you're only setting one listener
The boolean variable isNeeded can take only one of the two values, either true or false. Therefore only one of the if conditions will be executed.
Since only one of the condition is true, you are only setting an onClickListener to one of the buttons and thus the other button won't be clickable and the log message within the onClick method won't be visible in the logs.
Hope this answered your question.
All the best :)
set isNeeded = true before
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
then test it again.
Good lucky!
There was an error posting my ans so i have taken a screenshot of it. Please review it for your answer

Categories

Resources