How to show different strings in a Another Activity? - android

I'm trying to display different texts for a textview according to the listview listitem click. For now Am creating Lot of new activities to achieve this task. Following Codes are one of that scenario. Is there any simple way to show the different texts in a Same Activity(BSC Activity). I dont want to create Post Activity. For an example If listview position == 0 item clicked
R.String.Bsc
have to display in bsc Activity. If position == 1 clicked
R.String.Post
Have to display in bsc activity.
ListView class
public class AHSMLS extends AppCompatActivity{
ListView list;
String[] itemname ={
"Degree in Physiotherapy",
"Post Graduate options"
};
Integer[] imgid={
R.drawable.mlsico,
R.drawable.mls
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cp_listview_main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Physiotherapy");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageView img = (ImageView)findViewById(R.id.thumbnail);
img.setImageResource(R.drawable.physiotherapy);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
Intent intent = new Intent(getApplicationContext(), Bsc.class);
startActivity(intent);
}else if (position == 1){
Intent intent = new Intent(getApplicationContext(), Post.class);
startActivity(intent);
}
}
});
}
}
Bsc activity
public class Bsc extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_text_view);
TextView tv3 = (TextView)findViewById(R.id.durationtextView);
tv3.setText("•\t 3 or 4 Year");
TextView tv4 = (TextView)findViewById(R.id.institutiontextView);
tv4.setText(R.string.bsc);
}
}
Post activity
public class Post extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_text_view);
TextView tv3 = (TextView)findViewById(R.id.durationtextView);
tv3.setText("•\t 3 or 4 Year");
TextView tv4 = (TextView)findViewById(R.id.institutiontextView);
tv4.setText(R.string.Post);
}
}

If i got your question correctly. Is this one You looking For? With this solution you dont need your post class. You can change your texts with the Bsc class.
ListView class
public class AHSMLS extends AppCompatActivity{
ListView list;
String[] itemname ={
"Degree in Physiotherapy",
"Post Graduate options"
};
Integer[] imgid={
R.drawable.mlsico,
R.drawable.mls
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cp_listview_main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Physiotherapy");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageView img = (ImageView)findViewById(R.id.thumbnail);
img.setImageResource(R.drawable.physiotherapy);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), Bsc.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putInt("x", position);
//Add the bundle to the intent
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Bsc activity
public class Bsc extends AppCompatActivity {
TextView tv3;
TextView tv4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_text_view);
tv3 = (TextView)findViewById(R.id.durationtextView);
tv4 = (TextView)findViewById(R.id.institutiontextView);
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
int stuff = bundle.getInt("x");
if(stuff == 0){
tv3.setText("•\t 3 or 4 Year");
tv4.setText(R.string.bsc);
}else if(stuff == 1){
tv3.setText("•\t 3 or 4 Year");
tv4.setText(R.string.Post);
}
}
}
So you dont need Your Post activity Here. Good luck Bro!

(sorry about the crappy formatting... still have never figured out how to preserve indentation when pasting code)
Anyhoo.... I think this is what Reaz Murshed is try to say:
public class AHSMLS extends AppCompatActivity{
// START NEW CODE
TextView tv3;
TextView tv4;
// END NEW CODE
ListView list;
String[] itemname ={
"Degree in Physiotherapy",
"Post Graduate options"
};
Integer[] imgid={
R.drawable.mlsico,
R.drawable.mls
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cp_listview_main_activity);
// START NEW CODE
tv3 = (TextView)findViewById(R.id.durationtextView);
tv3.setText("•\t 3 or 4 Year");
tv4 = (TextView)findViewById(R.id.institutiontextView);
// END NEW CODE
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Physiotherapy");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageView img = (ImageView)findViewById(R.id.thumbnail);
img.setImageResource(R.drawable.physiotherapy);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
// START NEW CODE
tv4.setText(R.string.bsc);
}else if (position == 1){
tv4.setText(R.string.Post);
// END NEW CODE
}
}
});
}
}

Related

Pass TreeMap value to Toolbar Title in second activity

I have a TreeMap with a ListView, I want the second activity to take the selected item of the ListView and use it as the Title of the second activity.
(Using Strings or Arrays instead of TreeMap I can make it work, but then the TextWatcher won't work, and using TreeMap the TextWatcher works fine).
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView resultsListView = (ListView) findViewById(R.id.listView);
EditText buscar = (EditText) findViewById(R.id.busqueda);
TreeMap<String, String> nameAddresses = new TreeMap<>();
nameAddresses.put ( "Coso", "Desear " );
nameAddresses.put ( "бажа́ти", "Desear " );
nameAddresses.put ( "бала́кати", "Charlar " );
final List<HashMap<String, String>> listItems = new ArrayList<>();
final SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = nameAddresses.entrySet().iterator();
while (it.hasNext())
{
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry)it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
resultsListView.setFastScrollEnabled(true);
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Want to Pass", resultsListView.getItemAtPosition(position).toString());
startActivity(intent);
}
});
}
This is the main activity, and now the second:
public class SecondActivity extends AppCompatActivity {
Toolbar mToolbar;
TextView mTextView;
TextView mTextView2;
ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//changing statusbar color
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
}
mToolbar = (Toolbar) findViewById(R.id.toolbar2);
mTextView = (TextView) findViewById(R.id.textView2);
mTextView2 = (TextView) findViewById(R.id.textView3);
button = (ImageButton) findViewById (R.id.imageButton);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mToolbar.setTitle(bundle.getString("Want to Pass"));
if (mToolbar.getTitle().toString().equalsIgnoreCase("Coso")) { писа́ти
mToolbar.setSubtitle("Escribir");
The ListView shows two elements in each item ("Coso and Desear" for example) and I want either of these two to be used as Title in the toolBar of the second activity........

How to implement onItemClickListener on each item in the ListView to go to another activity?

How to implement onItemClickListener on each item in the ListView to go to another activity / to a new class?
public class MainActivity extends Activity{
ListView list;
String[] itemname ={
"Resturants",
"Coffee Shops",
"Hotels",
"Gas Stations",
"Hospitals",
"Airports",
"ATM",
"Cinemmas",
"Phamacies"
};
Integer[] imgid={
R.drawable.restaurantz,
R.drawable.coffeeshop,
R.drawable.hotel,
R.drawable.gaspump,
R.drawable.hospitalblue,
R.drawable.airporticon,
R.drawable.atm,
R.drawable.cinemma,
R.drawable.hospitalblue,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
inside onItemClick:
startActivity(new Intent(MainActivity.this, NewActivity.class));
if you need to differenciate the clicked item, you can use the position parameter, for example:
if (position == 0)
// do something
else
// do something else

Title on Action Bar displaying garbage value

I'm trying to display the name of the Listview item on the action bar as it's title on the next activity screen using intents.
Here is my code.
MathematicsDBEntry.java
public class MathematicsDBEntry extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
String newActionBarTitle = i.getStringExtra("Position");
super.onCreate(savedInstanceState);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(newActionBarTitle);
setContentView(R.layout.activity_mathematics_dbentry);
}
}
MathsActivity.java
public class MathsActivity extends ListActivity implements AdapterView.OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maths);
// storing string resources into Array
String[] math_data = getResources().getStringArray(R.array.maths_list_data);
ListView listView = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, math_data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
//Toast.makeText(this, textView.getText().toString() + " " + position, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MathematicsDBEntry.class);
intent.putExtra("Position", textView.toString());
startActivity(intent);
}
}
But all I get is the garbage value shown below.
You need to use the getText() method of TextView to get the data from it.
You need to change:
intent.putExtra("Position", textView.toString());
to:
intent.putExtra("Position", textView.getText().toString());

android ListActivity onListItemClick to open data in another activity

Hey I have tried to make a ListActivity that show the internal saved data, there have been saved from another activity. And when I click on an item, it should open a new activity with the internal data, and then open the data.
Here is the first activity. there shall show the listview and when clicked on an item, the user should be send to second activity whith more details
public class ShowListActivity extends ListActivity {
private ArrayAdapter<String> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_data);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.holo_blue_bright);
String[] filenames = getApplicationContext().fileList();
List<String> list = new ArrayList<String>();
for(int i = 0; i<filenames.length; i++){
//Log.d("Filename", filenames[i]);
list.add(filenames[i]);
}
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String product = ((TextView)v).getText().toString();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("product", product);
startActivity(i);
}
Here is the other activity there shall recived the data and open it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
TextView showCloseRange = (TextView) findViewById(R.id.show_close_range);
TextView showToRight = (TextView) findViewById(R.id.show_to_right);
TextView showToCenterRight = (TextView) findViewById(R.id.show_to_center_right);
TextView showToCenter = (TextView) findViewById(R.id.show_to_center);
TextView showToCenterLeft = (TextView) findViewById(R.id.show_to_center_left);
TextView showToLeft = (TextView) findViewById(R.id.Show_to_left);
TextView showFT = (TextView) findViewById(R.id.show_ft);
TextView showTreRight = (TextView) findViewById(R.id.show_tre_right);
TextView showTreCenterRight = (TextView) findViewById(R.id.show_tre_center_right);
TextView showTreCenter = (TextView) findViewById(R.id.show_tre_center);
TextView showTreCenterLeft = (TextView) findViewById(R.id.show_tre_center_left);
TextView showTreLeft = (TextView) findViewById(R.id.show_tre_left);
TextView showAssist = (TextView) findViewById(R.id.show_assist);
TextView showSteals = (TextView) findViewById(R.id.show_steals);
TextView showFouls= (TextView) findViewById(R.id.show_fouls);
TextView showTotalPt = (TextView) findViewById(R.id.show_total_pt);
TextView showDataBlocks = (TextView) findViewById(R.id.show_data_blocks);
TextView showDataRebounds = (TextView) findViewById(R.id.show_data_rebounds);
TextView showDataTurnOcers = (TextView) findViewById(R.id.show_data_turn_overs);
TextView showDataPlayerTime = (TextView) findViewById(R.id.show_data_player_time);
TextView showNote = (TextView) findViewById(R.id.show_note);
String value = "";
FileInputStream fis;
Intent i = getIntent();
String product = i.getStringExtra("product");
ActionBar actionBar1 = getActionBar();
actionBar1.setTitle(product);
try {
fis = openFileInput(product);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){
value += new String(input);
fis.close(); }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String[] strArray = value.split(";");
showCloseRange.setText(strArray[0]);
showToRight.setText(strArray[1]);
showToCenterRight.setText(strArray[2]);
showToCenter.setText(strArray[3]);
showToCenterLeft.setText(strArray[4]);
showToLeft.setText(strArray[5]);
showFT.setText(strArray[6]);
showTreRight.setText(strArray[7]);
showTreCenterRight.setText(strArray[8]);
showTreCenter.setText(strArray[9]);
showTreCenterLeft.setText(strArray[10]);
showTreLeft.setText(strArray[11]);
showDataPlayerTime.setText(strArray[12]);
showTotalPt.setText(strArray[13]);
showAssist.setText(strArray[14]);
showSteals.setText(strArray[15]);
showDataBlocks.setText(strArray[16]);
showDataRebounds.setText(strArray[17]);
showDataTurnOcers.setText(strArray[18]);
showFouls.setText(strArray[19]);
showNote.setText(strArray[20]);
}
I have trouble with sending the data between the activies and open it, so it will split up in my many differents textviews.
Any ideas ?
And sorry for my bad English
Try this way,hope this will help you to solve your problem.
Instead of geting product from ListView item TextView just declare your ArrayList object outside oncreate() and try to get your particular product using position from list in ListView item click listener.
public class ShowListActivity extends ListActivity {
private List<String> list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_data);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.holo_blue_bright);
list = Arrays.asList(getApplicationContext().fileList());
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, list));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, MainActivity.class);
i.putExtra("product", list.get(position));
startActivity(i);
}
}
You can check what you are sending to second activity, mean using System.out.println() you can check on logcat . Also i done small change in your code please try this.
private ArrayAdapter<String> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_data);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.holo_blue_bright);
String[] filenames = getApplicationContext().fileList();
ArrayList<String> arrlist = new ArrayList<String>();
for(int i = 0; i<filenames.length; i++){
//Log.d("Filename", filenames[i]);
arrlist.add(filenames[i]);
}
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, arrlist ));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String product = arrlist.get(position);
//Here you can check what yo are sending to second activity using System.out.println
System.out.println("click product is : "+product);
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("product", product);
startActivity(i);
finish();
}
Ans also in other activity mean second activity you can add System.out.println to check list item value is coming or not.
String product = getIntent().getStringExtra("product");
System.out.println("product in second activity : "+product);

access textview from another class

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

Categories

Resources