Problems creating a new screen - android

I am creating a project in which i need to take in some numbers, makes some calculations and then on a new screen create show the answers. I am using an Intent object to go to the new screen:
final Button button = (Button) findViewById(R.id.save);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent();
myIntent.setClass(HelloAndroid.this, screen2.class);
myIntent.putExtra("eFiber", Double.toString(E_fiber));
startActivity(myIntent);
}
});
but when i do this it crashes when i click the button. If i use the same xml file as i do in the first screen then it works just fine, its when i use a different xml file that i have the problems.

Have you registered your second Activity as an Activity in the android-manifest xml?
Under the <application> node, something to the effect of:
<activity android:name=".my.screen2" android:label="#string/app_name"></activity>
With your specific Activity information in place of ".my.screen2"

Related

How to save add button dynamically in android?

The code should add a button dynamically when the user needs it.
When an users click on addService this code takes (a name for button and a value to use in the intent for this button) from a second Activity, then adds the button dynamically in this Activity with the name and intent and the user can click it for service.
How to save the button dynamically added by the user?
Button btn = new Button(MainActivity.this);
btn.setText(buttonName);
layout.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String phone = serviceNum;
Intent e = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(e);
}
});
Include the save button in your layout, then hide it before the view loads. When the user performs the action, simply unhide the button. The layout should handle it automatically for you.

i wanted to start a new activity after pressing the three buttons ...please let me know how can i achieve this?

Hi i am creating a simple educational game in which user has to press three images in order to proceed to the next level and in next level user has to press 5 images.
i have gone through the on click but i using onIntent intent = new Intent(this.getApplicationContext(), Activity.class);
this.startActivity(intent, 0); i am only able to start new activity on single button pressed but i wanted to start new activity when user has finished pressing three image Buttons.
Thanks in advanced.
you can use global int variable and increase it every button click and if its more than your button number open new activity
public int btn = 0;
MyButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(btn >= 2) {
//open your activity
}
else{
btn++;
}
}
});
Based on my understanding of your problem,I have provided a possible simple implementation.
Use a simple data structure like queue or stack. When a image is tapped, add information about the image to the data structure. After adding the information to data structure see if number of items in data structure is equal to 3 ? if yes check data structure has info about the required three images and not just info about the same image (happens if user taps on same image more than once). If condition is met then call startActivity(). Generalize this so that you can reuse the logic in different activities, irrespective of the number of images.
Hi i finally achieved my desired activity. now with this after checking two check box app will start new activity.
`private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
checkBoxListener =new OnClickListener() {
public void onClick(View v) {
if(chkIos.isChecked()&&chkAndroid.isChecked()) {
Intent i1=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
if(chkAndroid.isChecked()) {
Intent i2=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
}
}
}
};
chkIos.setOnClickListener(checkBoxListener);
chkAndroid.setOnClickListener(checkBoxListener);
}
}
`
thanks for all your answers.

Launch new contentView onClick (to obtain new window from another XML layout)

I'm trying to execute a new contentView this way. What am I missing? I get a force close onClick.
final Button btnStatus = (Button) findViewById(R.id.Status);
btnStatus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
startActivity(intent);
setContentView(R.layout.newlayout);
}
});
This is just wrong. You can do one of two things here.
Not recommended but could work for what you are doing
Simply using setContentView(R.layout.newlayout) will set the new layout assuming newlayout.xml is a layout in your layout folder
public void onClick(View v)
{
setContentView(R.layout.newlayout);
}
Recommended
Create a new Activity and set the content in it to this new layout and call that Activity in your onClick()
public void onClick(View v)
{
Intent intent = new Intent(CurrentActivityName.this, NextActivityName.class);
startActivity(intent);
}
});
if you want to change layout then try to,
{setContentView(R.layout.newlayout);}
if you want to change activity, then try to intent to forward another activity, and check also to entry of this activity in manifest file in android.

In Android: How do you show layout twice using intent?

Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView

How to connect two activities

I have made one screen with two images and I would like to add a button lower on the page which will navigate to a second page when I click it. Do you know how to code this? I know how to create a button but I don't know how to connect the two screens!
This task is accomplished with the startActivity(); method using Intents.
Intent i = new Intent(FromActivity.this, ToActivity.class);
startActivity(i);
In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.
Make sure that you add your second Activity to the manifest also (it resides in the tag)!
<activity android:name=".ToActivity"
android:label="#string/app_name">
</activity>
To sum it up:
ImageView myImage = (ImageView) findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
#Override
onClick(View v) {
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}
}
);
Intent intent = new Intent(currentActivity.this,nextActivity.class);
this.finish();
startActivity(intent);
Button start_button=(Button)findViewById(R.id.btnsend);
start_button.setonClickListener(new onClickListener( ){
#override
onClick(View view){
Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);
}
}
);
Lets break the answer in two parts, XML & JAVA part as every activity has each of these two. Assuming we are having only two activity, 'Activity1' being the one with the button which would redirect user to 'Activity2'.
As we have 2 activity, we would be having 4 files related for these 2 activity.
XML
so lets first do the easy way, as soon as you open the .xml file of Activity1, you should shift to design tab.
After reaching the design part you can insert a button from pallet, now you can select button which is inside your layout. After selection you could see the properties of button in right section of your screen where you can effectly change multiple properties of the button.
Here you shall find the "onClick" option, fill the box next to it with anything very simple, or something which you can remember. For example enter "nextAct"
or
Hard way would be entering the onClick property manually by typing follwing line in button code in XML
android:onClick="nextAct"
This is all on XML part.
JAVA
Open the .java file of Activity1, here you have to make a new method. This method should be named same as in the "onClick" property of button. Over here i would be taking "nextAct" as that is what i had used in XML. You can place this new method anywhere inside the class of the java file, i prefer keeping it at the end of the class as i could easily locate it if any issue in future.
Now you have to write the body of nextAct method. This can be sumed up in these two lines
public void nextAct(View v){
Intent i = new Intent(this, Activity2.class);
startActivity(i);
}
After this both should be connected and working fine.
give id to your button and mention it in your MainActivity.class.Then you can call OnClickListener to listen to your click.
Button mButton = (Button)findViewById(R.id.buttonid);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//you can use anything in place of i
Intent i = new Intent(MainActivity.this, NextActivity.class);
startActivity(i);
}
});

Categories

Resources