I make my first step in android and want to use MVVM in my first project. But I faced with problem: when i try to call function from .xml file nothing happens. Below you can see important parts of project.
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.hfad.firebasechat.viewmodels.LoginViewModel"/>
<variable name="activity" type="com.hfad.firebasechat.views.MainActivity"/>
<import type="android.widget.Toast"/>
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="#+id/inEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="8dp"
android:text="#={viewModel.userEmail}" />
<EditText
android:id="#+id/inPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:text="#={viewModel.userPassword}" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="#{() -> activity.showToast()}"
android:layout_marginTop="8dp"
/>
</LinearLayout>
</ScrollView>
</layout>
MainActivity.java
package com.hfad.firebasechat.views;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.app.Dialog;
import android.content.Context;
import android.widget.Button;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import com.hfad.firebasechat.R;
import com.hfad.firebasechat.databinding.ActivityLoginBinding;
import com.hfad.firebasechat.viewmodels.LoginViewModel;
public class MainActivity extends AppCompatActivity {
private LoginViewModel mViewModel;
private Dialog mChatRoomDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ActivityLoginBinding activityLoginBinding= DataBindingUtil.setContentView(this, R.layout.activity_login);
mViewModel= new LoginViewModel();
activityLoginBinding.setViewModel(mViewModel);
activityLoginBinding.setLifecycleOwner(this);
}
public void showToast(){
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(),
"Text here", Toast.LENGTH_LONG);
toast.show();
}
});
}
}
Thank yu for your answers.
Good luck.
P.S. if you have some advices for a baby in the world of android development i will glad to see it.
I spent some hours for this trying change fuctions which returns context of getApplicationContext() getContext() etc. It did not give me result. Button works correctly, but when I click it logcat does not show anything.
Also there was a try to write a fuction in xml #{() -> Toast(context, text, time).show()} but it also did not bring result. An unclever solution.
Related
I'm trying to create a listener from the simplest kind but for some reason I can't.
Code:
package com.example.check;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "this is a test",
Toast.LENGTH_LONG).show();
}
});
}
}
And the .xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttton"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
It's a very simple listener but for some reason it doesn't work. The IDE doesn't detect any error, the problem is in this part of the code: new View.OnClickListener()
The IDE marks it in gray as it never been used in the code. I searched for days in every possible forum but no one has this problem.
Here's a Picture: new View.OnClickListener() is grey
If I put my mouse on: new View.OnClickListener() it suggests I replace it with lambda. If I do replace it, there's no more gray section but when I run the app the code inside the onClick(View view) function is not been executed.
When I run the app with new View.OnClickListener() or with lambda instead, I don't get any error messages.
I think the problem is somehow related to the Gradle but that's just an idea. Please someone help, I am really desperate because the code is correct and it's not working!
I was creating an app then a question come out:
how can i keep user in activity?
I've put two editTexts with input of int.
i want user be kept in activity until he/she puts valid numbers in edittexts.
any ideas?
The solution is very simple all you need to do is get the string values from the edit text box and have a condition in your setonclicklistener that checks the strings if they match to a certain value, if yes the intent from current activity will take you to next if not it will show an error on the edit text box (by using .setError()) and return.
Here is the Java Class Code:
package com.example.text_to_speech;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity);
EditText editText1,editText2;
Button button;
editText1=findViewById(R.id.ed1);
editText2=findViewById(R.id.ed2);
button=findViewById(R.id.check_input);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!editText1.getText().toString().equals("Don"))
{
Toast.makeText(ActivityA.this, "You Can't Leave this Activity, Input Correct Fields", Toast.LENGTH_SHORT).show();
editText1.setError("Input Correct values");
return;
}
if(!editText1.getText().toString().equals("Tom"))
{
Toast.makeText(ActivityA.this, "You Can't Leave this Activity, Input Correct Fields", Toast.LENGTH_SHORT).show();
editText2.setError("Input Correct values");
return;
}
Intent intent=new Intent(ActivityA.this,MainActivity.class);
startActivity(intent);
}
});
}
}
XML Code:
<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=".ActivityA">
<EditText
android:layout_margin="10dp"
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Input Data Here"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:layout_margin="10dp"
android:layout_below="#id/ed1"
android:id="#+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Input Data Here"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ed2"
android:id="#+id/check_input"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:text="Go to Next Activity"
/>
</RelativeLayout>
Output:
So so what im trying to implement has two parts
One im giving my edittext the passwordToggle for which im using
android.support.design.widget TextInputLayout+TextInputEditText
So this is how my edittext looks like
Part two is i want to add validation and set appropriate error message.
I need the error message to be shown as follows
My layout code is as follows
<android.support.design.widget.TextInputLayout
style="#style/editTextBold"
android:id="#+id/input_pwd_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintAnimationEnabled="false"
app:hintEnabled="false"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/input_pwd"
style="#style/editTextBold"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:ems="10"
android:hint="#string/hint_pwd"
android:inputType="textPassword"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
So what i want to know is
1.How do i hide/unhide the password toggle icon in the edittext via code?
2.Also how do i make the setError message appear in place of the passwordToggle icon(once i hide it via code)
Ended up creating a custom view
The component can be found here
https://github.com/vnh1991/CustomValidatorEditText
Import the lib project as a module ,
in your layout create a container
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.v2dev.customedittextdemo.MainActivity">
<LinearLayout
android:id="#+id/llContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"></LinearLayout>
<Button
android:id="#+id/btnValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#+id/llContainer"
android:padding="10dp"
android:text="Validate" />
</RelativeLayout>
And in your activity load the component into the container
package com.v2dev.customedittextdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.v_sQr_dev.customvalidatoredittext.CustomEdittext;
public class MainActivity extends AppCompatActivity {
private LinearLayout llContainer;
private Button btnValidate;
private CustomEdittext inputPwd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llContainer = (LinearLayout) findViewById(R.id.llContainer);
btnValidate = (Button) findViewById(R.id.btnValidate);
inputPwd = new CustomEdittext(llContainer, this);
inputPwd.setHint("PASSWORD");
btnValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(inputPwd.validate()){
Toast.makeText(MainActivity.this,"Input Valid",Toast.LENGTH_SHORT).show();
}
}
});
}
}
I am new to android programming and there are a few things I don't quite know how to do yet. I am doing a course on udemy and was trying to put together everything I have learned up to a certain point.
What I am trying to do is have the user click on a button (i have 12) and have it bring up a textField where they can enter two numbers. I just want to be able to get the user's two numbers and i'm pretty sure I can figure out the rest ( I hope). I just don't understand how to go about doing this.
Any help would be much appreciated. Basically all I want to do is to be able to have the user click on one of the 12 buttons and be asked to enter two values, then take those values and perform a calculation on it.
Your xml could be like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/b_ok"
android:text="Click Me"/>
<EditText android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/et_showme"/>
</LinearLayout>
and your activity could be like this:
package com.william.kinaan.welcomeback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test1 extends Activity implements OnClickListener {
private Button b_ok;
private EditText et_showme;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
initialize();
}
private void initialize() {
this.b_ok = (Button) findViewById(R.id.b_ok);
this.b_ok.setOnClickListener(this);
this.et_showme = (EditText) findViewById(R.id.et_showme);
}
#Override
public void onClick(View v) {
this.et_showme.setVisibility(View.VISIBLE);
}
}
Create an Activity that has 2 EditTexts
When user click a button the Activity is started and user can enter the Numbers
Try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_id"
android:visibility="invisible"
android:text="sample text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/click"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And in your activity, you can do like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button button = (Button) findViewById(R.id.click);
final EditText text = (EditText) findViewById(R.id.tv_id);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setVisibility(View.VISIBLE);
}
});
}
I have created a simple xml with one button and one edittext.
Upon clicking the button, i can get into the CallLog page.
Is it possible to display the selected numbers into my EditText, after i click on any numbers in my CallLog??
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/edittext"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_weight="0.29"
android:hint="CONTACT NUMBER" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL LOG" />
</LinearLayout>
Coding:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CallLogRetrieveActivity extends Activity {
Button myCallLogBtn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myCallLogBtn=(Button)findViewById(R.id.btn);
myCallLogBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Intent myIntent=new Intent();
myIntent.setAction(Intent.ACTION_CALL_BUTTON);
startActivity(myIntent);
}});
}
}
You should start reading manual and tutorial first, not asking here about elementary things that you'd simply know after doing RTFM. So "yes, you can". But now please do your homework, by reading "android call log tutorial" googled materials. Nobody here is going that for you.