Fragment Refresh - java

I am trying to refresh a fragment class without detaching from fragment.
I have inserted some data by sqlite but unless I restart my application that change doesn't show up. Please be specific where to add refreshing code and what will be the code.

I haved solved the issue by adding swipe refresh layout. Here is my code :
this is refresh layout :
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
this is refresh listener :
swipeContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh);
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
//do all refreshing tasks
swipeContainer.setRefreshing(false);
}
});

Related

How to update RecyclerView without recreating (restarting) the activity. I use SQLite Database to store and retrieve data to the Recycler view

As far as I know there are two ways
1- using startActivity(new Intent(this, MainActivity.class);
2- Override onResume(); method and put update method inside onResume();
which neither of these two ways are efficient.
The first method recreates the activity every time I add a note to SQLite Database, the second method every time I press home button the RecyclerView gets updated though there is no need to update because the data hasn't changed in that situation.
What is the third way to update the RecyclerView whenever SQLite data has changed?
try this
Use Swipe to refresh layout
activity_main_xml
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
MainActivity
SwipeRefreshLayout swipeRefreshLayout;
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
//TODO operation
}
});

Button OnClickListener giving ViewPostImeInputStage ACTION_DOWN error

I'm trying to create a simple Android program. I've created an activity and added just one button on which I've set up an OnclickListener to print "Hello World" in the log, a simple task. But whenever I run the app, every time I click the button, I get the error "ViewPostImeInputStage ACTION_DOWN".
I've tried to put my layout inside a FragmentLayout, or build another project, or update Android Studio, all of them are solutions recommended here. None of these have worked, I still get the same dreadful "ViewPostImeInputStage ACTION_DOWN".
here's my java file
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.print("Hello World");
}
});
}
and my xml file
`<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 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">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="243dp"
android:text="Button" />
</RelativeLayout></FrameLayout>
If anyone could please help me resolve this issue, it would be greatly appreciated. OnclickListener has always worked smoothly for me but for some reason, now I can't do the most basic of tasks. Help!
Germain Edouard try to remove the backtick ` before the

Replace a full screen fragment with another

I have a fragment (fragment_1) which gets displayed when the main activity starts and displays a full screen layout (layout_1). Now i have added a button, clicking on which will start a new fragment (fragment_2)and replace the entire layout (layout_1) with my new layout (layout_2).
My first fragment (fragment_1) has a ScrollView as my root view.
<ScrollView
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"
android:fitsSystemWindows="true"
android:background="#fff"
android:scrollbarSize="0dp"
android:id="#+id/scroll_main"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
....
</RelativeLayout>
</ScrollView>
Now i want to start a new fragment (fragment_2) on a button click and display another layout (layout_2) but i don't know how to do that.
My main activity had a frame layout like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root_container">
And i used to set this as my default layout using
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
After this i start my first fragment and display the layout_1.
Now from fragment_1 i want to start fragment_2 and display the layout_2. I have read online articles and documentation where i can't find and solution regarding this. Pleas Note :" I don't want to simply replace a part of my layout_1 and replace it with my layout_2 rather i want my layout_1 totally gets replaced with layout_2 on a button click"
Replacing fragments seems like a job for the fragment manager.
This answer might help you Replacing a fragment with another fragment inside activity group

Freeze the activity state - Android

Im trying to find a method to freeze the activity state, but without success.
Im creating a game, and I want to add a pause button. When the user clicks, the activity should freeze with the components, and when click again, keep from where it was before. Is that possible to do that?
Please check the solution it can be helpful for u. As u want to freez the screen for sometime than u have to play with View Visibility.
For it u have to create the view like i have use the rlView in my below layout. Visible it when u want to freez the screen.And again when u want to remove it than set the visibility of view is gone..
We can manage the View visiblity like below:-
RelativeLayout rlView= (RelativeLayout) findViewById(R.id.rlView)
YOUR_BUTTON_CLICK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(rlView.getVisibility()==View.VISIBLE)
{
rlView.setVisibility(View.GONE);
}
else {
rlView.setVisibility(View.VISIBLE);
}
}
});
}
Please check the layout:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
HERE IS YOUR WHOLE LAYOUT VIEW
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#android:color/transparent"
>
</RelativeLayout>
</RelativeLayout>

Android: Recyclerview not resizing when softinput keyboard appears

I have an activity that contains a fragment and the fragment contains a customview. The customview contains an edittext and directly below it a recyclerview with height of match_parent. When the user gets focus on the edittext a softinput keyboard appears.. unfortunately this keyboard hides half the recyclerview underneath it. I want the recyclerview to resize to the remaining visible height of the screen (under the edittext) so that items in the list aren't hidden by the keyboard.
I've tried setting adjustresize for the activity in the manifest but it has no effect:
android:windowSoftInputMode="adjustResize"
I've tried setting my app theme to a base theme (in case something in my custom theme was causing a problem) - no effect:
android:theme="#style/Theme.AppCompat.NoActionBar"
I've checked to make sure there are no framelayouts anywhere down the hierarchy to the view that has to be resized (I read an SO thread that it could be a problem) - there are none.
I've checked the activity, the fragment, the customview and every adapter connected to make sure that I'm not calling getWindow anywhere (I'm not).
The customview is inflated at runtime from a viewstub. Could this have a connection?
Edit:
contents of the parent:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
...
<ViewStub
android:id="#+id/viewstub_outername"
android:layout="#layout/viewstub_thatIneed"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
contents of the viewstub:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewstub_root"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#android:color/white"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:visibility="invisible"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:visibility="invisible"
/>
</RelativeLayout>
Showing the keyboard thusly:
public static void showSoftKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
Don't do anything in manifest file. Just use
((LinearLayoutManager)recyclerView.getLayoutManager()).setStackFromEnd(true);
This will take care of recyclerview resizing.
window.decorView.setOnApplyWindowInsetsListener { view, insets ->
val insetsCompat = toWindowInsetsCompat(insets, view)
mainNavigation.isGone = insetsCompat.isVisible(ime())
view.onApplyWindowInsets(insets)
}

Categories