View hiding behind navigation bar android - android

I am using Sliding Activity,
the View is not fitting to the screen, it is hiding behind the Navigation Bar.
I tried ,
<item name="android:fitsSystemWindows">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
but nothing works.
here Snackbar is hiding behind the Navigation Bar:

Hey there best practive would be to use tha NavigationDrawer, but for your problem create a class CustomSlidingMenu which extends SlidingMenu and Override methods like this. Then use the CustomSlidingMenu instead of SlidingMenu and it should work fine
public class CustomSlidingMenu extends SlidingMenu {
private boolean mActionbarOverlay = false;
public CustomSlidingMenu(Context context) {
super(context);
}
public CustomSlidingMenu(Activity activity, int slideStyle) {
super(activity, slideStyle);
}
public CustomSlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSlidingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#SuppressLint("NewApi")
#Override
protected boolean fitSystemWindows(Rect insets) {
if (mActionbarOverlay) return true;
setMyPadding(insets);
return true;
}
#TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
#Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (mActionbarOverlay) return insets.consumeSystemWindowInsets();
Rect rect = new Rect(
insets.getSystemWindowInsetLeft(),
insets.getSystemWindowInsetTop(),
insets.getSystemWindowInsetRight(),
insets.getSystemWindowInsetBottom()
);
setMyPadding(rect);
return insets.consumeSystemWindowInsets();
}
private void setMyPadding(Rect rect) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
switch (manager.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
case Surface.ROTATION_270:
rect.right += getNavBarWidth();
break;
case Surface.ROTATION_180:
rect.top += getNavBarHeight();
break;
default:
rect.bottom += getNavBarHeight();
}
}
setPadding(
rect.left, rect.top, rect.right, rect.bottom
);
}
private int getNavBarWidth() {
return getNavBarDimen("navigation_bar_width");
}
private int getNavBarHeight() {
return getNavBarDimen("navigation_bar_height");
}
private int getNavBarDimen(String resourceString) {
Resources r = getResources();
int id = r.getIdentifier(resourceString, "dimen", "android");
if (id > 0) {
return r.getDimensionPixelSize(id);
} else {
return 0;
}
}
}
I'm assuming that you use AppCompat with my approach, so the reason is that the paddings set by the SlidingMenu are in conflict with the AppCompat configurations and Override them.

Related

click gesture detection on part of edittext android

I am working on a text editor for a programming language, it has two parts, the line number box (2) and the code box (1) like in the figure below
..................
. . .
. . .
. 2. 1 .
. . .
. . .
. . .
..................
If the user clicks in the line number box (2), I need to execute some code, here I just show a toast with the message 'Click' but ignoure the default behavior of edit text.
If the user clicks inside box 1, just delegate the default edittext behavior by calling super.onTouchEvent(event).
Here's what I did, but I don't know how to get it to work properly, i need to play with the return value of onTouchEvent:
public class MyEditText extends AppCompatEditText {
private Rect drawingBound;
private Rect leftRect; // area 2
private Paint rectPaint;
private static final int PADDING = 100;
private boolean down = false;
public MyEditText(#NonNull Context context) {
super(context);
init(context);
}
public MyEditText(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyEditText(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setPadding(PADDING, 0, 0, 0);
drawingBound = new Rect();
leftRect = new Rect();
rectPaint = new Paint();
rectPaint.setColor(Color.GREEN);
rectPaint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
getDrawingRect(drawingBound);
leftRect.set(drawingBound);
leftRect.right = drawingBound.left+ PADDING;
canvas.drawRect(leftRect, rectPaint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
down = true;
return false;
}
else if (action == MotionEvent.ACTION_UP) {
if (down) {
down = false;
int x = (int) event.getX();
int y = (int) event.getY();
if (leftRect.contains(x, y)) {
Toast.makeText(getContext(), "Click", Toast.LENGTH_SHORT).show();
return true;
}
}
return super.onTouchEvent(event);
} else {
return super.onTouchEvent(event);
}
}
}

Top and bottom text cut of in Webview

I'm coding an ebook reader.
For paging I had the smart (stupid ?) idea to use a custom WebView. I changed it's behavior and it scrolls down of 1 page when I swipe left. It works really good expect on one point... The top and bottom text are often cut off. I got the height of my WebView and my font size but then I don't know what to do with that... Is there a way to have no cut off text at the top and at the bottom of my WebView ? It looks like they used the same same technic but with a ScrollView in the opensource PageTurner app but the code is quite complicated, I can't get the code part who could be interesting for me.
public class BookWebView extends WebView {
private final String LOG_TAG = BookView.class.getSimpleName();
private final int SWIPE_LIMIT = 80;
private float startX,diffX;
public BookWebView(Context context) {
super(context);
}
public BookWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BookWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
break;
case MotionEvent.ACTION_UP:
if(Math.abs(diffX) > SWIPE_LIMIT) {
int visibleTextHeight = this.getHeight();
if(diffX < 0) {
Log.d(LOG_TAG,"SCROLLING DOWN");
this.scrollTo(0, getScrollY() + visibleTextHeight);
} else {
Log.d(LOG_TAG,"SCROLLING UP");
this.scrollTo(0, getScrollY() - visibleTextHeight);
}
} else {
Log.d(LOG_TAG, "==== NO SCROLL ====");
return false;
}
break;
case MotionEvent.ACTION_MOVE:
float endX = event.getX();
diffX = endX - startX;
break;
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
}
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BookWebView bwvText = (BookWebView)findViewById(R.id.bwv_text);
String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
bwvText.loadData(String.format(text,getString(R.string.lorem_ipsum)),"text/html; charset=utf-8", "utf-8");
WebSettings webSettings = bwvText.getSettings();
int fontSize = webSettings.getDefaultFontSize();
bwvText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
bwvText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
bwvText.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
int webViewHeight = bwvText.getMeasuredHeight();
adjustHeight(webViewHeight,fontSize);
}
});
}
private void adjustHeight(int webViewHeight, int fontSize) {
// Is there something I can do here ?
}
activity_main.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.severalpagestext.MainActivity">
<com.example.android.severalpagestext.BookWebView
android:id="#+id/bwv_text"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

seekbar thumb onclicklistener

I need to customize default android seekbar to control music player. I know this sounds very simple, but I just don't know how to set up seekbar thumb listener. I want to control music and change icon accordingly to play and pause when user press on seekbar thumb icon. How can I achieve this? I know that this is possible because I previously saw apps like PocketGuide where this functionality is implemented. Here's the screenshot from PocketGuide app
Maybe this helps you. Adjust the code for your needs.
public class SeekbarWithThumbTouch extends SeekBar {
private int scaledTouchSlop = 0;
private float initTouchX = 0;
private boolean thumbPressed = false;
public SeekbarWithThumbTouch(Context context) {
super(context);
init(context);
}
public SeekbarWithThumbTouch(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SeekbarWithThumbTouch(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Drawable thumb = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
thumb = getThumb();//works only for API >=16!
if (thumb != null) {
//contains current position of thumb in view as bounds
RectF bounds = new RectF(thumb.getBounds());
thumbPressed = bounds.contains(event.getX(), event.getY());
if (thumbPressed) {
Log.d("Thumb", "pressed");
initTouchX = event.getX();
return true;
}
}
break;
case MotionEvent.ACTION_UP:
if (thumbPressed) {
Log.d("Thumb", "was pressed -- listener call");
thumbPressed = false;
}
break;
case MotionEvent.ACTION_MOVE:
if (thumbPressed) {
if (Math.abs(initTouchX - event.getX()) > scaledTouchSlop) {
initTouchX = 0;
thumbPressed = false;
return super.onTouchEvent(event);
}
Log.d("Thumb", "move blocked");
return true;
}
break;
}
return super.onTouchEvent(event);
}
}

horizontal recyclerview inside vertical recycerview adapter

enter code here i am using recyclerview but i need to implement one more
recyclerview inside the adapter layout of the recyclerview. Can
anyone tell me how can i set the json data to the recyclerview
which is inside the cardview of verticsal recycler. Dont tell me
about how to set the horizontal recyclerview i know it very well.
the problem with me is that i am confused that how to set data to
recyclerview which is inside cardview of vertical recyclerview
You can pass a List> to the vertical RecyclerView's adapter and then in the onBindViewHolder, pass the inner list at some index i to the constructor of horizontal RecyclerView's adapter.
For the outer vertical RecyclerView, use this extended class:
public class BetterRecyclerView extends RecyclerView{
private static final int INVALID_POINTER = -1;
private int mScrollPointerId = INVALID_POINTER;
private int mInitialTouchX, mInitialTouchY;
private int mTouchSlop;
public BetterRecyclerView(Context context) {
this(context, null);
}
public BetterRecyclerView(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BetterRecyclerView(Context context, #Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final ViewConfiguration vc = ViewConfiguration.get(getContext());
mTouchSlop = vc.getScaledTouchSlop();
}
#Override
public void setScrollingTouchSlop(int slopConstant) {
super.setScrollingTouchSlop(slopConstant);
final ViewConfiguration vc = ViewConfiguration.get(getContext());
switch (slopConstant) {
case TOUCH_SLOP_DEFAULT:
mTouchSlop = vc.getScaledTouchSlop();
break;
case TOUCH_SLOP_PAGING:
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(vc);
break;
default:
break;
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
final int action = MotionEventCompat.getActionMasked(e);
final int actionIndex = MotionEventCompat.getActionIndex(e);
switch (action) {
case MotionEvent.ACTION_DOWN:
mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
mInitialTouchX = (int) (e.getX() + 0.5f);
mInitialTouchY = (int) (e.getY() + 0.5f);
return super.onInterceptTouchEvent(e);
case MotionEventCompat.ACTION_POINTER_DOWN:
mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
mInitialTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
mInitialTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
return super.onInterceptTouchEvent(e);
case MotionEvent.ACTION_MOVE: {
final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
if (index < 0) {
return false;
}
final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
if (getScrollState() != SCROLL_STATE_DRAGGING) {
final int dx = x - mInitialTouchX;
final int dy = y - mInitialTouchY;
final boolean canScrollHorizontally = getLayoutManager().canScrollHorizontally();
final boolean canScrollVertically = getLayoutManager().canScrollVertically();
boolean startScroll = false;
if (canScrollHorizontally && Math.abs(dx) > mTouchSlop && (Math.abs(dx) >= Math.abs(dy) || canScrollVertically)) {
startScroll = true;
}
if (canScrollVertically && Math.abs(dy) > mTouchSlop && (Math.abs(dy) >= Math.abs(dx) || canScrollHorizontally)) {
startScroll = true;
}
return startScroll && super.onInterceptTouchEvent(e);
}
return super.onInterceptTouchEvent(e);
}
default:
return super.onInterceptTouchEvent(e);
}
}
}
For the inner horizontal RecyclerView, use this extended class:
public class FeedRootRecyclerView extends BetterRecyclerView{
public FeedRootRecyclerView(Context context) {
this(context, null);
}
public FeedRootRecyclerView(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public FeedRootRecyclerView(Context context, #Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
/* do nothing */
}
}
You can find proper explanation as to what these classes do over here : http://nerds.headout.com/fix-horizontal-scrolling-in-your-android-app/

How to make Android ScrollView fading edge always visible?

By default scrollview's fading edge is visible only if it is possible to scroll in that direction. How can I make it visible at all times?
I don't want to put any drawables on top or something like that. I want to accomplish it using the builtin fading edge, probably by overriding some scrollview functions.
Yes, extend ScrollView and override these methods (based on Donut-release2):
#Override
protected float getTopFadingEdgeStrength() {
if (getChildCount() == 0) {
return 0.0f;
}
return 1.0f;
}
#Override
protected float getBottomFadingEdgeStrength() {
if (getChildCount() == 0) {
return 0.0f;
}
return 1.0f;
}
For comparison's sake, this is the original code, which shortens the fading edge as you get close to the end of the list:
#Override
protected float getTopFadingEdgeStrength() {
if (getChildCount() == 0) {
return 0.0f;
}
final int length = getVerticalFadingEdgeLength();
if (mScrollY < length) {
return mScrollY / (float) length;
}
return 1.0f;
}
#Override
protected float getBottomFadingEdgeStrength() {
if (getChildCount() == 0) {
return 0.0f;
}
final int length = getVerticalFadingEdgeLength();
final int bottomEdge = getHeight() - mPaddingBottom;
final int span = getChildAt(0).getBottom() - mScrollY - bottomEdge;
if (span < length) {
return span / (float) length;
}
return 1.0f;
}
You can also use the following code:
public class TopFadeEdgeScrollView extends ScrollView {
public TopFadeEdgeScrollView(Context context) {
super(context);
}
public TopFadeEdgeScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TopFadeEdgeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected float getBottomFadingEdgeStrength() {
return 0.0f;
}
}

Categories

Resources