Skip to content

Instantly share code, notes, and snippets.

View jobinjj's full-sized avatar

jobin jobinjj

View GitHub Profile
Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show();
Here you can increase the timeout by using Toast.LENGTH_LONG instead of Toast.LENGHT_SHORT.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#DAAA"
>
<ImageView android:src="@drawable/ic_launcher_foreground"
android:layout_width="44dp"
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<?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"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
package com.techpakka.sharedpreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_save = findViewById(R.id.btn_save);
btn_load = findViewById(R.id.btn_load);
btn_second = findViewById(R.id.btn_second);
ed_name = findViewById(R.id.ed1);
ed_email = findViewById(R.id.ed2);
btn_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
String name = sharedPreferences.getString("name","default_value");
String email = sharedPreferences.getString("email","default_value");
txt_name.setText(name);
txt_email.setText(email);
}
});
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//getdata from edittext and store in corresponding string file
str_email = ed_email.getText().toString();
str_name = ed_name.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("my_sharedpreference_file_name",Context.MODE_PRIVATE);
//we need editor to edit created shared preference file
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name",str_name);