SwipeRefreshLayout Refresh Listener not calling onRefresh() - java

I currently have a SwipeRefreshLayout that isn't calling its OnRefresh method whenever I pull down. Is something wrong with my code?
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipe_view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe_view = (SwipeRefreshLayout) findViewById(R.id.this_will_work);
swipe_view.setOnRefreshListener(new OnRefreshListener()
{
#Override
public void onRefresh()
{
Toast.makeText(getApplicationContext(), "OnRefresh() test", Toast.LENGTH_SHORT).show();
}
});

You should have a service or something that will be updated like below :
private void refresh() {
startService(new Intent(this, UpdaterService.class));
}
then Refresh :
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
refresh();
}});
//here refreshed Item
//getLoaderManager().initLoader(0, null, this);
if (savedInstanceState == null) {
refresh();
}
and your onRefresh() should contain the same method refresh().

Related

how to change actionBar title of Activity1 from a button of Activity2

I am trying to make a button in activity2(settings page), when the button is clicked, then the title of activity1(MainActivity) will change to what I set to. I have been trying to use interface to carry out the function but it gives me a null pointer exception error.
MainActivity
#Override
public void changeActionBarTitle(String editText){
actionTitle = editText;
setTitle(actionTitle);
};
Interface
public interface ActionBarInterface {
void changeActionBarTitle(String editText);
}
Setting page (activity 2)
public class SettingsPage extends AppCompatActivity {
ActionBarInterface actionBarInterface;
Button editCompanyNameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_page);
setTitle("Settings");
editCompanyNameButton = findViewById(R.id.editCompanyNameButton);
editCompanyNameButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
actionBarInterface.changeActionBarTitle("test");
}
});
}
}
Thanks.
You can try this code without using the interface
MainActivity:
#Override
protected void onResume() {
setTitle(Main2Activity.title);
super.onResume();
}
activity2:
public class SettingsPage extends AppCompatActivity {
Button editCompanyNameButton;
static String title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setTitle("Settings");
editCompanyNameButton = findViewById(R.id.editCompanyNameButton);
editCompanyNameButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
title = "test";
}
});
}
}
It gives a null pointer exception error because actionBarInterface was not initialised.
Check out topics on 'Getting a Result from an Activity', making use of classes and functions such as Intent, startActivityForResult, onActivityResult.
Android: how to make an activity return results to the activity which calls it?

reset progressbar to 0 on backpressed

I am trying to use progressbar like a 30 secs timer. It works well but when i click back button the progressbar progress continues. I am unable to reset the progressbar progress to 0 when i click the backbutton.
Here is my code:
MainAcitivty:
public class MainActivity extends AppCompatActivity {
public Button btnA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnA = (Button)findViewById(R.id.btnA);
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentA = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intentA);
}
});
}
}
SecondActivity:
public class SecondActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Context context;
float from;
float to;
final TextView textV;
final ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
final int[] i = {0};
textV = (TextView)findViewById(R.id.tvFinal);
mProgressBar = (ProgressBar)findViewById(R.id.progressbar);
mProgressBar.setProgress(i[0]);
mCountDownTimer=new CountDownTimer(30000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress"+ i[0] + millisUntilFinished);
i[0]++;
mProgressBar.setProgress((int) i[0] *100/(30000/1000));
}
#Override
public void onFinish() {
i[0]++;
mProgressBar.setProgress(100);
textV.setText("finish");
Intent intent = new Intent(SecondActivity.this, SecondActivity.class);
startActivity(intent);
}
};
mCountDownTimer.start();
}
}
in order to implement behaviour of back button you need to override onBackPressed() and then implement method to reset progressbar result.
Example:
#Override
public void onBackPressed() {
super.onBackPressed();
progressBar.setProgress(0);
}

How to start an Activity task using Threads?

I want to start the Main2Activity's task(Loading the WebView) when button is pressed but not to show the Main2Activity's Screen until onPageFinished() is called. i had some ideas to get this by Intents however it seems doesn't work.
Here my code :
MainActivity.java :
public class MainActivity extends AppCompatActivity {
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Runnable runnable = new Runnable() {
#Override
public void run() {
handler.sendEmptyMessage(0);
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
);
}
}
Main2Activity.java :
public class Main2Activity extends AppCompatActivity {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final WebView webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webView, url);
intent.putExtra("DONE",1);
// Toast.makeText(getApplicationContext(), "Done!",
//Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("file:///android_asset/2.2.html");
}
}
is this possible using Intents or i have to do this with some other techniques?
How can i Achieve this goal ?
Thanks All.
I think you should directly go to Main2Activity on button click, without calling any thread in MainActivity.
Then in Main2Activity show an image or something until onPageFinished() is called.

Android QR reader with Zxing

When I read Qr code, textView doesn't change.(Question 1) What is the problem ?
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private Button buton;
private TextView textView;
private ZXingScannerView myview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buton = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
buton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myview = new ZXingScannerView(MainActivity.this);
myview.setResultHandler(MainActivity.this);
setContentView(myview);
myview.startCamera();
}
});
}
#Override
protected void onPause() {
super.onPause();
myview.stopCamera();
}
#Override
public void handleResult(Result result) {
setContentView(R.layout.activity_main);
textView.setText(result.getText().toString());
myview.stopCamera();
}
}
And when I finished reading Qr code,I want to start new activty with result.
(Question 2) How I do it ? Will this code work ?
#Override
public void handleResult(Result result) {
myview.stopCamera();
Intent intent = new Intent(getApplicationContext(),SecondActivty.class);
intent.putExtra("Result",result);
startActivity(intent);
}
If it doesn't, how should I fix it ?
Remove setContentView(R.layout.activity_main); from handleResult. You're replacing the view that you have references to with a new view.
As for communicating the Result over intent, as is, what you have will not work. Result does not inherit from Parcelable, and you can't just stick it in an intent and expect it to work. Better would be to get all relevant info from the Result and put it in the Intent as a String.

Can i declare and use the same button in an Activity and a class?

I want to use the button for different calls in the different classes. However, when I declare and try to call the Button click method in the Activity class, it throws a null exception. In my Class I want to do this:
public class CustomFeedListViewAdapter extends BaseAdapter {
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
private class ViewHolder {
Button feedUpVoteButton;
}
And in my main activity I want to do this:
public class Feed extends AppCompatActivity {
Button upVoteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
upVoteButton = (Button) findViewById(R.id.feedUpVoteButton);
feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
}
}
You are getting NullPointerException because you are trying to access feedUpVoteButton which is present in the item layout used in CustomFeedListViewAdapter and not in the layout used by Feed activity.
Do you want to have access to the ClickListener whenever you create an adapter of type CustomFeedListViewAdapter? If yes then do this...
Your Activity class
public class Feed extends AppCompatActivity {
CustomFeedListViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
adapter = new CustomFeedListViewAdapter( new OnClickListener() {
#Override
public void onClick(View v) {
// Handle click here
}
});
}
}
And your custom adapter class
public class CustomFeedListViewAdapter extends BaseAdapter {
View.OnClickListener clickListener;
// Constructor
public CustomFeedListViewAdapter(View.OnClickListener listener) {
clickListener = listener;
}
#Override
void getView(){
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
}
}
This way you can set a different listener for each instance of the adapter you create.

Categories