I have a View in Android which is basically a large image, with many small images in it that can be dragged. I want to add an option to create text boxes inside this view also, i.e. for the user to be able to create a text bix, write in it, move it around, etc. Does anyone know of a way to do this? Thanks!!!
Try to put a text box on small images as disable to set, and for example setOnLongClickListener and enable it so you can provide user to write textbox... Then setOnTouchListener on every small images to move around on the big one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ViewGroup vg = (ViewGroup) findViewById(R.id.lout);
final ImageView img = (ImageView) findViewById(R.id.imageView1);
final TouchListener listen1 = new TouchListener();
img.setOnTouchListener(listen1);
DragListener listen2 = new DragListener();
vg.setOnTouchListener(listen2);
final EditText et = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#SuppressWarnings("deprecation")
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView lbl = new TextView(vg.getContext());
lbl.setText(et.getText());
lbl.setLayoutParams(new LayoutParams(0,0,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lbl.setOnTouchListener(listen1);
vg.addView(lbl);
}
});
}
Something like this...
Related
I have one main layout with one LinearLayout. In this LinearLayout I inflates xml layouts that contain a form with multiple edittext fields(4 EditText) in it. every time i click on add button xml layouts that contain a form with multiple edittext fields(4 EditText) add in linearlayout.
On first attempt I show only one form. There is button for adding more forms. Suppose If user clicks on "Add" button two times then user have three total forms. I successfully get all these three forms.
i am set Tag child view so easy to get one bye one edittext value
but my problem is when i click on save button all edittext data
get and add in array ..
Button buttonAdd = (Button) findViewById(R.id.pdBtnAddDoc);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myRoot = (LinearLayout) findViewById(R.id.linearLayoutAdd);
LayoutInflater inflater = (LayoutInflater)PrimaryDoctorFragment.this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
childView = inflater.inflate(R.layout.fragment_primary_doc_add,myRoot);
// childView.setId(tag++);
childView.setTag(tag++);
}
});
here is my save button:-
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < tag; i++) {
System.out.println("i:- " + i);
View aView = myRoot.findViewById(i);
EditText name = (EditText) aView
.findViewById(R.id.childEtName);
Log.d("Gettext :- ", name.getText().toString());
}
}
});
on save button all four text field data get and add in array..
I need a button that, when clicked, would add a TextView below it. Also, the TextView cannot be made invisible and appear on click in this case, it must add the TextView. Was looking for this and couldn't find anything that suits my needs.
Set a listener on the button and for each click, create a new TextView and add it to the layout.
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
Update: Added text color above
I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }
I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).
All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.
Hope that all makes sense.
UPDATE:
I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);
//set the title of the popup
TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
titletext.setText(activityName);
if (activityName.equals("Root"))
{
switch (arg0.getId())
{
case R.id.color:
//Rename the string so to get the value from the string xml
String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
break;
case R.id.polarity:
//Rename the string so to get the value from the string xml
String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
//polarityDesc.setText(activityName);
polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
break;
}
}
I think
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
should be
Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
Button btnOpenPopupSecond = (Button) findViewById(R.id.color);
you should declare different different button for diffrerent findviewbyid
also in my eclipse it is not accepting
btnOpenPopup.setOnClickListener(new Button.OnClickListener()
instead it works with
btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
and you need to provide more clear view of what you want to perform
new thoughts,try doing this:
btnOpenPopupFirst.setOnClickListener(this);
btnOpenPopupSecond.setOnClickListener(this);
then option will come on both the above code lines
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
choose this
let MainActivity implement OnClickListener
then this option will come
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
choose
add unimplemented method
now
#Override
public void onClick(View v)
will be created
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.polarity:
Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
break;
case R.id.color:
Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
default:
break;
}
}
And post your views after implementing this way.
int[] id={R.id.button1,R.id.button2};
Button b=(Button)findViewById(id[i]);
The onClick method in Button.OnClickListener has a View parameter... you can call getId() on that view to get the id of that button that was clicked on.
It doesn't make too much sense to me. If what you really want is this:
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
All you need to do is set your value in the onClick(View view) method of your OnClickListener
public void onClick(View view) {
btnOpenPopup = (Button)view;
}
I'm completely new to Java and android. I ended a tutorial and I'm building a simple calculator to learn the ropes.
I basically have a view like this:
<button android:onClick="addText" android:text="2" />
And function addText is this:
public void addText(View view) { // Add view's text
TextView calc = (TextView) findViewById(R.id.edit_text);
t.setText( calc.getText() + ????? );
}
onclick it should add the text from the clicked button to a TextView. If you click 1, it does calc.getText() + "1" if you click 2 it does calc.getText() + "2". I don't know how to get the clicked view's text. I tried this: t.setText( calc.getText() + this.getText() ); which didn't work. How is this done?
You can do this:
public void addText(View view) { // Add view's text
Button button = (Button) view; //casts the View into the Button class
TextView calc = (TextView) findViewById(R.id.edit_text);
t.setText(calc.getText().toString() + button.getText().toString());
}
Since we know that Button is a subclass of View, and the getText() method works with buttons, we can make a new variable and turn the View parameter into a Button, via class casting. From there we can use all the Button's methods and continue with execution.
Other way is by setting TAG object to your views
Button mButtonCalcOne = (Button) findViewById(R.id.Button1);
mButtonCalcOne.setTag(1);
OnClickListener mButtonListener = new OnClickListener()
{
#Override
public void onClick( View v )
{
int lButtonValue = (Integer) v.getTag(); //DO STH WITH IT ; )
};
}
mButtonCalcOne.setOnClickListener( mButtonListener );
I have this problem with my ToggleButton.
I want it to create/delete a button upon being toggled, and at the same time add content/functions to the button, like drawable and such.
This is the current code:
public class BillardScoreboardActivity extends Activity {
/** Called when the activity is first created. */
Button minuskegle, minuskugle, pluskugle, pluskegle, plusmidkegle, minusmidkegle, miss;
ToggleButton toggle;
LinearLayout bottomlayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pluskugle = (Button) findViewById(R.id.bBallhole);
minuskugle = (Button) findViewById(R.id.bBallhole);
pluskegle = (Button) findViewById(R.id.bKegle);
minuskegle = (Button) findViewById(R.id.bKegle);
plusmidkegle = (Button) findViewById(R.id.bKeglemid);
minusmidkegle = (Button) findViewById(R.id.bKeglemid);
bottomlayout = (LinearLayout) findViewById(R.id.bottomlayout);
miss = (Button) findViewById(R.id.bMiss);
if(toggle.isChecked())
{
minuskugle.setBackgroundResource(R.drawable.redballinhole);
minuskegle.setBackgroundResource(R.drawable.redkegle);
minusmidkegle.setBackgroundResource(R.drawable.midkegleminus);
miss.setBackgroundResource(R.drawable.missbutton);
miss.setVisibility(View.VISIBLE);
}
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
miss.setVisibility(View.GONE);
}
}
});
}
The current problem is that it can't find the (buttontest) in this part of the code:
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
bottomlayout.removeView(buttontest);
}
And as mentioned earlier, the second problem is to make the button inherit some functions/content.
for bigger version: http://i.imgur.com/KxKvh.png
Btw... Everytime i start up the application, it gives me 2 apps to choose between, whereof only the bottom one works:
I guess the problem is that the togglebutton's initial state is 'checked'. That means when you click it the first time, isChecked() will return false and the else-part of your code will be executed. But at that point, buttontest hasn't been added to bottomlayout yet.
I recommend you to have the button inside the layout by default and call buttontest.setVisibility(View.GONE) when you would like to hide it and buttontest.setVisibility(View.VISIBLE) when it needs to be shown.
As for your second question, just call setBackgroundResource/Drawable to add content (like you're already doing it with the other buttons). If you say you want to add functionality, I assume you intend to do something when the button is clicked? If yes, add a View.OnClickListener.
Hope I could help you.