I have to display the textviews dynamically in android and need to write onclick action for each textview. I am able to display the textviews dynamically but I didnt get
how to write the onclick action for each textview. Please help me regarding this...Will be thankful to you..
You should check this code. Create an onclicklistener and then use setOnClickListener(); method.
private TextView textview1, textview2;
//initialize them
OnClickListener customTextviewOnClicklistener = new OnClickListener()
{
#Override
public void onClick(View v)
{
if (v == textview1)
{
// Here your code for textview1
Log.i("Clicked Item", "textview1");
}
else if (v == textview2)
{
// Here your code for textview2
Log.i("Clicked Item", "textview2");
}
else
{
//Here your code for others
}
}
};
textview1.setOnClickListener(customTextviewOnClicklistener);
textview2.setOnClickListener(customTextviewOnClicklistener);
i hope this may help you.
Create the views dinamically however you want to do and add the listener right then.
TextView tv = new TextView(ActivityName.this);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ActivityName.this, "tv text: " + ((TextView) v).getText().toString()).show();
//Do whatever you want to do here.
}
});
layout.addView(tv); //layout added on the xml for example, or by an inflater.
Related
I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html
I have this FlowLayout where I have a set of TextView's which I build programatically. After getting the wanted names, I create a TextView for each name inside the layout.
What I want to do, if I click on the TextView, I want to move it into another layout. I manage to do that but I also want to move it back. I could also do that until I program it to, but I can't program it to be like a infinite loop.
This is a piece of code which will make you understand better what I'm talking about hopefully.
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tvSelected = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tvSelected.setText(tv.getText().toString());
tvSelected.setLayoutParams(params);
tv.setVisibility(View.GONE);
filteredLayout.addView(tvSelected);
}
});
unfilteredLayout.addView(tv);
Is it possible to make it work? Thanks.
LE: As you can see in the onClickListener event of the TextView, I create the other TextView I add in the other layout, to move it back I could also add an onClickListener event to this TextView but this is not the solution.
Try following:
boolean isInFilterLayout = false; //Class variable
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isInFilterLayout){
filteredLayout.remove(tv);
unfilteredLayout.addView(tv);
isInFilterLayout = false;
}else{
unfilteredLayout.remove(tv);
filteredLayout.addView(tv);
isInFilterLayout = true;
}
}
});
unfilteredLayout.addView(tv);
I have a button that begins life "Unticked" with text going to a Label that says "NO". When you push the button it changes the image to "Ticked" and displays text in a Label as "YES". This all works perfectly. What I can't do or find is how to change it back to "Unticked" and "NO" if I then push it again?
Here is the code for the button:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
button9.setBackgroundResource(R.drawable.androidnearmisson);
TextView text = (TextView) findViewById(R.id.textView2);
text.setText("YES");
}
};
Any help will be greatly appreciated.
Many Thanks
You can get TextView's current text and make a comparison. If its YES, change to NO, else vice verse:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
TextView text = (TextView) findViewById(R.id.textView2);
if(text.getText().toString().equals("NO")){
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else {
button9.setBackgroundResource(R.drawable.otherimage); //Replace otherimage with proper drawable id
text.setText("NO");
}
}
};
Hope this helps.
First move this code to your onCreate :
TextView text = (TextView) findViewById(R.id.textView2);
Inside your onClick :
if(text.getText().toString().equals("NO"))
{
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else
{
//change what you want
}
I have a layout which contains some views. I want to set some actions when a user clicks anywhere on the layout, so I have set an OnClickListener for the layout which works as it should. But how can I know which view was clicked?
I want to assign different actions depending on the view which was clicked; or maybe no view was clicked and it was only the layout itself.
So the problem is if I set OnClickListener for one of the views, both OnCLickListeners related to the layout and the view will get activated, but I want only the view action. Thanks in advance.
Other answers are quite abstract and some are incorrect. You can't Override onClick method for an Activity as it doesn't have one (unless of course you implement an OnClickListener).
Furthermore, if you set the listener for the layout the View argument received by the onClick method will always be the layout.
You can either create your own custom layout that intercepts the clicks and check if you clicked on a view, or you can set listeners for all the views.
A somewhat cleaner solution is using a single listener and checking if it's a view that was clicked.
private final String TAG = "MainActivity";
List<View> views = new ArrayList<View>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
TextView tv1 = (TextView) findViewById(R.id.tv1);
TextView tv2 = (TextView) findViewById(R.id.tv2);
TextView tv3 = (TextView) findViewById(R.id.tv3);
views.add(tv1);
views.add(tv2);
views.add(tv3);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isView = false;
for (View view : views) {
if (v.equals(view)) {
isView = true;
break;
}
}
if (isView) {
Log.e(TAG, "Click on view");
} else {
Log.e(TAG, "Click on layout");
}
}
};
tv1.setOnClickListener(clickListener);
tv2.setOnClickListener(clickListener);
tv3.setOnClickListener(clickListener);
rLayout.setOnClickListener(clickListener);
}
If you want different actions for different views, just create a listener for each of them.
For example Layout has two views View1 and View2. I assume that you have already inflated them. So you have to set OnClickListiner for each of them
Layout.setOnClickListener(this);
View1.setOnClickListener(this);
View2.setOnClickListener(this);
Then you have to override method:
public void onClick(View v)
{
switch(v.getId())
{
case R.id.id_for_layout:
// do what you want when user click layout
break;
case R.id.id_for_first_view:
// do what you want when user click first view
break;
case R.id.id_for_second_view:
// do what you want when user click second view
break;
}
}
Remember that the method OnClick have one parameter that indicates which View is clicked!
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn1:
// first button
break;
case R.id.btn2:
// second button
break;
}
}
Assign the id to each of your views by android:id="#+id/v1"
then in
onClick(View v)
check for the view like
if(v == findViewById(R.id.v1))
{
// v1 specific action
}
else if(v == findViewById(R.id.v2))
{
// v2 specific action
}
Ok, it's easy.
You have to add an OnClickListener to each view (button, textView and so on), and to the whole layout as well.
In this case, the onClickListeners that will be launched if you click are the view's onClickListener and the layout's onClickListener. So no need for a whole bunch of classes. One will be enough if you try this (do actions depending on the id and don't forget the layout ;) ):
public void onClick(View v) {
switch(v.getId()) {
case R.id.layout:
layout actions here
break;
case R.id.textview:
textview actions here
break;
this onClickListener is for all of your views ;)
There is View v parameter in onClickListner(View v) method :
#Override
protected void onCreate(Bundle savedInstanceState) {
LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
layout.setOnclickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.linearLayout){
//your logic
}
}
for anyone interested you can also log the currently clicked view:
getActivity()
.getWindow()
.getDecorView()
.findViewById(android.R.id.content)
.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.v("[onClick]", v.getClass().getCanonicalName());
}
return false;
});
In my application i have added textviews dynamically using array,and given id's for each with a count variable, which increment's count according to the addition of textviews.In onClickListener of each textview i want to perform some oparations,but when i'm trying to do this operation is getting performed on all textviews.
Below is the code,i'm not getting what's wrong.please help me.
// here i have added textview dynamically
mtxtview[colTextCount]=new TextView(this);
mtxtview[colTextCount].setId(colTextCount);
mtxtview[colTextCount].setLayoutParams(new LayoutParams(20,20));
And In onclickListener-
#Override
public void onClick(View v) {
System.out.println("onclick...");
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
//if i added 3 textview.its giving me all 3 textview's text(getText())
}
}
use switch-case and View.getId() to check which TextView is Clicked before starting for loop . try it as :
#Override
public void onClick(View v) {
System.out.println("onclick...");
switch(v.getId())
{
case _mTextViewId:
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
}
break ;
// same for others....
}
}
see TextView onClick() not working
You should do setOnClickListener for each TextView Object.