Retrieving view name property in android? - android

Is there anyway to retrieve the name of a view in android? Have been walking through the api and haven't seen any method to do this.

View only contains id not name, I think it's not able to retrieve it.
From your comment, I think you can put the index to the android:tag attribute
such as
<RadioButton android:tag="1" />
Then you can invoke button.getTag to fetch the tag value. Hope this helps you get rid of the switch case statements..

use view.getId();, which is usually used in onClick(View v) method
// from View.OnClickListener
#Override
public void onClick(View v){
switch( v.getId() ) { // <= this is it
case R.id.first_id:
// do something
break;
}
}

Related

Is there a way to write a same onclick function for multiple controls?

I have came across a problem where i have multiple text view that should have the same onclick function. I can obviously write multiple onclick but i want an optimized code.
For example i have textviews of color red and othe textviews of color blue and i want all red to have same onclick and all blue to have a same onclick function different than red.
Is there a way to achieve this in android java?
declare your click event in a variable from type View.OnClickListener and then assign it to your textviews like this:
View.OnClickListener eventVar = new View.OnClickListener() {
#Override
public void onClick(View view) {
//do what you need
}
};
textview1.setOnClickListener(eventVar);
textview2.setOnClickListener(eventVar);
textview3.setOnClickListener(eventVar);
textview4.setOnClickListener(eventVar);
Assign an id for red colour and another id for blue. And write a switch statement, if as below
switch (id)
case 0:
runAmethod();
break;
case 1:
runAnotherMethod():
break;
default:
Write a common function for your all the color textview like
private void clickOfRedTextView(){
// your code.
}
Now implement your activity or fragment with OnClickListener listener and pass the on click
textviewred1.setOnClickListener(this);
textviewred2.setOnClickListener(this);
// so on ...
In the override method of onclick you can have a switch case with view id as follow.
public void onClick(View v) {
switch (v.getId()) {
case R.id.textviewred1:
case R.id.textviewred2:
so on....
// your function you want to call will come here.
clickOfRedTextView();
break;
}
}
You can use this simplest way:
Define your onClick in your xml file for your red type textviews and blue type textviews like this.
<TextView
//other attributes...
android:onClick="redTextViewMethod"/>
<TextView
//other attributes...
android:onClick="blueTextViewMethod"/>
And then define the redTextViewMethod method in your corresponding Java file like this
private void redTextViewMethod() {
//your code
}
private void blueTextViewMethod() {
//your code
}
Solution:
First, create common public method for getting TextView click,
public void onTextViewClick(View v) {
switch (v.getId()) {
case R.id.VIEW_ID:
case R.id.VIEW_ID:
case R.id.VIEW_ID:
// Performs action for Blue Text View
break;
case R.id.VIEW_ID:
case R.id.VIEW_ID:
// Performs action for Red Text View
break;
}
}
Then, in your xml write TextView onClick like this,
<TextView
...
android:onClick="onTextViewClick"/>

how to check which image view i have added in xml programmatically?

I added some imageviews in xml layout. Now programmatically i need to check which imageView i have added from the xml file and if same image then on click of that image i need to replace new image and visa versa.
Is the way i implemented it good or can I find a better solution for this?
This is my code so far:
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.bankTickImg.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.black_untick))) {
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}else{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});
Imageview Code
<ImageView
android:id="#+id/defaultBankTick"
android:layout_width="#dimen/TwentyFour"
android:layout_height="#dimen/TwentyFour"
android:layout_alignParentRight="true"
android:layout_marginBottom="#dimen/FiftyFive"
android:layout_marginRight="#dimen/Thirty"
android:layout_marginTop="#dimen/FiftyFive"
app:srcCompat="#drawable/black_untick" />
If i got your question.
Why don't you use a integer/boolean variable to save the state. eg.
lets say, you start with
int clicked = 0;
then in on click check if its value it 0 then treat it as fresh and change the value to 1.
and if the value is already 1 then treat it as it clicked 2nd time and change it to 0 .
There are many ways to check imageView which you added
holder.bankTickImg.setImageResource(R.drawable.black_untick); //default image , you can also set in xml code
holder.bankTickImg.setId(0);//you also do with string setTag();
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int istick=holder.bankTickImg.getId();
if (istick==1)
{
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}
else
{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});

ViewPager change to another page on textview click

I have used an example code from the following site:
http://www.journaldev.com/10096/android-viewpager-example-tutorial
It works but I need to add another page where there is a textview one can click to go to one of the pages (for eg. BLUE page). I have put the TextView in the xml file with the 2 lines:
android:clickable="true"
android:onClick="click"
I have also added the following incomplete method in the MainActivity.
public void click(View view) {
switch (view.getId()) {
case R.id.change_page:
//what do I need to add in here?
}
}
I am not sure I am setting this up the right way. Any advice will be helpful.
case R.id.change_page :
mViewPager.setCurrentPage(yourpageindex);
break;
case: R.id.another_text view:
//go to another page
create another activity, and separate layout file, and use an explicit intent in the main activity,
in the new layout file , use the desired ViewPager
probably this may work .
as #hash-set suggested. setCurrentPage(yourpageindex); will do the trick.
all you need to know is what is index value of your BLUE page. and set it in place of yourpageindex.
for Example : let's assume index of Your Blue page is "1".
public void click(View view) {
switch (view.getId()) {
case R.id.change_page:
viewPager.setCurrentItem(1);
}
}

What are these values printed by Log.d("LOG","Value of v: " + v) and how do I use them? Android

If I have an onClick for example:
public void onClick(View v) {
Log.d("LOG","Value of v: " + v);
}
I am getting the below output:
v android.widget.Button{424b9f10 VFED..C. ...P.... 0,2-70,72 #7f0a0080 app:id/rowButtonRemove}
I was reading android.widget.Button's android reference documents, but still couldn't figure out what they are, but what I know for sure is that I want to grab this particular unique value "424b9f10" somehow and store it into a variable. Can someone tell me what it is, and how I can put it into a variable?
you can use this
String s = v.gettext().toString();
and after that you can set that in some text view o where ever you want.
for example
text1.settext(s);
Try this and please let me know
you are trying to log an object and you shouldn't do that!
you can use v.getId() instead.

How to get the value of pressed button?

I am searching the solution, how to get the value of the button which is pressed.
When I try something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button numb1 = ((Button)this.findViewById(R.id.numb1));
numb1.setOnClickListener(this);
}
public void onClickHandler(View v){
String pressed = null;
switch (v.getId()) {
case R.id.numb1:
pressed=numb1.getText().toString();
break;
//OR
case R.id.numb1:
pressed=R.id.numb1.getText().toString();
break;
}
new AlertDialog.Builder(this).setTitle("Info").setMessage(pressed).setNeutralButton("Okey", null).show();
}
Both cases in switch are unfortunately bad.
And I still can't get the value of the pressed button... Can you help me please with this problem yet?
Thank you.
Well guys, I am pretty new in this area, but I solved this problem like that:
public void onClick(View view) {
int intID = view.getId();
Button button = (Button) findViewById(intID);
String message = button.getText().toString();
}
pressed=((Button)v).getText(); should do the job.
Also, let your activity implement View.OnClickListener and instead of onClickHandler() override the method public void onClickHandler(View v) with your implementation.
try this.
Button sender = (Button)v;
sender.text;
Looking at your code, is it possible you have two variables with the same name but different scopes causing you confusion?
In onCreate, you declare Button numb1, but onClickHandler appears to expect numb1 to have also been declared outside of onCreate. So, should onCreate just assign a value to numb1, instead of also declaring it?
At any rate, if you were to post a more complete code example, it may help others to identify the problem, instead of just guessing about how things are.

Categories

Resources