Button not clickable on scrollview Android - java

Here is my layout file code
Everything is working fine but Button in not clickable.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/maps"
android:gravity="center_horizontal"
android:orientation="vertical">
<include layout="#layout/custom_actionbar" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="30dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/pf_color"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="TRACK"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Please help related this problem. I am thankful to you.
I am not able to sort out what is the actual problem. I tried android:clickable="true" but still same problem
Thanks in advance.

Try putting this line into your button in the XML:
android:onClick="myClickFunction"
So it would be:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/pf_color"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="TRACK"
android:textSize="20dp"
android:onClick="myClickFunction" />
And then put this in your activity:
public void myClickFunction(View v) {
// do stuff when clicked
}

dont forget to set onCLickListener
does ur main class implements onClickListener if so do:
public void onClick(View v) {
switch (v.getId()) {
case: R.id.YOURBUTTON:
//do ur stuff
break;
default:
//todo when the screen is touched
break;
}
}
I couldnt find your buttons ID btw thats why I called it YOURBUTTON

Do you use latest support library, com.android.support:support-v4:24.0.0? I think it's a bug because i have the same problem..

Related

AndroidStudio EditText can't be edited (cursor disappear)

I'm having a weird problem i've never encountered.
I'm building a layout for an activity. My only issue is, i can't edit the edittexts in any way. I've tried both with the emulator and the real device. To be more specific, when i click on the edittext, the cursor appears for a very short moment, then disappears. The keyboard shows up but get stuck and doesn't work. In the java code only enable / disable the the editext :
#Override
public void onClick(View v) {
if(bottlename.isEnabled()){
//bottlenumber.setEnabled(false);
ArrayList<Bottle> bottle = BottleView.getBottlelist();
bottle.set(Integer.parseInt(bottlenumber.getText().toString())-1, new Bottle(bottlename.getText().toString(),Integer.parseInt(bottlenumber.getText().toString()), R.drawable.vodka));
bottlename.setEnabled(false);
}else{
bottlename.setEnabled(true);
//bottlenumber.setEnabled(true);
}
}
});
Any idea what could be the issue?
I've already tried many solutions from other posts, with no luck.
Attaching the xml code here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/bottleimage"
android:layout_width="150dp"
android:layout_height="160dp" />
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/bottlename"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:text="Bottle"
android:textAlignment="center"
android:inputType="text"
android:textSize="36sp"
android:textStyle="bold" />
<EditText
android:id="#+id/bottlenumber"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:inputType="number"
android:text="X"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
<ImageButton
android:id="#+id/editbottle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:scaleType="center"
android:src="#android:drawable/ic_menu_edit" />
</LinearLayout>
</RelativeLayout>
Is all your EditText fields disabled (android:enabled="false") in your layout intentionally?
the behavior it's a bit weird, I suggest to move the action of reading the value of the editText to a button, so you will have to change the text of the button like this :
When EditTexts enabled ==> "Send"
When EditTexts disabled ==> "Enable"

Error-popup in EditText is not showing?

I have an EditTextField with a DatepickerListener on it. If I press a submit button it should validate that field and if they are empty it should show an error message. To display this error message I use the EditText.setError() method but the popup doesnt show. I dont know how to fix it.
I tested it on 2 Devices. Once with a OnePlus 5 on Android 7.1.1 and once on a Samsung Galaxy 7 on Android 7.0.0. Same behaviour on both devices.
Thank you in Advance.
Here is my Code (Dummy Version):
private void initRequestButton(View view) {
Button button = view.findViewById(R.id.submitButton);
EditText dateTextField = view.findViewById(R.id.someField);
button.setOnClickListener(v -> {
String dateText = dateTextField.getText().toString();
if (dateText.isEmpty()) {
dateTextField.setError("A date is required");
} else {
//do Something
}
});
}
The Layout
<?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="match_parent"
android:orientation="vertical"
app:errorEnabled="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_marginTop="10dip"
android:orientation="horizontal">
<EditText
android:id="#+id/someField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:layout_weight="0.5"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="none" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_marginTop="5dip"
android:orientation="horizontal">
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.3"
android:text="#string/someText" />
</LinearLayout>
Just remove the
android:focusable="false"
android:focusableInTouchMode="false"
properties from the edittext.
The code you are using to set error is alright, but the popup pops only when the edittext is focussed either by touching manually or by programmatically, just add,
yourEdtext.requestFocus()
after setting error to the field. This will show the popup after validation fails

onClick method in fragment never gets called

I tried to set up a onClickListener inside my fragment.
public class HomeFragment extends Fragment implements View.OnClickListener {
Button btn_eventList;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
btn_eventList = (Button) view.findViewById(R.id.buttonEventList);
btn_eventList.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
btn_eventList.setText("TEST");
Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show();
}
}
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nvd"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:gravity="center"
android:text="test"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
/>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:gravity="center"
android:text="test"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="45dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonEventList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/lst_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
>
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
Does anyone know why the onClick method never gets called when I hit my button? Did I miss anything?
EDIT
Since Sevin made me notice I was not giving you a solution but an alternative way for reaching your goal, I'm editing this.
First: your code must work
The code you posted is correct
Now, do some checks:
Check if in debug mode the button is not null (should have throwed exception)
Check if in debug mode you are reaching the onClick, even if the text doesn't change and even if the toast doesn't show up.
Check if you are showing the fragment properly. Here is the official documentation about how to create a fragment.
Check if the fragment is clickable and if it has anything over it (simply, the button once clicked shows the classic "click" animation?)
Check in the xaml code if the id you used is assigned to the correct button.
And finally do some the following operations:
Clean your project
Rebuild your project
Check if in both xaml and java code you have any alert (in basic configurations, yellow mark over the line)
Uninstall the app and re-install it (pretty much the same, but since we don't know your project, it can still help)
After those, let us know
Possible solution:
You have a recycleview which is on top of the button. you have this view set with match parent both height and width inside a relativelayout, it means that this view will cover the button.
Remove this empty recycleview and the code will work
EDIT
after posting your layout I can conclude with total certainty that your REcyclerView is taking all the space of your layout thus taking all the touch inputs from whats behind.
Solutions:
You can either delete the recycler view or re-arrange it and add it to last RelativeLayout and use layout_below and placing it below the a LinearLayout
The problem is that you didn't implement this method right. onClick() method doesn't know, what view concretely has been clicked. Change your onClick() method to the following form:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.your_button_id:
btn_eventList.setText("TEST");
Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show();
break;
}
}

Android scrollview width full screen, also Uri.parse(location)

i think this question has been asked alot, and yes i have been looking for hours here, nothing really helped me..
I can show the problem with this picture:
http://i.imgur.com/554oMa5.png
sorry that i give link( i don't have enough reputation for image)
As you can see, the width doesn't fit the whole screen..
Trust me, i tried so much things to fix it:
fill_parent
match_parent
gravity=fill
viewport=true
and more..
So here is my XML code for this moment:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#000000"
tools:context=".Succes" >
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/google"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="onClickGoogle"
android:text="Go google" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="onClickSudan"
android:text="Go to Sudan!" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="onClickAwesome"
android:text="Awesome button" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="Rate me!"
/>
<RatingBar
android:id="#+id/lol2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numStars="5"
android:gravity="center" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="onClickHai"
android:text="Hai" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="onClickSuper"
android:text="Super button" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="onClickOut"
android:text="Go back" />
</LinearLayout>
</ScrollView>
I also have another problem(not connected to first one):
public void onClickSudan(View view)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("GO TO SUDAN?");
dialog.setMessage("Are you really sure go to Sudan?" +
" By pressing yes you will not have the ability to come back!" +
" Please think good before clicking yes(think really good).");
dialog.setCancelable(true);
dialog.setPositiveButton("yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
//if clicked okay, go to there ;o
Uri location = Uri.parse("geo:18.130191,31.683355");
Intent start = new Intent(Intent.ACTION_VIEW, location);
startActivity(start);
}
});
dialog.setNegativeButton("No.", null);
AlertDialog dialog2 = dialog.create();
dialog2.show();
}
The Uri cannot recognize this coordinates, what is wrong here?
The application crashes.
If the problem is with coordinates so how to get correct coordinates with google maps?
Thanks (:
Two questions, two answers. Your ScrollView is not taking up the entire width of your screen because your top-level RelativeLayout has padding set. Remove the padding attributes and the ScrollView will fit the entire screen. That said, I have no idea why you'd want your ScrollView to take up the entire screen width. Seems like it wouldn't look very nice...
As you can see here, you must prepend your location with "geo:" for it to be a valid URI. You should separate your questions into separate SO posts.

Android WebView displayed on only half screen

My web view displayed well without any problem. However, after I apply "Selected Text" function, the web view is then shown on only a half of the screen (see the image below).
Here is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.content);
Intent i = this.getIntent();
final int wordId = i.getIntExtra("id", -1);
mCurrentWord = i.getStringExtra("word");
mSelectedDB = i.getStringExtra("db");
mContentStyle = i.getStringExtra("style");
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
loadHistoryFromPreferences();
loadFavouriteFromPreferences();
wvContent = (WebView) findViewById(R.id.wvContent);
initWebview();
String content = getContentById(wordId);
showContent(content);
//This is the beginning of what I added
webkit=new Webkit(this);
wvContent.addView(webkit);
WebkitSelectedText webkitSelectedText=new WebkitSelectedText(getApplicationContext(), webkit);
webkitSelectedText.init();
wvContent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
//This is the end of what I added
I have no idea what I have done wrongly. Except this display problem, all other functions including new added one Selected text work just fine.
I hope you guys could realise the problem and do me a favour to fix it. Thank you very much.
EDITED
Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom|fill_vertical"
>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:layout_weight="1"
>
<WebView
android:id="#+id/wvContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/home"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnPronounce"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/pronounce"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnShowHistory"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/history"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnAddFavourite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/add_favourite"
android:layout_weight="0.25"
/>
</LinearLayout>
The problem may be with the layout.
In content.xml in res/layout, make sure you set the properties of webview's android:layout_height to fill_parent.
That should fix. It not, paste the contents of the layout too. And we can help
I'm not sure if this may help guys with similar problem or not, but in the end I solve my problem by changing:
wvContent.addView(webkit);
to
wvContent.addView(webkit,0,0);
Not really a solution but a workaround, perhaps.
I was getting the same problem and think I fixed it with the following code in the WebViewClient:
webView.setVisibility(View.VISIBLE);
My issue was that it only displayed half screen on first load, as soon as I clicked on a menu or changed the orientation of the device, the view was re-drawn at full screen. setting it to Visible seemed to trigger a re-draw that fixe

Categories