Hey guys and gals I am having trouble figuring out the process and method to best solve my problem.I have an app in which user created text is shown in a secretive font/language, while English(non-secretive) text is displayed in the textview.
JAVA
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.view.KeyEvent;
public class MainActivity extends ActionBarActivity {
TextView tv;
EditText et;
private Button convert;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Loading our AvianKingdom Codex Font
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/WWAvianKingdom-Regular.ttf");
//Text view label
tv = (TextView) findViewById(R.id.CustomFontText);
et = (EditText) findViewById(R.id.CodexMessage);
//Applying the font
et.setTypeface(tf);
//Convert the text
et.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
String inputText = et.getText().toString();
tv.setText(inputText);
return true;
default:
break;
}
}
return false;
}
});
}
In my app I am trying to update a textview as the user enters text, but so far I have only been able to change upon click/press of a button.My question is,
How do I update TextView with EditText in realtime using java inside Android Studio?
I want to do this before I hit the send/enter button, during the users type phase. What is the best solution in your opinion? I have looked up TextWatcher/KeyListener/KeyClick but being new to Java doesn't help me decide or understand completely.
FIGURED IT OUT!!!
I ended up researching a lot and found a very helpful blog post. here is the answer
package com.christianlopez.helloworld;
//De-Raptor-Codex Android Prototype
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;
import android.widget.EditText;
import android.text.TextWatcher;
import android.text.Editable;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Log;
import android.graphics.Typeface;
import android.content.Intent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
public class MainActivity extends ActionBarActivity {
TextView CodexTV;
EditText CodexET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Loading our AvianKingdom Codex Font
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/WWAvianKingdom-Regular.ttf");
//Text view label
CodexET = ((EditText) findViewById(R.id.CodexMessage));
//REAL-TIME Textview change
CodexET.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { //Convert the Text to String
String inputText = CodexET.getText().toString();
CodexTV = ((TextView) findViewById(R.id.CustomFontText));
CodexTV.setText(inputText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
//Applying the font
CodexET.setTypeface(tf);
//Screenshot our Codex'ed Image
CodexET.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
doShare(getDefaultIntent());
captureScreen();
case KeyEvent.KEYCODE_ENTER:
doShare(getDefaultIntent());
captureScreen();
default:
break;
}
}
return false;
}
});
}
The answer begins at e.addtextChanged Listener. This sets up the app to be paying attention to any changes in the EditText and then I take that into a string, and relay it back to Textview in realtime.
Just trying to keep your answer short...It is the .addTextChangedListener method and inside that we're coding in afterTextChanged method.
CodexET.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { //Convert the Text to String
String inputText = CodexET.getText().toString();
CodexTV = ((TextView) findViewById(R.id.CustomFontText));
CodexTV.setText(inputText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Does not do any thing in this case
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Does not do any thing in this case
}
});
Related
So, I want all of those variables to get the value after going through the OnClick method. When I put the log in the method they are ok, but outside... I know that they destroy because it's OnClick method and they are local but I don't know how to keep them. Please if you know how to help rewrite the code. Thanks!
package com.exemple.android.calendar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class NewEventActivity extends AppCompatActivity {
//step 1.A:create objects
public EditText DayEditText;
public EditText MonthEditText;
public EditText YearEditText;
public EditText StartingHourEditText;
public EditText StartingMinuteEditText;
public EditText EndingHourEditText;
public EditText EndingMinuteEditText;
public EditText Title;
public RadioGroup answer1;
public Button NewEventButton;
int day, month, year, s_hour, s_min, e_hour, e_min;
String title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_event_page);
//Step 1.B: assign objects
DayEditText = findViewById(R.id.DayEditText);
MonthEditText = findViewById(R.id.MonthEditText);
YearEditText = findViewById(R.id.YearEditText);
StartingHourEditText = findViewById(R.id.StartingHourEditText);
StartingMinuteEditText = findViewById(R.id.StartingMinuteEditText);
EndingHourEditText = findViewById(R.id.EndingHourEditText);
EndingMinuteEditText = findViewById(R.id.EndingMinuteEditText);
Title = findViewById(R.id.TitleEditText);
answer1 = findViewById(R.id.Answer1);
NewEventButton = findViewById(R.id.CreateEventButton);
//Step 2: get data into variables
//Step 2.B: get data and assign
//PROBLEM: If we just extract, the code won't be run because there's nothing to extract yet. We need a conditional.
//SOLUTION: Create a condition for pressing the button, and then extract the variables when the button is pressed.
NewEventButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
title = Title.getText().toString();
day = Integer.parseInt(DayEditText.getText().toString());
month = Integer.parseInt(MonthEditText.getText().toString());
year = Integer.parseInt(YearEditText.getText().toString());
s_hour = Integer.parseInt(StartingHourEditText.getText().toString());
s_min = Integer.parseInt(StartingMinuteEditText.getText().toString());
e_hour = Integer.parseInt(EndingHourEditText.getText().toString());
e_min = Integer.parseInt(EndingMinuteEditText.getText().toString());
}
});
Log.i("INFO:",title+" "+day+" "+month+" "+year+" "+s_hour+" "+s_min+" "+e_hour+" "+e_min);
}
}
Your code is correct, it is just a understanding issue.
In fact, it's normal the Log method prints nothing because your variables are not set. The code inside the OnClick is called only if a click happens. Then, when click happens, your variables are set correctly.
Understood ?
Here is an example of TextWatcher
DayEditText = findViewById(R.id.DayEditText);
DayEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
day = Integer.parseInt(s.toString());
}
});
Do the same for each EditTexts you have
I am making an album music application on Android Studio with ListView. now I want to make music notifications that are being played using textview. if the item in the listview is clicked, then the textview will change as written in the listview. can anyone help me? Here my code. Thanks for your attention. :)
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main2Activity extends AppCompatActivity implements
AdapterView.OnItemClickListener {
ListView songListView;
MediaPlayer player;
int[] IMAGES = {R.drawable.adzan_mp3_offline_1, R.drawable.adzan_mp3_offline_4};
String[] songs = {"song 1","song 2"};
int[] ids={R.raw.adzan_mp3_offline_turkey_a,R.raw.adzan_mp3_offline_turkey_b};
private AdView mAdview;
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
songListView = findViewById(R.id.list);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,songs);
songListView.setAdapter(adapter);
songListView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (player!=null && player.isPlaying())
player.stop();
player=MediaPlayer.create(this,ids[i]);
player.start();
}
#Override
public void onBackPressed ()
{
if (player != null)
player.stop();
super.onBackPressed();
}
#Override
public void onPause ()
{
if (player != null)
{
player.pause();
player.stop();
}
super.onPause();
}
}
To get the text of the clicked listener use this
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String currentText = parent.getItemAtPosition(position); // Assuming you've used only string
}
Another way to get the item clicked is to pass the position to Adapter and get the selected object from Adapter. Then you can use the selected object in any way you like.
Hope it helps
I have a problem with my code and still confused about intent which each item have their own layout like a product with their own description
Anybody know how to solve my problem? thank you before
and this is my main_activity code
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import static com.example.vinznolimit.herbindonesia.R.id.theList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list= (ListView) findViewById(theList);
EditText theFilter = (EditText) findViewById(R.id.searchFilter);
Log.d(TAG, "onCreate: Started.");
ArrayList<String> names = new ArrayList<>();
names.add("Mitch");
names.add("Blake");
names.add("Blade");
names.add("Bruno");
names.add("Joe");
names.add("Barry");
adapter = new ArrayAdapter(this,R.layout.list_item,names);
list.setAdapter(adapter);
theFilter.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) {
(MainActivity.this).adapter.getFilter().filter(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
Try this one
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent myIntent = new Intent(v.getContext(), UserSubmissionLog.class);
startActivity(myIntent);
}
}
);
So i am working on my university project nowadays and needed to use addTextChangedListener method on it. There weren't any problem at first but when i implement TextWatcher() it has a red underscore says that "invalid method declaration". And also override methods got "annotations are not allowed here". I dont understand what causes this problem so if you can help me i appreciate a lot.
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
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;
import android.text.TextWatcher;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText activityTxt = (EditText) findViewById(R.id.txtActivity);
EditText whereTxt = (EditText) findViewById(R.id.txtWhere);
EditText whenTxt = (EditText) findViewById(R.id.txtWhen);
EditText withTxt = (EditText) findViewById(R.id.txtWith);
Button addBtn = (Button) findViewById(R.id.btnCreate);
mobileNumber.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mobileNumber.setError(null);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
mobileNumber.setError(null);
}
});
}
It looks like you forget to define: mobileNumber, this should be a textEdit. Your TextWatcher code has no problem.
Please also remove your unused import:
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
Optimize imports by pressing CTRL + ALT + O together
package com.example.ss.new_my_wid;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
public class ItemChanging extends Activity implements View.OnClickListener, View.OnKeyListener
{
EditText EdT;
Button btn_OK;
Button btn_Cancel;
Button btn_Delete;
Intent intent;
String str, changed_text;
Toast tst;
final static String CURRENT_TEXT_IN_ITEM = "current_text_in_item";
final static String POSITION = "position";
public ItemChanging() {
super();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.type_to_item);
EdT = (EditText) findViewById(R.id.editText);
intent = getIntent();
str = intent.getStringExtra(CURRENT_TEXT_IN_ITEM);
EdT.setText(str, TextView.BufferType.EDITABLE);
btn_OK = (Button)findViewById(R.id.but_OK);
btn_Cancel = (Button) findViewById(R.id.but_Cancel);
btn_Delete = (Button) findViewById(R.id.but_Del);
btn_OK.setOnClickListener(this);
btn_Cancel.setOnClickListener(this);
btn_Delete.setOnClickListener(this);
EdT.setOnKeyListener(this);
}
#Override
public void onClick(View v)
{
int pos = intent.getIntExtra(POSITION,0);
switch (v.getId())
{
case R.id.but_OK:
changed_text = EdT.getText().toString();
MyFactory.data.set(pos, changed_text);
/*HERE need update of ListView*/
tst = Toast.makeText(this, changed_text, Toast.LENGTH_SHORT);
tst.show();
finish();
break;
case R.id.but_Cancel:
finish();
break;
case R.id.but_Del:
MyFactory.data.remove(pos);
MyFactory.data.add("");
finish();
break;
}
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
return false;
}
}
I have widget with ListView and activity with button. In activity I change an item in the ListView. When I click on the button changes are saved in ListView, but I dont know how to update the listView and the widget from activit.How can I do this?
P.S. sorry for my english =)
Call notifyDataSetChanged() on your Adapter object once you've modified the data in that adapter.
How to refresh Android listview?
When ever you Made changes in your listview's data then you need to notify your adapter with new data
you can do it by calling notifyDataSetChanged(); method on your adaptor
for examlple
adapter.notifyDataSetChanged();