I'm a beginner in android developing , I tried to make many activities ( screens ) , when clicking the first button it takes me to the second screen , but when I click on the button of the second screen to move to the third screen the app won't work !
This is my XML code :
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:background="#drawable/fal"
android:onClick="kahwah" />
and this is how it is in java :
public void kahwah(View V)
{
Intent i2 = new Intent (this,Third.class);
startActivity(i2);
}
You need to mention your activities in Android manifest file of application tag like:
<activity android:name="com.your_package_name.YourActivity" />
in the application tag.
Related
I have created an emergency pager as my first Android app. Some users are reporting that they can't turn off the pager sound and they have to stop the app completely. I think this may because in the heat of the moment they are sliding their finger across the Silence Button and therefore creating a swipe instead of a touch.
The button on my Activity XML file is
<Button
android:id="#+id/button_gotit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textColor="#color/white"
android:onClick="gotit"
android:text="#string/silence"
android:textAlignment="center"
android:textSize="24sp" />
and the relevant part of the corresponding Java file is
public void gotit(View view) {
vibrator.cancel();
ringtone.stop();
Intent intent = new Intent(this, SummaryActivity.class);
intent.putExtra(EXTRA_MESSAGE, inc_summary);
startActivity(intent);
}
How can I add in "swipe" (and possibly "fling", which I'd never heard of until today) so that the button responds whatever the user does to it?
Ok y'all, I'm trying to learn here. I just learned how to create a basic appWidget and it works so far. Now try as I might, I do not understand how to add an image button to my appWidget. I mean it's been physically added to the xml file named simple_app_widget.xml. What can't figure out is how do I add an onClick event to the SimpleAppWidget.java file? I have tried it like a normal one click event, but AIDE tells me that it doesn't recognize the info being input.
Please don't just do my work for me. I want to learn, but my search skills are lacking and what I do find is irrelevant or not at me skill level.
as you said... to add an image button first must add the image in your drawable folder ( search it in your proyect tree), then go to the layout where want to use it and write your code like this (note : in, android:background =" " must enter the same name of your image copied in drawable folder):
<ImageButton
android:id="#+id/arrow"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="4dp"
android:background="#drawable/arrow"
android:contentDescription="#string/test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.902"
app:layout_constraintStart_toStartOf="parent" />
After it, go to your Activity.class and type your code:
public class MainActivity extends AppCompatActivity {
ImageButton test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here should put what you want to show or where you want to go after click the button.
//For example if you want to go to the next activity use Intents like this:
Intent intent = new Intent(this, MainActivity1.class);
startActivity(intent);
finish();
}
});
}
}
So this is my first app and I am trying to code and need some help with buttons. After searching for a answer I just couldn't find one that I understood. I want to be able to make different pages for the app and make imagebuttons that link to these pages. This is the very basic code I have at the minute for my button. Please try to explain where to put the code etc.
Thanks in advance.
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:background="#drawable/home_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:nestedScrollingEnabled="true" />
</RelativeLayout>
Since this is your first app, let's start it simple with using only Activities.
You start with a MainActivity, which shall contain your ImageButtons. By clicking on one of these buttons, you will be directed to another activity. If you press the back button, you will arrive back at your MainActivity.
I shall demonstrate some code which shows you how to navigate from one activity to another one. First add the two activities so your AndroidManifest.xml will look something like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second_activitity" >
</activity>
If you're using AndroidStudio, it will do this for you when you create a new activity.
Your MainActivity.java will look something like this:
public class MainActivity extends Activity {
//Define your views
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find your views
imageButton = (ImageButton) findViewById(R.id.image_button);
//Assign a listener to your button
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Start your second activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
Repeat these steps for every Activity you want to add to your application.
For more information, you will find the Android Docs an usefull source. Please check this link out as a start.
Good luck!
I dont think its a question to be questioned ! Though,make your desired button in your main .xml file and the using java access the button and apply the task which you want to perform from that button ..
You use this in .xml to make a button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
/>
Here's java code for accessing this button
private Button button;
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override public void onClick(View view) {
//do what you want here
}
});
}
I'm trying to Make a little app which when I click on a button it makes a call and after few seconds (in the same default dialer activity) it displays automatically another number in the same default dialer activity.
I tried few ways but it didn't work !
there is my code for now :
Intent localIntent5 = new Intent(Intent.ACTION_CALL);
localIntent5.setData(Uri.parse("tel:0153204255"));
startActivity(localIntent5);
//wait a few seconds
localIntent5 = getIntent();
localIntent5.setData(Uri.parse("tel:71 0651755275%23"));
startActivity(localIntent5);
For now it just call the first number
Is it possible ? Thank you
you can use auto link for this purpose
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="phone"
android:text="#string/phoneValue"/>
I have a hard time switching from one activity to another using intent. My app is almost done, I have there several activities and I am able to switch between them with intent. I have added one more activity to my project (in Eclipse: File ... New ... AndroidActivity). As I was used to, it created .java and .xml file. In my main activity, I have defined my button (xml) and in java class method to perform on buttonClick. Everything seems fine, but when I click my button nothing happens. What could be wrong?
Here is defined my main activity class called "Ponuka":
package com.example.s_home;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class Ponuka extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setBackgroundDrawableResource(R.drawable.pozadie);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ponuka);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_ponuka, menu);
return true;
}
// switch to activity: Osvetlenie --- works fine
public void obrOsv(View view) {
Intent intent = new Intent (this, Osvetlenie.class);
startActivity(intent);
}
// switch to activity: Kurenie --- works fine
public void obrKur(View view) {
Intent intent = new Intent (this, Kurenie.class);
startActivity(intent);
}
// switch to activity: Ochrana --- this isnt working
public void obrBez(View view) {
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
System.out.println("ok"); // It will not even print out "ok"
}
}
Here is activity_ponuka.xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>
One more note: When my new activity called "Ochrana" was created, I had to manually update the R.java file. What did I do wrong here?
You shouldn't use System.out.println on Android, rather log the message using the Log class, then you can find it in LogCat. System.out.println might get redirected to LogCat too, but it's not guaranteed. Why doesn't "System.out.println" work in Android?
Is your problem that it starts a different activity than you want or doesn't it start one at all? Because you're creating an intent with Kurenie.class where you obviously want to start the Ochrana activity (according to the comment above the obrBez() method).
PS: Zdravim z Brna. :)
You should never manually update the R.java because it automatically updates itself if your code is correct.
You also need to get an instance of your button in your activity and add a OnClickListener to call the methods that switch the activity.
And the last thing you need to do is to add those activities you want to use in your manifest file.
Probably unrelated to your issue but you're calling the wrong Activity in your new button event:
public void obrBez(View view) {
Intent intent = new Intent (this, Kurenie.class);
I would recommend using setOnClickListener() for the buttons instead of the android:onClick in the XML - XML ment for layout and design and java activity files ment for logic, mixing the two can cause confusion. You can also try that for solving your problem (Although I don't see any problem with your code):
Button btn = (Button) findViewById(R.id.bezpecnost);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Assuming you want Ochrana activity from your comment
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
}
});
Two more things that won't solve your problem but might help you:
You might want to define Intent intent; for the entire activity class and than create new intent on the same variable to save memory allocating.
And last thing - give your XML file [Ctrl]+A and than [Ctrl]+i to automatically order the spacing - it will do only good (:
P.S.
If you have any problems with your R file - just delete it! Eclipse auto re-create it immediately - solving all kinds of R bad/not updated...
The answer is in your XML layout.... look closely...
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
The comment should rather be in this format <!-- .... //-->
You left out the two forward slashes - that's why nothing is happening!
Edit:
Eclipse would have shown the entire block commented out highlighted in colour green instead of XML syntax highlight colour!
In regards to R.java - that is generated at compile time, so whenever you make changes to your XML layouts, the Android SDK will re-generate the R.java file each time!
The AndroidManifest.xml should have the lines specifying the activity within the tags application, as in:
<activity android:name="com.example.s_home.Ochrana"
android:label="Ochrana Activity"/>
Your xml is all wrong. Your button with "#+id/pohod" is mixed up with button "#+id/bezpecnost" because you missed android:layout_below="#id/lin1" in your second linear layout. I tested the layout below and it is OK. I change to android:text because I have no drawable.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ochrana"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:layout_below="#id/lin1"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>