my text-view is in view class and i want to access it in setting class to change it's font size ... i had tried different methods but still i don't have solution :( please help me out... my code for two classes are ...
Setting class code :
public void addItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Small");
list.add("Medium");
list.add("Large");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
apply = (Button) findViewById(R.id.apply);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
apply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(String.valueOf(spinner1.getSelectedItem())=="Small")
{
// small1=(TextView)findViewById(R.id.textfile1);
//small1.setText("raman rayat");
// setContentView(R.layout.view);
//LayoutInflater inflater = getLayoutInflater();
//View myView = inflater.inflate(R.layout.view, null);
//TextView myTextView = (TextView)myView.findViewById(R.id.textfile1);
//view1.text1.setTextSize(50);
// myTextView.setTextSize( 5);
//setContentView(R.layout.view);
view1 obj =new view1();
obj.small();
}
else if(String.valueOf(spinner1.getSelectedItem())=="Medium")
{
// code
}
else if(String.valueOf(spinner1.getSelectedItem())=="Large")
{
// code
}
}
});
view class code :
public class view1 extends menu {
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
text1=(TextView)findViewById(R.id.textfile1);
Intent myintent = getIntent();
String product = myintent.getStringExtra("product");
text1.setText(product);
}
public void small() {
text1.setText("small font");
}
}
You should use SharedPreferences. This allows you to store the font settings in your Settings class and load them from the View class.
You need to pass activity instance to the class for ex using constructor. and now u can find view or can call methods of that activity class
Related
I'm having a trouble with findViewById but I can't find where the problem is.
Here's my FirstFragment class code:
public class VehicleInformation extends Fragment implements AdapterView.OnItemSelectedListener {
private TextView text_vehicle_no, text_chassis_no;
private TextView text_company_name,text_insurance_id;
RadioGroup radioGroup;
RadioButton radioButton;
private SpinnerAdapter text_vehicle_class;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_vehicle_information, container, false);
return rootView;
radioGroup = (RadioGroup) rootView.findViewById(R.id.insuranceYesNo);
text_vehicle_no = (TextView) rootView.findViewById(R.id.vehicle_no);
text_chassis_no = (TextView) rootView.findViewById(R.id.chassis_no);
Spinner spinner_vehicle_class = (Spinner) rootView.findViewById(R.id.vehicle_class);
Spinner spinner_vehicle_company = (Spinner) rootView.findViewById(R.id.vehicle_company);
Spinner spinner_vehicle_model = (Spinner) rootView.findViewById(R.id.vehicle_model);
Spinner spinner_vehicle_fuel = (Spinner) rootView.findViewById(R.id.vehicle_fuel);
Spinner spinner_vehicle_color = (Spinner) rootView.findViewById(R.id.vehicle_color);
Spinner spinner_vehicle_types = (Spinner) rootView.findViewById(R.id.vehicle_types);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.vehicle_class_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.vehicle_company_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,R.array.vehicle_model_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this,R.array.vehicle_fuel_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter4 = ArrayAdapter.createFromResource(this,R.array.vehicle_color_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter7 = ArrayAdapter.createFromResource(this,R.array.vehicle_type_array, android.R.layout.simple_spinner_item);
//specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//aaply the adapter to the spinner
spinner_vehicle_class.setAdapter(adapter);
spinner_vehicle_company.setAdapter(adapter1);
spinner_vehicle_model.setAdapter(adapter2);
spinner_vehicle_fuel.setAdapter(adapter3);
spinner_vehicle_color.setAdapter(adapter4);
spinner_vehicle_types.setAdapter(adapter7);
spinner_vehicle_class.setOnItemSelectedListener(this);
spinner_vehicle_company.setOnItemSelectedListener(this);
spinner_vehicle_model.setOnItemSelectedListener(this);
spinner_vehicle_fuel.setOnItemSelectedListener(this);
spinner_vehicle_color.setOnItemSelectedListener(this);
spinner_vehicle_types.setOnItemSelectedListener(this);
}
public void onStart(){
super.onStart();
EditText datePicker=(EditText) findViewById(R.id.datePicker);
datePicker.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasfocus){
if(hasfocus){
DateDialog dialog=new DateDialog(view);
android.support.v4.app.FragmentTransaction ft =getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
}
});
}
public void radiobtn(View view) {
int radiobtnselect = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) view.findViewById(radiobtnselect);
Toast.makeText(VehicleInformation.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String sSelected=adapterView.getItemAtPosition(i).toString();
Toast.makeText(VehicleInformation.this,sSelected,Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Also showing error on line
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.vehicle_class_array, android.R.layout.simple_spinner_item);"
saying wrong 1st argument type
Also showing error on line Toast messsage saying cannot resolve method maketext
Unlike the Activity class, Fragment doesn't have a built-in findViewById() method. This means you need to use View.findViewById(), which you already are doing everywhere except in your onStart() method. Here, you can just change this line:
EditText datePicker=(EditText) findViewById(R.id.datePicker);
to this:
EditText datePicker=(EditText) getView().findViewById(R.id.datePicker);
As for your adapter and toast issues, the problem is that (again, unlike Activity), the Fragment class is not a subclass of Context, so you need to pass a different first argument. Try passing getContext() instead of VehicleInformation.this.
I am new to android,and in this code i have two spinners and based on the values selected,different layouts should open accordingly. Im stuck with what logic should i use to open different layouts, inside the OnItemSelected for the spinner.Say i have 5 items in spinner and i have 5 different layouts for each item.So how should i do??
Any help or ideas would be much appreciated. Thanks in advance.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(i);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner1 = (Spinner)parent;
Spinner spinner2 = (Spinner)parent;
if(spinner1.equals(position))
{
}
if(spinner2.getId() == R.id.spinner2)
{
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}
Try below.
check the onClickListener of button which will decide which activity to open.
Spinner spinner1;
Spinner spinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
if(spinner1_selected_item.equals("Maruti"))
{
if(spinner2_selected_item.equals("1Lakh-5Lakh"))
i=new Intent(SecondActivity.this,ThirdActivity.class);
if(spinner2_selected_item.equals("5Lakh-10Lakh"))
i=new Intent(SecondActivity.this,FourthActivity.class);
if(spinner2_selected_item.equals("10Lakh-20Lakh"))
i=new Intent(SecondActivity.this,FifthActivity.class);
}
startActivity(i);
}
});
}
String spinner1_selected_item;
String spinner2_selected_item;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinner1.getId() == R.id.spinner1)
{
spinner1_selected_item = parent.getItemAtPosition(position).toString();
}
if(spinner2.getId() == R.id.spinner2)
{
spinner2_selected_item = parent.getItemAtPosition(position).toString();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}
public class StartCalvert extends Activity {
Spinner spnr,spnr2;
String[] numsys = {
"Binary",
"Decimal",
"Octal",
"Hexadecimal"
};
// this numsys how will i declare
String[] numsys2 = {
"Binary",
"Decimal",
"Octal",
"Hexadecimal"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startcalvert);
spnr2 = (Spinner)findViewById(R.id.spinner2);
spnr = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, numsys);
//declare here , like what above alike .
spnr.setAdapter(adapter);
spnr.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener()
Each Spinner has it's adapter has bind its Datasource with view on it so , most likely you need to make one for each Spinner and any other Data Views
in onCreate
spinner1 = (Spinner) findViewById(R.id.spinner1 );
spinner2 = (Spinner) findViewById(R.id.spinner2 );
ArrayAdapter<String> adapterSpinner1 = new ArrayAdapter<String>(
SpinnerActivity.this, R.layout.simple_spinner_item,
numsys);
spinner1.setDropDownViewResource(R.layout.simple_spinner_item);
spinner1.setAdapter(adapterSpinner1);
ArrayAdapter<String> adapterSpinner2 = new ArrayAdapter<String>(
SpinnerActivity.this, R.layout.simple_spinner_item,
numsys2);
spinner2.setDropDownViewResource(R.layout.simple_spinner_item);
spinner2.setAdapter(adapterSpinner2);
ItemSelectedListener itemSelectedListener = new ItemSelectedListener();
spinner1.setOnItemSelectedListener(itemSelectedListener);
spinner2.setOnItemSelectedListener(itemSelectedListener);
Declare inner class
class ItemSelectedListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long arg3) {
LoggerGeneral.info("OnItemselected pressed");
switch (parent.getId()) {
case R.id.spinner1:
break;
case R.id.spinner2:
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
String[] songList = {
"1",
"2",
"3",
};
Spinner sp;
TextView selection;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter<Object> aa = new ArrayAdapter<Object>(
this,
android.R.layout.simple_spinner_item,
songList);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
selection.setText(songList[position]);
String song = songList[position];
Intent intent = new Intent(this, Tabview.class);
Bundle b = new Bundle();
b.putString("song", song);
intent.putExtras(b);
startActivityForResult(intent, 0);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
{
I have this code written right now. Pretty much what I'm trying to do is use the spinner selection to open a new activity (which I have done) BUT, I am also trying to use httpget to download information based on a link.
So, for ex:
If I press "1", it will open up a new activity, then it will call an httpget method, then it will download the data based on what you pressed. and that data will change for each option you press (EX. "1"= google.com, "2"=facebook.com, etc.) and then that data will be displayed in the activity.
I also want to only use ONE activity to display the data for each selection.
Also in my "Tabview.class" I have:
Bundle b = new Bundle();
String song = b.getString("song");
Thanks for any help!
What exactly is the error you get? The code seems to work. If you wanted to choose something according to the selection, you can use a simple if condition on the bundled string. Or if the issue here is about http get, take a look at the documentation here. You can use an AyncTask to make an http request without blocking.
For a reference, I have attached my code which just an improved version of your code.
public class ActivityxActivity extends Activity {
String[] songList = {"ONE","TWO","THREE"};
Intent intent1;
Bundle strs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<Object> aa = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, songList);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(new MyOnItemSelectedListener());
intent1 = new Intent(getApplicationContext(),AnotherActivity.class);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strs = new Bundle();
strs.putInt("item", pos);
strs.putString("song", parent.getItemAtPosition(pos).toString());
intent1.putExtras(strs);
startActivity(intent1);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
}
And in the other activity,
public class AnotherActivity extends Activity {
Bundle data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data = getIntent().getExtras();
Integer item = data.getInt("item");
//Use a switch(item) here to switch to different links based on selection
TextView tv = (TextView) findViewById(R.id.tv1);
tv.setText("Another Activity, Item is :" + item.toString());
}
}
Make sure you add your activity in the Android Manifest file.
Thanks
I am trying to change textview properties of a textview that looks like this:
In a seperate Activity that looks like this:
I tried to do this with bundles but I can't get it to work.
This is how my BookActivity looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_activity);
//this is where the size property comes in
Integer size = getIntent().getExtras().getInt("SGRkey");
TextView test2 = (TextView) findViewById(R.id.booktext);
test2.setTextSize(size);
Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] theporn = getResources().getStringArray(R.array.allkapitel);
TextView text = (TextView) findViewById(R.id.booktext);
text.setText(theporn[pos]);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
(i pick the chapter string in the spinner and that works just fine.)
And this is how my SettingsActivity looks like:
public class SettingsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_view);
Spinner SGRspinner = (Spinner) findViewById(R.id.schriftgroeße_spinner);
ArrayAdapter<CharSequence> SGRadapter = ArrayAdapter.createFromResource(
this, R.array.schriftgroesse_list, android.R.layout.simple_spinner_item);
SGRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SGRspinner.setAdapter(SGRadapter);
}
public class SGROnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Intent answer = new Intent();
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] SGRstring = getResources().getStringArray(R.array.schriftgroesse_list);
int SGRint = Integer.parseInt(SGRstring[pos]);
Bundle size = new Bundle();
size.putInt("SGRkey", SGRint);
Intent nextActivity = new Intent(com.asm.reader.SettingsActivity.this, com.asm.reader.BookActivity.class);
nextActivity.putExtras(size);
com.asm.reader.SettingsActivity.this.startActivity(nextActivity);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
I get an error when I try this. All activities are declared in the manifest. I really don't know how to go on. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)
Make your textview static. That is, declare it as a public static class variable. Then you can call it directly from the other activity like this: firstActivity.myTextView.setText("foo");