Application crashes on clicking a link in TextView - java

I am working on an application called Android Glossary. In my second activity I have created a TextView which has a link embedded in it. Everything seems fine; the link is highlighted in blue. The problem is that when I click on the link, my application crashes down. I don't know what is wrong. Please help.
Code in my second activity :
package com.mavenmaverick.androidglossary;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class CActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
Intent browser= new Intent(Intent.ACTION_VIEW, adress);
startActivity(browser);
}
});
}

see your code
you have enter url like
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
replace with this
Uri adress= Uri.parse("http://www.cyanogenmod.com");
problm is ; in Http; replace with http:

you should use setData()
String url = "http://www.cynogenmod.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browser = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cyanogenmod.com"));
startActivity(browser);
}
});

Related

Open Another Activity in Android

I am new to Android Development and I have this problem of opening another activity from the main activity.
The problem is that whenever I click on the button, the app closes.
Below is my Java code.
package com.Notely.SplashScreenandAccounts;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ActivityWelcome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Button signup_btn = (Button) findViewById(R.id.signup_btn);
signup_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, ActivitySignup.class);
startActivity(intent);
}
}
I need suggestions on what is the problem and on how to rectify that.
You should add your Activity(ActivitySignup.class) in your
AndroidManifest file.
Like this..
<Activity android:name = .ActivituSignup android:theme="AppTheme"/>
i hope this will help you .
If it not works you should add this line in your ActivitySignup.java at on create before super.onCreate()
setTheme(R.style.AppCompat);
send your logcat error to understanding best.
You should take a tour of Activity in
android.developers.com web

Data passing between two activities repetitively

I'm a beginner in android studio and i want to write a code with 3 activities.
1st is for starting the app.2nd is for showing an English word and 3rd is for showing the English word and a description of it in two texts.
I want to transfer data of English word itself and its description and the number of the word to show the words one by one.
I wrote the code with help of tutorial clips but it ain't work and in 3rd activity shows nothing.
these are my codes:
main activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view)
{
Intent i = new Intent(this,ActivityOne.class);
startActivity(i);
}
}
ActivityOne
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ActivityOne extends AppCompatActivity {
int a=0;
String E , P;
private Button show;
private TextView Eword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Eword = (TextView) findViewById(R.id.Eword);
show = (Button) findViewById(R.id.show);
}
public void show(View view)
{
Intent intent = new Intent(this,ActivityTwo.class);
startActivity(intent);
int a = getIntent().getIntExtra("counter",0);
Eword.setText(Eng[a]);
E = Eword.getText().toString();
P = Fa[a];
a++;
intent.putExtra("counter",a);
intent.putExtra("EWord",E);
intent.putExtra("PWord",P);
}
private String[] Eng = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
private String[] Fa = {
"Abundance Description",
"Anxiety Description",
"Bruxism Description",
"Discipline Description",
"Drug Addiction Description"
};
}
ActivityTwo
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
TextView Eng , Fa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Eng = (TextView) findViewById(R.id.eng);
Fa = (TextView) findViewById(R.id.fa);
// recieve data from activity one
String EWord = getIntent().getStringExtra("EWord");
String PWord = getIntent().getStringExtra("PWord");
int a = getIntent().getIntExtra("counter",0);
Eng.setText(EWord);
Fa.setText(PWord);
}
public void iknow(View view)
{
Intent myIntent = new Intent(this,ActivityOne.class);
startActivity(myIntent);
int a = getIntent().getIntExtra("counter",0);
myIntent.putExtra("counter",a);
}
public void idknow(View view)
{
Intent myIntentTwo = new Intent(this,ActivityOne.class);
startActivity(myIntentTwo);
int a = getIntent().getIntExtra("counter",0);
myIntentTwo.putExtra("counter",a);
}
}
And it shows this result:
It is the third activity and it can clear the pretext that was set in design page but can not replace EWord or PWord
Can someone help me?????
(1) To get you started, you need to put your variables into the intent before you start the activity. (2). I don't think its a good idea to have activity one start two, which then can start one. It is better to just close second activity which then automatically returns to one. (3) To pass data back to the activity you are returning you need to startActivityForResult https://developer.android.com/training/basics/intents/result.html
activity one starts activity two using startActivityForResult. Activity Two returns an intent (which is the result intent) and activity one recieves this in onActivityResult.
check out this: Android: how to make an activity return results to the activity which calls it?

Android save EditText and recall it next time the app is opened

I have code in which it has an editText box and a button. I want to save the text entered in the textbox and recall it the next time the app is opened.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText txtLink;
Button btnOpenLink;
String defaultLink;
String secondLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
defaultLink = "http://";
secondLink = ".whatver.com";
txtLink = (EditText) findViewById(R.id.editText);
btnOpenLink = (Button) findViewById(R.id.button);
btnOpenLink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String server = txtLink.getText().toString();
if(!TextUtils.isEmpty(server)){
Intent intent=new Intent(MainActivity.this,webactivity.class);
intent.setData(Uri.parse(defaultLink+server+secondLink));
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "Please enter your server name.", Toast.LENGTH_LONG).show();
}
}
});
}
}
How can this be accomplished? I have tried numerous things that I have found through google but none have seemed to work right, I know it is something that I am doing most likely, I just can not make it work right.
Use the preferences....
Example
SharedPreferences spref = getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("myTextViewValue", prefVal); //
editor.commit();
and somewhen latter when the app starts again read it back:
Example:
SharedPreferences preferences = getPreferences(Activity.MODE_PRIVATE);
String storedPreference = preferences.getStr("myTextViewValue", null);
so validate what you store and check if the preference you get is null, that means nothing was stored before...

MediaPlayer.create cannot be resolved to a type

I'm trying to get an mp3 file to play a sound when a button is clicked (Android app). However, despite following tutorials with similar code, I get this error message 'MediaPlayer.create cannot be resolved as a type. I have refreshed the project and cleaned the project and it made no difference. Here is my code:
package com.example.h.a.t;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Hypo_Info extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hypo_info);
final MediaPlayer mediaPlayer = new MediaPlayer.create(this, R.raw.pill_bottle);
Button a = (Button) findViewById(R.id.treatment_hypo);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Hypo_Info.this, Hypo_Treatment.class);
startActivity(i);
mediaPlayer.start();
}
});
}
The problem is the "new" keyword here:
new MediaPlayer.create(this, R.raw.pill_bottle);
because the MediaPlayer.create is a static method.
==> remove the new

Android Fragment Code issue

I'm wondering how I can get the code below to work with my project as to combine the two so that it could work. What I have is an implemented Navigation Drawer in the app
1st Java Code Using Fragment:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PagesFragment extends Fragment {
public PagesFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
return rootView;
}
}
2nd Java Code Using Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AppointmentActivity extends Activity {
Button sendEmail;
EditText msg;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_layout);
sendEmail = (Button) findViewById(R.id.sndBtn);
sendEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
msg = (EditText) findViewById(R.id.msgTxt);
String message = msg.getText().toString();
sendEmail(message);
}
});
}
protected void sendEmail(String message) {
String[] to=new String[]{"Shop#email.com"};
String subject=("Shop Appointment");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Gmail"));
}
}
I've tried to combine the two myself but I don't have a lot of experience dealing with android to know how to make both of these codes work side by side and not give me a force close. Anything would be helpful!
To combine these codes, you need to declare your Activity like a FragmentActivity (it will be "host" your Fragment). See this answer: https://stackoverflow.com/a/10609839/2668136
And Google training Fragment
Also Google documentation FragmentActivity
Hope this help.

Categories