relationship between EditText and TextView - android

If I have an EditText in layout and Text view in another layout my code take which in EditText to another layout and put it in the textView ..
the format of TextView is different from format of EditText like font-size and font-family .. i want my code to take the format also to another layout and apply it on textview which exist in another layout
so to be more specific : I write something in the 1st layout and change its font size ... this EditText will be a textView in the second layout but without saving format of EditText .. I want textView be same format of EditText ..
can I do that ?? and how ??
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int position = spinner.getSelectedItemPosition();
int fontSizeInt;
try
{
fontSizeInt = Integer.parseInt(items[position]);
}
catch (NumberFormatException e)
{
fontSizeInt = 12; // Default size.
}
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) fontSizeInt);
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
);
and this is my touch listener
public void addListenerOnImageg1() {
final Context context = this;
imageView = (ImageView) findViewById(R.id.g1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent1 = new Intent(context , g1.class);
intent1.putExtra("fname" , et.getText().toString());
intent1.putExtra("font_size", fontSizeInt);
startActivity(intent1);
}
}); }
what should i write in the second layout ??

Since you do let user change format of the text in EditText, keep those changes, put 'em as more extras to your Intent, and read extras in the other activity.
You should have only one Intent and one call to startActivity(), unless you really want to start two activities, which I doubt.
I don't see where format data is, from this code, but if you have, for instance, your font size stored in int fontSize, call:
intent.putExtra("font_size", fontSize);
In that other activity, call:
int receivedFontSize = intent.getIntExtra("font_size", defaultFontSize);
As defaultFontSize, put 12 instead of the variable, if it's 12. Put 18, if it's 18. The purpose of this variable is to avoid absence of the value passed from parent activity. For instance:
int receivedFontSize = intent.getIntExtra("font_size", 18);

Related

Using AutoCompleteTextView to change current dispaly page

There is a AutoCompleteTextView and when user write for-example blue and select it from AutoCompleteTextView, the current page change to that particular page.
Here is the code:
public class MainActivity extends Activity {
private AutoCompleteTextView et_search_game;
private TextView TextViewForAutoComplete;
private static String[] GAMES_NAME = new String[] { "blue", "red", "black" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//AutoCompleteTextView Setting.
et_search_game = (AutoCompleteTextView)findViewById(R.id.actv_search_game_name_xml);
//TextView Setting.
TextViewForAutoComplete = (TextView)findViewById(R.id.tv_for_auto_complete_tv_xml);
ArrayAdapter<String> arrayAdapterForGamesName = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, GAMES_NAME);
et_search_game.setThreshold(1);
et_search_game.setAdapter(arrayAdapterForGamesName);
et_search_game.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// TODO Auto-generated method stub
if(et_search_game.getText().toString().trim()=="blue"){
setContentView(R.xml.blue);
} else if (et_search_game.getText().toString().trim()=="red"){
setContentView(R.xml.red);
} else if (et_search_game.getText().toString().trim()=="black") {
setContentView(R.xml.black);
}
}
});
}
The red,black,blue pages are normal XML files in directory named 'xml'.
After running the code when i write b, the black row display and i click on it but nothing happen.
I don't want to go with activity(intent) method.
thanks.
Instead of == use .equals
Instead of taking value from "et_search_game.getText().toString().trim()" you can take from adapter and position "i"
It is always a best practise to place layout xmls in "layout" directory

Update the Activity based on what item is selected in the Spinner

In the current Activity I'm printing a graph (using Androidplot) of the closing prices of a selected stock from the previous activity.
In this activity I have a spinner of a list of indicators the user can overlay.
Now I want the graph to be redrawn with this new selection from the spinner.
I did try refreshing/reloading the page onItemSelected but that causes the page to keep refreshing even without waiting for a user input.
public class DispGraph extends Activity {
private XYPlot plotstock;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.graph);
// PRINTING SELECTED STOCK_NAME
Bundle bundle = getIntent().getExtras();
String sname = bundle.getString("SN");
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(sname);
// INDICATOR LIST
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Indicators, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
String iname = spinner.getSelectedItem().toString();
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
startActivity(getIntent());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
//PASSING STOCK-TICKER & INDICATOR TO PHP
// PLOTTING GRAPH
plotstock = (XYPlot) findViewById(R.id.mySimpleXYPlot);
Number[] series1Numbers = ind;
Number[] series2Numbers = closing;
XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, iname);
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Closing Price");
plotstock.setDomainLabel("Date");
plotstock.setRangeLabel("Price");
plotstock.addSeries(series1,new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(0,100, 0), null, new PointLabelFormatter(Color.TRANSPARENT)));
plotstock.addSeries(series2,new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100),null, new PointLabelFormatter(Color.TRANSPARENT)));
plotstock.setTicksPerRangeLabel(2);
}
}
Using your current approach, each time the user select a spinner value your current activity will be started again, so it will be put on Android Stack. Soon, if the user change your spinner n times, you should press n + 1 times to close your app. This is a really annoying behavior.
Well, I do not know the AndroidPlot, but looking its API I found the method redraw.So, i think you could try on that way:
Every time the user select a spinner value, i can do your modifications on plotstock after call plotstock.redraw()
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//Change the graphics value here!
plotstock.redraw();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});

Dialog Spinner does not return value

I have a dialog setup that has two spinners connected to a cursor. I have worked through a couple of problems with the help of this site, but I can not seem to get past this point. Everything I find are things I have already tried.
The problem is that when I click on a spinner selection or click Submit to exit the dialog, the spinner value is not the value it should be. I am getting the package name with some code. I am trying to get the string from the spinner with .getSelectedItem().toString();
I currently have the code set up to use onItemSelected, but before that I tried to use the getItemSelected once Submit was clicked. Neither seem to work.
Here is the code for this section.
At the end the values are going into a textview. The value shown is "android.database.sqlite.sqliteCursor#414175e0"
Any ideas?
private void transfer() {
dialog = new Dialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth);
dialog.setContentView(R.layout.transfer_dialog);
dialog.setTitle(R.string.transfer_accounts);
Button btnCancel = (Button)dialog.findViewById(R.id.btnCancel);
Button btnSubmit = (Button)dialog.findViewById(R.id.btnSubmit);
Cursor load_spinner = mDbHelper.spinnerAccounts();
startManagingCursor(load_spinner);
String[] columns = new String[] { RegisterDbAdapter.ACCOUNTS_ACCOUNT };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, load_spinner, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFrom = (Spinner)dialog.findViewById(R.id.spinnerFrom);
spinnerTo = (Spinner)dialog.findViewById(R.id.spinnerTo);
spinnerFrom.setAdapter(mAdapter);
spinnerTo.setAdapter(mAdapter);
dialog.show();
spinnerFrom.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
fromAccount = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinnerTo.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
toAccount = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tvFrom.setText(fromAccount);
tvTo.setText(toAccount);
dialog.dismiss();
}
});
}
You need two separate adapters for two spinners.
You are only using one... each time you make a selection, the adapter sets your selection to your choice ( for both of the spinners since you have the same adapter backing each one ).
Create a second adapter and things should work more like you expect.
Also, try this in your onItemSelected method (using the parameter names you show):
String fromAccount = parent.getItemAtPosition(arg2).toString();
I think you have to use your cursor to get the String you want. Arg2 should be the selected position. Use it to place your cursor to the right data row and use the getstring method of the cursor to get the string from the database column.
So I finally got it to work. Thanks for the suggestions, I really do appreciate them, but they didn't work for me. I am not sure why the code will not return the value, since the way I tried it supposedly works for others.
Anyway, I found a way to get my values by using 'Long arg3'. Since it holds the rowId of the value in the table, I used that to return the selection in the spinner. I am posting the code for this below.
Thanks again.
The code from the selected spinner:
spinnerFrom.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
getAccountName(arg3);
fromAccount = returnAccount;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
And here is the method I wrote to return the value I wanted. I put it a separate method because it is getting used from multiple spinners. The code:
private void getAccountName(long arg3) {
if (returnAccount != null){
returnAccount = null;
}
RegisterDbAdapter tAdaptor = new RegisterDbAdapter(this);
tAdaptor.open();
Cursor tCursor = tAdaptor.fetchAccount(arg3);
startManagingCursor(tCursor);
returnAccount = tCursor.getString(1);
}

error in accessing variable of main activity's class from mapactivity

it seems to be a typical question but its not i am facing problem in accessing a variable of main activity's onitemselected class(spinner class)from mapavtivity
in main avtivity map is shown on button click
now i want to use a variable of main activity's class from mapctivity
mapactivity code
String dest ;
OnItemSelectedListener place = new OnItemSelectedListener();
dest = place.onItemSelected();
now onitemselected class
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
name = "Destination:" + parent.getSelectedItem().toString()+ "\n";
etTextOut.setText(name);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public String onItemSelected() {
// TODO Auto-generated method stub
//dest = name;
return name;
}
}
problem is in mapactivity the variable is null it cannot get value
tell me what i am doing wrong
I'm not sure if I understand the way your program is set up but from what I understand you have a main Activity which has a spinner in it.
First make sure you're actually setting the onItemSelectedlistener to the spinner after you create it.
OnItemSelectedListener place = new OnItemSelectedListener();
//Replace "spinnerId" with the id you have set for your spinner in your layout
Spinner spinner = (Spinner) findViewById(R.id.spinnerId);
spinner.setOnItemSelectedListener(place);
Then if you want to start the MapActivity and have it have access to the "name" value, you'll have to pass the value in the intent you use to start the activity.
String dest = place.onItemSelected();
//You might want to rename this method to something clearer like "getName" but that's just me.
Intent intent = new Intent(this, MapActivity.class);
intent.putExtra("DestValue", dest);
startActivity(intent);
Now, in your MapActivity, you will have access to this value by using:
Intent intent = getIntent();
String dest = intent.getStringExtra("DestValue");
If your main activity is not starting the MapActivity, you can also try looking into SharedPreferences to pass values between activities.
Hope this was helpful.
Corrected Answer:
Try this in your activity. It should work:
private EditTextBox etTextOut;
#Override
protected void onCreate(Bundle savedInstanceBundle){
EditTextBox etTextOut = (EditTextBox) findViewById(R.id.etTextOutId);
Spinner spinner = (Spinner) findViewById(R.id.spinnerId);
OnItemSelectedListener spinnerListener = new OnItemSelectedListener{
private String name;
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
name = name = "Destination:" + parent.getSelectedItem().toString()+ "\n";
etTextOut.setText(name);
}
public void onNothingSelected(AdapterView<?> parent){}
public String getName(){
return name;
}
}
spinner.setOnItemClickListener(spinnerListener);
}

Passing data in a ordered way to another activity

I have a list of college careers, choosing one of the options takes me to a LinearLayout with multiple spinners, which contain class schedules. What is selected in the spinner, is sent to a "textview1" in the same activity.
My question is what would be the best way to put in another activity, in an ordered way, the information within the "textview1"? And of course be saved there.
I have some example code , using a clickable button that only sends the data, there is another button that allows me to start the another activity when you want to see it, but the problem is to organize the information in the second activity, do I really have to do so many strings? I hope you can help me to find a better way. I also want to say that this application is directed to the 2.1 api, if that matters.
Activity1=Administracion.java
public class Administracion extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.administracion);
final TextView lblMensaje = (TextView)findViewById(R.id.LblMensaje);
final Spinner cmbOpciones = (Spinner)findViewById(R.id.CmbOpciones);
final String[] datos =
new String[]{"0031711 Lab Biologia","Lun-Mar 9:30am prof. Marcarian","Elem3","Elem4","Elem5"};
ArrayAdapter<String> elefante =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, datos);
elefante.setDropDownViewResource(
R.layout.multiline_spinner_dropdown_item);
cmbOpciones.setAdapter(elefante);
cmbOpciones.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
android.view.View v, int position, long id) {
lblMensaje.setText("Seleccionado: " + datos[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
lblMensaje.setText("");
}
});
final TextView lblMensaje1 = (TextView)findViewById(R.id.LblMensaje1);
final Spinner cmbOpciones1 = (Spinner)findViewById(R.id.CmbOpciones1);
final String cuervo[] =
new String[] {"00311712 Biología I","Mar-Jue 7:00am Prof.Briceño","Elem3","Elem4","Elem5"};
ArrayAdapter<String> adaptador1 =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cuervo);
adaptador1.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
cmbOpciones1.setAdapter(adaptador1);
cmbOpciones1.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
android.view.View v, int position, long id) {
lblMensaje1.setText(cuervo[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
lblMensaje1.setText("");
}
});
Button BotonPasar1;
BotonPasar1 = (Button)findViewById(R.id.VB1);
BotonPasar1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String Aguila;
String Canario;
Aguila = lblMensaje.getText().toString();
Canario = lblMensaje1.getText().toString();
SharedPreferences mypreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mypreferences.edit();
editor.putString("Canario", Aguila);
editor.commit();
SharedPreferences mypreferences1 = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = mypreferences1.edit();
editor1.putString("Canario", Aguila);
editor1.commit();
}
});
Button BotonPasar2;
BotonPasar2 = (Button)findViewById(R.id.VB2);
BotonPasar2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent pasarahorario = new Intent("com.reversiblelabs.unisvenecas.udobeta.HORARIO");
startActivity(pasarahorario);
}
});
}
}
Activity 2 = Horario.java
public class Horario extends Activity{
TextView llegada, llegada2, llegada3, llegada4;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.horario);
llegada = (TextView) findViewById(R.id.tv1);
llegada2 = (TextView) findViewById(R.id.tv2);
SharedPreferences mypreferences = getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences mypreferences1 = getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
String teamnamestring = mypreferences.getString("canario", "no_name");
String hola = mypreferences1.getString("Canario","no_name");
llegada.setText(teamnamestring);
llegada2.setText(hola);
Here is the first activity, the last botton send the data on the textwiews to the second activity, and the first button start the second activity.
1st Activity
Whatever you choose on your spinner, would be seen on its own textview.
Now here is the second activity, I want the data to get organized vertically by hours
7:00 9:00 math
9:00 11:00 chemestry
11:00 1:00 extra
2nd Activity
What I can do to acomplish this?
Here is the first activity, the last botton send the data on the
textwiews to the second activity, and the first button start the
second activity.
No need to do this. Only last button can send all the data and as well start the second activity.
Bundle bundle = new Bundle();
bundle.putString("some constant1", value1);
bundle.putString("some constant2", value2);
Intent intent = new Intent (this, newActvity.class);
intent.putExtra(bundle);
startActivity(intent);
Now here is the second activity, I want the data to get organized
vertically by hours
Use RelativeLayout, laydown view one below another and receive bundle in second activity and show them to user.

Categories

Resources