Android Buttons dont work click on click - android

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"

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>

using scrollview to show button behind edittext keyboard

I have a layout in which I have an image,textview, editbox and a button so whenever the keyboard opens I wanted my screen to scroll down to the button so that the user can always type and press submit button. I did do this by using scrollview but than I removed the scroll view did some changes and now when I again use a scrollview it does not work.
here is the code please check.
layout_file:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="myapp.anis.hotel.MainActivity"
android:background="#drawable/backgroundhotel"
android:fitsSystemWindows="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Please share your feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#android:color/background_dark"
android:fontFamily="serif"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:text="#string/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thanks"
android:shadowColor="#android:color/background_light"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHighlight="#android:color/background_dark"
android:textColor="#android:color/background_dark"
android:fontFamily="serif"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<ImageView
android:layout_width="wrap_content"
app:srcCompat="#drawable/hotellogo"
android:id="#+id/imageView"
android:layout_height="150dp"
android:layout_marginTop="15dp"
android:layout_below="#+id/thanks"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:ems="10"
android:maxLines="50"
android:background="#android:drawable/editbox_background"
android:layout_marginTop="15dp"
android:id="#+id/feedback"
android:hint="Enter FeedBack"
android:inputType="textMultiLine"
android:gravity="left"
android:textColorHint="#android:color/background_dark"
android:textColor="#android:color/background_dark"
android:textColorHighlight="#android:color/background_dark"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="180dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/submitting1"
android:background="#android:color/transparent"
android:onClick="onSubmit"
android:id="#+id/submit"
android:layout_below="#id/feedback"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
and in my main activity I am using the scrollto method to go to my image button.
public class MainActivity extends AppCompatActivity {
ImageButton submit;
EditText feedBack;
static public String saveFeedBack="";
ScrollView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (ImageButton) findViewById(R.id.submit);
feedBack = (EditText) findViewById(R.id.feedback);
view = (ScrollView) findViewById(R.id.scroll);
view.scrollTo(submit.getScrollX(),submit.getScrollY());
}
public void onSubmit(View view){
saveFeedBack = feedBack.getText().toString();
if(saveFeedBack.length() >=4) {
Intent intent = new Intent(this, Main3Activity.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(),"Enter valid Feedback please.",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onRestart() {
super.onRestart();
feedBack.setText("");
feedBack.setHint("Enter Feedback");
}
}
but when I run my application the scrollview scrolls to my edit text but not my button. I have tried adjustResize,adjustPan and all those things but none of them worked.
ok after much of research I have solved your problem. Actually the thing is when you use scroll to any position inside onCreate it doesn't work because a small delay is required to inflate the view.
So use the below code inside your onCreate:
view.postDelayed(new Runnable() {
#Override
public void run() {
view.fullScroll(ScrollView.FOCUS_DOWN);
}
},200);
Hope it Helps!!!

Trying an intent and crash the app

I'm trying to create an intent that connect a button with a class (definition of intent). The problem is that, when I press this button, everything crash. I think that the problem is in the intent class. Can you see what's wrong? I'm trying to open an alert dialog when button bet is pressed. I have already create it:
Chips class, the class aI want to use after the button is pressed:
public class Chips extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
private AlertDialog.Builder dialog;
public void scommessa () {
dialog = new AlertDialog.Builder(this);
final EditText txtInput = new EditText (this);
final String stringInput = txtInput.getText().toString();
dialog.setTitle("Welcome to poker");
dialog.setMessage("Player 2, place your bet!");
dialog.setView(txtInput);
dialog.setPositiveButton("Bet", new Dialog.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
int bet1 = 100;
int intInput = Integer.parseInt(stringInput);
int newPlayer1Bet = bet1 - intInput;
String total1 = "" + newPlayer1Bet;
}
});
dialog.setNegativeButton("Pass", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which){
Toast.makeText(getApplicationContext(), "Player 2 won", Toast.LENGTH_SHORT).show();;
}
});
AlertDialog dialogPoker = dialog.create();
dialogPoker.show();
}
public void onClick(View v){
switch (v.getId()){
case R.id.Cover1:
scommessa();
break;
}
}
}
Then here is the class where I defined the button and then try to call the class Chips:
final Button Cover1 = (Button) findViewById(R.id.Cover1);
final Button button1 = (Button) findViewById(R.id.A);
final Button button2 = (Button) findViewById(R.id.B);
final Button button3 = (Button) findViewById(R.id.C);
final Button button4 = (Button) findViewById(R.id.D);
final Button button5 = (Button) findViewById(R.id.E);
final Button Bet = (Button) findViewById(R.id.button1);
Cover1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
if(v.equals(Cover1)){
button1.setBackgroundResource(R.drawable.back);
button2.setBackgroundResource(R.drawable.back);
button3.setBackgroundResource(R.drawable.back);
button4.setBackgroundResource(R.drawable.back);
button5.setBackgroundResource(R.drawable.back);
}
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View x){
if(x.equals(Bet)){
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
}
Intent intent1 = new Intent(GameActivity.this, Chips.class);
//startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
}
});
Here is the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pokeroriginal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.pokeroriginal.FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="GameActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.pokeroriginal.GameActivity"
android:label="#string/title_activity_game"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name= "com.example.pokeroriginal.Chips"
android:label= "#string/Bet"
android:screenOrientation= "landscape"
android:theme= "#android:style/Theme.NoTitleBar">
</activity>
</application>
</manifest>
Also the activity_game.xml where all the buttons are defined:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/green_table_cloth"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".GameActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal" >
<Button
android:id="#+id/Cover1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cover"
android:onClick="onClickChips" />
<Button
android:id="#+id/A"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/B"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/C"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/D"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/E"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="0dp" >
</View>
</LinearLayout>
<ImageSwitcher
android:id="#+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick(View x)"
android:text="#string/Bet" />
<Button
android:id="#+id/Button01"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Bet" />
</ImageSwitcher>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="horizontal" >
<Button
android:id="#+id/Cover2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cover" />
<Button
android:id="#+id/A2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="#+id/B2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_weight="1" />
<Button
android:id="#+id/C2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="#+id/D2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="#+id/E2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<!-- <ImageView
android:id="#+id/Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView> '
-->
</LinearLayout>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Bet" />
</LinearLayout>
It's difficult to determine what the error is without seeing the logcat - you should really include it in the future.
Looking at your code, I notice this in the button1 OnClickListener:
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
To start another activity, all you need to do is this:
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
I believe that trying to instantiate the Activity using it's constructor is causing problems. When you do this, the Activity's Views have not been setup properly, and when you try to show a dialog, you will encounter errors.
Try removing these lines:
Chips chip = new Chips();
chip.scommessa();
Your OnClickListener should look like this:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View x) {
if (x.equals(Bet)) {
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
}
});
I think you're getting NullPointerException here.
if(x.equals(Bet))
and your xml onClick() implemented wrongly.
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick(View x)"
android:text="#string/Bet" />
java code should be like this,
final Button Cover1 = (Button) findViewById(R.id.Cover1);
final Button button1 = (Button) findViewById(R.id.A);
final Button button2 = (Button) findViewById(R.id.B);
final Button button3 = (Button) findViewById(R.id.C);
final Button button4 = (Button) findViewById(R.id.D);
final Button button5 = (Button) findViewById(R.id.E);
final Button Bet = (Button) findViewById(R.id.button1);
Cover1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
button1.setBackgroundResource(R.drawable.back);
button2.setBackgroundResource(R.drawable.back);
button3.setBackgroundResource(R.drawable.back);
button4.setBackgroundResource(R.drawable.back);
button5.setBackgroundResource(R.drawable.back);
}
});
Bet.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Chips chip = new Chips();
chip.scommessa();
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
});
Or if you're xml implementation should be like this.
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="NavigateToChips"
android:text="#string/Bet" />
and you should implement onClick() method in activity like this,
public void NavigateToChips(View v) {
Chips chip = new Chips();
chip.scommessa();
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
In your xml layout file, I see an attribute android:onClick="onClickChips" for the component having id as android:id="#+id/Cover1". Hence the java code would typically look for the method with name onClickChips(), whereas you've used an setOnClickListener() for Cover1 button.
Solution:
Either remove the attribute (android:onClick="onClickChips") from your XML or remove the setOnClickListener() for that button. Keep only one.

pass intent with extra to defined layout text view

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);

Categories

Resources