0% found this document useful (0 votes)
7 views12 pages

Aplicacion Mensaje

Uploaded by

emanuelamber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Aplicacion Mensaje

Uploaded by

emanuelamber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Sánchez Martínez Gabriel

Tecnológico Cuicalli
Android Studio
Proyecto Mensaje

Paso 1. Creamos una clase java con el nombre de; KeyboardUtils

import android.app.Activity;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;

public class KeyboardUtils {

private KeyboardUtils() {
}

public static void hideSoftKeyboard(@NonNull Activity activity) {


InputMethodManager imm = (InputMethodManager)
activity.getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm != null && activity.getCurrentFocus() != null) {

imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
0);
}
}

Paso 2. Creamos una clase java con el nombre de MainActivity

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.widget.NestedScrollView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private EditText txtMessage;


private TextView lblText;
private ImageView btnSend;
private NestedScrollView scvText;
private final SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("HH:mm:ss",
Locale.getDefault());

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}

private void setupViews() {


lblText = ActivityCompat.requireViewById(this, R.id.lblText);
txtMessage = ActivityCompat.requireViewById(this,
R.id.txtMessage);
btnSend = ActivityCompat.requireViewById(this, R.id.btnSend);
scvText = ActivityCompat.requireViewById(this, R.id.scvText);

TextViewUtils.setOnImeActionDoneListener(txtMessage, (v, event) -


> sendMessage());
TextViewUtils.addAfterTextChangedListener(txtMessage, s ->
checkIsValidForm());
btnSend.setOnClickListener(v -> sendMessage());
checkInitialState();
}

private void checkInitialState() {


checkIsValidForm();
doScroll(scvText);
}

private void sendMessage() {


String text = txtMessage.getText().toString();
if (isValidForm()) {
KeyboardUtils.hideSoftKeyboard(this);
lblText.append(getString(R.string.main_log_message,
simpleDateFormat.format(new Date()), text));
txtMessage.setText("");
doScroll(scvText);
}
}

private boolean isValidForm() {


return !TextUtils.isEmpty(txtMessage.getText().toString());
}

private void doScroll(@NonNull final NestedScrollView scv) {


// Must be posted in order to calculate the end position
correctly.
scv.post(() -> {
scv.smoothScrollTo(0, scv.getBottom());
txtMessage.requestFocus();
});
}

private void checkIsValidForm() {


btnSend.setEnabled(isValidForm());
}

Paso 3. Creamos una clase java con el nombre de: TextViewUtils


import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;

@SuppressWarnings("unused")
public class TextViewUtils {

private TextViewUtils() {
}

public interface AfterTextChangedListener {


void afterTextChanged(Editable s);
}

public interface BeforeTextChangedListener {


void beforeTextChanged(CharSequence s, int start, int count, int
after);
}

public interface OnTextChangedListener {


void onTextChanged(CharSequence s, int start, int before, int
count);
}

public static void addAfterTextChangedListener(@NonNull TextView


textView,
@NonNull
AfterTextChangedListener listener) {
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int
before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
listener.afterTextChanged(s);
}
});
}
public static void addBeforeTextChangedListener(@NonNull TextView
textView,
@NonNull
BeforeTextChangedListener listener) {
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
listener.beforeTextChanged(s, start, count, after);
}

@Override
public void onTextChanged(CharSequence s, int start, int
before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
}
});
}

public static void addOnTextChangedListener(@NonNull TextView


textView,
@NonNull
OnTextChangedListener listener) {
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int
before, int count) {
listener.onTextChanged(s, start, before, count);
}

@Override
public void afterTextChanged(Editable s) {
}
});
}

public static void addTextWatcherListeners(@NonNull TextView


textView,
BeforeTextChangedListener
beforeTextChangedListener,
OnTextChangedListener
onTextChangedListener,
AfterTextChangedListener
afterTextChangedListener) {
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
if (beforeTextChangedListener != null) {
beforeTextChangedListener.beforeTextChanged(s, start,
count, after);
}
}

@Override
public void onTextChanged(CharSequence s, int start, int
before, int count) {
if (onTextChangedListener != null) {
onTextChangedListener.onTextChanged(s, start, before,
count);
}
}

@Override
public void afterTextChanged(Editable s) {
if (afterTextChangedListener != null) {
afterTextChangedListener.afterTextChanged(s);
}
}
});
}

public interface OnImeActionDoneListener {


void onActionDone(TextView v, KeyEvent event);
}

public static void setOnImeActionDoneListener(@NonNull TextView


textView,
@NonNull
OnImeActionDoneListener listener) {
textView.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
listener.onActionDone(v, event);
return true;
}
return false;
});
}

Estas serian las 3 clases de java que se ocuparan para este proyecto.

Despues en la carpeta de res realizaremos los xml


Ingresamos a la carpeta drawable y creamos un xml con el nombre;
ic_send_black_24dp

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z"/>
</vector>

En la carpeta Color agregamo el siguiente xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z"/>
</vector>

En la carpeta Values creamos los siguientes xml

colors.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="primary">#673AB7</color>
<color name="primary_dark">#512DA8</color>
<color name="primary_light">#D1C4E9</color>
<color name="accent">#8BC34A</color>
<color name="white">#FFFFFF</color>
<color name="disabled">#808080</color>
</resources>

dimens.xml
<resources>
<dimen name="main_horizontal_margin">8dp</dimen>
<dimen name="main_vertical_margin">16dp</dimen>
</resources>

integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="main_txtMessage_maxLines">5</integer>
</resources>

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ScrollView</string>
<string name="main_btnSend">Send</string>
<string name="main_txtMessage">Message</string>
<string name="main_lblTitle">Messages</string>
<string name="main_log_message">[%1$s] %2$s\n\n"</string>
</resources>

styles.xml
<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">


<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>

</resources>

Ahora nos ubicamos en la carpeta Layout

<?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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/lblTitle"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/main_horizontal_margin"
android:layout_marginStart="@dimen/main_horizontal_margin"
android:layout_marginTop="@dimen/main_vertical_margin"
android:text="@string/main_lblTitle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<androidx.core.widget.NestedScrollView
android:id="@+id/scvText"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="@dimen/main_horizontal_margin"
android:layout_marginTop="@dimen/main_vertical_margin"
app:layout_constraintBottom_toTopOf="@+id/llPanel"
app:layout_constraintLeft_toLeftOf="@+id/lblTitle"
app:layout_constraintRight_toRightOf="@+id/lblTitle"
app:layout_constraintTop_toBottomOf="@+id/lblTitle"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">

<TextView
android:id="@+id/lblText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</androidx.core.widget.NestedScrollView>

<LinearLayout
android:id="@+id/llPanel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/primary_light"
android:paddingBottom="@dimen/main_horizontal_margin"
android:paddingEnd="@dimen/main_horizontal_margin"
android:paddingStart="@dimen/main_vertical_margin"
android:paddingTop="@dimen/main_horizontal_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1">

<EditText
android:id="@+id/txtMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:hint="@string/main_txtMessage"
android:scrollHorizontally="false"
android:maxLines="@integer/main_txtMessage_maxLines"
android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
tools:ignore="Autofill">

<requestFocus />
</EditText>

<ImageView
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="@dimen/main_horizontal_margin"
android:contentDescription="@string/main_btnSend"
android:padding="@dimen/main_horizontal_margin"
android:tint="@color/ic_send_tint"
android:focusable="true"
android:clickable="true"
app:srcCompat="@drawable/ic_send_black_24dp" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

y revisamos que el diseño quede de la siguiente forma;


Despues nos diriguimos a la carpeta XML
Y agregamos los siguientes xml:

backup_descriptor.xml
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<!-- Exclude specific shared preferences that contain GCM
registration Id -->
</full-backup-content>

backup_rules.xml
<?xml version="1.0" encoding="utf-8"?><!--
Sample backup rules file; uncomment and customize as necessary.
See https://developer.android.com/guide/topics/data/autobackup
for details.
Note: This file is ignored for devices older that API 31
See https://developer.android.com/about/versions/12/backup-restore
-->
<full-backup-content>
<!--
<include domain="sharedpref" path="."/>
<exclude domain="sharedpref" path="device.xml"/>
-->
</full-backup-content>

data_extraction_rules.xml
<?xml version="1.0" encoding="utf-8"?><!--
Sample data extraction rules file; uncomment and customize as
necessary.
See https://developer.android.com/about/versions/12/backup-
restore#xml-changes
for details.
-->
<data-extraction-rules>
<cloud-backup>
<!-- TODO: Use <include> and <exclude> to control what is backed
up.
<include .../>
<exclude .../>
-->
</cloud-backup>
<!--
<device-transfer>
<include .../>
<exclude .../>
</device-transfer>
-->
</data-extraction-rules>

Como ultimo paso nos ubicamos en la carpeta manifest

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.scrollview">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning"
android:fullBackupContent="@xml/backup_descriptor">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>

</manifest>

You might also like