I'm wondering, how can I get any current view property? For example, I create a button, change it's properties (color, height, etc) in code and I want to get it back as a string.
Is there any chance to get any property I want, such as: visibility, background, layout_width, layout_height, gravity, minWidth etc.
How can I do it quickly and should I use listener for this purpose?
If I understand you correctly, here is code, that allows you obtain some properties in string:
Button button = (Button) findViewById(R.id.button);
String visibility = String.valueOf(button.getVisibility());
String width = String.valueOf(button.getWidth());
String height = String.valueOf(button.getHeight());
Please note that getWidth() and getHeight() methods returns size in pixels (not dp).
You need to use the Listener action on a View and then fetch the properties that you would like to check when that view is clicked.
Example for a TextView and fetching it's properties upon a click on that TextView:
TextView tv = (TextView) findViewById(R.id.textView);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int colorCode = cd.getColor();//colorCode in integer
}
});
In case you want to access just the property of an View then you can just access it by calling the property you want to access, such as:
TextView tv = (TextView) findViewById(R.id.textView);
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int colorCode = cd.getColor();//colorCode in integer
Related
What I need is for the textview to be added onto a linear layout where it will look organised and nice. Whenever the button is clicked again, it should replace the old textview and update it again.
At the moment, the button onclick listener will produce a textview, but I don't like it because it looks messy and unorganised.
I tried doing this:
varlinear.addView(varkebabTotal);
but it caused an error.
I have looked at other examples but they didn't explain how it will work with an onlick listener.
Here's the code for what happens when the button is clicked:
varKebab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = "Kebab Wrap";
if (menu.containsKey(name)) {
kebabquantity = list.get(name);
if (kebabquantity == null) {
kebabquantity = 0;
}
kebabquantity++;
list.put(name, kebabquantity);
//kebab qty calc
double kebabTotal = kebabquantity * menu.get(name);
overallTotal = overallTotal + menu.get(name);
varkebabTotal.setText(String.format("%s %s # £%s = £%.2f", kebabquantity.toString(), name, menu.get(name), kebabTotal));
varTotalPrice.setText(String.format("Total = £%.2f", overallTotal));
varlinear.addView(varkebabTotal); // this line caused an error
}
}
});
Edit:
The error I received is when I tested the app, and when I click the button, the app stops. It shuts itself down due to that one line: varlinear.addView(varkebabTotal);
The variables are as follows:
varkebabTotal = (TextView) findViewById(R.id.kebabTotal);
varTotal = (TextView) findViewById(R.id.TotalPrice);
varlinear = (LinearLayout) findViewById(R.id.myLinear);
one method you can try is to set varkebabTotal.setVisibility(View.GONE); when you initialize the TextView, and then on your onClick event, you can change it to varkebabTotal.setVisibility(View.VISIBLE);
I have in my program a scroll view with a linearlayout inside of it. I add dynamically TextView's to the linearlayout and I have no way to know how much TextView's I'll end up with. When a certain TextView is clicked I need to get it's text. Any idea what is the best way to do it?
Thanks in advance.
I've tried to add a listener to the text view but I am not sure how to get the text. I saw in some posts that you can do a listener to the LinearLayour/ScrollView though I am not sure what is the best option.
This happenes every time a message is added:
TextView messageText = new TextView(RecordedMessagesScreen.this);
messageText.setText(content);
messageText.setClickable(true);
messageText.setOnClickListener(RecordedMessagesScreen.this.textViewListener);
RecordedMessagesScreen.this.messagesLayout.addView(messageText);
this is the listener:
this.textViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("message", ***NEED TO GET THE TEXT***)
}
};
At the class level declare a String variable:
private String text = "";
and a View.OnClickListener variable:
private View.OnClickListener listener;
Initialize the listener in onCreate() of your activity like this:
listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v;
text = tv.getText().toString();
}
};
and every time you create a new TextView set the listener:
textView.setOnClickListener(listener);
This way the variable text each time you click a TextView will get the clicked TextView's text.
You can customize the code inside onClick() yo suit your needs.
I'm having a problem with this code. I need to dynamically add buttons to my layout. This code works fine, with one exception. The second button sits on top of the first. This must have something to do with LayoutParams, but I'm not sure what.
private void buttonmaker (Button button)
{
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rlayout.addRule(RelativeLayout.CENTER_VERTICAL);
rlayout.addRule(RelativeLayout.ALIGN_LEFT);
rlayout.width = 100;
button.setId(Atom.count);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
atoms[id].getname();
TextView textview = (TextView)findViewById(R.id.textView2);
textview.setText(textview.getText()+String.valueOf(atoms[id].getname()));
}
});
if (Atom.count > 1) rlayout.addRule(RelativeLayout.RIGHT_OF,Atom.count-1); else rlayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setLayoutParams(rlayout);
RelativeLayout v = (RelativeLayout) findViewById(R.id.rlayout);
v.addView(button);
}
the problem is you set de button in the relativelayout, in this component the objects are being added one above the other, you try create linearlayout global with orientation vertical or horizontal depending on what you want and added buttons, and its all
Right now I have an Adapter class, which extends PagerAdapter. Inside I have my InstantiateItem() method.
Here I have instantiate several buttons on the view:
#Override
public Object instantiateItem(ViewGroup parent, final int position) {
final int delPosition = position;
//Get the inflater
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate the root layout
View view = inflater.inflate(R.layout.collection, null);
//Grab instance of image
ImageView imageView = (ImageView) view.findViewById(R.id.region);
//Sets a drawable as the content of this ImageView.
imageView.setImageResource(regionImages.get(position));
//scores toggle buttons
zero = (ToggleButton) view.findViewById(R.id.number_zero);
one = (ToggleButton) view.findViewById(R.id.number_one);
two = (ToggleButton) view.findViewById(R.id.number_two);
three = (ToggleButton) view.findViewById(R.id.number_three);
four = (ToggleButton) view.findViewById(R.id.number_four);
five = (ToggleButton) view.findViewById(R.id.number_five);
six = (ToggleButton) view.findViewById(R.id.number_six);
seven = (ToggleButton) view.findViewById(R.id.number_seven);
eight = (ToggleButton) view.findViewById(R.id.number_eight);
nine = (ToggleButton) view.findViewById(R.id.number_nine);
ten = (ToggleButton) view.findViewById(R.id.number_ten);
one.setOnClickListener(createClickListener(1));
two.setOnClickListener(createClickListener(2));
three.setOnClickListener(createClickListener(3));
four.setOnClickListener(createClickListener(4));
five.setOnClickListener(createClickListener(5));
six.setOnClickListener(createClickListener(6));
seven.setOnClickListener(createClickListener(7));
eight.setOnClickListener(createClickListener(8));
nine.setOnClickListener(createClickListener(9));
ten.setOnClickListener(createClickListener(10));
//Back Arrow Button
final ImageView back_button = (ImageView) view.findViewById(R.id.back_nav_arrow);
back_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//it doesn't matter if you're already in the first item
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
});
//forward button
final ImageView forward_button = (ImageView) view.findViewById(R.id.forward_nav_arrow);
forward_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//it doesn't matter if you're already in the last item
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
});
parent.addView(view, 0);
return view;
}
Here's the onClick action for the buttosn:
//onClickListener method that returns an interface
private View.OnClickListener createClickListener(final int value) {
return new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonValue = value;
ToggleButton clickedButton = (ToggleButton) view;
RadioGroup radioGroup= (RadioGroup) clickedButton.getParent();
for (int i = 0; i < radioGroup.getChildCount(); ++i) {
View nextChild = radioGroup.getChildAt(i);
if (!(nextChild instanceof ToggleButton)) {
continue;
}
if (nextChild.getId() != clickedButton.getId() || !clickedButton.isChecked()) {
ToggleButton tb2 = (ToggleButton) nextChild;
tb2.setChecked(false);
}
}
}
};
}
I want to be able to save the state of the button press (right now it's like a radiobutton and when the user clicks on it, the button state is pressed down until they move to another button) and the buttonValue between the views in the viewpager.
Ultimately, I want a view at the end of my viewpager where I can list all the scores for each view as well as have the user go back to the other views and still have their button state pressed so that they can confirm it while swiping between views.
I am facing two problems right now:
1) Only the last buttonValue is saved and not all the buttonValues for all the views. How can I achieve saving all the scores in all the views and not just the last score in the last view?
2) Going from one view to another and back, the button state is still pressed. However, if I go to a third view from the last point, the button state pressed is no longer true. How can I fix this?
I think it is time to use fragment, although you can achieve what you want without it but using fragment gives a lot of benefits like saving your value when your activity is destroyed (rotation) or using the life cycle callback methods or reusing it to your future projects. anyway:
1) Only the last buttonValue is saved and not all the buttonValues for
all the views. How can I achieve saving all the scores in all the
views and not just the last score in the last view?
save the state of your button in anyway you like for example in sharedpreference, database or a list of custom object as a member of your activity or adapter something like below:
class MyToggleButton{
boolean mState; // false = unpressed, true = pressed
}
and use List<MyToggleButton> to store them.
2) Going from one view to another and back, the button state is still
pressed. However, if I go to a third view from the last point, the
button state pressed is no longer true. How can I fix this?
you can use List<MyToggleButton> at instantiateItem to determine which button must have which state so you can set it correctly. when user change the state of any button you must update MyToggleButton state so that when ever you are going to look at List<MyToggleButton> find the correct one.
I have ScroolView and it's inside Linear layout. In this linear layout i added programmically around 20 TextView. When i press any of those TextView i change it color to RED (at first it was white). When i press again the same TextView i check his color and if it's is RED i again make it white.
What i want to do:
I press for example 1 TextView and make it RED. Then when i press 2 TextView i make this one RED and i want to make 1 TextView WHITE. This functionality should be at all TextView.
So any idea's how to do this ?
You mean to say at a time you need only one textview to be red. You can do this using 2 variables. One is a boolean colored. This indicates that at least one TextView is colored. Another is a TextView variable. Create a TextView variable lastColoredTextView. Let it be null initially. Then whenever the textview is clicked, assign the lastColoredTextView to the clicked TextView. Then whenever you are clicking, just check if colored then change the color of lastColoredTextView to white.
Change class name and it will work fine.
public class Test_stflowActivity extends Activity {
TextView current_red_txt_box = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = null;
LinearLayout lp = new LinearLayout(getApplicationContext());
lp.setOrientation(LinearLayout.VERTICAL);
View.OnClickListener txt_click = new View.OnClickListener() {
#Override
public void onClick(View v) {
current_red_txt_box.setTextColor(Color.WHITE);
TextView tv = (TextView) v;
tv.setTextColor(Color.RED);
current_red_txt_box = tv;
}
};
for (int i = 0; i < 20; i++) {
tv = new TextView(getApplicationContext());
tv.setId(i);
tv.setTextSize(40);
tv.setText("you text");
tv.setTextColor(Color.WHITE);
tv.setOnClickListener(txt_click);
lp.addView(tv);
current_red_txt_box = tv;
}
setContentView(lp);
}
}