I set an android startup screen to my app, so that I can move directly after clicking on it to a loading screen (which is a frame animation). The problem is that my app closes directly when clicking on my startup screen. Can you please tell me what's wrong with my program?
Here is my main activity code:
package test.com.myapplication;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MyActivity extends Activity {
private AnimationDrawable frameAnimation;
private ImageView view;
ImageButton start;
RelativeLayout background;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
start = (ImageButton) findViewById(R.id.startup);
view = (ImageView) findViewById(R.id.imageAnimation);
frameAnimation = (AnimationDrawable) view.getBackground();
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
frameAnimation.start();
}
});
}
}
and the XML files i created :
layout file :
<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"
android:id="#+id/back"
tools:context=".MyActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/startup"
android:id="#+id/startup"
/>
<ImageView
android:id="#+id/imageAnimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
</RelativeLayout>
and the animation file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/frame01" android:duration="210"/>
<item android:drawable="#drawable/frame02" android:duration="210"/>
<item android:drawable="#drawable/frame03" android:duration="210"/>
<item android:drawable="#drawable/frame04" android:duration="210"/>
<item android:drawable="#drawable/frame05" android:duration="210"/>
<item android:drawable="#drawable/frame06" android:duration="210"/>
</animation-list>
If you want to run the animation inside the ImageView, you have to set background property of the ImageView with your animation file.
<ImageView
android:id="#+id/imageAnimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/animation" />
Related
I have made custom buttons before and they work just fine. But something is wrong in this one. It does not take up the drawable I specified . Activity layout file :
<?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">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="#drawable/button_continue"
android:id="#+id/continue_button"
android:onClick="goToStart"
android:layout_gravity="center_vertical"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="86dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/dialogue_textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Custom Button(button_continue.xml) :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/button_continue_pressed"/>
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/button_continue_focused"/>
<item
android:state_enabled="true"
android:drawable="#drawable/button_continue_enabled"/>
</selector>
I don't think that it really has much to do with the activity file but still :
package com.mycompany.whackamole;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import static com.mycompany.whackamole.GameView.*;
public class Pop extends AppCompatActivity{
int width , height ;
TextView textView;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_activity);
width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;
getWindow().setLayout(width , (int)(height * 0.5));
textView = (TextView) findViewById(R.id.dialogue_textView);
setScoreText();
intent = getIntent();
}
public void goToStart(View view) {
Intent boo = new Intent(this , StartActivity.class);
startActivity(boo);
}
private void setScoreText() {
textView.setText(String.valueOf("Time's up! Your score was " + score));
}
}
Great if you could help!
In your xml file change:
android:background="#drawable/button_continue"
in place of:
android:drawable="#drawable/button_continue"
It is because you have set the wrong attribute in the layout file. It should be android:background and not drawable.
JAVA:
package com.example.scott.testapplication;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeScreen extends Activity {
RelativeLayout Backround;
Button InitButton;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Backround = (RelativeLayout) findViewById(R.id.Backround);
InitButton = (Button) findViewById(R.id.InitButton);
image = (ImageView) findViewById(R.id.image);
InitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*insert code here*/
}
});
}
}
In order to insert the image, i'm thinking that I need to use a command like ImageView image = new (image), and then use another command to draw it on the display. I dont know which commands I should use.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Backround"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:scaleType="fitXY"
android:src="#drawable/DESERT2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initiate"
android:id="#+id/InitButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I would like to create the image on the event of the click of a button, I also created the ImageView in the xml file. Not sure if I should change something there.
you can use
image.setImageResource(R.drawable.yourDesiredImageName);
I have the following code:
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
public class MainActivity extends Activity {
AnimationDrawable mAnim;
ImageView mouse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mouse = (ImageView)findViewById(R.id.mouse);
mouse.setBackgroundResource(R.anim.eyes);
mAnim = (AnimationDrawable)mouse.getBackground();
Button kill = (Button)findViewById(R.id.kill);
kill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mAnim.start();
}
});
}
}
It gives an error despite it is very simple and unfortunately I cannot check logCat, because my comp is too weak to handle an emulator. Is there any idea what is wrong with this code?
activity_main.xml:
<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"
android:background="#drawable/background"
tools:context=".MainActivity"
>
<ImageView
android:id="#+id/mouse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/desc"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/u1"
/>
<Button
android:id="#+id/kill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:background="#drawable/playselector" />
</RelativeLayout>
eyes.xml (animation file):
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/u1" android:duration="15"/>
<item android:drawable="#drawable/u2" android:duration="15"/>
<item android:drawable="#drawable/u3" android:duration="15"/>
....
<item android:drawable="#drawable/u28" android:duration="15"/>
</animation-list>
Your code is correct. I've tested it.
download :
http://ubuntuone.com/1hRi0O5UMYyPoqfQj76t2i
Thank you! I figured out what the problem was, my images were too big (2000x2830).
How can I make multiple buttons with different background color and round corners?
I can make only one button with white-round-corner background through making rounded_edittext.xml under the drawable folder and having this code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
and in the layout.xml I set the name of rounded_edittext.xml to background attribute
<Button
android:id="#+id/loginBtn"
android:text="Log In"
android:layout_width="276dp"
android:layout_height="40dp"
android:ems="10"
android:background="#drawable/rounded_edittext"
android:textColor="#aaa"
/>
If I want to make another color for another button I should make another xml file ! It's not usable way of coding.. Is there another better idea to make different background colors and rounded corner at the same time?
EDIT
After trying to make the code programatically as #hamad answer this error occurs
12-14 22:13:47.711: E/AndroidRuntime(1369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guidi/com.trasport.guidi.MainActivity}: java.lang.NullPointerException
12-14 22:13:47.711: E/AndroidRuntime(1369): at com.trasport.guidi.MainActivity.onCreate(MainActivity.java:25)
12-14 22:15:21.841: E/AndroidRuntime(2322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guidi/com.trasport.guidi.MainActivity}: java.lang.NullPointerException
12-14 22:15:21.841: E/AndroidRuntime(2322): at com.trasport.guidi.MainActivity.onCreate(MainActivity.java:25)
I created a project on gradiant drawable it works like charm for me,here is the code:
XML Layout layout_gradiant:
<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=".Gradiant" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Gradient Drawable"
android:textColor="#899999"
android:textSize="30sp" />
<Button
android:id="#+id/btnGradient"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="60dp"
android:layout_marginTop="150dp"
android:text="" />
</RelativeLayout>
Activity Code:
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.shapes.Shape;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Gradiant extends Activity {
//controls declaration
Button btnGradient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gradiant);
//best practice to initialize controls
initializeControls();
}
private void initializeControls() {
//Button Instance
btnGradient=(Button)findViewById(R.id.btnGradient);
//set click listener to Button
btnGradient.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setBtnBackGround(Color.CYAN, 10,10, Color.GREEN, btnGradient);
}
});
}
public void setBtnBackGround(int color,int r,int stkW,int stkColor,Button btn){
//Create instance of Gradient Drawable
GradientDrawable gdDefault = new GradientDrawable();
//set color
gdDefault.setColor(color);
//set corner radius
gdDefault.setCornerRadius(r);
//stroke width
gdDefault.setStroke(stkW,stkColor);
//it works below api level 16
btn.setBackgroundDrawable(gdDefault);
}
}
//use this above API level 16
btn.setBackground(gdDefault);
change color on your wish!
cheers,
Hamad
I have this code:
package com.problemio;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProblemioActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addProblemButton = (Button)findViewById(R.id.add_problem_button);
Button browseProblemsButton = (Button)findViewById(R.id.browse_problems_button);
Button searchProblemsButton = (Button)findViewById(R.id.search_problems_button);
Button myProblemsButton = (Button)findViewById(R.id.my_problems_button);
addProblemButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(ProblemioActivity.this, AddProblemActivity.class);
ProblemioActivity.this.startActivity(myIntent);
}
});
}
}
It compiles fine and displays the addProblemButton button, but when that button is clicked, the system gives a runtime exception.
Here is the AddProblemActivity class:
package com.problemio;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AddProblemActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TextView text = (TextView) dialog.findViewById(R.id.addProblemText);
//text.setText(R.string.addProblemText);
TextView tv = new TextView(this);
tv.setText("Please Add a Problem");
setContentView(tv);
}
}
and here is the layout 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:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First, add the problem you want to solve!"
/>
<TextView
android:id="#+id/add_problem_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add a Problem You Want To See Solved"
/>
<Button
android:id="#+id/add_problem_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add a Problem"
/>
<Button
android:id="#+id/browse_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Browse Problems"
/>
<Button
android:id="#+id/search_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search Problems"
/>
<Button
android:id="#+id/my_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View My Problems"
/>
</LinearLayout>
any idea what might be going wrong? By the way, I can't seem to locate the stack trace of the exception. Where should I look for that in Eclipse? All it currently shows me is AndroidRuntimeException - dalvik.system.NativeStart.main
Thanks!!
The only problem I can think of is that your Activity "AddProblemActivity" is not register in the manifest.
See the logs under LogCat...you will find it in Window >Show View >Android > LogCat