Using setOnClickListener() in dynamically generated TextView - android

This is the TextView Layout.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trailer"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp"/>
This is the main root layout.
<LinearLayout
android:id="#+id/review_layout"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
And this piece of code add the textViews as childView of the above linear layout, based on total number of response from server.
#Override
public void addData(List<MovieVideoData> movieVideoDataList) {
trailerLinearLayout.removeAllViews();
List<TextView> textViews = new ArrayList<TextView>();
for (MovieVideoData movieVideoData:movieVideoDataList){
View view = getActivity().getLayoutInflater().inflate(R.layout.trailer_text_view,trailerLinearLayout,false);
((TextView)view.findViewById(R.id.trailer)).setText(movieVideoData.getName());
textViews.add(((TextView)view.findViewById(R.id.trailer)));
trailerLinearLayout.addView(view);
}
}
Now, for each TextView objects which was added in the Linear Layout, on the click of those i want to open an youtube video link specific to that textView.
I was wondering, how should i use the setOnClickListener() on each textView as id's of all are same.
Any suggestions, would be appreciated.
I added this piece of code and it's working fine.
private void watchYoutubeVideo(String key){
try{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + key));
startActivity(intent);
}catch (ActivityNotFoundException ex){
Intent intent=new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v="+key));
startActivity(intent);
}
}
textView.setText(movieVideoData.getName());
trailerLinearLayout.addView(view);
final String key = movieVideoData.getKey();
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
watchYoutubeVideo(key);
}
});
On click of each textview, it's opening its respective link on youtube, which is fine.
What i am not sure of is, after movieVideoDataList completes looping, and then on click of text view, how it remembers the specific key, as it inside the loop.
Any type of clarification on this would be appreciated.

I might be missing something, but why not just do it like this:
#Override
public void addData(List<MovieVideoData> movieVideoDataList) {
trailerLinearLayout.removeAllViews();
List<TextView> textViews = new ArrayList<TextView>();
for (MovieVideoData movieVideoData:movieVideoDataList){
View view = getActivity().getLayoutInflater().inflate(R.layout.trailer_text_view,trailerLinearLayout,false);
TextView tv = (TextView)view.findViewById(R.id.trailer);
tv.setText(movieVideoData.getName());
textViews.add(tv);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the url of your video and action you want to perform
}
});
trailerLinearLayout.addView(view);
}
}

Related

Custom View Snackbar from XML file will not display bottom of screen

We have tried two ways to display a Custom Snackbar (1) as a masquerading Dialog which will not move to the bottom of the screen It does however not dismiss the current Activity view just makes it opaque. I know why it is in the center of the screen but I am not able to move it to the bottom. (2) next is a view that takes over the entire screen because it is a new content view that I am guessing dismisses the current Activity view BUT it is at the bottom of the screen.
So my question is how to use design number 1 and move the Dialog to the bottom of screen?
Second question how to stop the new view in design number 2 from dismissing the view of the current Activity? After careful reading and little thought and extreme testing I do not think this is possible! I have posted the code for my two methods below. The XML file uses a Relative Layout as the base container.
public void seeSB(){
setContentView(R.layout.custom_snackbar);
// Line of Code above shows XML file
// Line of code tested but no control over the "viewMyLayout"
//LayoutInflater inflater = LayoutInflater.from(ListActivity.this);
//final View viewMyLayout = inflater.inflate(R.layout.custom_snackbar, null);
//viewMyLayout.setEnabled(true);
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// viewMyLayout.setEnabled(false);
// Line above does not function
// CODE BELOW WORKS BUT FAR FROM elegant
setContentView(R.layout.activity_list);
//Intent intent = new Intent(ListActivity.this, ListActivity.class );
//startActivity(intent);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
public void displaySB(){
final Dialog openSnack = new Dialog(context);
openSnack.setContentView(R.layout.custom_snackbar);
Button btnAB = (Button)openSnack.findViewById(R.id.btnAB);
TextView tvSB =(TextView)openSnack.findViewById(R.id.tvSB);
//Dialog dialog = new Dialog(ListActivity.this);
//dialog.setContentView(Bottom);
// if YES delete Master Password from TABLE_MPW
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSnack.dismiss();
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
openSnack.show();
}
This is far from functional in my book because the method design has just one Custom Snackbar to look at so you need to work on how to have multiple fixed Custom Snackbars. One suggestion might be to have multiple sub views in your parent view and call the sub view you want. I will post just the sub view I added to the parent XML file and the not so real dynamic method to implement which is implemented in this case with a button click. For this to work in a real application the code would need be called from some method or event.
You might consider a switch statement for multiple views ? ? ?
TAKE NOTE THE RELATIVE LAYOUT has its visibility set to GONE at the start
<RelativeLayout
android:id="#+id/hold_snackbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_Black"
android:visibility="gone"
android:orientation="vertical">
<TextView
android:id="#+id/tvSB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="#string/snackbar_text"
android:textColor="#color/color_Yellow"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="350dp"
android:layout_marginTop="5dp"
android:background="#color/color_Transparent"
android:focusable="false"
android:text="#string/snackbar_action"
android:textColor="#color/color_Red"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
Notice the View subViewGroup is declared when the Activity starts
View subViewGroup;
public void makeSB(View view) {
subViewGroup = findViewById(R.id.hold_snackbar);
subViewGroup.setVisibility(View.VISIBLE);
seeSB();
}
public void seeSB(){
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
subViewGroup.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
Countdown Timer to close a Snackbar with no Action Button
public void makeCDT(View view) {
cdt = new CountDownTimer(5000, 100) {
// 5 sec 5000,100
// 10 sec 10000,100
#Override
public void onTick(long secsUntilFinished) {
etCPW.setText(String.valueOf(secsUntilFinished / 1000));
//etCPW.setText("seconds remaining: " + secsUntilFinished / 1000);
subViewGroup = findViewById(R.id.SB_NO_ACTION);
subViewGroup.setVisibility(View.VISIBLE);
}
#Override
public void onFinish() {
etCPW.setText("Counter Done");
subViewGroup.setVisibility(View.GONE);
if(cdt!=null){
cdt.cancel();
}
}
};
cdt.start();
}

I want to use clickable text (Android)

I want to use clickable text to move from main activity to another activity, can somebody give me a sample code to understand that how this works.
TextView textview = (TextView) findViewById(R.id.textView);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,AnotherActivity.class);
startActivity(i);
}
});
In the layout of your activity, define the onclick on the textview item.
<TextView ....
android:onClick="onClick"
android:clickable="true" ... </TextView>
OnClick Method
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);

Re-using same button for multiple rows in table layout

I have a Table layout that contains 3 rows with each row having a character name, current attributes and a button for re-rolling character attributes.
Like this:
Malarku s:10 i:15 c:5 [Re-Roll]
Zipak s:17 i:3 c:22 [Re-Roll]
Ambra s:6 i:24 c:7 [Re-Roll]
My problem is the button, because it has an id attached to it like this:
<Button
android:id="#+id/ButtonRollStats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_rollStats"
android:tag="#string/char1_name" />
And in my main activity code file, I have this:
Button rollStats = (Button) findViewById(R.id.ButtonRollStats);
rollStats.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddStatsView(view);
}
});
public void AddStatsView(View view) {
Intent intent = new Intent(this, DisplayStatsView.class);
String tag = view.getTag().toString();
intent.putExtra(EXTRA_NAME, tag);
startActivity(intent);
}
So I can't use the same button ID in each table row but I need the ID to find it and I don't want to have to use a different ID and method for each button when they all do the same thing(rolling stats).
How do I get around this issue?

how do i switch between sets of images?

Hi I'd like to create 2 sets of images where one set is displayed if the user selects something from a radio button and the other is displayed if the user selects the other radio button,
In truth I'm an amateur at this and it may not even be radio buttons that I want but its one set of images for a girl and another for a boy, any suggestions welcome
this is what I did... its inside an edittext but it shouldnt make any difference :
<EditText
android:id="#+id/exerciseInputFilter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="search..."
android:textColor="#android:color/holo_blue_light" />
<ImageView
android:id="#+id/drillListSearchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/exerciseInputFilter"
android:layout_alignParentRight="true"
android:src="#drawable/search_white" />
in the xml for example... frame is also possible...
and in code...
SearchImage = (ImageView) findViewById(R.id.drillListSearchImage);
SearchImage.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!filterText.getText().equals(null)) {
filterText.setText("");
SearchImage.setImageResource(R.drawable.search_white);
}
return false;
}
});
so just create 1 image view and then on touch do something...
if you want to use a radio or checkbox its the same principle... on item click... set imageview to whatever you want...
rd2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rd1.setChecked(false);
SearchImage.setImageResource(R.drawable.search_white);
}
});
for example... does this answer your question?
Edit
after reading your comment (which wasnt understandable from your original question)... what you want is just to pass information between activities to do that use this:
Activity A:
create a boolean and a 2 check or radios and
CheckBox maleBox = (CheckBox) findViewById(R.id.Male);
maleBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
isMale = true;
isFemale = false;
femaleBox.setChecked(false);
}
}
});
you can do the same for female if you want several radios or checkboxs... then...you pass the information to activity two... like so:
intent = new Intent(whateverclass.this, whoeverclass.class);
intent.putExtra("Gender", isMale);
startActivity(intent);
now you can read it in activity two with:
if (getIntent().getBooleanExtra("Gender", false)) {
// set Images or buttons or what ever...(like i showed you above)
SetImage1.setImageResource(R.drawable.boy);
.
.
.
}
you can also use bundle to get your variables...
Intent intent = new Intent();
intent = getIntent();
Bundle bundle = intent.getExtras();
then just get the boolean...

Android position buttons at bottom of screen

Hi i'm having an issue with placing buttons at the bottom of the screen i have laid it out and in the graphical layout in eclipse it looks exactly how i want it but when i run it on the device the buttons both go to the very bottom of the screening the same position. so only one button is visible. does anyone know why this is or do you know how to place thing s at the bottom with a bit of padding?
heres what i have tried
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/ic_background_credits"
android:orientation="vertical" >
<Button
android:id="#+id/fm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:background="#drawable/fmbtn" />
<Button
android:id="#+id/ph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fm"
android:layout_alignLeft="#+id/fm"
android:background="#drawable/visitphbtn" />
</RelativeLayout>
heres my code where i inflate this view
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Check Preferences which sets UI
setContentView(R.layout.singlenews);
findViewById(R.id.progress).setVisibility(View.GONE);
loadData();
Button backbtn = (Button) findViewById(R.id.backbtn);
//Listening to button event
backbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent previousScreen = new Intent(getApplicationContext(), InfoActivity.class);
startActivity(previousScreen);
}
});
}
public void loadData(){
name = getIntent().getStringExtra("name");
Log.v("lc", "infoname=" + name);
if (name.equals(name1")) {
NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.infodetail,
null);
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText("About name1");
TextView mainText = (TextView) NewsView.findViewById(R.id.maintext);
mainText.setText("name1");
ImageView NewsImage = (ImageView)NewsView.findViewById(R.id.imageView2);
NewsImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_evostik));
}
if (name.equals("name2")) {
NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.infodetail,
null);
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText("About name2");
TextView mainText = (TextView) NewsView.findViewById(R.id.maintext);
mainText.setText("name2");
ImageView NewsImage = (ImageView)NewsView.findViewById(R.id.imageView2);
NewsImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_pitchero));
}
if (name.equals("name3")) {
NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.creditsdetail,
null);
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText("name3");
Button phbtn=(Button)NewsView.findViewById(R.id.ph);
phbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Uri uri = Uri.parse("http://www.pitchero.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Button fmbtn=(Button)NewsView.findViewById(R.id.fm);
fmbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Uri uri = Uri.parse("http://www.fantasticmedia.co.uk/mobile/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
ListView list = getListView();
list.setVerticalFadingEdgeEnabled(false);
Log.v("BGThread", "Filled results");
adapter = new MergeAdapter();
adapter.addView(NewsView);
setListAdapter(adapter);
}
}
I think you have some attributes mixed up.
In your second button don't add the '+' operator to id because it's making a new one. Just remove it and leave the '#'.
Remember '+' adds to id and '#' references it.
Well relative layout is relative to the other items inside the RelativeLayout container which is a ViewGroup. So for example you could specify android:layout_leftOf="#+id/fm" in the description of pm. If you want padding you might also expiriment with margin and padding like inside the buttons.
android:layout_marginBottom="20dip"
android:layout_leftOf="#+id/fm"
android:paddingBottom="10dip"
I actually try your Layout in my project and the two buttons are correctly placed.
Are you sure you didn't make any other actions in your code ?

Categories

Resources