Ui elements in xml doesn't show up in emulator? - android

Here is the problem.My UI elements in xml doesn't show up when i run my app in emulator.
public void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.single_coll);
Button one = (Button) findViewById(R.id.all_stampii);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), AllStampii.class);
startActivity(intent);
}
});}
single_coll.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">
<Button android:text="All Stampii" android:id="#+id/all_stampii" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Owned" android:id="#+id/owned" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Repeated" android:id="#+id/repeated" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Last Acquired" android:id="#+id/last_ac" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
There are no errors. I just can't see the Ui elements in emulator, in only this class.
Any suggestions?

I think you are having a minor problem in function declaration. What you have is:
public void OnCreate(Bundle savedInstanceState)
Make the 'OnCreate' to 'onCreate' and put #Override before the declaration.

You don't seem to be overriding the onCreate method. That means that the OnCreate method you've got there is not being called on startup.
If you change the first line to
#Override
public void onCreate(Bundle savedInstance){
(note the lower-case 'o' in onCreate)
then the method will be called correctly

Related

Android - Button under Relative layout wont initiate OnClickListener when clicked

Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}

How to make a button redirect to another XML layout

I'm making a button in xml(res / layout / activity_home.xml), like this:
<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=".HomeActivity" >
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/schkopwide"
android:contentDescription="#string/HTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="Intent i = new Intent(activity_store.xml);
startActivity(i);"
android:text="#string/HTI" />
</RelativeLayout>
so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?
Thank you
If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.
You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Add Two View’s Here -- >
</ViewSwitcher>
You can take reference from this link: http://abhiandroid.com/ui/viewswitcher
Try out as below:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="start"
android:text="#string/HTI" />
In your main activity :
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
Here is your ActivityStore Class code:
public class ActivityStore extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
}
Also add the activity into your mainfest file.
<activity
android:name=".ActivityStore"
android:label="#string/app_name"/ >
You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.
In your code:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
And in the OnCreate of the ActivityStore class, put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
NOTE: I supposed that yous activity_store class is called ActivityStore
You need to check on Android documentation :
Activity
OnClickListener
Intent
http://developer.android.com/training/index.html
Good luck.
Try this,
Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml
<include layout="#layout/activity_home"/>
Sure you will get solution.
A simple way would be to create an Activity with another xml attached to it and then use intent.

screen to screen command

Hi am new to android and have spent over 10 hours looking for this answer but i cant seem to find and understand it. I am at the main xml page. I am trying to create a button that goes to another page. I need the easiest and simplest way to do this. Can anyody please help me? Heres my code for my 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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
Go to your activity Initialize your button
Button btn=(Button)findViewById(R.id.button1);
// Register listener to button btn
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your action
Intent newActivityIntent= new Intent(currentActivity.this, NewClass.class);
startActivity(newActivityIntent);
}
});
You can also do it different...
You can instantiate a Button in your Activity:
private Button button;
Inside the onCreate(Bundle bundle) method you find your button, as defined at this Activity XML, which is the one you used setContentView(R.layout.yourxml);:
button = (Button) findViewById(R.id.button);
Then you use an OnClickListener:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Inside the onClick method, you instantiate an Intent
Intent intent = new Intent(CurrentActivity.this, ActivityToGo.class);
CurrentActivity = the one you are, and ActivityToGo the one you want to load.
startActivity();
Read the basics on Android here.
public class MyActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
// Use a switch on v.getId() if you have multiple clickable views.
// Since there's just one button, ...
Intent intent = new Intent(this, TargetActivity.class;
startActivity(intent);
}
}
You have to learn How to switch between Activities/Screens in Android, you can find here a well explained tutorial for beginners
This process is documented on the website concerning buttons. Google search Android Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button"
android:clickable="true"
android:onClick"insertMethodNameHere"
/>
This will call the method you define in the onClick tag, then start a new activity or update the view. Whatever you need to do

Make a Buttonistener in a second layout

How would I implement a Buttonlistener for a second layout which is still be called in the main Acitivity?
I already tried it by a named Button listener and via an anonymous. But still get nullpointer Exceptions.
Code:
back = (Button) findViewById(R.id.backToMain);
if(back != null)
back.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
setLayout(R.layout.main);
}
});
Layout.xml
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/backToMain"
android:text="Zurück">
</Button>
Try this
Change the button attr tag
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/backToMain"
android:text="Zurück"
android:onClick="goBack">
</Button>
In your activity create a method
public void goBack(View v) {
//Write code here
}

button onclicklistener error

i'm getting this error when i try to run my app in the emulator (any android version): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.test/com.android.test.HomeScreen}: java.lang.NullPointerException
my question is: is this because in 2.2 there is no "golf button" in the layout, and in 3.0 there is?
this is my activity:
public class HomeScreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
Button basketball = (Button)findViewById(R.id.basketball);
basketball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent basketball = new Intent(HomeScreen.this, basketball.class);
startActivity(basketball);
}
});
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent golf = new Intent(HomeScreen.this, golf.class);
startActivity(golf);
}
});
}
}
i got 2 layouts
layout 1: layout-normal-mdpi <= running in 2.2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/basketbal"
android:layout_marginTop="46dp"
android:layout_marginRight="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
</Button>
</RelativeLayout>
layout 2: xlarge-port <= running in 3.0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:background="#drawable/basketbal"
android:layout_height="225dp"
android:layout_width="225dp"
android:layout_marginRight="70dp"
android:layout_marginTop="90dp"
android:layout_alignParentRight="true">
</Button>
<Button
android:id="#+id/golf"
android:background="#drawable/golf"
android:layout_height="550dp"
android:layout_width="211dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="390dp"
android:layout_alignParentLeft="true">
</Button>
</RelativeLayout>
if you are getting error in the below line,
Button golf = (Button)findViewById(R.id.golf);
which is not actually present in the first layout you have provided here, it is no wonder. This definitely doesn't exist and which obviously returns null to you and there you go, getting null pointer exception.
To avoid this either you have to catch the exception else you will have change the layout accordingly such that it contains both the buttons in both the layouts.
You're lacking golf button in your first layout, as you stated.
So, when you run
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
golf is set to null (first line) and then you're trying to assign it something...
Try this:
Button golf = (Button)findViewById(R.id.golf);
if (golf != null) {
golf.setOnClickListener(new View.OnClickListener() {
EDITED:
Put these lines in your 2.2 layout:
<Button
android:id="#+id/golf"
android:visibility="invisible"
android:layout_height="1dp"
android:layout_width="1dp"
android:layout_alignParentLeft="true">
</Button>
With attribute visibility="invisible" your app should work and your layout shouldn't be wasted

Categories

Resources