How to convert textview as link in android? - android

I have created a login page which consists of a username password login button and a signup button which should be a link. I want to convert this signup(text view) to a link that can be navigated to the next page. How would I do that? For example if I click on don't have an account? sigup,it should be navigated to welcome.java..
Please help....
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"
android:background="#drawable/bg_gradient"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="45dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_form_rounded"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#null"
android:hint="#string/email"
android:padding="5dp"
android:singleLine="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:hint="#string/password"
android:inputType="textPassword"
android:padding="5dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#drawable/bg_button_rounded"
android:text="#string/login"
android:textColor="#color/white" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:text="#string/signup"
android:autoLink="web"
android:textColor="#color/white" />
</RelativeLayout>
mainactivity.java
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getActionBar().hide();
}
}

Try this :
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
TextView txtSignUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSignUp=(TextView)findViewById(R.id.txt_sign_up);
txtSignUp.setText(Html.fromHtml("<u>Dont have account?SignUp</u>")
txtSignUp.setOnClickListener(signUpListener);
//getActionBar().hide();
}
public OnClickListener signUpListener=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Welcome.class));
}
};
}

You have to setOnTouchListener to the TextView. Here I give you an example:
public class MainActivity extends Activity implements View.OnTouchListener {
private TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// HERE YOU HAVE TO CHECK WHICH ID IS SELECTED, IF YOU HAVE MORE THAN ONE,
// AND WHICH EVENT HAS THE USER DONE.
// IN YOUR CASE.. OPEN WEBSITE LIKE THIS:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
return true;
}
}

you can try Linkify here
textview.setText("www.androiddeveloper.com");
Linkify.addLinks(textview, Linkify.WEB_URLS);

Related

Changing Activities through a button and display xml

I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main xml file
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}

Add checkboxes on click

I have an image file on which if clicked anywhere I would like X'd checkboxes to show up. (Right now its set up as check marks). The code that I have is just for connecting page 1 to page 2 with a button click. Here it is
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnclickButtonListener();
}
public void OnclickButtonListener() {
button1 = (Button) findViewById(R.id.nextpage);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText userTextEntry = (EditText)findViewById(R.id.BRinfo);
String userdata = userTextEntry.getText().toString();
int userNumber = Integer.parseInt(userdata);
Intent intent = new Intent(MainActivity.this,Page2.class);
intent.putExtra("para name",userNumber);
startActivity(intent);
}
});
}}
Part of the XML code is
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="vt.tacticalcombat.MainActivity"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/combat2"
android:layout_alignBottom="#+id/editText17"
android:layout_alignLeft="#+id/last4"
android:layout_alignStart="#+id/last4"
android:layout_alignRight="#+id/tableLayout"
android:layout_alignEnd="#+id/tableLayout"
android:layout_alignTop="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:id="#+id/nextpage"
android:layout_alignBottom="#+id/tableLayout"
android:layout_alignRight="#+id/otherinfo"
android:layout_alignEnd="#+id/otherinfo"
tools:ignore="HardcodedText,RtlHardcoded"
android:nestedScrollingEnabled="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox28"
android:checked="false"
android:visibility="visible"
android:layout_alignBottom="#+id/rarmtypeinfo"
android:layout_toRightOf="#+id/mvc"
android:clickable="true"
android:layout_toEndOf="#+id/mvc" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_alignBottom="#+id/checkBox28"
android:layout_alignRight="#+id/injury"
android:layout_alignEnd="#+id/injury"
android:layout_alignTop="#+id/larmtype"
android:layout_alignLeft="#+id/checkBox28"
android:layout_alignStart="#+id/checkBox28" />
</RelativeLayout>
I tried using this link! but it did not work. How can I do this? Any help is appreciated

how to display value of textbox on same screen in android application just like whatsaap

This is my main file where i am calling the click event on button to pass the value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle("aakash");
SetContentView(R.id.lst);
Button b = (Button) findViewById(R.id.sendbtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(android.view.View v) {
startActivity(new Intent(View.this,View.class));
}
});
}
**xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ScrollView
android:id="#+id/lst"
android:background="#fefefe"
android:layout_below="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_holder"
>
</ScrollView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
I want the value of textbox to display on the same screen(scrollview)box and also i want to pass a httppost request to pass a value..plz help me out
Thnks in advance
I think, you can use a ListView insted of scroll view. and add list items from the chat text value dynamically.
call notifydatasetchanged() every time when you add a new item to it.
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ListView
android:id="#+id/listView_chats"
android:layout_width="match_parent"
android:layout_height="100dp"
>
</ListView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
Write a custom adapter for the listView and add the code to manage the list data.
i got the answer to my question
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
public void setText(){
Button myButton = (Button)findViewById(R.id.button1);
final TextView myText = (TextView)findViewById(R.id.text1);
final EditText myInput = (EditText)findViewById(R.id.edit);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
myText.setText(myInput.getText());
}
});
}
}

Setting a ProgressBar to VISIBLE on click in android

I am trying to make my ProgressBar turn VISIBLE when the LogIn button is clicked. Here's my XML for the Activity:
<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"
android:gravity="center_vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/txtView_UserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="#string/login_username_hint">
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtView_Password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtView_UserName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:hint="#string/login_password_hint"
android:inputType="textPassword" />
<Button
android:id="#+id/button_LogIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtView_UserName"
android:layout_below="#+id/txtView_Password"
android:layout_marginTop="10dp"
android:onClick="logIn"
android:text="#string/login_button" />
<CheckBox
android:id="#+id/checkbox_RememberPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtView_Password"
android:layout_below="#+id/button_Login"
android:layout_marginTop="15dp"
android:text="#string/remember_my_password" />
<CheckBox
android:id="#+id/checkbox_LogMeInAutomatically"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkbox_RememberPassword"
android:layout_below="#+id/checkbox_RememberPassword"
android:layout_marginTop="-5dp"
android:text="#string/log_me_in_automatically" />
<ProgressBar
android:id="#+id/progressBar_LogIn"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_LogMeInAutomatically"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:visibility="invisible" />
<TextView
android:id="#+id/txtView_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:text="#string/version_number" />
</RelativeLayout>
And here is where I am trying to change it to VISIBLE in the code.
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Get Views and Buttons
EditText txt_Username = (EditText) findViewById(R.id.txtView_UserName);
EditText txt_Password = (EditText) findViewById(R.id.txtView_Password);
Button btn_LogIn = (Button) findViewById(R.id.button_LogIn);
CheckBox chkbox_RememberMyPassword = (CheckBox) findViewById(R.id.checkbox_RememberPassword);
CheckBox chkbox_LogMeInAutomatically = (CheckBox) findViewById(R.id.checkbox_LogMeInAutomatically);
ProgressBar progressbar_LogIn = (ProgressBar) findViewById(R.id.progressBar_LogIn);
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
}
The program is crashing without rendering my Activity's Views. Any ideas?
I have a feeling its because you have an onClick for login, but login doesn't take a view parameter. Try setting the onclick for the button programmatically, instead of using the xml.
Edit: Try changing
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
to
public void logIn(View v) {
progressbar_LogIn.setVisibility(0);
}
or to set it programatically
btn_LogIn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
//CODE GOES HERE
}
});
Further Edit:
Also noticed that theres no sign of an onCreateView so make sure that all that view code is inside of, or atleast inside of onCreate:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
}

how to assign a string value associated with an image to a textwidget when image is clicked

package com.me.trial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setClickable(true);
OnClickListener l;
}
private void hasBeenClicked(<method invoked when the user has clicked>){
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="56dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="64dp" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitEnd"
android:tag="Employee name"
android:src="#drawable/img18" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I need the application to pass some string value associated to it to be passed into the text as soon as the user clicks on the image.How do i use the click Listeners in android to achieve the following task.
It is not necessary to fill in the code snippet that i have given.Any new ideas regarding the design are most welcome.
U can set text as Tag to the Image so that u can get that text onClick event. Tag can be set either in XML or dynamically.
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setTag("YOur text");
img.setClickable(true);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String urText= arg0.getTag().toString();
EditText edt = (EditText)findViewById(R.id.editText1);
edt.setText(urText);
}
});
}
}
This code demonstrates how to achieve your requirement dynamically.

Categories

Resources