How do I choose a method called "favorited" through an xml layout file with the
android:onClick
xml method?
the "favorited" method is located here:
tk.talcharnes.popularmovies.MovieDetailsFragment
I have tried many things but nothing works!
Here is the code for favorited
/**
* A placeholder fragment containing a simple view.
*/
public class MovieDetailsFragment extends Fragment {
public MovieDetailsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movie_details, container, false);
//get movie object in order to extract details
Intent intent = getActivity().getIntent();
int movie_number = intent.getIntExtra("Movie_number", 0);
MovieModel movie = PostersFragment.getMovieModelList().get(movie_number);
//set title in details view
TextView titleView = (TextView) rootView.findViewById(R.id.movie_details_text);
titleView.setText(movie.getTitle());
//set poster into details view
ImageView poster = (ImageView)rootView.findViewById(R.id.poster);
Picasso.with(getContext()).load(movie.getPoster_path()).placeholder(R.drawable.temp_poster).into(poster);
// set movie year in details view
TextView release_date = (TextView)rootView.findViewById(R.id.release_date);
if(movie.getRelease_date().length() > 3){
release_date.setText(movie.getRelease_date().substring(0,4));}
else if (movie.getRelease_date() == null){
release_date.setText("Release date not available");
}
else{
release_date.setText((movie.getRelease_date()));
};
//set vote average in details view
TextView vote_average = (TextView) rootView.findViewById(R.id.vote_average);
vote_average.setText(movie.getVote_average() + " /10");
//set overview in details view
TextView overview = (TextView) rootView.findViewById(R.id.overview);
overview.setText(movie.getOverview());
return rootView;
}
public void favorited(){
CheckBox favorited = (CheckBox) getView().findViewById(R.id.favorite);
if (favorited.isChecked()){
Toast.makeText(getContext(), "ITS CHECKED", Toast.LENGTH_SHORT).show();
}
}
}
if you declared android:onClick="favorited" in your xml you need to implement the following method in the activity the fragment is in:
public void favorited(View view) {
//handle click
}
Important: the method needs to be in the activity. You can find more info at this answer
Try below code
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/btn_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="favorited" />
In your xml view specify onClick method like this:
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="favorited" />
and then in your fragment define method like this:
public void favorited(View v) {
// does something very interesting
}
Remember without favorited(View v) your method won't be called from xml.
Use onClick listener instead xml click declaration:
in your xml ad id to clickable element.
<Button android:id="#+id/btn_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
And in your fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movie_details, container, false);
Button faw = (Button) rootView.findViewById(R.id.favorite);
faw.setOnclickListener(new View.OnClickListener(){
#Override
onClick(){
favorited(); //or do another action
}
});
}
This method better to reusing and more safety
add this to the xml
android:onClick="ButtonClick"
and this in the code
public void ButtonClick(View v) {
favorited();
}
you can also add multiple buttons to the same ButtonClick method, just simply add this in there:
switch (v.getId()) {
case R.id.button1:
doSomething1();
break;
}
all of this and more is available all over the place though, here for example: Android OnClickListener - identify a button
Related
I'm pretty new to developing, I am working on a school project and I would like to set call action intents to different items on a listView.
I got the onClickListener set on each item I would like to pass the intent but I can't figure how to get them to call each specific phone number.
Here's my code
` public class WordAdapter extends ArrayAdapter implements View.OnClickListener {
/**
* Create a new {#link WordAdapter} object.
*
* #param context is the current context (i.e. Activity) that the adapter is being created in.
* #param words is the list of {#link Word}s to be displayed.
*/
public WordAdapter(Context context, ArrayList<Word> words) {
super(context, 0, words);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {#link Word} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID
TextView titleTextView = (TextView) listItemView.findViewById(R.id.title_name);
titleTextView.setText(currentWord.getTitleId());
// Find the TextView in the list_item.xml layout with the ID
TextView priceTextView = (TextView) listItemView.findViewById(R.id.price_name);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
priceTextView.setText(currentWord.getPriceId());
// Find the TextView in the list_item.xml layout with the ID
TextView hoursTextView = (TextView) listItemView.findViewById(R.id.hours_name);
hoursTextView.setText(currentWord.getHoursClearwaterId());
// Find the TextView in the list_item.xml layout with the ID
TextView phoneTextView = (TextView) listItemView.findViewById(R.id.phone_name);
phoneTextView.setText(currentWord.getPhoneClearwaterId());
//Set the OnlcickListener for this view
phoneTextView.setOnClickListener(this);
// Find the TextView in the list_item.xml layout with the ID
TextView webTextView = (TextView) listItemView.findViewById(R.id.web_name);
webTextView.setText(currentWord.getWebClearwaterId());
phoneTextView.setOnClickListener(this);
// Find the ImageView in the list_item.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
// Check if an image is provided for this word or not
if (currentWord.hasImage()) {
// If an image is available, display the provided image based on the resource ID
imageView.setImageResource(currentWord.getImageResourceId());
// Make sure the view is visible
imageView.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
imageView.setVisibility(View.GONE);
}
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
return listItemView;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.phone_name:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(**Here the method will get the many different phone numbers from my list data**);
startActivity(callIntent);
}
}
}
`
The idea is that the user can click and call different businesses.
Thanks
permission in manifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
then on click
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your phone"));
startActivity(intent);
in your case
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + String.valueOf(word.get(i))));
startActivity(intent);
You may be better off setting the onClick listener with an anonymous class so it can capture the current Word and use that to generate the right intent
final Word currentWord = getItem(position);
phoneTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+**get from `currentWord`**);
startActivity(callIntent);
}
});
Ok, so after turning the project my reviewer showed me how to use auto link and I add that to the textviews on the layout then.
so on the list_item those were the changes:
android:autoLink=”phone”
<TextView
android:id="#+id/txtViewPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Phone no: 12345"
android:autoLink="phone"
android:textSize="16sp"
android:layout_margin="5dip">
</TextView>
android:autoLink=”web"
<TextView
android:id="#+id/txtViewWeb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Web: www.pareshnmayani.wordpress.com"
android:autoLink="web"
android:textSize="16sp"
android:layout_margin="5dip">
</TextView>
this worked for me.
am trying to make the text under my login button "no account yet? signup now" when clicked to send me to my RegisterFragment.so i added an OnClickListener for it like this in my LoginFragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView signup = (TextView) getView().findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginFragment.this.startActivity(new Intent(getActivity(), RegisterFragment.class));
}
});
but when i run the code the app crashes on this line
TextView signup = (TextView) getView().findViewById(R.id.signup);
this is my fragment_login.xml file
<TextView android:id="#+id/signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="No Account Yet? Signup_now"
android:gravity="center"
android:textSize="16dp"
android:clickable="true"/>
please guys i really need your help.
onCreateView() method is responsible for creating view, so you should first create it.
You can't directly use getView() inside onCreateView().
You need to inflate it first like
View view = inflater.inflate(R.layout.my_layout, container, false);
Then use
TextView signup = (TextView) view.findViewById(R.id.signup);
So full code should look like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
TextView signup = (TextView) view.findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}
On side note, setting up click listener initializing TextView's etc should be done inside onViewCreated(). onCreateView() should just create & return the view.
like
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
TextView signup = (TextView) view.findViewById(R.id.signup);
// and so on...
}
I would recommend not setting a click listener into something you want to type into
You can't call getView before onCreateView has actually returned a proper View
findViewById in Fragment
RegisterFragment.class is not an Activity, you cannot startActivity for it.
Use the FragmentManager
getSupportFragmentManager().beginTransaction()
.replace(R.id.<someContainer>, RegisterFragment.class)
.commit();
Or you can see the documentation on Communication between Fragments to see how you might implement on onLogin() or onRegistrationSelected() action to swap out to the registration fragment or "post-login" main fragment.
I have an image inside my group_row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#8fbfff" >
<TextView
android:id="#+id/tv_category_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="TextView"
android:layout_weight="1" />
<ImageView
android:id="#+id/img_add_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ico_add_item"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp" />
</LinearLayout>
I want to detect when this image was clicked and the id of the Group, and then StartActivityForResult. I was able to do it inside my custom ExpandableListView_Adapter:
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final Category category = arr_categories.get(groupPosition);
convertView = inflater.inflate(R.layout.row_category, parent, false);
((TextView)convertView.findViewById(R.id.tv_category_name)).setText(category.getCategory_name());
ImageView img_add = (ImageView) convertView.findViewById(R.id.img_add_category);
img_add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Add item to category: "+category.getCategory_id(), Toast.LENGTH_LONG).show();
// Declare on intent to use Add/Edit Note activity
Intent open_add_new_item = new Intent(context, Activity_Add_Edit_Base_Item.class);
// Pass the currently selected category ID to the Intent
open_add_new_item.putExtra("CURR_ITEM_CATEGORY", category.getCategory_id());
// Start the activity
((Activity) context).startActivityForResult(open_add_new_item, Constants.Request_Codes.REQUEST_CODE_CREATE_NEW_ITEM);
}
});
return convertView;
}
This works just fine. However, If there's a possibility to do so, I'd like to separate this image click detection from the Adapter and do it in my fragment (I think it'll be easier to implement OnActivityResult this way). I tried to do it by setting OnGroupClickListener for my ExpandableListView:
master_lv.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
final Category c = arr_all_categories.get(groupPosition);
ImageView img_add = (ImageView) v.findViewById(R.id.img_add_category);
img_add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Category c = arr_all_categories.get(groupPosition);
Toast.makeText(getActivity(), "group position: "+c.getCategory_id(), Toast.LENGTH_LONG).show();
// Declare on intent to use Add/Edit Note activity
Intent open_add_new_item = new Intent(getActivity(), Activity_Add_Edit_Base_Item.class);
// Pass the currently selected category ID to the Intent
open_add_new_item.putExtra("CURR_ITEM_CATEGORY", c.getCategory_id());
// Start the activity
startActivityForResult(open_add_new_item, Constants.Request_Codes.REQUEST_CODE_CREATE_NEW_ITEM);
}
});
return false;
}
});
However this didn't work at all: I'm not getting the Toast message and the Intent doesn't fire.
Is it possible to do so? If it is - how? Thanks!
The first one you need to do is set clickable=true to root layout of the custom cell xml.
After that, what we are going to do is Custom Event Raising. We will use interfaces.
Create a interface class
Example :
public interface OnImageClickListener {
public void onImageClicked();
}
Then create a instance in adapter
public OnImageClickListener mListener;
Also set OnClickListener to imageview in getView method of the adapter and add the following line in OnClick method.
mListener.OnImageClicked();
Lastly, in Activity;
mAdapter.mListener = new OnImageClickListener();
Magic will happen here :)
or you can implement this interface like
public MyActivity implements OnItemClickListener and let the implement methods.
Then you can
mAdapter.mListener = this;
Good luck there :)
I am working through the Big Nerd Ranch guide for android programming, and I am at the challenge for Chapter 16. The challenge is to make an EmptyView for a ListView, and then make a button on the EmptyView that adds stuff. I got the EmptyView to work but I can't figure out where I should make my button. Here is my code.
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v= super.onCreateView(inflater, parent, savedInstanceState);
inflater.inflate(R.layout.list_frame_layout, parent);
return v;
}
and here is my XML.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<LinearLayout android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/empty_no_crime" />
<Button
android:id="#+id/empty_new_crime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty_new_crime">
</Button>
</LinearLayout>
</FrameLayout>
The book is telling us to use fragments, hence the inflate. I figure the code should be
mNewCrime=(Button)getView().findViewById(R.id.empty_new_crime)
but that isn't working. Any ideas?
Edit*: Hmmm, apparently this also really isn't working that well. When I do add stuff, the EmptyView does not go away, it just gets pushed down while items are listed. Any ideas on how to make the EmptyView go away as soon as I add things?
I had trouble with this challenge at first as well. I over thought it! You have probably solved this issue by now but I thought it would be useful to post an answer for others. The following worked for me:
Create a new XML file specifying the "empty" and "list" views as you have done already.
Modify your existing onCreateView method to inflate the new modified layout which contains the "empty" and "list" views you have defined in your XML.
Create a new button and setup the onClickListener for the button.
Here is the code:
#TargetApi(11)
#Override
// We override the onCreateView to set the subtitle by default if we are rocking >3.0
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
super.onCreateView(inflater, parent, savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
if(mSubtitleVisible){
getActivity().getActionBar().setSubtitle(R.string.subtitle);
}// End inner if
}// End if
View v = inflater.inflate(R.layout.empty_layout, parent, false);
mNewCrimeButton = (Button)v.findViewById(R.id.add_crime);
//Define an click event listener for the button and launch the new crime fragment when clicked
mNewCrimeButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Crime crime = new Crime();
//Get the crimelab from the activity and add the crime
CrimeLab.get(getActivity()).addCrime(crime); //getActivity returns the activity this fragment is attached to
Intent i = new Intent(getActivity(), CrimePagerActivity.class);
startActivityForResult(i,0);
}//End onClick
});
return v;
}// End onCreateView
This should work with your existing xml layout. I hope this helps.
I too struggled initially with this, essentially solving it the same way the above poster did. However my problem was a bit different. I was getting bombed out of the application on startup, because my code that set up the onClick listener looked like this:
Button mCrimeButton = (Button)v.findViewById(R.id.crime_button);
mCrimeButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
initiateCrimeRecord();
}
});
It wasn't until I moved the declaration of mCrimeButton up to the class level making it an instance variable of the class that I was able to successfully execute the app:
public class CrimeListFragment extends ListFragment {
private static final String TAG = "CrimeListFragment";
private ArrayList<Crime> mCrimes;
private boolean mSubtitleVisible;
private Button mCrimeButton;
*
*
*
#TargetApi(11)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_empty_crime_list, parent, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if ( mSubtitleVisible) {
getActivity().getActionBar().setSubtitle(R.string.subtitle);
} else {
getActivity().getActionBar().setSubtitle(null);
}
}
// Set the button up on the empty view
mCrimeButton = (Button)v.findViewById(R.id.crime_button);
mCrimeButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
initiateCrimeRecord();
}
});
return v;
}
I then went back and noticed that in all the other examples in the book, the widgets that get manipulated are declared as private instances of the class. Why is this? Android doesn't allow you to just get a local instance to attach the listener?
I'm developing an app which on start up will show a pre-defined layout like Image(1) in below screenshot.
Now onclick of a button, I want to dynamically add another view like Image(2) in below screenshot to existing view resulting into some like Image(3) in below screenshot.
If onclick is clicked again, Image(2) will be added to existing view resulting into something like Image(4).
How do I achieve this? By searching, I found that it required something like LayoutInflater.addView() like this or LinearLayout.addView() like this.
But I don't know what exactly to use in my case.Also, I'm not trying to add just a single view on button click, but a group of certain views like imageview, 2 textviews,etc. as shown in Image(2).
Any help appreciated.
Edit 1:
I tried something like this:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:onClick="addViews"
android:text="Add" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
LinearLayout main;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (LinearLayout) findViewById(R.id.main);
}
public void addViews(View view) {
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setLayoutParams(lparams);
count++;
btn.setText("Hello World : " + count);
main.addView(btn, count);
}
}
It yields something like this:
Now, how do I recognize which button is clicked?
So, you can inflate a view from an XML layout from an Activity like this
View v = View.inflate(this, R.layout.whatever, null);
and then you can add it to your LinearLayout like this:
linearLayout.addView(v);
If you want to access the inner views in your items, you can do it like this:
TextView textView = (TextView) v.findViewById(R.id.textView1);
So, you have to define that group of views in a XML layout, inflate it, manipulate its views as you need, and then add it to your LinearLayout.
Note that you'll need your LinearLayout orientation to be vertical or it won't work as you need.
You can do a lot of things to get that working, but the best approach could be using ListView and ArrayAdapter
Create a class that extends ArrayAdapter<Integer>. There, create a interface to create a Listener.
public interface OnListButtonItemClickedListener{
public int onListButonItemClicked(int position);
}
Define a private OnListButtonItemClickedListener on your ArrayAdapter, and create a public setter.
private OnListButtonItemClickedListener listener;
public void setOnListButtonItemClickedListener(OnListButtonItemClickedListener listener){
this.listener = listener;
}
Define a button inside a Layout in XML. Something like this will do:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dip" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Create a inner ViewHolder class inside your ArrayAdapter class like this:
private class ViewHolder{
public Button b;
}
Override getView and create something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent){
ViewHandler vh;
if (convertView == null){
convertView = View.inflate(getContext(), R.layout.your_layout, null);
vh = new ViewHolder();
vh.b = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.b.setText(String.valueOf(getItem(i).intValue()));
vh.b.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if (listener != null){
listener.onListButonItemClicked(getItem(position).intValue());
}
}
});
return convertView;
}
Set the adapter to a ListView, and when you want to add a new one, just do this:
adapter.add(i);
adapter.notifyDataSetChanged();
Maybe you can try this
Create a custom view that extends LinearLayout, orientation: vertical.
Create another custom view, this view will be the "row". This view is the container of the image, text in bold and text below.
In the first custom view that extends linearLayout, you can addView(View v) and pass the other custom view, the row.
Am I clear? It is something similar that adapter and listview works.
I don't know if this fits whatever your app's purpose is, but try using a ListView with an ArrayAdapter. You will begin with an empty ListView, as defined in XML, then add items to a connected ArrayAdapter in code. Each time the button is pressed, you can add an image into the ArrayAdapter and call .notifyDataSetChanged(). This should stack them just as shown in your images above. You can also use a secondary LinearLayout to group items.
EDIT:
To determine which button is clicked you simply reference the View passed to your addViews(View v) method. You can either switch on the id:
public void addViews(View v){
int id = v.getId();
switch(id){
case R.id.id1:
//do something
case R.id.id2:
//do something
}
}
Or you can get the text from the button in a similar manner by using:
public void addViews(View v){
Button b = (Button)v; //make sure you know that it will be a button
String s = b.getText().toString();
switch(s){
case "test case 1":
//do something
case "test case 2":
//do something
}
}
If you aren't sure how many buttons there will be, I would suggest using the strings method. If the buttons won't have names that are convenient to parse in this manner, store references to the buttons as keys in a HashMap and use a String as the value. You can then plug in the button, get the string and do whatever is needed.