Changing Activity on a clickable layout - android

I have this clickable LinearLayout view, im trying to have it change Activity when clicked by every time i click the object i get a error.
final LinearLayout lindet = (LinearLayout) findViewById(R.id.detials);
lindet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(SellingActivity.this, DetailsActivity.class);
startActivity(i);
finish();
}
});

Did you remember to add the DetailsActivity to the AndroidManifest?

Supporting Macarse I would add that use SellingActivity.this.finish() instead of finish()
I believe this will prove to be your problem solver.

Related

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);

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

Displaying ImageButtons in an Activity

I want to display ImageButtons like in an activity in my android application.
Now when i click on any one of the ImageButton, it should start a new Activity.
I don't know whether this type of view is possible or not. ?
This will help you,
http://www.inter-fuser.com/2010/01/android-coverflow-widget.html
Also this one
http://stackoverflow.com/questions/5116825/android-circular-gallery-or-listview-with-zoom-in-and-out-option/5116945#5116945
You should check the Gallery widget.
yah that's possible.like an button clck listener there is also a listener for the image button .by that calling the another activity a
You can try the below code:-
ImageButton imageButton = new ImageButton(getApplicationContext());
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("<activity_class_name or intent action");
context.startActivity(intent);
}
});

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