This is the code I am using
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
showVoiceText.setText("I am working");
}
However it seems not to be called... It never displays "I am working"
This is my xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.phoenix.coreai.HomeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Core A.I."
android:id="#+id/showVoiceOutput"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable voice control"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="167dp" />
</android.support.constraint.ConstraintLayout>
This is not my main activity, my main activity is just an intro playing a small mp4 file
This is the main activity code
package com.example.phoenix.coreai;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView view;
private static int SPLASH_TIME_OUT = 2700;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (VideoView) findViewById(R.id.videoView);
videoPlay(view);
new Handler().postDelayed(new Runnable(){
#Override
public void run(){
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
}
public void videoPlay(View v){
String videoPath = "android.resource://com.example.phoenix.coreai/" + R.raw.spashscreen;
Uri uri = Uri.parse(videoPath);
view.setVideoURI(uri);
view.start();
}
}
and this is the xml of the main activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context="com.example.phoenix.coreai.MainActivity">
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="-27dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp">
</VideoView>
</LinearLayout>
I cannot debug the application through my mobile it has a custom rom and android studio wont recognize it
Let's assume that I would like to call this void from onCreate
private void musicScan(){
File f = new File(path);
File file[] = f.listFiles();
fileArray = new String[file.length];
for (int i = 0; i < file.length; ++i){
fileArray[i] = file[i].getName();
}
}
It does not seem to execute the void that I am calling
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
TextView showVoiceText = (TextView) findViewById(R.id.showVoiceOutput);
showVoiceText.setText("I am working");
}
[maybe you need to remove the first "TextView" if it is already declared in another place, but since we don't have the whole code...]
But still will be good to know how your main activity start/call this activity
I think the problem is that you declared a global TextView and called it showVoiceText but you never initialise it
the solution is to initialise it after the setContentView like the following:
showVoiceText=(TextView) findViewById(R.id.showVoiceOutput);
Related
When I am trying to run my app on my phone it is getting successfully installed in my phone but I am getting white screen instead of my splash page . Attaching my xml code and java code for your reference . Your help is highly appreciated !!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:fontFamily="sans-serif-black"
android:text="MY APP"
android:textColor="#color/Black"
android:textSize="50sp" />
</Relativelayout>
activity_main.java
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
finish();
}
}
The finish() method is called and the activity destroys and returns to the home screen.
Your code should be like this
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
remove the last line which is :-
finish();
This question already has answers here:
How to start new activity on button click
(28 answers)
How do I pass data between Activities in Android application?
(53 answers)
Closed 1 year ago.
I am new to this Java programming and android development and still just working with Hello World App while following a youtube video Series.
Youtube tutorial link
Need to change the interface from send Messeage Button press to Welcome Message screen.
My project have two Java claases
MainActivity.java
package com.example.helloworldapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Specify the onclick method for the button
// Access modifier must be public
// Method name is send Message
// Specify parameter and object of view class
//WHen user click the button system will invoke this method
//With in the method have to create newly created activity
//TO create a new activity have to create a object of intend
// intent is intention of doing something by the android application
// E.g. Start a new activity start a new service broadcast a message
public void sendMessage (View view){
//TO create a new activity have to create a object of intent
//Have to pass 2 parameter 1. context and 2. class name of the target activity
// Context name is this, target activity is MessageActivity
Intent intent = new Intent(this,MessageActivity.class);
//To start the activity have to call the method call start activity
//Then pass intent parameter
startActivity(intent);
}
}
MessageAcitivity.java
package com.example.helloworldapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="sendMessage"
tools:context=".MainActivity">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="156dp"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="154dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="#string/button_label"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MessageActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/welcome_message"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
You need to handle your button-click listener to navigate to the next screen.
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(v -> openMessageAcitivity());
}
public void openMessageAcitivity (View view){
String message = "Your message";
Intent intent = new Intent(this,MessageActivity.class);
intent.putExtra("STRING_YOU_NEED", message);
startActivity(intent);
}
}
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_YOU_NEED");
}
}
The button click not working could be solved via declaring sendMessage in the layout activity_main.xml in the Button code inserted android:onClick = "sendMessage"
If someone can elaborate on the above it will be very helpful since it is not mentioning in the tutorial itself. Onlick attribute in the layout has alrady as sendMessage as instructed in the tutorial.
I'm using a SlidingSplashScreen library by #Chabbal to slide between images and to show small dots below the images to keep track of the image I am showing. And using a fullScreenActivity to show the image in full screen. I want to show the pressed image in full screen but what happens that the fullScreenActivity starts from the first image not the one i pressed on it. here is my code the
MainActivity.java
package com.hodhod.testproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.chabbal.slidingdotsplash.OnItemClickListener;
import com.chabbal.slidingdotsplash.SlidingSplashView;
public class MainActivity extends AppCompatActivity {
private int[] images = {
R.drawable.heart,
R.drawable.pexels,
R.drawable.download
};
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SlidingSplashView slidingSplashView = findViewById(R.id.sliding_splash_view);
slidingSplashView.setImageResources(images);
slidingSplashView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onPagerItemClick(View view, int position) {
Intent myIntent = new Intent(MainActivity.this, FullScreenActivity.class);
myIntent.putExtra("imagesId", images);
myIntent.putExtra("position", position);
startActivity(myIntent);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<com.chabbal.slidingdotsplash.SlidingSplashView
android:id="#+id/sliding_splash_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true">
</com.chabbal.slidingdotsplash.SlidingSplashView>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="150dp"
/>
</RelativeLayout>
FullScreenActivity.java
package com.hodhod.testproject;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.chabbal.slidingdotsplash.SlidingSplashView;
public class FullScreenActivity extends AppCompatActivity {
int[] imageId;
int position1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen);
final SlidingSplashView slidingSplashViewFull = findViewById(R.id.sliding_splash_view_full);
Intent myIntent = getIntent();
Bundle bundle = myIntent.getExtras();
if(bundle != null){
imageId = bundle.getIntArray("imagesId");
position1 = bundle.getInt("position");
if(position1 == 0){
Toast.makeText(getApplicationContext(), "image1", Toast.LENGTH_SHORT).show();
}
if(position1 == 1){
Toast.makeText(getApplicationContext(), "image2", Toast.LENGTH_SHORT).show();
}
if(position1 == 2){
Toast.makeText(getApplicationContext(), "image3", Toast.LENGTH_SHORT).show();
}
slidingSplashViewFull.setImageResources(imageId);
}
}
}
fullscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.chabbal.slidingdotsplash.SlidingSplashView
android:id="#+id/sliding_splash_view_full"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.chabbal.slidingdotsplash.SlidingSplashView>
</LinearLayout>
I'm following this guide: https://developer.android.com/training/basics/firstapp/starting-activity.html
And trying to send a message between activities.I want the user to enter text in the main activity, press send and have it show up in a different activity. For some reason, after I play the app on my phone and press the "send" button that's supposed to transfer intent and message content to the next activity - it just shows a blank activity.
I think that I may not have needed to change content_main, as I got a little confused with this xml file and activity_main and was not sure where each chunks of code should go.
Current code:
MainActivity.java
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
activity_display_message.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: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="com.example.myfirstapp.DisplayMessageActivity"
android:id="#+id/activity_display_message"
>
</RelativeLayout>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
Edit
public class DisplayMessageActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity {
in question.
In the new Activity,i.e DisplayMessageActivity.java retrieve those values:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString(EXTRA_MESSAGE);
//The key argument here must match that used in the other activity
EditText editText = (EditText) findViewById(R.id.edit_message);
editText .setText(value)
}
}
Use this technique to pass variables from one Activity to the other.
You have to declare a TextView inside your activity_display_message.xml
<TextView
android:id="#+id/show_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then in your DisplayMessageActivity.java
public class DisplayMessageActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
String message = bundle.getString(EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.show_message);
textView.setText(message);
}
}
If you want to get the extraString in DisplayMessageActivity, that you have sent from previous activity MainActivity try this
// try this in onCreate() of DisplayMessageActivity
String myExtraString = getIntent.getStringExtra(EXTRA_MESSAGE);
if(myExtraString == null )
{ // no extraString was attached to the intent
}
I am trying to add a video played on button click with android studio.
However, when I click the button a "sorry, this video cannot be played" message box appears on the emulator screen.
Can you help me see where I'm going wrong.
Below is the code I approached the goal with
Trialvideo.java
package android.com.trialvideo;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class TrialVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** // Video view: to view our video
VideoView video = (VideoView) findViewById(R.id.surface_view);
//set video path to our video(in this case man-cheetah-gazalle.3gp)
video.setVideoPath("/raw/jeewan.mp4");
video.start();
**/
final Button play =(Button)findViewById(R.id.play);
play.setOnClickListener(new OnClickListener(){
public void onClick(View V){
videoPlayer();
}
});}
public void videoPlayer(){
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = (VideoView)findViewById(R.id.surface_view);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoPath("/TrialVideo/raw/lic.3gp");
videoHolder.requestFocus();
videoHolder.start();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
Hi try the following code:
VideoPlaying.java
public class VideoPlaying extends Activity {
private MediaController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.requestFocus();
vd.setVideoURI(uri);
vd.start();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/VideoView"></VideoView>
</LinearLayout>
place the video on the raw folder and run the code. Sometimes video will not be correctly shown on the emulator, try to check it also on the actual device.
Android does not have a C: drive. You need to put the video file on the device (e.g., copy it to the device's external storage), then supply VideoView with an appropriate path to the file (e.g., using Environment.getExternalStorageDirectory()).