Android WebView displayed on only half screen - java

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

Related

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

Button not clickable on scrollview Android

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..

Crouton centering custom view and issues with disappearing

Today I changed my Toast to Crouton.
But I had these problems :
1)Showing Crouton in center
2)Whenever I want to display new Crouton it is displaying previous Crouton again, though previous one hided long ago
I want to know if there is better way to solve above problems?? Cause I looked at sample and library but could not find"
Currently I'm solving these ways
activity layout:
<RelativeLayout
android:id="#+id/bigcontainer"
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"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DilmancSRActivity"
tools:ignore="MergeRootFrame" />
<RelativeLayout
android:id="#+id/alternate_view_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Creating my custom toast view and centering it horizontally:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/kdrw"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="8dp"
android:contentDescription="#string/strnull"
android:src="#drawable/logo" />
<TextView
android:id="#+id/toasttextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
Showing Crouton:
LayoutInflater inflater = localdsr.getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, null);
TextView text = (TextView) layout.findViewById(R.id.toasttextView1);
text.setText(info);
if (toast != null)
toast.cancel();
Configuration croutonConfiguration=new Configuration.Builder().setDuration(1500).build();
toast=Crouton.make(localdsr, layout ,R.id.alternate_view_group ,croutonConfiguration);
toast.show();
For solving the Second one I had to download library source and use it. .As I do not want to use animations firstly, I disabled animations
//croutonView.startAnimation(crouton.getInAnimation());
issue on removing function that was making things appear again inside
protected void removeCrouton(Crouton crouton)
//sendMessageDelayed(crouton, Messages.DISPLAY_CROUTON, crouton.getOutAnimation().getDuration());
//I changed it with below .plus removed crouton.getOutAnimation().getDuration() so it does not throw exception
sendMessage(crouton, Messages.DISPLAY_CROUTON);
I believe I am missing something. Hope there is already right way to do those .
Making changes within the library itself is not encouraged as it might create issues that only appear within your version of Crouton.
You can change the Animation via Configuration.Builder.setInAnimation(...) and .setOutAnimation(...).
The Configuration can be re-used and does not have to be created every time you want to show the Crouton. The same is valid for your View. It does not have to be inflated every time.
Regarding the duplicate display of a Crouton: This behavior is not intended.
The code above looks like it should display a Crouton with the info text set. If your info text has changed during the display of Croutons it might be a bug within the library. If that is the case, feel free to report it.

Android Resize MapView after Visibility change

I've got an activity with the following 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">
<com.google.android.maps.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<LinearLayout
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="5dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
android:padding="5dp" />
</LinearLayout>
</LinearLayout>
I want to be able to hide/show the seperate LinearLayout with id 'view' and then let the MapView resize according to the space left. When I try this the seperate LinearLayout is never shown. I'm not sure what is going on! Is the LinearLayout forever hidden? is it behind the Mapview?
private void showMessage() {
LinearLayout layout = (LinearLayout) findViewById(R.id.view);
layout.setVisibility(View.VISIBLE);
}
private void hideMessage() {
LinearLayout layout = (LinearLayout) findViewById(R.id.view);
layout.setVisibility(View.GONE);
}
Problem seems to be resolved by changing the order of initialization in the XML layout file, and it probably won't occur in future Android versions.

Categories