Not able to create Custom button in pop up activity - android

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.

Related

Moving from start up screen to loading screen in android app

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" />

Android Layout programmatically

layout\layout_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rR1MessageBubble"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rR2MessageBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/drawable_sky_background_frame">
</RelativeLayout>
</RelativeLayout>
layout\layout_component.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="50dp"
android:orientation="vertical"
android:background="#drawable/frame">
<TextView
android:id="#+id/txtViewTest"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Hello!"
android:gravity="center" />
</LinearLayout>
drawable\frame.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#FF07DD43" />
<corners android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
</shape>
MainActivity.java
![package com.kore.layoutdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* #author ayadav
*
*/
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
RelativeLayout rR2MessageBubble = (RelativeLayout)findViewById(R.id.rR2MessageBubble);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams();
//View-1
View view1 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null);
rR2MessageBubble.addView(view1);
//View-2
View view2 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null);
TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest);
txtViewTest.setText("World");
//params.addRule(RelativeLayout.RIGHT_OF, view1.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
view2.setLayoutParams(params);
rR2MessageBubble.addView(view2);
}
}
Output
Requirement
All i want to add multiple instances of layout_component.xml in layout_main.xml so instead of negative voting please suggest me the way via i can achieve this...thanks
You have made a small mistake buddy, you are assigning a wrong LayoutParam to the view2.
Replace,
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams();
with
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100); //or whatever width, height as per your need.
Hope you understand your mistake.
Probably because you set the text to "World".
TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest);
txtViewTest.setText("World");

Simple code does not work

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 to make multiple buttons with different background color and round corners

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

Orientation change does not redraw layout completely

This is a Major Edit on my previous question
I'm creating a custom (composite) component named OneLineSeeker that consists of a TextView and a SeekBar.
When I have multiple OneLineSeekers declared in a layout, they don't retain their state on configuration change. All seeker thumbs assume the same position (the position of the last OneLineSeeker) on configuration change.
If I don't use the custom component and use only SeekBars individually (uncomment //a() in main activity), they will retain their individual positions on configuration change.
Can anyone help?
src files
A_seekbarsActivity.java
package com.an;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class A_seekbarsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//a();
}
void a() {
setContentView(R.layout.main_1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SeekBar sb = (SeekBar) findViewById(R.id.seekBar1);
if(sb.getProgress() > 50) sb.setProgress(0);
else sb.incrementProgressBy(5);
}
});
}
}
OneLineSeeker.java
package com.an;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class OneLineSeeker extends LinearLayout {
public OneLineSeeker(Context context, AttributeSet attrs) {
super(context, attrs);
View ols = LayoutInflater.from(context).inflate(R.layout.one_line_seeker, this, true);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OneLineSeeker, 0, 0);
String text = array.getString(R.styleable.OneLineSeeker_name);
if (text != null)
((TextView) ols.findViewById(R.id.name)).setText(text);
array.recycle();
}
}
res/layout files
main_1.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"
>
<SeekBar android:id="#+id/seekBar1" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<SeekBar android:id="#+id/seekBar2" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<SeekBar android:id="#+id/seekBar3" android:layout_height="wrap_content" android:layout_width="match_parent"></SeekBar>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:oneLineSeeker="http://schemas.android.com/apk/res/com.an"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.an.OneLineSeeker
android:id="#+id/s1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name="1"
/>
<com.an.OneLineSeeker
android:id="#+id/s2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name="2"
/>
<com.an.OneLineSeeker
android:id="#+id/s3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
oneLineSeeker:name='3'
/>
</LinearLayout>
one_line_seeker.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id='#+id/name'
android:text='name'
/>
<SeekBar
android:id='#+id/seekbar'
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</merge>
res/values files
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="OneLineSeeker">
<attr name="name" format="string"/>
</declare-styleable>
</resources>
Its not supposed to reset its position to zero. This is actually a feature. When view has id associated with, it usually saves and the restores its state on configuration changes.
If you don't want specific view to save state on configuration change, you can remove android:id from view's entry in layout (but you won't be able to control that view then). Another alternative is to extend ProgressBar and disable its ability to restore state.
The last one option is the easiest one: override onRestoreInstanceState() and reset state there:
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
bar.setProgress(0);
}
But I would strongly recommend not resetting view state. After all the whole idea of restoring state on configuration change is to make user think as if nothing really changed. And you need to have very strong reasons to disable this behavior.
In that u have to define Orientation Change Methods.....
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
onCreate(null);
}
Try this Code may u get any Help.....

Categories

Resources