First activity is only working on my Android project - java

I am new to android and java programming so i try to learn from doing few projects and i am stuck with this problem.
When i try to run the app only first activity is working and second activity is not working.
Where did i missed on this app?
I am confuse and two java projects are working but the only first activity works.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.android_examples.wallpaper_android_examplescom.MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/img76"
android:layout_above="#+id/btn_right"
android:layout_alignParentTop="true"
android:layout_marginBottom="30dp"
android:scaleType="fitXY" />
<Button
android:id="#+id/btn_right"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_left"
android:layout_alignBottom="#+id/btn_left"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:text="RIGHT" />
<Button
android:id="#+id/btn_left"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="43dp"
android:layout_marginLeft="43dp"
android:layout_marginBottom="100dp"
android:text="LEFT" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:text="Click here to Set this image as Wallpaper in android programmatically" />
</RelativeLayout>
MainActivity.java
package com.android_examples.wallpaper_android_examplescom;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
WallpaperManager wallpaperManager ;
Bitmap bitmap1, bitmap2 ;
DisplayMetrics displayMetrics ;
int width, height;
BitmapDrawable bitmapDrawable ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
bitmap1 = bitmapDrawable.getBitmap();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GetScreenWidthHeight();
SetBitmapSize();
wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try {
wallpaperManager.setBitmap(bitmap2);
wallpaperManager.suggestDesiredDimensions(width, height);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void GetScreenWidthHeight(){
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
public void SetBitmapSize(){
bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);
}
}
login.java
package com.android_examples.wallpaper_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class login extends AppCompatActivity {
private ImageView imageView;
private Button btn_right, btn_left;
private int current_image_index;
private int[] images = {R.drawable.img76, R.drawable.img2};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayImage();
SwitchButton();
}
void DisplayImage(){
imageView = (ImageView)findViewById(R.id.imageView);
}
void SwitchButton(){
btn_right = (Button)findViewById(R.id.btn_right);
btn_left = (Button)findViewById(R.id.btn_left);
btn_right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imageView.setImageResource(images[current_image_index]);
}
});
btn_left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index--;
if(current_image_index < 0){
current_image_index = images.length - 1;
}
imageView.setImageResource(images[current_image_index]);
}
}
);
}
}
Androidmanifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_examples.wallpaper_android_examplescom">
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>

Where ever you want to call the next activity use this:-
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

Theres no code to navigate to MainActivity
Trigger below code
private void startMainActivity(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}

You have to start your second activity
startActivity(new Intent(getApplicationContext(), MainActivity.class));
make sure that you have added second activity in manifest file

void SwitchButton() {
btn_right = (Button) findViewById(R.id.btn_right);
btn_left = (Button) findViewById(R.id.btn_left);
btn_right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imageView.setImageResource(images[current_image_index]);
startMainPage();
}
});
btn_left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index--;
if (current_image_index < 0) {
current_image_index = images.length - 1;
}
imageView.setImageResource(images[current_image_index]);
startMainPage();
}
}
);
}
private void startMainPage() {
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
}

write this code in your click listener:
btn_right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imageView.setImageResource(images[current_image_index]);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});

Related

Not able to save my Data to firebase in Android

I want to retrieve 4 string from Edittext and save it firebase realtime database
all the permission in firebase is set to true. After clicking the button no data is saved in the firebase.
I am not able to debug it
below is my COde
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<EditText
android:id="#+id/SOURCE"
android:layout_width="284dp"
android:layout_height="49dp"
android:layout_marginTop="32dp"
android:hint="SOURCE"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.464"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.136" />
<EditText
android:id="#+id/DESTINATION"
android:layout_width="276dp"
android:layout_height="51dp"
android:layout_marginTop="108dp"
android:hint="DESTINATION"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.466"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.268" />
<EditText
android:id="#+id/TIME"
android:layout_width="276dp"
android:layout_height="51dp"
android:layout_marginTop="196dp"
android:hint="TIME"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.389" />
<EditText
android:id="#+id/FARE"
android:layout_width="276dp"
android:layout_height="51dp"
android:layout_marginTop="268dp"
android:hint="FARE"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.525"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/SWITCH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="312dp"
android:layout_marginBottom="296dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
my MainActivity file
package com.example.rupam;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
EditText source;
EditText destination;
EditText time;
EditText fare;
Button save;
//////////////////////////////
///////Firebase ref//////////
DatabaseReference databaseReference;
////////////////////////////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////Find view by id section//////////
source = (EditText) findViewById(R.id.SOURCE);
destination = (EditText) findViewById(R.id.DESTINATION);
time = (EditText) findViewById(R.id.TIME);
fare = (EditText) findViewById(R.id.FARE);
save = (Button) findViewById(R.id.SWITCH);
/////////////////////////////////////////
databaseReference = FirebaseDatabase.getInstance().getReference().child("USERS");
////////////On button click//////////////
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
public void saveData() {
String src2 = source.getText().toString().trim();
String dst2 = destination.getText().toString().trim();
String tm2 = time.getText().toString().trim();
String fr2 = fare.getText().toString().trim();
SaveData saveData = new SaveData(src2, dst2, tm2, fr2);
databaseReference.setValue(saveData);
}
}
Savedata(class)
package com.example.rupam;
public class SaveData {
///Take 4 String////////
String src;
String dst;
String tm;
String fr;
//////////////////////
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getDst() {
return dst;
}
public void setDst(String dst) {
this.dst = dst;
}
public String getTm() {
return tm;
}
public void setTm(String tm) {
this.tm = tm;
}
public String getFr() {
return fr;
}
public void setFr(String fr) {
this.fr = fr;
}
public SaveData(String src, String dst, String tm, String fr) {
this.src = src;
this.dst = dst;
this.tm = tm;
this.fr = fr;
}
}
my Mainfest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rupam">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
please help me where I am wrong
there is no error in the build
You onClick() method is empty. You should call saveData() here.
So change from:
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
in to
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Call method which storing values
saveData();
}
});
You make a small mistake but don't worry .
When you set save.setOnClickListener method thats you don't put your saveData() method in this methods.So you should be put your saveData() method on save.setOnClickListener:
Your Mistake:
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
But Correct is:
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveData();
}
});
I have try your mentions code, According to your code you are not call
saveData(); method any where . so to resolve this issue call
saveData(); method on your save button click like this .
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Call method which storing values
saveData();
}
});
If you have Any query regarding this answered ,so add your query in comment

Call measure how far you dragged in Android

I made an application running in the background, and I want to add a function to measure the slide on the screen. I'll leave you the code for both, and someone can help me. I'm a beginner and I've been looking all over trying to solve this without results.
I know the post is too long, but I did not know how to shorten it so you can understand it.
The code for running the application in the background
App.java
package com.codinginflow.foregroundserviceexample;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class App extends Application {
public static final String CHANNEL_ID = "exampleServiceChannel";
#Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Example Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager =
getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codinginflow.foregroundserviceexample">
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleService" />
</application>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
</manifest>
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.codinginflow.foregroundserviceexample.MainActivity">
<EditText
android:id="#+id/edit_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="startService"
android:text="Start Service" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stopService"
android:text="Stop Service" />
</LinearLayout>
ExampleService.java
package com.codinginflow.foregroundserviceexample;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import static com.codinginflow.foregroundserviceexample.App.CHANNEL_ID;
public class ExampleService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this,
CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
MainActivity.java
package com.codinginflow.foregroundserviceexample;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editTextInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = findViewById(R.id.edit_text_input);
}
public void startService(View v) {
String input = editTextInput.getText().toString();
Intent serviceIntent = new Intent(this, ExampleService.class);
serviceIntent.putExtra("inputExtra", input);
ContextCompat.startForegroundService(this, serviceIntent);
}
public void stopService(View v) {
Intent serviceIntent = new Intent(this, ExampleService.class);
stopService(serviceIntent);
}
}
Code for sliding measurement
Point p1;
Point p2;
View view = new View(this);
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
p1 = new Point((int) event.getX(), (int) event.getY());
else if(event.getAction() == MotionEvent.ACTION_UP)
p2 = new Point((int) event.getX(), (int) event.getY());
return false;
}
});
Can someone call me this code in my app?

OnPause() and also onDestroy() never called when other activity opened

I have an app with many activities using intent. I have a sharedPreferences method for saving values.
I have a welcome screen that I set to display when the app launches (onCreate is called). In the same activity, I delete the sharePreferences method so when I reopen the app the welcome screen will launch again.
The problem is, when I change to another activity and back again to the main activity and press the exit button. When I launch the app again, it does not show the welcome screen. I think, the main activity is still running so, onPause(), onStop() and onDestroy() are never called. When I stay in the same activity (main) and press exit, it will call onDestroy() (that contains the erase sharedPreferences method).
I didn't set the finish() method. If I set that, every moved activity will call onDestroy in Main activity so the welcome screen will launch every move to that activity.
My mainactivity.java
package com.bani.latihan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class Main extends AppCompatActivity {
RelativeLayout PopupScreen, layoutAsli;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onDestroy(){
super.onDestroy();
SharedPreferences settings = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE);
settings.edit().remove("ditekan").apply();
}
#Override
public void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.main);
Button btn1 =(Button)findViewById(R.id.button1);
final Button btn2 =(Button)findViewById(R.id.button2);
PopupScreen = (RelativeLayout)findViewById(R.id.PopupScreen);
layoutAsli = (RelativeLayout)findViewById(R.id.layoutAsli);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(Main.this, Intent2.class);
startActivity(intent2);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
moveTaskToBack(true);
finish();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Isi dewek cuk!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
EditText tvText = (EditText) findViewById(R.id.editText1);
SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
if (prefs.contains("text")){
tvText .setText(prefs.getString("text", ""));
}
SharedPreferences prefs2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE);
if (prefs2.contains("ditekan")){
PopupScreen.setVisibility(View.INVISIBLE);
layoutAsli.setVisibility(View.VISIBLE);
}
}
public void dismisWelcomeMessageBox(View view) {
PopupScreen.setVisibility(View.INVISIBLE);
layoutAsli.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPause() {
super.onPause();
EditText tvText = (EditText) findViewById(R.id.editText1);
Button btWelcome = (Button) findViewById(R.id.button_welcome);
SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit();
SharedPreferences.Editor prefEditor2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE).edit();
prefEditor.putString("text", tvText.getText().toString());
prefEditor2.putBoolean("ditekan", btWelcome.isPressed());
prefEditor.apply();
prefEditor2.apply();
}
}
My another activity code:
package com.bani.latihan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
/**
* Created by Bani Burhanuddin on 21/02/2016.
*/
public class Intent2 extends AppCompatActivity {
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.intent);
Button btnnext = (Button) findViewById(R.id.button5);
Button btnhome = (Button) findViewById(R.id.button6);
Button btnback = (Button) findViewById(R.id.button7);
btnnext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Intent3.class));
finish();
}
});
btnhome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Main.class));
finish();
}
});
btnback.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Main.class));
finish();
}
});
CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1);
final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setVisibility(View.GONE);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
imageView2.setVisibility(View.VISIBLE);
} else {
imageView2.setVisibility(View.GONE);
}
}
});
SharedPreferences prefs = getSharedPreferences("Preferences2", Context.MODE_PRIVATE);
if (prefs.contains("text2")) {
toggle.setChecked(prefs.getBoolean("text2", true));
}
}
#Override
public void onPause() {
super.onPause();
CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1);
if (toggle.isChecked()) {
SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit();
editor.putBoolean("text2", true);
editor.apply();
} else {
SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit();
editor.putBoolean("text2", false);
editor.apply();
}
}
}
My Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.bani.latihan.splashscreen"
android:label="#string/app_name"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.bani.latihan.Main"
android:screenOrientation="sensor"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent2 activity -->
<activity
android:name=".Intent2"
android:screenOrientation="sensor"
android:noHistory="true"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent3 activity -->
<activity
android:name=".Intent3"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent4 activity -->
<activity
android:name=".Intent4"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
</application>
</manifest>
My main layout
<RelativeLayout
android:id="#+id/PopupScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#123456"
android:orientation="vertical"
android:layout_margin="16dp"
android:padding="16dp"
android:visibility="visible">
<TextView
android:id="#+id/welcome_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="#string/welcome"
android:textColor="#ddd333"
android:textSize="28sp"
android:textStyle="bold" />
<TextView
android:id="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome_title"
android:text="#string/welcome_message"
android:textColor="#0dff00"
android:textSize="18sp" />
<Button
android:layout_width="wrap_content"
android:id="#+id/button_welcome"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom"
android:background="#3b978d"
android:onClick="dismisWelcomeMessageBox"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="#string/ok"
android:textColor="#fff" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutAsli"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/hello_world"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:layout_below="#+id/hello_world"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:src="#drawable/imageview"
android:layout_gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button1"
android:layout_marginTop="111dp"
android:layout_centerHorizontal="true"
android:layout_gravity="left|bottom" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:id="#+id/button2"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:layout_gravity="right|bottom" />
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
I got ur problem. Currently u are using the MoveTaskToBack . It will not destroy the activity ,it just move back in ativity stack after that u finish
activity. So its not calling other callback methods. It always call onDestroy
Use either moveTasktoBack or finish. Dont use both, it will confusing.

Admob Banner Ad is not displaying on Android App

I have a Android App. I need to integrate admob. I have included the following code but banner ad is not displaying in main layout please help regarding this your answers will be appreciated and it would be a great help. thank you.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:background="#drawable/hanumanji1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HanumanActivity" >
<Button
android:id="#+id/pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/circle_shape_drawable"
android:text="#string/pau"
android:textColor="#ffff00" />
<Button
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/circle_shape_drawable"
android:gravity="center"
android:text="#string/pl"
android:textColor="#ffff00" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/circle_shape_drawable"
android:gravity="center"
android:text="#string/de"
android:textColor="#ffff00" />
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ads:loadAdOnCreate="true"/>
</RelativeLayout>
MainActivity.java
package e.hanuman;
import e.hanuman.Constants;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import e.hanuman.R;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class HanumanActivity extends Activity implements OnClickListener{
Button play_button, pause_button, button1 ;
private AdView adView;
MediaPlayer player;
private int[] LEAVES = {
R.drawable.leaf_green,
R.drawable.leaf_red,
R.drawable.leaf_yellow,
R.drawable.leaf_other,
};
private Rect mDisplaySize = new Rect();
private RelativeLayout mRootLayout;
private ArrayList<View> mAllImageViews = new ArrayList<View>();
private float mScale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hanuman);
// Create the adView
adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="#+id/mainLayout"
RelativeLayout layout = (RelativeLayout)findViewById(R.id.main_layout);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
//adRequest.setTesting(true);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
getInit();
Display display = getWindowManager().getDefaultDisplay();
display.getRectSize(mDisplaySize);
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
mScale = metrics.density;
mRootLayout = (RelativeLayout) findViewById(R.id.main_layout);
new Timer().schedule(new ExeTimerTask(), 0, 5000);
}
PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
player.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
player.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
player.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
public void startAnimation(final ImageView aniView) {
aniView.setPivotX(aniView.getWidth()/2);
aniView.setPivotY(aniView.getHeight()/2);
long delay = new Random().nextInt(Constants.MAX_DELAY);
final ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(Constants.ANIM_DURATION);
animator.setInterpolator(new AccelerateInterpolator());
animator.setStartDelay(delay);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
int angle = 50 + (int)(Math.random() * 101);
int movex = new Random().nextInt(mDisplaySize.right);
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = ((Float) (animation.getAnimatedValue())).floatValue();
aniView.setRotation(angle*value);
aniView.setTranslationX((movex-40)*value);
aniView.setTranslationY((mDisplaySize.bottom + (150*mScale))*value);
}
});
animator.start();
}
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int viewId = new Random().nextInt(LEAVES.length);
Drawable d = getResources().getDrawable(LEAVES[viewId]);
LayoutInflater inflate = LayoutInflater.from(HanumanActivity.this);
ImageView imageView = (ImageView) inflate.inflate(R.layout.ani_image_view, null);
imageView.setImageDrawable(d);
mRootLayout.addView(imageView);
mAllImageViews.add(imageView);
LayoutParams animationLayout = (LayoutParams) imageView.getLayoutParams();
animationLayout.setMargins(0, (int)(-150*mScale), 0, 0);
animationLayout.width = (int) (60*mScale);
animationLayout.height = (int) (60*mScale);
startAnimation(imageView);
}
};
private class ExeTimerTask extends TimerTask {
#Override
public void run() {
// we don't really use the message 'what' but we have to specify something.
mHandler.sendEmptyMessage(Constants.EMPTY_MESSAGE_WHAT);
}
}
public void getInit() {
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player = MediaPlayer.create(this, R.raw.hanu);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
player.start();
break;
case R.id.pause_button:
player.pause();
break;
case R.id.button1:
button1Click();
break;
}}
private void button1Click() {
// TODO Auto-generated method stub
startActivity(new Intent("e.hanuman.details"));
}
#Override
public void onBackPressed(){
if(player != null && player.isPlaying())
player.stop();
finish();
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
MANIFEST XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="e.hanuman"
android:versionCode="4"
android:versionName="4.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/hanuman"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:name="e.hanuman.Splash" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="e.hanuman.HanumanActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="details"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="e.hanuman.details" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
Change com.google.android.ads to com.google.android.gms.ads In your main activity and your manifest because you're using the old ads, use the .gms change your xml too, to the .gms adview.

how to move from my MyAcctivity.java (login page) to HomeActivity.java (home page) with a click of button- INTELLIJ

It doesnt have any errors ..when i run on my emulator it UNFORTUNATELY STOPS
i am trying to make my button when i click it takes me to the next page....kindly help!
Here is codes for MyActivity.java
package com.example.INIKO_EVENTS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
Button login;
Button sign_up;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login=(Button)findViewById(R.id.Button15);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this,HomeActivity.class);
MyActivity.this.
startActivity(i);
}
});
sign_up=(Button)findViewById(R.id.signUpButton);
sign_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this,SignUpActivity.class);
MyActivity.this.
startActivity(i);
}
});
}
}
and heres my HomeActivity.java - it has buttons linking to other pages
package com.example.INIKO_EVENTS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HomeActivity extends Activity {
/**
* Created by eddie kamau on 2/14/14.
*/
Button button2;
Button button3;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
button2 = (Button)findViewById(R.id.toEventButton);
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,EventActivity.class);
HomeActivity.this.
startActivity(i);
}
});
button3=(Button)findViewById(R.id.manage_your_guestButton2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,GuestActivity.class);
HomeActivity.this.
startActivity(i);
}
});
}
}
main.xml
<Button
android:layout_width="141dp"
android:layout_height="wrap_content"
android:text="#string/login"
android:layout_margin="10sp"
android:id="#+id/Button15"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:onClick="onClick"/>
<Button
android:layout_width="121dp"
android:layout_height="wrap_content"
android:text="#string/sign_up"
android:id="#+id/signUpButton"
android:layout_gravity="center_horizontal"
android:maxWidth="600dp"
android:clickable="true"
android:onClick="onClick"/>
</LinearLayout>
home.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create_your_event"
android:id="#+id/toEventButton"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:onClick="onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/manage_your_event"
android:id="#+id/manage_your_guestButton2"
android:layout_gravity="center_horizontal" android:clickable="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/your_events"
android:id="#+id/your_eventsButton3"
android:layout_gravity="center_horizontal" android:clickable="true"/>
</LinearLayout>
try this
mainactivity.java
public class MainActivity extends Activity {
Button login;
Button sign_up;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login=(Button)findViewById(R.id.button1);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,HomeActivity.class);
startActivity(i);
}
});
sign_up=(Button)findViewById(R.id.button2);
sign_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,HomeActivity.class);
startActivity(i);
}
});
}
}
homeactivity.java
public class HomeActivity extends Activity{
Button button2;
Button button3;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,EventActivity.class);
HomeActivity.this.
startActivity(i);
}
});
button3=(Button)findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,GuestActivity.class);
HomeActivity.this.
startActivity(i);
}
});
}
}
manifest.xml
define all this class in mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.target"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.target.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.target.EventActivity" >
</activity>
<activity android:name="com.example.target.GuestActivity" >
</activity>
<activity android:name="com.example.target.HomeActivity" >
</activity>
</application>

Categories