Start activity after finish current - java

I need to complete three task in one button click. its look like on button click called SaveQuote
1) Hide Adview
2) Make Screenshot of Layout and Save it
3) Show Adview
Now I have implemented method for do above three task in my java as like below
else if(menuItem.getItemId() == R.id.save_image) {
adView.setVisibility(View.GONE);
saveQuote();
adView.setVisibility(View.VISIBLE);
But I am facing issue that ad always staying visible. If I use on ViewGone method and saveQuote method than its working fine, but if I add VISIBLE method than its not hiding my adview.
Note : I am not getting any error for same. I just need to know how can I achieve above three task via one button click
My saveQuote method is like below
private void saveQuote(){
String id=getQuote(mItemIndx).get(KEY_ID);
View v1=null;
List<Fragment> activeFragments=getSupportFragmentManager().getFragments();
for(Fragment fragment:activeFragments){
QuoteCard cardFrag=(QuoteCard)fragment;
if(cardFrag!=null&&cardFrag.mId.equals(id)){
v1=cardFrag.getCardView();
}
}
TextView textView = (TextView)v1.findViewById(R.id.textAuthorSign);
textView.setVisibility(TextView.VISIBLE);
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
ImageLoader.getInstance().saveQuoteImage(bitmap);
Snackbar.make(v1,"Quote Saved",Snackbar.LENGTH_SHORT).show();
adView.setVisibility(View.VISIBLE);
}
I am getting error like below if set VISIBLE method on end of saveQuote
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.NativeExpressAdView.setVisibility(int)' on a null object reference
Thanks

You can do something like this.
adView.setVisibility(View.GONE);
saveQuote();
now in your saveQuote() method after all your logic is set.. add last line..
adView.setVisibility(View.VISIBLE); make note that your adView should be defined globally and you have initialized your adVIew.

You should review "Handle,Looper,Message"
adView.setVisibility(View.GONE);
saveQuote();
adView.post(runable)
Runnable runable=new Runnable(){
#Override
public void run(){
adView.setVisibility(View.VISIBLE);
}
}

Declare as global
final Handler handler = new Handler();
Change your code like this
else if(menuItem.getItemId() == R.id.save_image) {
adView.setVisibility(View.GONE);
saveQuote();
handler.postDelayed(new Runnable()
{ #Override public void run() {
adView.setVisibility(View.VISIBLE); } }, 5000);
}
Here the delay is 5 s reduce the delay if don't need that much.

Related

LinearLayout.getwidth is zero when outside Runnable

I tried to get the width of a LinearLayout.
Here is the code of the MainActivity.java:
public class MainActivity extends AppCompatActivity {
BoardClass board;
private int widthareagame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout gamearea;
ImageView im1 ;
Button abutton;
abutton = (Button) findViewById(R.id.buttonnew);
gamearea = ( LinearLayout) findViewById(R.id.boardarea);
gamearea.post(new Runnable(){
public void run(){
widthareagame = gamearea.getWidth();
}
});
board = new BoardClass(this,widthareagame);
gamearea.addView(board);
}
The value of widthareagame at new BoardClass(this,widthareagame); is still Zero.
Thanks
Here is what documentation says about View#post():
Causes the Runnable to be added to the message queue. The runnable
will be run on the user interface thread.
Your task, of modifying the value of widthareagame variable, has been pushed to the message queue of the view. It doesn't guarantee that it will get executed at the very same instance. The control then proceeds to the next line, where you still get the unmodified value.
You can try something like this, to ensure that you are able to use the modified value:
gamearea.post(new Runnable(){
public void run(){
widthareagame = gamearea.getWidth();
board = new BoardClass(this,widthareagame);
gamearea.addView(board);
}
});
This is because post method call queued the setting of widthareagame where as your view is rendering.You didn't guarantee the order of execution.
You have to make sure the statements inside the run method execute first and then new Board(.. is invoked.For that you can do something like this
final AtomicBoolean done = new AtomicBoolean(false);
run(){
//inside run method
done.set(true);
notify();
}
then do something like this
synchronized(task) {
while(!done.get()) {
task.wait();
}
new Board(..
}
where task is your runnable task defined something like this
final Runnable task = new Runnable() {
#Override
public void run() {
The reason is zero is because within the onCreate the LinearLayout has not been measured yet.
And the reason it only works when within the Runnable is because since this one has been posted then it will run on the next execution cycle, which is after the onCreate and the rest of the Activity lifecycle methods (onStart, onResume, etc.) and even onAttachedToWindow have been called, at which point will be already measured and give the correct size.
Said all that, a safer way to get your layout metrics with certainty would be to listen when the layout state changes.
gamearea.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Remove the listener here unless you want to get this callback for
// "every" layout pass, which can get you into an infinite loop if you
// modify the layout from within this method
gamearea.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// A this point you can get the width and height
widthareagame = gamearea.getWidth();
}
});

How to execute code after Activity creation in Android?

I want to execute some code immediately after the form is shown.
I want to check the size of a button on the form and use the same size to create a new button at runtime.
I tried onStart() and onResumed(), but they do not work.
Thanks,
Ashok
You can add a globallayout listener. Add the listener at onActivityCreated.
Check this example:
button1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
button1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
button1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//here the size is already available. create new button2 here with the size of button1
}
});
you are looking at the onCreate() method, that is the function that will run when your app is first loaded in to memory

Android View onResume method?

When returning to an activity using the back button and
startActivity(new Intent(this, ActivityMainMenu.class));
is called, are there any methods that automatically go to a custom view?
I've noticed that when going back to the view, it's no longer invalidated.
Basically without using the activity's onResume I want to be able to resume my custom view.
For anyone else who wants to know you can use:
protected void onAttachedToWindow()
It's called every time a view is attached to the Window.
In the Android Source for TextView, it posts a Runnable.
if (ss.error != null) {
final CharSequence error = ss.error;
// Display the error later, after the first layout pass
post(new Runnable() {
public void run() {
setError(error);
}
});
}

Android ListView within fragment doesn't refresh without a button click

My ListView doesn't refresh its contents when I call the appropriate method unless the method was invoked with a button click.
The follow example code is how my test button works (temporary button to check to see if it was a problem with my refresh code):
testBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
exampleRefresh();
}
});
And the method it calls:
public void exampleRefresh() {
exampleAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayOfItems());
exampleListView.setAdapter(exampleAdapter);
}
This works fine.
However, if I call exampleRefresh() in the switch statement for a context menu, nothing happens. Again, when I click the test button, the ListView refreshes instantly. These are calling the same method, I don't understand the issue.
I have tried adding nofifyDataSetChange(), but it doesn't work. The ListView only refreshes when I invoke a button press.
It's also worth noting that even if I call the method on the invoke of a context menu, it refreshes. It does not do anything without an invoke, it appears.
Any help will be very much appreciated.
have you tried to
exampleAdapter = new ArrayAdapter<String>();
exampleAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayOfItems());
exampleListView.setAdapter(exampleAdapter);
this should force it to clear and re-add the
if you are changing data in list and then you want to refresh listview then dont call setadapter method again. just call below method:
exampleAdapter.notifyDataSetChange();
Try invoking notifyDataSetChange() from exampleListView.post() like this:
exampleListView.post(new Runnable() {
public void run() {
exampleAdapter.notifyDatasetChange();
}
}

OpenGL ES 2.0 - How can multiple views use the same renderer?

Right now I have my ApplicationActivity, this activity is responsible for managing multiple views (GLSurfaceViews). Can / Should I have all the views set the renderer to a "global" renderer?
Code:
public class ApplicationActivity extends Activity
{
private static final String TAG = ApplicationActivity.class.getSimpleName();
private final Stack<Screen> screens = new Stack<Screen>();
private GlRenderer glRenderer;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG, "Main Activity Created");
setupGraphics();
ChangeScreen(new MainMenu(this, glRenderer)); //Creating a new Screen sets the renderer
}
private void setupGraphics()
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
glRenderer = new GlRenderer(this);
}
public void Draw() //Is called by the glRenderer onDrawFrame() { mainActivity.Draw() }
{
}
}
Its the same activity switching between GLSurfaceViews and by my knowledge I believe that the method setRenderer sets the view renderer and then starts the rendering thread (creating a new thread) but I don't want to recreate the thread every time I switch between views - may create potential problems.
So in the end I want a Renderer class just to keep graphics sepreate from business logic and such but, I don't know if using one Renderer is even possible, without setting the thread again?
You can only use Multiple Views with the same Renderer only if you properly switch out between them with GLSurfaceView.onPause() / .onResume();
My specific case:
#Override
protected void onPause() //Overrides onPause from Activity
{
surfaceViews.peek().onPause();
super.onPause();
}
So everytime the activity pauses I would have to pause the current View. And if the Activity resumes then resume the View also.
I also have a method called SetView which will either (pause and remove then change to another View) or (pause and then change to another View) this is accomplished using a Stack
public void SetView(View screen)
{
if (!screens.empty())
{
screens.peek().onPause();
screens.pop();
}
screens.push(screen);
setContentView(screens.peek());
}
Of course though because we are using Views instead of Activities now we must Override the onBackPressed() to go back to previous Views.
#Override
public void onBackPressed()
{
if (screens.size() == 1)
super.onBackPressed();
else
{
screens.pop();
setContentView(screens.peek());
screens.peek().onResume();
}
}
By doing new GLRenderer() you create new instance of your class. So there is no problem to have the same renderer used in different activities.
EDIT: I seem to misunderstand your question - if you want many GL surfaces visible at once, then no, it is not possible. But it got nothing to do with reusing renderer code.

Categories