how to click on textview in android - android

Click on text view is not working
My xml is
<TextView
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="3"
android:gravity="center"
android:textColor="#color/white"
android:text="click to download sevenstepstocollege"
android:textSize="15dp"
android:onClick="downloadLink"
android:clickable="true">
</TextView>
and My Activity code is
public void downloadLink(View v)
{
//String requestId = PurchasingManager.initiatePurchaseRequest(skuKye);
//String requestId=PurchasingManager.initiateItemDataRequest("DeveloperSKU-1234");
skuSet.add(skuKye);
final String requestId = PurchasingManager.initiateItemDataRequest(skuSet);
}
But it is not working.I am not able to click that link.please guide me

Well, I use the following code to make a TextView clickable.
First, add this at your activity.xml, to make TextView clikable:
<TextView
android:id=android:id="#+id/button2" <!-- id to get this TextView -->
(...)
android:onClick="onClick" <!-- Add the function onClick() -->
android:clickable="true" <!-- Set the boolean clickable to true -->
/>
Then, at your MainActivity.java, you add:
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the TextView by id
textview = (TextView) findViewById(R.id.button2);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO when clicked on your link(TextView)
}
});
}
As I see now that you are trying to make a link from the TextView clickable, not just be able to click on the TextView I will let a link below to a similar question solved in Stack Overflow that you might find helpful.
The android developer reference to TextView:
https://developer.android.com/reference/android/widget/TextView.html
The similar question in Stack Overflow about how to make a links in a clickable that might be helpful:
How do I make links in a TextView clickable?

You may use like this
<TextView
android:id="#+id/topPaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:clickable="true"
android:text="#string/topPaid"
android:textColor="#000000"
android:textColorLink="#33CC33" />
and At activity
TextView topPaid = (TextView) findViewById(R.id.topPaid);
Linkify.addLinks(topPaid, Linkify.ALL);
topPaid.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//write ur logic
}
}

TextView textview = (TextView) findViewById(R.id.text);
textview.setText(Html.fromHtml("<b>Text:</b> Text with a " + "link " + "Created in the Java source code using HTML."));

XML Code
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
JAVA Code
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(Html.fromHtml("google "));
textView.setMovementMethod(LinkMovementMethod.getInstance());

In your XML as the following:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onTextViewPressed"
/>
In your attached activity
public void onTextViewPressed(View view) {
...
}

Related

Text Fields in android-studio

Can someone tell me how to use a Text Field with an onclick listener on a button in android studio and give me the xaml code too.
First you need write in xml file what you need, we need button and textview:
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="textEdit"
android:layout_marginLeft="5dp"
android:textColor="#fff"
android:layout_marginRight="2dp"
android:background="#333"
android:onClick="ButtonOneClick"
android:layout_marginBottom="4dp"
/>
<TextView
android:id="#+id/txtView"
android:layout_width="50dp"
android:layout_height="30dp"
android:hint="text view"
/>
After xml you need define this in class. Because we define android:onClick="ButtonOneClick" in xml,
we can just call it , and set what you want to do.
TextView tv;
tv =(TextView)findViewById(R.id.txtView);
public void ButtonOneClick(View view) {
tv.setText("Test");
}
Other method is to declare button to, and set onClickListener.
Button butt;
butt= (Button) findViewById(R.id.button1);
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv.setText("Test")
}
});
in tampil.xml
<Button
android:id="#+id/button1"
android:text="PENCET"
android:onClick="onPencet" />
and in u're class, use public void nameonClick(view v){ statement }
public class Tampil {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onPencet(View v){
**U're statement }
}

Programmatically adding EditText box

I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout 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"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());

Can't get text from edittext?

I have updated to ADT version 20. After that I can't use EditText. For API level 16 it shows some error. For lower API levels there is no error but I can't get the input of EditText. It shows an empty result.
Sample code I used:
java code
public class sample extends Activity {
EditText edt;
Button btn;
TextView txt;
String str;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView2);
str = edt.getText().toString();
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
}
}
XML CODING
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Search"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10pt"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
Actually, you are trying to get text from EditText in onCreate() of Activity, which has no any text contains yet.
So Just put the line, str = edt.getText().toString(); in button's click() then you will get the result,
Like,,
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
str = edt.getText().toString();
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
Your statement str = edt.getText().toString(); should be inside the onClick handler.
The code cannot access the value because after onCreate ends the value found in the EditText abd str is beyond the scope. So put the str = edt.getText().toString(); statement inside the onClick so that when user clicks the button it can access the value.
I think you are trying to get EditText (i.e. edt) value in onCreate() and I think you are entering value after the completion of onCreate() method.So that means you are not getting value for your str.
That's why you are not able to set the value to TextView.
just put text inside your button otherwise it will not work becz whatever u are getting in strings is also working like u are just cre

findResourceById() returning null

I've read a few questions and answers regarding this topic, but none of them applied to my issue.
My problem is in the following code:
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.alt);
}
public void buttonPress(View v){
TextView tv1 = (TextView) findViewById(R.id.text1);
TextView tv2 = (TextView) findViewById(R.id.text2);
Button b1 = (Button) findViewById(R.id.button1);
if((tv1 != null) & (tv2 != null) & (b1 != null)){
tv1.setText(R.string.changed);
tv2.setText(R.string.changed);
b1.setText(R.string.changed);
}
else
Toast.makeText(getApplicationContext(), "VIEW ELEMENTS ARE NOT BEING ASSIGNED", Toast.LENGTH_SHORT).show();
}
alt.xml looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:name="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/stuff" />
<TextView
android:name="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stuff2" />
<Button
android:name="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonText"
android:onClick="buttonPress" />
The IDs are being generated in R.id, but they aren't being assigned to my two TextView elements and my one Button element. (I'm seeing the toast popup every time)
Does anyone have any idea?
Instead of android:name="..." use android:id="#+id/..."
Try replacing
android:name="#+id/text1"
By
android:id="#+id/text1"

How to add a text under the image button in Android?

I want to add text under a image button, which are also clickable, and will be redirected to the same activity as the image button. basically it's like the app list in any android phone, e.g.
If you have your ImageButton declared in XML, then just put it into a LinearLayout which also contains a TextView and set the onClickListener on the LinearLayout. The structure would be like
<LinearLayout
... > <!-- This is the LinearLayout for the xml document -->
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton
... />
<TextView
... />
</LinearLayout>
</LinearLayout>
And then in your java:
LinearLayout layout = (LinearLayout)findViewById(R.id.linLayout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
If you're adding each ImageButton dynamically via java code, then it will still maintain the same structure. Let me know if I need to add anything.
setAlpha and do the below
<Button
android:id="#+id/comparePack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:onClick="compareButtonClick"
android:text="#string/compare"
android:drawableTop="#drawable/compare1edit"
android:gravity="left|center_vertical" />
if you want to create this king of list use gridview
and in getview method inflate customlayout with imageview and textview
Also you can set onClickListener to TextView
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton android:id="#+id/btn1"
... />
<TextView android:id="#+id/tv1"
... />
</LinearLayout>
java code:
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});

Categories

Resources