I don't have my source code right now, but I was wondering if it is possible to use visibility(GONE) with the ID's or something like that?
The reason : I have a form where I want to have 20 dropdowns, and a button "add a new activity" . When you click on the button, it unhide a new control. The problem is in how to tell the app what dropdown to unhide...
Example :
dropdown1 (visible) [Button add new]
dropdown2 (invisible)
dropdown3 (invisible)
[...]
user click on [Button Add new]
dropdown1 (visible)
dropdown2 (visible)
dropdown3 (invisible)
[...]
Or something similar in process.
Thanks! (sorry not english speaking person... I hope this is understandable as a question!)
In xml give id to all spinner (Dropdown) like drop1,drop2,...........
like this
<Spinner
android:id="#+id/drop1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/spinner_title"/>
In Java file add
Spinner drop1,drop2,............drop20;
Under onCreate Method
drop1 = (Spinner)findViewById(R.id.drop1);
drop2 = (Spinner)findViewById(R.id.drop2);
--------------------------------------
drop20 = (Spinner)findViewById(R.id.drop20);
Set Visibility
if(condition) // your condition to hide dropdown
{
drop1.setVisibility(Visible.GONE);
}
else
{
drop1.setVisibility(Visible.VISIBLE);
}
After mapping the control you can use setVisibility() method with it
eg:
Button btn=(Button)findViewById(R.id.button1);
btn.setVisibility(View.Visible);`
Yes, you can do it. Using android:id tag in XML layout.
In your Java code you can do it like this:
Button button1 = (Button) findViewById('button1');
Button button2 = (Button) findViewById('button2');
buton1.setOnClickListener( new View.OnClickListener( ) {
#Override
public void onClick( View v ) {
button2.setVisibility( View.VISIBLE );
}
} );
Related
How can I set a programmatically created checkbox's text to be aligned on the left instead of right side of the checkbox. Below is a code snippet:
Checkbox check1 = new Checkbox(getApplicationContext());
check1.setLayoutParams(new ActionBar.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
check1.setId(fieldNo);
check1.setTextColor(Color.BLACK);
check1.setGravity(Gravity.RIGHT);
check1.setText(formField.get(fieldNo));
The above code resulted in the text shown on the right of the checkbox.
Here is a screenshot :
How can I have the text on the left of the checkbox?
You need to change the following
check1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
In this line you were using ActionBar params and then LinearLayoutCompat params. Try to stick to 1 category and in custom views like checkbox just LinearLayout would do.
UPDATE 1:
You should use CheckedTextView. I have used standard android drawable for that but you can also use your custom Check box design as well.
So your overall code would look like -
final CheckedTextView check1 = new CheckedTextView(getApplicationContext());
check1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
check1.setId(fieldNo);
check1.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
check1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (check1.isChecked()){
check1.setChecked(false);
check1.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}else{
check1.setChecked(true);
check1.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
}
}
});
check1.setTextColor(Color.BLACK);
check1.setGravity(Gravity.LEFT);
check1.setText(formField.get(fieldNo));
First let me preface this with being completely new to Android development. I am trying to simply have text on a different buttons change when I click on "this" button. I can change the visibility fine with setVisibility() so I think I am referencing the buttons correctly - but when I attempt to setText() I get the error: "Cannot resolve method 'setText(Java.Lang.String)'"
Why will it allow me to change visibility but not text? What do I need to do to correct the problem?
Here's a portion of the XML for one of the buttons I am attempting to change text on:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/songbutton4"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/button4"
android:layout_alignEnd="#+id/button4"
android:visibility="invisible" />
Here is the code for the onclick event:
public void onbutton1click(View v){
//on click turn the 4 buttons invisible, and show the other 5
View b1 = findViewById(R.id.button1);
b1.setVisibility(View.INVISIBLE);
View b2 = findViewById(R.id.button2);
b2.setVisibility(View.INVISIBLE);
View b3 = findViewById(R.id.button3);
b3.setVisibility(View.INVISIBLE);
View b4 = findViewById(R.id.button4);
b4.setVisibility(View.INVISIBLE);
View b5 = findViewById(R.id.button5);
b5.setVisibility(View.INVISIBLE);
View sb1 = findViewById(R.id.songbutton1);
sb1.setVisibility(View.VISIBLE);
sb1.setText("hello"); // THIS IS WHERE THE ERROR OCCURS
View sb2 = findViewById(R.id.songbutton2);
sb2.setVisibility(View.VISIBLE);
View sb3 = findViewById(R.id.songbutton3);
sb3.setVisibility(View.VISIBLE);
View sb4 = findViewById(R.id.songbutton4);
sb4.setVisibility(View.VISIBLE);
}
Are you doing it in Fragment or in Activity?
Anyway, you should do:
Button sb1 = (Button) findViewById(R.id.songbutton1);
This is how we add elements. If you set Button widget in your layout, in this way you need to have reference to this widget. Declaring proper type and casting using ( ).
If you are doing it in Fragment, not in Activity, you also need to bind it to proper view.
Button sb1 = (Button) v.findViewById(R.id.songbutton1);
And advice - don't get reference to views (widgets) in listener. Do it before listener.
Firstly assign the button to a variable i.e: Button btnSong = (Button) findViewById(R.id.songbutton4); then set an onclick listener to the button through the method btSong.setOnClickListener(); which will require you to declare a new onClickListener inside the button and a method will be created within which u can place the above code under the method. Alternatively add an XML attribute that points the button straight to the method.
I'm trying to implement a ExpandableListView in java that when click deployment space elements inside, not the typical wish list allows choosing an option. What I try to do is basically something like this
One can see that the spinner "wraps" to have items and does not have a list, Would I could help a little?
You can use ExpandableListView .. check this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can easily create one on your own. Just create a Button and assign a Layout below. When the Button gets clicked set the View of you Layout to Visible and otherwise to Invisible. Additionaly you can set up and down arrows to the right of your Button.
final Button button = (Button) findViewById(R.id.button);
final LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (linearLayout.getVisibility() == View.VISIBLE) {
linearLayout.setVisibility(View.GONE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expand, 0);
} else {
linearLayout.setVisibility(View.VISIBLE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.collapse, 0);
}
}
});
i have dialog and text inside eache view that i can click on .
i want all the view to be clickable insted only the text .
the code :
final Dialog dialog = new Dialog(List_Lists.this);
dialog.setContentView(R.layout.dialog_edit_tables);
dialog.setTitle("Action for " + table);
TextView delete = (TextView) dialog
.findViewById(R.id.tvDeleteTable);
TextView cancel = (TextView) dialog.findViewById(R.id.tvCancel);
OnClickListener l = new OnClickListener() {
#Override
public void onClick(View v) {
dbAdapter = new DBmethods(getApplicationContext());
switch (v.getId()) {
case R.id.tvDeleteTable:
viewListsAdapter.listsV.remove(pos);
dbAdapter.deleteTable(table);
break;
case R.id.tvCancel:
dialog.dismiss();
break;
case R.id.bTableRenameName:
if(dbAdapter.checkTableNameOK(List_Lists.this ,newName.getText().toString())){
viewListsAdapter.listsV.remove(pos);
viewListsAdapter.listsV.add(pos, newName.getText().toString().trim());
dbAdapter.renameTable(table , newName.getText().toString().trim());
renamedialog.dismiss();
};
break;
default:
break;
}
// if button is clicked, close the custom dialog
dialog.dismiss();
lv.invalidateViews();
}
};
delete.setOnClickListener(l);
cancel.setOnClickListener(l);
dialog.show();
}
});
}
as i said this code work fine if user press on text inside the view , but the empty view obviously wont respond . thanks !
if user press on red spot it activate the enter , if green than duplicate and so on...the current status is u have to press on the word , if u press on the colored spots it will do nothing
If im not wrong you need this to make layout clickable
try something like this
You can add a OnClickListener on it :
//onCreate
LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout01);
layout.setOnClickListener(yourOnClickListener);
Should be working ;)
create an id of your dialog_edit_tables
let say android:id=#+id\testing in your xml.
now write this
LinearLayout yourlayout = (LinearLayout) findViewById(R.id.testing);
yourlayout.setOnClickListener(l);
hope this will help.
You haven't shown the XML layout but setting the width of the clickable text view to fill_parent should work, also make sure your parent layout also has the width set to fill_parent.
The onClickListener is not based on the text it is based on the actual component, therefore it looks as if the layout_width attribute is set to wrap_content, so the component is only then length of the text, therefore only where the text is, is clickable. Hope this makes sense.
If I am not wrong than you are using listview or tableview to display the text.
You can do one thing. Make the textview layout_width as fill_parent That way you will able to get the touch on the place that you marked.
Hope this will help.
I am working on a android program. A user clicks on a button I do some math and I would like to change the values that I have on my view in some TextView objects. Can someone please tell me how to do it in my code?
I presume that this question is a continuation of this one.
What are you trying to do? Do you really want to dynamically change the text in your TextView objects when the user clicks a button? You can certainly do that, if you have a reason, but, if the text is static, it is usually set in the main.xml file, like this:
<TextView
android:id="#+id/rate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/rate"
/>
The string "#string/rate" refers to an entry in your strings.xml file that looks like this:
<string name="rate">Rate</string>
If you really want to change this text later, you can do so by using Nikolay's example - you'd get a reference to the TextView by utilizing the id defined for it within main.xml, like this:
final TextView textViewToChange = (TextView) findViewById(R.id.rate);
textViewToChange.setText(
"The new text that I'd like to display now that the user has pushed a button.");
First we need to find a Button:
Button mButton = (Button) findViewById(R.id.my_button);
After that, you must implement View.OnClickListener and there you should find the TextView and execute the method setText:
mButton.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.my_text_view);
mTextView.setText("Some Text");
}
});
First, add a textView in the XML file
<TextView
android:id="#+id/rate_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/what_U_want_to_display_in_first_time"
/>
then add a button in xml file with id btn_change_textView and write this two line of code in onCreate() method of activity
Button btn= (Button) findViewById(R.id. btn_change_textView);
TextView textView=(TextView)findViewById(R.id.rate_id);
then use clickListener() on button object like this
btn.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
textView.setText("write here what u want to display after button click in string");
}
});