Sunday, 11 June 2017

How To Edit Textview in Android

How To Edit Textview in Android
In activity_main.xml

<?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">

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="25sp"
        android:textStyle="bold"
        android:gravity="center" />
</LinearLayout>

In MainActivity.java.
package com.mahesh.androidissimples.textviewtoedit;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textview;
    AlertDialog alertDialog;
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview= (TextView) findViewById(R.id.textview);
        alertDialog=new AlertDialog.Builder(this).create();

        editText=new EditText(this);
        alertDialog.setTitle("Edit the text");
        alertDialog.setView(editText);
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "SAVE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                textview.setText(editText.getText());
            }
        });
        textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText(textview.getText());
                alertDialog.show();
            }
        });
    }
}


Output



Android Settings Tutorial using PreferenceFragment

                        Android Settings Tutorial using PreferenceFragment
First add dependencies in build.gradle

compile 'com.android.support:preference-v7:25.3.1'

Create xml directory under res folder under xml folder. create xml folder name is preferences.

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

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<SwitchPreference
    android:defaultValue="true"
    android:key="updateNotification"
    android:summary="Remind you to update"
    android:title="Update"
    />

    <ListPreference
        android:defaultValue="1"
        android:entries="@array/listArray"
        android:entryValues="@array/listValueforSharedPreference"
        android:key="languageListSetting"
        android:summary="Select any language"
        android:title="Language"
        />
</PreferenceScreen>

Create a new layout file under value folder. array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="listArray">
        <item>English</item>
        <item>Hindi</item>
    </string-array>
    <string-array name="listValueforSharedPreference">
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>

Create a new Class and extends with PreferenceFragment and imlements OnSharedPreferenceChangeListener

package com.mahesh.androidissimples.setting;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.widget.Toast;




/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
    public static final String KEY_LANGUAGE="languageListSetting";
    public static final String KEY_SETTINGS_MENU="updateNotification";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getPreferenceManager().setSharedPreferencesName("SettingPrf");
        getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);

        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(KEY_LANGUAGE)){
            Toast.makeText(getActivity(), "Changed Language", Toast.LENGTH_SHORT).show();
        }
        if (key.equals(KEY_SETTINGS_MENU)){
            Toast.makeText(getActivity(), "Changed Settings menu", Toast.LENGTH_SHORT).show();
        }
    }
}

In MainActivity class

package com.mahesh.androidissimples.setting;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        getFragmentManager().beginTransaction().replace(android.R.id.content,new SettingFragment())
                .commit();
    }
}

Output: