I have an app in android in which I wanna achieve the following thing:
I want to click on an image on android mobile through python script.
Does anyone know how could I achieve that?
You can create a layout with a single button. Set the background of the button as the image you want. Make the button width and height to match parent. Then register the button in a normal way to start the activity. So something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:id="#+id/button1"
android:layout_height="match_parent"
android:background="#drawable/background">
</Button>
</LinearLayout>
With your activity like this:
public class ActivityA extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final Intent intent = new Intent(getApplicationContext(), ActivityB.class);
startActivity(intent);
}
}
When the user presses "back", he or she will go back to the giant button activity.
Please try this whenever you want to move from one activity to another activity..
Intent myIntent = new Intent(CurrentActivity.this,NextActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
Above code will take you from CurrentActivity to NextActivity....
Whenever you want that to happen just execute those three statements,
that will do your work....
Related
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);
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...
I'm new to Android development, and to this site!
I have done a few tutorials etc and am working on a project at the moment, and had a good look through other answers to similar questions, but haven't been able to find quite what i'm looking for (but loads of good suggestions!)
I am trying to get buttons on my main screen linking to individual pages. I am using my phone instead of an emulator, but every time i click on a button, the app dies... can you help me?
This is my main Screen code for button1:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//calling the page1 function
page1(view);
}
});
This is the page1 function:
public void page1(View view) {
Intent intent = new Intent(this, Page1.class);
startActivity(intent);
}
Here is the code for the Page1 class file:
public class Page1 extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_1);
}
}
This is the code for the layout file: (page_1.xml)
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"
tools:context="shannon.white.finalyear.DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
If you need anything else, let me know
Any ideas?
Thanks :)
Your coding looks to be correct.
The next thing to check would be to look inside your AndroidManifest.xml file to ensure you have added the activity to it so the android OS knows it exists. You add it like so:
<activity android:name="Page1" />
If your activity resides in a different package then the one declared inside the manifest file, then you need to specify the full package inside the "name" like so:
<activity android:name="some.other.package.name.Page1" />
Thats about all i can say from the provided code. If you are simply starting another activity which is Page1.class then your code looks correct and you might be missing the manifest declaration as i stated above.
Try moving this following code
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
underneath your page1 function so your code looks like this:
public void page1(View view) {
Button student1 = (Button) findViewById(R.id.button1); // Declaring and defining the buttons used
student1.setOnClickListener(new View.OnClickListener() { // Setting the onClickListener for button1
#Override
public void onClick(View v) {
startActivity(intent);
Intent intent = new Intent(this, Page1.class);
}
}
}
and your onCreate look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1(view); // NOTE I'm now declaring it on the onCreate instead of onClick
}
If that doesn't help, well your code still looks cleaner. Could just be my OCD though...
This is a very good tutorial by Mkyong on how to achieve what you are trying to do. If no other answers help, restarting using this tutorial will likely help you succeed. On multiple occasions I've tested his code and its worked.
I managed to have a button and then when I click on it, I go to a new activity that is called "TUTORIALONE"
and then I want to display some text in this new activity
so I have something like this
Button b = (Button) findViewById(R.id.tutorial1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("my.android.TUTORIALONE"));
TextView tv = (TextView)findViewById(R.id.tutorial1);
tv.setText("this is some text);
}
});
the problem is that it first displays the text on my button, and then it shows me the new activity, how would I achieve displaying the text on the new activity?
thanks in advance
In your TUTORIALONE activity you probably have an associated xml file for displaying content. Perhaps it iss set something like this
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.TUTORIALONE);
}
In the layout xml file for TUTORIALONE just add something like this
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
EDIT
To change the text of this TextView, do something like this in your TUTORIALONE activity.
protected void onStart()
{
super.onStart();
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("this string is set dynamically from java code");
}
Note that the id here (R.id.text) is the same as in the xml file ("#+id/text")
In the onCreate() of the new Activity, you can create a Button, set the text and then call setContentView() in order to show it.
In case you want to show to this Activity a string from the current Activity, you can pass this String an an Intent extra and then recover it to the new Activity.
Hope this helps!
I have a footer in all the layouts for a android application.
The footer will have Image buttons like "Help", "Home", this Image buttons directly link to Help class and Home class.
Can I have a one single activity class for all the footer Image buttons.
I tried with
public class FooterItems extends Activity implements OnClickListener {
#Override
public void onClick(View view) {
if(view.getId() == R.id.footerBtnHome)
{
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
return;
}
if(view.getId() == R.id.footerBtnFeedback)
{
Intent myIntent = new Intent(view.getContext(), Feedback.class);
startActivityForResult(myIntent, 0);
return;
}
}
}
but I am not getting how to call these in a class... for example the project is having MainActivity class in which I have
ImageButton buttonFeedback = (ImageButton) findViewById(R.id.btnFeedback);
buttonFeedback.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Feedback.class);
startActivityForResult(myIntent, 0);
}
});
When I call Feedback.class with onClick from one Image Button... layout and same footer items appears.
I want to use the generalised FooterItems class so I can have one class for footer and use in every other layout.
I am also using android:onClick="onClick" in xml for Image Buttons for footer only.
But how to call those generalised class FooterItems and make it work.
Looking forward to the reply.
thanks.
I can suggest another variant:
I think, you can add to XML android:onClick, for example:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
And when user click this button, android programm call method selfDistruct. You just have to implement this method. Android tutorial: http://developer.android.com/reference/android/widget/Button.html
What you are asking is not much clear.
You want to have Help, Home, etc. Image Buttons as common to all layouts in your application correct ?
If you click any Image Button, you have to show the layout on top and these buttons also has to appear on screen??
If yes my answer may help you.
You told i will create footer items as one activity, but its not good. I will prefer ViewFlipper in this case. See the layout.
<LinearLayout vertical>
<ViewFlipper id=vf>
<include layout1 />
<include layout2 />
<include layout3 />
</ViewFlipper>
<LinearLayout horizontal>
<ImageButton button1 />
<ImageButton button2 />
<ImageButton button3 />
</LinearLayout>
</LinearLayout>
Initially you will get layout1 and all image buttons on screen. If you want to show layout2 when you click button3 write onClickListener as below.
ViewFlipper vf = (ViewFlipper)findViewById(R.id.vf);
The variable vf is used to change layouts.
button3.setOnClickListener(new View.OnClickListener() {
public void onClick() {
vf.setDisplayChild(1);
}
});
I hope it may help you. Bye.
I am sure you want to implement Footer view with 2 buttons: Help and Home, this should be at bottom of every activities.
If you want to implement a code for once then follow the below steps:
Define a footer layout with 2 buttons, define android:onClick="btnHelp" for help button and android:onClick="btnHome" for home button.
Now you can include this layout inside any activities by using <include>.
Define a base activity with below 2 methods.
Now extends this base activity wherever you implements this footer layout.
public void btnHelpClick(View v)
{
// do your task for Help
}
public void btnHomeClick(View v)
{
// do your task for Home
}