I am developing a Android Application, where i have multiple textView and I want those textview clickable. When I will click those textView I will get a common empty form but with a static ID in the top for every textview.
For example: If I have 3 product name in 3 text view, if I click on textView1, a previously made layout will be shown and there will be a static ID with my product name in the top, then there will be form to filling up the details of the product.If I click on the textView2 then the form will be same only the Static ID and the product name will change.
I hope you guys understood what I wanted to explain.
I am new in application development so I need some simple solution.
So far I have made a layout of my form and I have also made a clickable Layout. So I need to know how I would make the class and function to call the layout and plus the static ID.
Layout of the product name and the Clickable TextView:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/p85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:textStyle="bold"
android:layout_marginTop="12dp"
android:text="#string/p85"
android:ems="7"
android:textSize="35sp"
android:textColor="#375C34"
android:clickable="true"
android:background="#drawable/product_list_view"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</ScrollView>
ProductList.java file that call the product_list.xml
import android.app.Activity;
import android.os.Bundle;
public class ProductList extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.product_list);
}
}
In xml in your textview put :
android:onClick="onClick"
And have this
public void onClick(View v) {
switch(v.getId()){
case R.id.TextViewFromXml):
// do something
break;
}
}
Or Remove this
Textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
First set Tag(i.e your Static ID) to TextView and then set Clicklistner
tv.setTag("1234");
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myId=tv.getTag();
}
});
Related
I have a RecyclerView where the user has to click the elements to cast a vote to that specific player.
Current Layout
As it's not that much intuitive, I'd like to add Buttons on the right side of each element to let the user understand that he needs to click if he wants to cast a vote.
Question
How do I make a custom layout like that? Probably using a GridLayout? And mostly, how do I get the Button's element's position when the button (and it only) gets clicked?
Code
list_players.xml | http://pastebin.com/F7Ei07x9
two_line_list_item_horizontal.xml | http://pastebin.com/s8qvwqt4
CourseAdapter.java | http://pastebin.com/qZJeimfe
Replace your CoursesAdapter's onBindViewHolder with this.
#Override
public void onBindViewHolder(CoursesViewHolder holder, int position) {
Player player = mArrayCourses.get(position);
holder.name.setText(player.getName());
holder.counter.setText(String.valueOf(player.getCount()));
holder.voteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your work here
}
});
}
You can use LinearLayout to add button to your Recyclerview item by giving them weight.
You can handle onClicklistener of that button in ViewHolder class and you can get position of clicked button by getAdapterPosition() in ViewHolder class.
As per your request :
XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="ABCD"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="A"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Vote"/>
</LinearLayout>
Adapter :
public class Holder extends RecyclerView.ViewHolder{
Button btnVote;
public Holder(View itemView) {
super(itemView);
btnVote = (Button) itemView.findViewById(R.id.btn_vote);
btnVote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//list.get(getAdapterPosition()); Use for get the data on selected item
}
});
}
}
I have set the onClick property of an EditText which is located in a fragment:
<EditText android:id="#+id/edittext1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:inputType="none" android:maxLines="1"
android:singleLine="true" android:layout_marginTop="16dp"
android:focusable="false"
android:longClickable="false"
android:clickable="true"
android:onClick="doSomething"
android:cursorVisible="false"
android:editable="false">
Then in the fragment class I have:
public void doSomething(View view) {
//show dialogfragment...
}
But the method doSomething is grayed out and I get the warning 'Method doSomething is never used'.
Note: This code was originally in an activity and was working fine.
Is there another way to handle onClick in fragments?
first initialize an EditText instance in the top of your fragment
EditText et;
then
in your onCreateView()
add:
etEmployee=(EditText)rootView.findViewById(R.id.edittext1);
then implement your onClickListener()
et.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//what ever you need to do goes here
}
});
Note: in fragments you have to refer to the inflatedView to be able to access your editText in this case rootView.
also the code you are using doesn't work becouse here you are using fragments and using onClick attribute in your xml would only make you able to use it in the MainActivity that contain your fragment.
hope this will help.
If you add android:onClick="doSomething" to your fragment then method will be invoked in your activity but not in Activity. If you want the callback in your fragment add through pragmatically.
edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
You can always try to add a OnClickListener to the EditText if it does not matter to you in which way the function gets called.
edittext1 = (EditText) view.findViewById(R.id.edittext1);
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
In your layout XML try specifying a tools:context="com.mypackage.path.MyFragment". Don't know if it'll fix it for 'onClick', but it has fixed similar issues for me in the past.
This goes in the top most 'ViewGroup' in your layout file, example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mypackage.path.MyFragment" />
I want to create a gui application for android where the user will be able to add or remove fields of certain type (4 different type of fields) to the application. Is there a way to do so in xml?
The only way I could figure to do so is by edditing the xml file from within the app which sounds as a bad idea for me.
Hope my question is clear.
Yotam.
Edit:
I have added a simple code for direct java implantation:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
public class Leonidas extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.counter);
TextView TV = new TextView (this);
TextView UV = new TextView (this);
TV.setText("hello");
UV.setText("goof");
//setContentView(TV);
//setContentView(UV);
ViewGroup.LayoutParams lpars = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
this.addContentView(UV,lpars);
this.addContentView(TV, lpars);
this.setVisible(true);
}
}
Edit2:
I have searched for example and got the following working:
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater = LayoutInflater.from(this);
Button b = (Button) this.findViewById(R.id.alert);
b.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final LinearLayout canvas = (LinearLayout)Leonidas.this.findViewById(R.id.counter_field);
final View cv = this.inflater.inflate(R.layout.counter,canvas,false);
canvas.addView(cv);
}
You can do it from within your handler too (in the implementation class).
After inflating your xml layout, you respond to some kind of user interactions.
In the handler you
either create a new View from
scratch, and specify its
layoutparams,
or inflate one using xml
After having the new view, you add it to the current (this) view, and due to its layoutparams, it will be the size, shape, color, etc. that you want.
Update:
If you'd like to add more complex views to your activity, it's better to write them in xml, and inflate them:
sample_component.xml: //inside res/layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="0px">
<TextView android:id="#+id/servicename_status" android:paddingLeft="15px"
android:paddingRight="5px"
android:textStyle="bold" android:focusable="false" android:textSize="14px"
android:layout_marginLeft="10px" android:layout_marginRight="3px"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#+id/lastcheck" android:focusable="false"
android:textSize="14px" android:layout_width="fill_parent"
android:layout_marginLeft="10px" android:layout_marginRight="3px"
android:layout_height="wrap_content" android:layout_below="#id/servicename_status" />
<TextView android:id="#+id/duration" android:focusable="false"
android:textSize="14px" android:layout_width="fill_parent"
android:layout_marginLeft="10px" android:layout_marginRight="3px"
android:layout_height="wrap_content" android:layout_below="#id/lastcheck" />
<TextView android:id="#+id/attempt" android:focusable="false"
android:textSize="14px" android:layout_width="fill_parent"
android:layout_marginLeft="10px" android:layout_marginRight="3px"
android:layout_height="wrap_content" android:layout_below="#id/duration" />
<TextView android:id="#+id/statusinfo" android:focusable="false"
android:textSize="14px" android:layout_width="fill_parent"
android:layout_marginLeft="10px" android:layout_marginRight="3px"
android:layout_height="wrap_content" android:layout_below="#id/attempt" />
<CheckBox android:id="#+id/alert" android:focusable="false"
android:layout_alignParentRight="true" android:freezesText="false"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="5px" />
</RelativeLayout>
Inside your Leonidas activity class you have the handlers that have to respond to different user actions by adding/removing items to/from the view.
Below is a sample handler of a click event, which uses LayoutInflater, to add the sample_component.xml view to your activity:
public final class MyClickListener implements View.OnClickListener
{
private LayoutInflater inflater;
public MyClickListener()
{
inflater = LayoutInflater.from(Leonidas .this);
}
#Override
public void onClick(View v)
{
// TODO: change RelativeLayout here to whatever layout
// you'd like to add the new components to
final RelativeLayout canvas = (RelativeLayout)Leonidas.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
// check which button was pressed
switch (view.getId())
{
case R.id.btn_prev:
//handler for the prev button
break;
case R.id.btn_next:
//handler for the next button
break;
default:
break;
}
}
}
Note, that MyClickListener is implemented as an inline class within your Leonidas activity, thay's why for the context parameter it is used: this.Leonidas.
Update
The R.id.my_canvas would be the id of the view that you want to add components to. it is in your main.xml (or whatever xml you use for your Leonidas view).
If you put the MyClickListener class inside your Leonidas.java class (declare as inline class), it will recognize it.
Instead of specifying elements in the XML, you can create them dynamically and add it to the UI. This is demonstrated in the Android Hello World Tutorial here.
I want to add views on click of button. My screen has a text view and an edit text and a "Add More" button. on the click of button another set of text view and edit text should get appended to the screen. I am also planning to add scroll view once the my screen grows longer.
My Code snippet is as follows
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
text.setText("hello how are you?"); //this is just an example, I may want to add lot more here.
linearLayout.addView(text);
setContentView(linearLayout);
}
I know I am missing something very basic but I do not know what is that. :(
Please help.
First of all, you need to initialize each TextView with a reference to the context. So, in your onClick, given that it is in MyActivity.java:
public void onClick(View v) {
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT); // Or what you decide
text.setText("Your text");
linearLayout.addView(text);
}
Second, given that you have a call to setContentView in onCreate which also adds your linearlayout to the view, you do not need to call setContentView(linearLayout) each time the button is clicked.
you should first create a new TextView and EditText add it to the linearLayout using linearLayout.addView(). No need to call setContentView() you probably already added it.
This One help you
In your XML file
<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:id="#+id/myMainRelativeLayout"
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" >
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp"
android:text="Add Button" />
And your Java file Like
public class MainActivity extends Activity implements
android.view.View.OnClickListener {
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnAdd) {
RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout);
Button btn = new Button(this);
btn.setId(1234);
btn.setText("Add Button Runtime");
btn.setOnClickListener(this);
rl.addView(btn);
}
if(v.getId() == 1234)
{
Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show();
}
}
}
This one just example you can add any view like this
I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.
I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.
Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?
You can set the click handler in xml with these attribute:
android:onClick="onClick"
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
...
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
This may not be quite what you are looking for but this is what worked for what I'm doing. All of this is after my onCreate:
boilingpointK = (TextView) findViewById(R.id.boilingpointK);
boilingpointK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ("Boiling Point K".equals(boilingpointK.getText().toString()))
boilingpointK.setText("2792");
else if ("2792".equals(boilingpointK.getText().toString()))
boilingpointK.setText("Boiling Point K");
}
});
OK I have answered my own question (but is it the best way?)
This is how to run a method when you click or tap on some text in a TextView:
package com.textviewy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class TextyView extends Activity implements OnClickListener {
TextView t ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener(this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
}
and my main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="#+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<ListView android:id="#+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
<LinearLayout android:id="#+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<TextView android:text="This is my first text"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28dip"
android:editable = "true"
android:clickable="true"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
from inside an activity that calls a layout and a textview, this click listener works:
setContentView(R.layout.your_layout);
TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
String TAG = "yourLogCatTag";
tvGmail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View viewIn) {
try {
Log.d(TAG,"GMAIL account selected");
} catch (Exception except) {
Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
}
}
});
the text view is declared like this (default wizard):
<TextView
android:id="#+id/tvGmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/menu_id_google"
android:textSize="30sp" />
and in the strings.xml file
<string name="menu_id_google">Google ID (Gmail)</string>
Although you can resolve the problem by setting the listener to textview, it's recommended not to. You should use flat button as it is a subclass of Button and it provides many attributes which TextView doesn't.
To use flat button, add style="?android:attr/borderlessButtonStyle" attribute -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>
in textView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
You must also implement View.OnClickListener and in On Click method can use intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
I tested this solution works fine.
To click on a piece of the text (not the whole TextView), you can use Html or Linkify (both create links that open urls, though, not a callback in the app).
Linkify
Use a string resource like:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
Then in a textview:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
Using Html.fromHtml:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
Then in your textview:
textView.setText(Html.fromHtml(getString(R.string.html)));
You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).
holder.bt_foo_ex.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// code during!
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// code before!
}
#Override
public void afterTextChanged(Editable s) {
// code after!
}
});