Image not showing(Blank space) while using glide library in android - java

Image is showing nothing not even the image I chose as default no error nothing just the image cannot be loaded I granted internet permission still this is happening plz help
No error is there in logcat.
App launches everything is fine but the image wont load i tried waiting still and the API is working fine......i dunno if its my code or the permission which is causing this...
Java Code
package com.example.meme_share;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ImageView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
ImageView i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView i = findViewById(R.id.imageView);
}
public void loadmeme() {
RequestQueue a = Volley.newRequestQueue(this);
String url = "https://meme-api.herokuapp.com/gimme";
JsonObjectRequest b = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
String u = null;
try {
u = response.getString("url");
System.out.println("Raunak"+u);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("Raunak"+u);
Glide.with(MainActivity.this).load(u).into(i);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error occurred", Toast.LENGTH_SHORT).show();
}
});
}
}
XML
<?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">
<Button
android:id="#+id/button2"
android:layout_width="182dp"
android:layout_height="71dp"
android:layout_marginTop="134dp"
android:text="Next"
app:backgroundTint="#android:color/tertiary_text_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.93"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/imageView"
android:layout_width="415dp"
android:layout_height="538dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06"
tools:srcCompat="#drawable/ic_launcher_background" />
<Button
android:id="#+id/button"
android:layout_width="182dp"
android:layout_height="71dp"
android:layout_marginTop="134dp"
android:text="Share"
app:backgroundTint="#android:color/tertiary_text_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintVertical_bias="0.956" />
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.meme_share">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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/Theme.Meme_Share">
<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>

You have created two ImageView i; variables: first in onCreate methon and second in class scope.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = findViewById(R.id.imageView); // < here
}
Besides this problem here are some others:
You don't call loadmeme method anywhere
You don't make network request
ImageView imageView; // 1. Use class scoped imageView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
// 2. Run loadmeme method
loadmeme();
}
public void loadmeme() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://meme-api.herokuapp.com/gimme";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
String u = null;
try {
u = response.getString("url");
System.out.println("Raunak" + u);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("Raunak" + u);
Glide.with(MainActivity.this).load(u).into(imageView);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error occurred", Toast.LENGTH_SHORT).show();
}
});
// 3. Add request to queue
requestQueue.add(jsonObjectRequest);
}

Related

Android App crashes on Button running thread pressed | Java

I am creating an app to check whether the given address of a server is valid or not. It sends request to a server when Connect button is pressed. If request succeed it is valid if not invalid ...
But I am having an error when I run a thread. I cant run it on Computer caz having issues enabling ADV but here is my code.
Layout
<?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"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Server Address"
app:layout_constraintBottom_toTopOf="#+id/addressInput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.869" />
<EditText
android:id="#+id/addressInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Enter your server address"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/addressBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"
android:onClick="onAddressButtonClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/addressInput"
app:layout_constraintVertical_bias="0.065" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.naveed.youtubestream;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private Button addressBtn;
private EditText addressInput;
private String serverAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addressBtn = findViewById(R.id.addressBtn);
addressInput = findViewById(R.id.addressInput);
}
private boolean checkAddress(String address){
try {
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
return true;
}
} catch (IOException e) { }
return false;
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
try{
if(checkAddress(serverAddress)){
//TODO --> Set new Layout
Toast.makeText(getApplicationContext(), "Server address was valid.", Toast.LENGTH_LONG).show();
}
else {
addressInput.setText("");
Toast.makeText(getApplicationContext(), "Invalid server address.", Toast.LENGTH_LONG).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
public void onAddressButtonClick(View view) {
try{
serverAddress = addressInput.getText().toString();
Thread thread = new Thread(runnable);
thread.start();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.naveed.youtubestream">
<uses-permission android:name="android.permission.INTERNET"/>
<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>

Suddenly getApplicationContext(), getApplication(), this.getApplicationContext() are Missing

I'm sure i'm not doing something weird or wrong last night. Last night, I'm still able to Toast.makeText(getApplicationContext(), "Some String", Toast.LENGTH_SHORT);
But this morning, When I try to debug it again for make sure the application still work like the last test, It show up an error. "Unresolvable...".
When I check my MainActivity.java code, the getApplicationContext() is red text. (I'm using material ui theme and red text mean error). Then i try to retype it. but, it's not showing the auto-complete of getApplicationContext() or kind of that. I'm not sure what's wrong with my code. I try it in the new project, and it's work fine.
Here's My Files Code :
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.belajar.belajar1">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.belajar.belajar1.MainActivity" android:weightSum="1">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textDirection="firstStrong"
android:hint="Title"
android:id="#+id/inputTitle"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="1"
android:gravity="top"
android:hint="Your Note"
android:id="#+id/inputContent"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVE"
android:onClick="cmdProcess"/>
</LinearLayout>
MainActivity.java
package com.belajar.belajar1;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import com.google.firebase.database.*;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity
{
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference TestLab1;
EditText mInputTitle, mInputContent;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInputTitle = (EditText)findViewById(R.id.inputTitle);
mInputContent = (EditText)findViewById(R.id.inputContent);
Toast.makeText(getApplicationContext(), "Some String", Toast.LENGTH_SHORT).show();
}
public void cmdProcess(View v)
{
Map<String, Object> conditionalMap = new HashMap<>();
Map<String, Object> map = new HashMap<String, Object>();
map.put(root.push().getKey(),
new Note(mInputTitle.getText().toString(),mInputContent.getText().toString()));
if (TestLab1 == null)
{
conditionalMap.put("TestLab1", map);
root.updateChildren(conditionalMap);
}
else
{
TestLab1.updateChildren(map);
}
}
#Override
protected void onStart()
{
super.onStart();
root.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if (!dataSnapshot.hasChild("TestLab1"))
{
TestLab1 = null;
}
else if (dataSnapshot.hasChild("TestLab1"))
{
TestLab1 = dataSnapshot.child("TestLab1").getRef();
}
}
#Override
public void onCancelled(DatabaseError databaseError)
{
}
});
}
}
ScreenShot Evidence :
I saw that you are using android studio.
Sometimes this weird problem happens, a project clean solves the problem for me.
If you're sure about your code is correct, this seems to be an IDE problem, try clean project and invalid IDE cache.

Unable to create facebook login in android application cannot find symbol method getAccessToken()

I am creating a facebook login for my android application. I am getting following below errors which i tried to solve but i can not because i am unable to find solution.
1) Error:(46, 46) error: cannot find symbol method getAccessToken()
2) Error:(41, 55) error: incompatible types: > cannot be converted to FacebookCallback
My activity_main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b9bd5"
tools:context="com.example.ratingapp.ratingapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/info"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="18sp"
/>
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</android.support.constraint.ConstraintLayout>
My MainActivity.java is:
package com.example.ratingapp.ratingapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.accountkit.LoginResult;
import com.facebook.login.LoginManager;
import com.facebook.login.widget.LoginButton;
import java.util.Arrays;
import java.util.List;
import static android.R.attr.data;
public class MainActivity extends AppCompatActivity {
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "public_profile");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
}
#Override
public void onCancel() {
info.setText("Login attempt canceled.");
}
#Override
public void onError(FacebookException exception) {
info.setText("Login attempt failed.");
}
});
}
}
Please help me solve these errors what i am doing wrong in this code.
Please alsoc check my AndroidManifest.xml incase i put a wrong facebook library.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ratingapp.ratingapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:label="#string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
/manifest>
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
#Override
public void onSuccess (final LoginResult loginResult)
{
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
#Override
public void onCompleted (JSONObject object, GraphResponse response)
{
try
{
String id = response.getJSONObject().getString("id");
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(passwordEditText.getWindowToken(), 0);
if (connect())
{
/*ENTER*/
}
else
{
Toast.makeText(getApplicationContext(), textProblemConnecting(), Toast.LENGTH_LONG).show();
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("YOUR FIELDS");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel ()
{
}
#Override
public void onError (FacebookException e)
{
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "first_name, email, id, last_name, birthday, locale");
request.setParameters(parameters);
request.executeAsync();

Splash screen in Android glitch

I have developed a splash screen in Android which comes for 2 seconds and go to the main activity. The problem is that when I start the application the main activity comes for few milliseconds and then goes to the splash screen activity. Can I know the solution for this?
splashscreen.java
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
public class splashscreen extends Activity {
private static int splashInterval = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
etWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(splashscreen.this, MainActivity.class);
startActivity(i);
this.finish();
}
private void finish() {
// TODO Auto-generated method stub
}
},
splashInterval);
};
}
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<ImageView android:src="#drawable/splash" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY"/>
<ProgressBar android:id="#+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="58dp" />
</RelativeLayout>
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="asdf.splashtest1" >
<application android:allowBackup="true" android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity android:name=".splashscreen" android:label="#string/app_name" android:screenOrientation="portrait" 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=".MainActivity" android:label="#string/app_name" ></activity>
</application>
</manifest>
//MainActivity.java
package asdf.splashtest1;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is my Splash Code Maybe This will helps,
Thread Background = new Thread() {
public void run() {
try {
sleep(2000);
Intent intent = new Intent(splashscreen.this, MainActivity.class);
startActivity(intent);
finish();
} catch (Exception e) {
}
};
Background.start();
}
At sleep(2000), enter your desired time in milliseconds.
Edit
private class SplashTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
}
#Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
finish();
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
}
}
Call This Async with in onCreate Method new SplashTask().execute();

Button's don't work when clicked on, Code is correct

I tried test files like HelloWorld and Count Down timer. Neither of the buttons on my app seem to work. These segments of code are ones I downloaded from the internet, and they have been reported to work. I don't know what the issue could be. I am also very new eclipse and android app programming.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:textColor="#FF0000"
android:background="#000000"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:text="CountDown Timer Demo"
/>
<Button
android:text="Seizure Detected"
android:id="#+id/start"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Override"
android:id="#+id/stop"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="click me"
android:id="#+id/Button01"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<TextView
android:id="#+id/tv"
android:textColor="#FF0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="sexy"/>
</LinearLayout>
CountDownTest.java >In the src folder
package com.example.epilepsytestapp;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class CountDownTest extends Activity {
Button start, stop;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)this.findViewById(R.id.start);
stop = (Button)this.findViewById(R.id.stop);
tv = (TextView)this.findViewById(R.id.tv);
tv.setText("10"); // startting from 10.
final MyCounter timer = new MyCounter(10000,1000);
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.start();
}
});
stop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
System.out.println("Timer Completed.");
tv.setText("Timer Completed.");
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText((millisUntilFinished/1000)+"");
System.out.println("Timer : " + (millisUntilFinished/1000));
}
}
}
Here is Helloworld.java
package com.example.epilepsytestapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) this.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
Toast.makeText(HelloWorld.this, "Hello World", Toast.LENGTH_SHORT).show();
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.epilepsytestapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".CountDownTest"/>
<activity
android:name="com.example.epilepsytestapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
android:name=".CountDownTest"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
try this.Your hello world code
package com.example.epilepsytestapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "Hello World", Toast.LENGTH_SHORT).show();
}
});
}
}

Categories

Resources