pass intent with extra to defined layout text view - android

I am at the beginning of an app, I'm an absolute amateur and what i want to do is simple.
I am trying to pull Extras from an intent on my first activity into a textview on my second activity
First page: what is your name? (type a name and push a button)
On the next page: okay so your name is ...(the name they entered)
I had an answer to this but had a little trouble understanding it, in the code i have, when i click the okay button just the word okay shows up in the textview rather than the edited text
here is my code....
main.java
Button ok;
EditText name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText);
ok=(Button)findViewById(R.id.button);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String a=ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a);
startActivity(intent);
}
});
}
main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:textStyle="bold|italic"
android:textSize="23sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignBottom="#+id/button"
android:hint="#string/text"
android:textSize="25sp"
android:textStyle="bold|italic"
android:layout_toLeftOf="#+id/button"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:src="#drawable/start"
android:contentDescription="#string/startimage"
android:layout_below="#+id/button"
android:paddingBottom="20dp"
android:paddingTop="20dp"/>
</RelativeLayout>
secondactivity.java
public class secondactivity extends Activity {
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t = (TextView)findViewById(R.id.textView3);
String n = this.getIntent().getStringExtra("NAMEDATA");
t.setText(n);
}
activity_second.xml
<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=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_sent"
android:textSize="23sp"
android:id="#+id/textView2"
android:textStyle="bold|italic"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_to_do"
android:id="#+id/textView"
android:layout_marginTop="13dp"
android:textSize="23sp"
android:layout_below="#+id/textView2"
android:textStyle="bold|italic"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bath_time"
android:id="#+id/button"
android:layout_marginTop="29dp"
android:layout_below="#+id/textView"
android:layout_alignRight="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:textSize="25sp"
android:textStyle="bold|italic"
android:onClick="viewimage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/school"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignRight="#+id/button"
android:layout_alignLeft="#+id/button"
android:textSize="25sp"
android:textStyle="bold|italic"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bed"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignRight="#+id/button2"
android:textSize="25sp"
android:textStyle="bold|italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:paddingStart="4dp"
android:layout_alignParentRight="true"
android:textSize="23dp"
android:textStyle="bold|italic" />
</RelativeLayout>
androidmainfest.xml
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".main"
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" >
</activity>
<activity
android:name=".bathactivity"
android:label="#string/title_activity_bath"
android:parentActivityName="com.example.myapplication.SecondActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapplication.SecondActivity" />
</activity>
</application>
can anyone tell me where I've gone wrong? Think its in my onCreate method on either Activity but really stuck,
many thanks

Actually - you don't pass anything from the EditText widget, you are only passing from the Button
Here lies the problem:
String a=ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a);
You could add the contents of the EditText onto the end of this string in the same way.
Try something like this:
String nameStr = name.getText().toString();
String a = ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a+nameStr);

Related

RegisterActivity does not call activity_main.xml

I'm trying to start this activity_main.xlm after a user's registration flow, but when I click on the button, the app closes. I work a little time with Android but I couldn't identify this error.
I need that after clicking the register button, the app remains pressed and calls activity_main.xml
As Logcat shows, the data passes through the api {"insert":"ok"}, but the application closes and does not call activity_main.
RegisterActivity.java
public class RegisterActivity extends AppCompatActivity {
EditText et_name, et_email, et_password, et_repassword;
Button btn_register, btn_login;
#Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setTitle("REGISTER");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
et_name = findViewById(R.id.et_name);
et_email =findViewById(R.id.et_email);
et_password = findViewById(R.id.et_password);
et_repassword = findViewById(R.id.et_repassword);
btn_register= findViewById(R.id.btn_register);
btn_login = findViewById(R.id.btn_login);
btn_register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(TextUtils.isEmpty(et_email.getText().toString()) || TextUtils.isEmpty(et_name.getText().toString()) || TextUtils.isEmpty(et_password.getText().toString()) || TextUtils.isEmpty(et_repassword.getText().toString())){
String message = "All input required";
Toast.makeText(RegisterActivity.this, message,Toast.LENGTH_LONG).show();
}else {
RegisterRequest registerRequest = new RegisterRequest();
registerRequest.setName_app(et_name.getText().toString());
registerRequest.setEmail_app(et_email.getText().toString());
registerRequest.setPassword_app(et_password.getText().toString());
sendRegister(registerRequest);
}
}
});
}
private void sendRegister(RegisterRequest registerRequest) {
Call<RegisterResponse> registerResponseCall=ApiClient.getService().registerUser(registerRequest);
registerResponseCall.enqueue(new Callback<RegisterResponse>() {
#Override
public void onResponse(Call<RegisterResponse> call, Response<RegisterResponse> response) {
if (response.isSuccessful()){
String message = "Successful";
Toast.makeText(RegisterActivity.this, message,Toast.LENGTH_LONG).show();
startActivity(new Intent(RegisterActivity.this,MainActivity.class));
finish();
}else{
String message = "An error occurred please try again later...";
Toast.makeText(RegisterActivity.this, message,Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<RegisterResponse> call, Throwable t) {
String message = t.getLocalizedMessage();
Toast.makeText(RegisterActivity.this, message,Toast.LENGTH_LONG).show();
}
});
}
}
activity_register.xml
<?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:orientation="vertical"
tools:context=".RegisterActivity">
<TextView
android:id="#+id/tv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/register"
android:textAlignment="center"
android:textSize="50sp"
android:layout_marginStart="25dp"
android:layout_marginBottom="5dp"
android:fontFamily="#font/indigo_daisy"
android:layout_marginTop="60dp"/>
<TextView
android:id="#+id/tv_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tag"
android:textSize="17sp"
android:fontFamily="#font/roboto_regular"
android:layout_marginStart="25dp"
android:layout_marginBottom="50dp"/>
<EditText
android:id="#+id/et_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="#string/your_name"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:padding="15dp"
android:inputType="textPersonName"
android:fontFamily="#font/roboto_regular"
android:background="#drawable/et_custom"
android:textSize="15sp" />
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="#string/e_mail"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:padding="15dp"
android:fontFamily="#font/roboto_regular"
android:inputType="textEmailAddress"
android:background="#drawable/et_custom"
android:textSize="15sp" />
<EditText
android:id="#+id/et_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="#string/password"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:padding="15dp"
android:fontFamily="#font/roboto_regular"
android:inputType="textPassword"
android:background="#drawable/et_custom"
android:textSize="15sp"
app:errorEnabled="true"/>
<EditText
android:id="#+id/et_repassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="#string/re_type_password"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:padding="15dp"
android:fontFamily="#font/roboto_regular"
android:inputType="textPassword"
android:background="#drawable/et_custom"
android:textSize="15sp"
app:errorEnabled="true"
app:hintEnabled="false"
app:passwordToggleEnabled="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_custom"
android:fontFamily="#font/roboto_regular"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="30dp"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
android:text="#string/register"/>
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="200dp"
android:background="#drawable/btn_custom"
android:fontFamily="#font/roboto_regular"
android:text="#string/login"
android:textColor="#android:color/white" />
</RelativeLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
LoginResponse loginResponse;
#Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if (intent.getExtras() != null) {
loginResponse = (LoginResponse) intent.getSerializableExtra("data");
Log.e("TAG", "====>" + loginResponse.getEmail());
}
}
}
activity_main.xml
<?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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ImageView
android:src="#drawable/ic_launcher_foreground"
android:layout_width="188dp"
android:layout_height="200dp"
android:background="#color/colorPrimaryDark"/>
<TextView
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#color/colorPrimaryDark"/>
</LinearLayout>
manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".RegisterActivity" />
<activity android:name=".MainActivity"/>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Logcat
2021-12-09 22:29:45.631 22677-23634/com.guincho.chamemeuguincho E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.guincho.chamemeuguincho, PID: 22677
java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.art/javalib/core-oj.jar)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onResponse(DefaultCallAdapterFactory.java:77)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:150)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
2021-12-09 22:29:45.675 22677-22677/com.guincho.chamemeuguincho I/ViewRootImpl#9ebca35[LoginActivity]: stopped(false) old=true
2021-12-09 22:29:45.678 22677-23634/com.guincho.chamemeuguincho I/Process: Sending signal. PID: 22677 SIG: 9
You should not get the support action bar before setcontentview is called.
Simply, move getSupportActionBar().hide(); after setContentView(R.layout.activity_main); and it should not crash anymore
Also please provide crash logs in the future, so we can debug quicker

Loading a URL by using a webview by passing URL in a second activty

I want to pass URL string from main activity to second activity and load the URL in second activity.... but when I click the go button at main activity it goes to second activity but it shows nothing but blank.
here is my code ..
public class MainActivity extends AppCompatActivity {
EditText editText;
Button go;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.urltext);
go=findViewById(R.id.button6);
final String link=editText.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Webview.class);
intent.putExtra("link",link);
startActivity(intent);
}
});
}
}
Second Activity:
public class Webview extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
}
Use the following in your manifest :
<uses-permission android:name="android.permission.INTERNET"/>
Refer these examples Please
Web View Android Developers
Can someone give one exact example of webview implementation in android
webView android
your code seems to be fine, just add internet permission in your manifest file
<uses-permission android:name="android.permission.INTERNET"/>
The problem is in this line
final String link=editText.getText().toString();
You have made the variable "link" final that means this variable can be set value only once. Try putting some logs in your code and see what values are passed in the intent extra into the second activity.
Also you have to set WebViewClient to your WebView instance.
Try below code. It worked for me:
private EditText editText;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.urltext);
button = (Button) findViewById(R.id.btn_go);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
String link = editText.getText().toString();
intent.putExtra("link",link);
startActivity(intent);
}
});
}
In the second activity -
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;
Log.d(WebViewActivity.class.getSimpleName(), url);
mWebView = (WebView)findViewById(R.id.wv_url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
I have several buttons those I have not used yet. I have used button 6.
Activity mainXML:
<?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"
android:background="#drawable/backgroundone"
tools:context="com.example.rakib.webbrowser.MainActivity">
<EditText
android:id="#+id/urltext"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/button"
android:layout_marginTop="20dp"
android:text=""
android:layout_marginRight="15sp"
android:textColor="#FFFF" />
<Button
android:id="#+id/button"
android:background="#drawable/buttonshapehome"
android:text="Facebook"
android:textColor="#fdf900"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/urltext"
android:layout_marginStart="24dp"
android:layout_marginTop="93dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:text="Youtube"
android:textColor="#fdf900"
android:background="#drawable/buttonshapehome"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_marginStart="23dp"
android:layout_toEndOf="#+id/button"
/>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google"
android:textStyle="italic"
android:textColor="#fdf900"
android:background="#drawable/buttonshapehome"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginStart="23dp"
android:layout_toEndOf="#+id/button2"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:background="#drawable/buttonshapehome"
android:text="linkdin"
android:textColor="#fdf900"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/button"
android:layout_below="#+id/button"
android:textStyle="italic"
android:layout_marginTop="72dp"
/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshapehome"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:text="gmail"
android:textStyle="italic"
android:textColor="#fdf900"
android:layout_alignStart="#+id/button2"
/>
<Button
android:id="#+id/newage"
android:layout_width="wrap_content"
android:background="#drawable/buttonshapehome"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:text="new age"
android:textStyle="italic"
android:textColor="#fdf900"
android:layout_alignBottom="#+id/button4"
android:layout_alignStart="#+id/button5" />
<Button
android:id="#+id/button6"
android:text="GO"
android:layout_width="50dp"
android:textStyle="bold"
android:layout_height="35dp"
android:layout_alignBaseline="#+id/urltext"
android:layout_alignBottom="#+id/urltext"
android:layout_toEndOf="#+id/urltext"
android:background="#039337"
/>
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/button3"
android:layout_marginBottom="53dp"
android:background="#drawable/settings_icon"
android:text=""
android:textColor="#ffaa00" />``
<TextView
android:id="#+id/textView"
android:text="settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button8"
android:layout_alignParentBottom="true"
android:layout_marginBottom="27dp"
android:layout_marginEnd="16dp"
android:textColor="#ffff" />
</RelativeLayout>
Activity webviewXML:
<?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="com.example.rakib.webbrowser.Webview">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="470dp">
</WebView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_below="#id/webview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#00ffffff"
>
<ImageView
android:id="#+id/home"
android:src="#drawable/homeicon"
android:clickable="true"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="36sp"
tools:ignore="OnClick" />
<ImageView
android:id="#+id/reload"
android:layout_weight="1"
android:clickable="true"
android:src="#drawable/reload"
android:layout_height="36sp"
android:layout_width="wrap_content"
/>
</LinearLayout>
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rakib.webbrowser">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Webview">
</activity>
</application>
</manifest>

"Could not find method in a parent or ancestor" for camera

I am a newbie in the android World and I am struggling with getting my camera to open on the click of a button in my app. I have followed several tutorials and still not managed to get it to work.
Finally I have come to a stage where I can run the app, and open the camera activity, but when I click the button to take a new picture my app fails.
I have tried various tutorials out now but just can't seem to make it work.
Can anyone tell me from my code below what I need to do to make this work?
I get the following errors:
java.lang.IllegalStateException: Could not find method dispatchTakePictureIntent(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_add_ph'
public class CameraActivity extends Activity {
ImageView result;
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_activity);
result = (ImageView)findViewById(R.id.imageView1);
Button takePic = (Button)findViewById(button_add_ph);
}
public void dispatchTakePictureIntent(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
result.setImageBitmap(imageBitmap);
}
}
and my xml
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:layout_marginTop="15dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:id="#+id/imageView1"
android:layout_centerVertical="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:layout_marginTop="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/notes"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Yo - no notes added for this photo this photo this photo this photo"
android:id="#+id/textView"
android:textSize="14dp"
android:layout_centerHorizontal="true"
android:maxLength="60"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_edit"
android:text="edit note"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/notes"
android:layout_marginTop="25dp">
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/button_add_note"
android:text="Add note"/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/button_delete"
android:text="Delete picture"/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/button_add_ph"
android:onClick="dispatchTakePictureIntent"
android:text="Take picture" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/backBtn"
android:onClick="buttonOnClick"
android:text="Hold dog mund"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
</LinearLayout>
And my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.europcar.mob1exam">
<uses-feature android:name="android.hardware.camera2" android:required="true"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/iconfaetter"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<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=".CameraActivity">
</activity>
</application>
</manifest>
Thanks a lot in advance

Android Buttons dont work click on click

I'm new new to Android and am working on a simple project that needs login and sign up.
When I click on the links to link to another activity, The buttons do not work.
here is my code....
<?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="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg_login"
android:gravity="center"
android:padding="10dp"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:weightSum="1">
<ImageView android:src="#drawable/logo_bg"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="100dp"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/Zilwacom_Logo" />
<TextView android:id="#+id/login"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:text="#string/welcome_to_zilwacom"
android:gravity="center"
android:textSize="24sp"
android:layout_weight="0.14" />
<!-- Email Label -->
<EditText
android:id="#+id/loginemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/Email" />
<!-- Password Label -->
<EditText
android:id="#+id/loginpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/Password"/>
<Button
android:id="#+id/btnlogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="#string/Login"/>
<TextView
android:id="#+id/linksignup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="#null"
android:text="#string/btn_link_to_register"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="15dp"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
and my login code
When the user clicks to sign up, the app shd open the signup activity
public class LoginActivity extends AppCompatActivity {
private EditText email;
private EditText password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Get reference to buttons and texts
//buttons
Button loginbtn =(Button) findViewById(R.id.btnlogin);
TextView signuplink=(TextView) findViewById(R.id.linksignup);
//Texts
email=(EditText) findViewById(R.id.loginemail);
password=(EditText) findViewById(R.id.loginpassword);
signuplink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(LoginActivity.this, SignupActivity.class);
startActivity(i);
}
});
}
}
I managed to do it guys,
The problem was in the manifest...
I made the change as below..
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Add/change in your code:
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(LoginActivity.this, SignupActivity.class);
startActivity(i);
}
});
}
Set onClickListener to your Button [loginbtn] not TextView [signuplink].
In your xml layout file add to TextView android:clickable = "true"

Buttons not responding to clicks - Android

I have a simple app that updates a counter with plus/minus buttons. I have implemented onClickListeners but I can't get the counter to update based on clicks. I am running my app in an emulator, the application uploads and installs successfully. I also don't see the print statements in the logcat. Please help. Thank you.
package testapp.two;
import testapp.two.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button okButton, minusButton, plusButton;
TextView textScore, scoreCard;
int score = 95;
private static final String TAG = "GolfScore";
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate started");
okButton = (Button)findViewById(R.id.buttonOK);
minusButton = (Button)findViewById(R.id.buttonMinus);
plusButton = (Button)findViewById(R.id.buttonPlus);
textScore = (TextView)findViewById(R.id.textScore);
scoreCard = (TextView)findViewById(R.id.scoreCard);
//set button listeners
okButton.setOnClickListener(this);
minusButton.setOnClickListener(this);
plusButton.setOnClickListener(this);
textScore.setText(String.valueOf(score));
Log.d(TAG, "onCreate finished");
}//onCreate
#Override
public void onClick(View v) {
Log.d(TAG, "in onClick");
switch (v.getId()){
case R.id.buttonMinus:
score--;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonPlus:
score++;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonOK:
break;
}//end of switch
}//end of my onclick
}//end of MainActivity
activity xml
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:text="Golf Score App"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_marginTop="14dp"
android:baselineAligned="false"
android:gravity="fill"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="-" />
<Button
android:id="#+id/buttonPlus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="+" />
<TextView
android:id="#+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="91"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_marginRight="33dp"
android:layout_weight="1"
android:text="Ok"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/scoreCard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="22dp"
android:text="Score card info goes here" />
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testapp.two"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You should probably just declare a new View.OnClickListener() rather than having Main activity implement it... that just seems weird to me. Then set the click listener to that

Categories

Resources