Opening a new activity on button click - android

I have looked at every example of switching between activities and I always get the same result. The app bombs.
As far as I can tell if you have a java class that populates the content of a layout then in order to switch to the other layout, you must 'link' to the java file which in turn will open the setContentView(R.layout.whatever);
When I try to do this, like I say my app bombs out. My code is as follows:-
FROM Java class:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.goesnews);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), ac2.class);
startActivityForResult(myIntent, 0);
}
});
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
TO java file (ac2)
public class ac2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}}
Can anyone help out here?

Try to remove the zero from this line:
startActivityForResult(myIntent, 0);
like this and change it to just:
startActivity(myIntent);
and change this line:
Intent myIntent = new Intent(view.getContext(), ac2.class);
To this:
Intent myIntent = new Intent(firstActivityName.this, ac2.class);
because you are getting here the context of the button and not of the activity.

The solution was simple and was based on the response from VSK (Thanks) with a little tweak.
The ImageButton required :-
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:onClick="move" />
Java required:-
public void move(View v)
{
Intent myIntent = new Intent(yourclass.this, ac2.class);
startActivityForResult(myIntent, 0);
}
VSK - please note '0' attribute of startActivityForResult
Thanks all

try this way
add onclick function to your button in xml file
like this
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:onClick="move" />
and in ur java file
make a function move like this
public void move(View v)
{
Intent myIntent = new Intent(yourclass.this, ac2.class);
startActivityForResult(myIntent,0);
}

Related

I want to use clickable text (Android)

I want to use clickable text to move from main activity to another activity, can somebody give me a sample code to understand that how this works.
TextView textview = (TextView) findViewById(R.id.textView);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,AnotherActivity.class);
startActivity(i);
}
});
In the layout of your activity, define the onclick on the textview item.
<TextView ....
android:onClick="onClick"
android:clickable="true" ... </TextView>
OnClick Method
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);

Android colouring app multiple background images

I am creating an android studio colouring app and need to have it so that the user can select which image they want to colour. I want it so that they click on an image button on one activity and this changes the background of the next activity that the button takes them to however I have no idea how to go about this. Any help would be greatly appreciated. Thankyou
From what I understand, you want to change the color of the next activity on button click of current activity. So, what you can do is:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredColor to get the color according to your logic
intent.putExtra("color", getDesiredColor());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("color")){
rootLayout.setBackgroundColor(intent.getExtra("color"));
}
...
}
Let me know in case you have any doubts.
UPDATE:
With drawable your code will become something like this:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("drawable")){
rootLayout.setBackground(intent.getExtra("drawable"));
//Or if you are using ImageView in your root layout to set the background image (I'm using Picasso here):
//Picasso.with(mContext).load(intent.getExtra("drawable")).into(myBackgroundImageView);
}
...
}
UPDATE 2:
You can have a hashmap mapping each imagebutton with a drawable.
e.g.
HashMap<Integer, Integer> mViewIdToDrawableMap = new HashMap<>();
mViewIdToDrawableMap.put(mImageButton1.getId(), R.drawable.image1);
mViewIdToDrawableMap.put(mImageButton2.getId(), R.drawable.image2);
mViewIdToDrawableMap.put(mImageButton3.getId(), R.drawable.image3);
public int getDesiredDrawable(View view){
return mViewIdToDrawableMap.get(view.getId());
}
How will you call this function:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable(view));
mContext.startActivity(intent);
}
});
Now, your last question what is rootLayout?
Lets say your activity2 where you want to show this image has somehitng like this as layyout:
<RelativeLayout>
<ImageView
...
android:id="id+/background_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
...
/>
</RelativeLayout>
In your Activity2's onCreate, do somehting like this (after getting the drawable as I explained earlier):
//Here mBackgroundImageView is the background_imageview in your layout
Picasso.with(mContext).load(drawable).into(mBackgroundImageView);

create buttons in java

for an exercise I have to create some buttons. When clicked, these buttons should make appear an animated imagine.
Can you helo to build the java code?
I just created the buttons and the ImageView, but I don't understand if I have to build one or two activity.
the buttons and the imageview have to stay in the same page: for example, when I click the "apple" button, the image view must show an apple, when I click "orange" it shows an orange, etc... all in the same screen.
my java code appear like this:
public class HomeWork extends Activity {
public static final int GET_CODE=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work);
Button getResultButton = (Button) findViewById(R.id.btn1);
getResultButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent resultIntent =new Intent();
resultIntent.setClass(HomeWork.this,HomeWork2.class);
HomeWork(resultIntent, GET_CODE);
}
but the onClick gives me an error
> Intent resultIntent =new Intent();
> resultIntent.setClass(HomeWork.this,HomeWork2.class);
> HomeWork(resultIntent, GET_CODE);
I don't cathes what are you doing in this. :)
if you need to open HomeWork2 Activity call
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startAcrivtyForResult(resultIntent, GET_CODE));
ok, I deleted the GET_CODE (I really don't know why I put it), and now my code appears like this:
ACTIVITY ONE:
public class HomeWork extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work);
Button getResultButton = (Button) findViewById(R.id.btn1);
getResultButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startActivity(resultIntent);
}
});
}
ACTIVITY TWO:
public class HomeWork2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work2);
ImageView img=(ImageView)findViewById(R.id.imageView);
img.setBackgroundResource(R.drawable.apple);
}
ACTIVITY ONE LAYOUT XML
<Button
android:id="#+id/btn1"
android:layout_width="108dp"
android:layout_height="32dp"
android:onClick="btn1_click"
android:text="Apple" />
ACTIVITY TWO LAYOUT XML
<Button
android:id="#+id/btn1"
android:layout_width="108dp"
android:layout_height="32dp"
android:onClick="btn1_click"
android:text="Apple" />
no errors, but when I start the emulator, it give me the error "Unfortunately, Homework has stopped"
i watch in the logcat but I don't know what I had to change...

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.

Linking xml pages with layout

Excuse the simplicity of this request - but is there a way for me to link onClick commands for buttons through the Layout rather than code. I am trying to create a simple app and I want to be able to make buttons and have clicks go from one "page" to the other.
Is there a site that might overview how to use the UI to code for droid?
Thanks!
Make how many ever buttons that you would like to have. Make sure if its more than the screen amount you must assign a scroll view.
assume that you have on your R.layout.main.xml. To find this go to res/layout/main.xml
Copy XML, rightclick and paste it in the layout folder. Then it will say Rename. Rename it to what ever you want.
Now copy this specifically so lets say you put
btn1
btn2
btn3
btn4
btn5
To make the id tag go to the properties on each button and scroll to id and rename the ending to btn1 , 2,3, 4, and so on.
Now, if you were to place that in your .java file under src/com.whateveryounamed.app/what ever you named .java
Place this code below and it will work. Below is a example of mine with 5 buttons in xml page.
package com.nashvillekurds.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class kurdishhistoryapp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 =(Button)findViewById(R.id.btn1);
Button btn2 =(Button)findViewById(R.id.btn2);
Button btn3 =(Button)findViewById(R.id.btn3);
Button btn4 =(Button)findViewById(R.id.btn4);
Button btn5 =(Button)findViewById(R.id.btn5);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn1);
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn2);
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn3);
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn4);
}
});
btn5.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn5);
}
});
}}
hope this helped if not sorry but your doing something wrong ...
What james newton said involves multiple buttons. this is how it goes with one button, just for clarity:
Button btn1 =(Button)findViewById(R.id.~btn1~);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.~btn1~);
}
});
Besides that nothing needs to be changed, just change the button id and xml id to work with your app. Button btn is just to label it to the system, so you should be keep that, and
btn1.setOnClickListener... can be kept as well. Just change the parts within the ~ . Make sure to change the ~ too! I probably wouldn't without a warning, and some others wouldn't either.

Categories

Resources