I am new to android and just trying to modify a basic example.
main.xml is like
<?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"
android:weightSum="1">
<Button android:layout_width="228dp" android:layout_height="wrap_content" android:id="#+id/addButton" android:text="#string/addBtn"></Button>
</LinearLayout>
And want to add TextField on Click of the button. How to add Child to view?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.addButton);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
EditText editText = new EditText(v.getContext());
//Append the editText to View
}
How to append it to current view/layout?
Something like this would help you i think.
ParentView.post(new Runnable() {
public void run() {
ParentView.addView(newView,0);
LinearLayout.LayoutParams rlparams = (LinearLayout.LayoutParams)rldeal.getLayoutParams();
rlparams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
rlparams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
newView.setLayoutParams(rlparams);
}
});
Try to use include and merge:
http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/
Related
I have a Linearlayouts, a ScrollView and a button, I am not able to figure out why when I programmatically add a Checkbox to the Linearlayout, at the end of the view, the method from the scrollview
scrollview.scrollto(0, ScrollView.FOCUS_DOWN);
will not focus the last item added.
here the XML
<LinearLayout 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:orientation="vertical"
android:weightSum="100"
tools:context="com.eidotab.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:padding="10dp"
android:layout_weight="30">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST"/>
</LinearLayout>
and here the java:
public class MainActivity extends AppCompatActivity {
ScrollView scrollView;
LinearLayout linearLayout;
Button button;
int num = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scroll);
linearLayout = (LinearLayout) findViewById(R.id.linear);
button = (Button) findViewById(R.id.boton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setText("TEST " + num);
checkBox.setTextColor(Color.BLACK);
linearLayout.addView(checkBox);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
Im also addind the .rar so you just have to download it and try it.. I have spent several hours trying to make it work.. also tried to get the specific child from the LinearLayout with the
y = Linearlayout.getchildat(N).getBottom();
and works similar to the FOCUS_DOWN... I can never see the very last item.. only the item before the last.
Thanks in advance for the help.
Here is the program
https://www.dropbox.com/home/Public?preview=MyApplication.rar
Well after some research, seems like there is some kind of problem when trying to perform scrolls programmatically and the solution I have found that work best is to add a runable inside the onClick method in my code so I added
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
and the end result was
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setText("TEST " + num);
checkBox.setTextColor(Color.BLACK);
linearLayout.addView(checkBox);
//scrollView.fullScroll(ScrollView.FOCUS_DOWN);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
});
it is now working flawlessly but that said, I donĀ“t think it is the ideal solution.
I know there are some questions about this topic but neither did work for me. I have created a simple button in a RelativeLayout and when I click on it I'd like the button to jump in the middle of the screen. So I have to set the button to CenterHorizontal and CenterVertical programatically.
Here is my code to set the button centerHorizontal:
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
final RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)btn.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,0);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setLayoutParams(layoutParams);
}
});
}
And here is my 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
When I press the button nothing happens. If anyone has an idea how to fix it please response.
So i found a few problems in your code:
1. You have to add the rule after a button click because you create a reference betwen the variable "layoutParams" and the object.
2.You need to get rid of the existing rules.
Sample code:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
btn.setLayoutParams(layoutParams);
}
});
You have to remove your align left rules which you have defined in the xml.
Button btn = (Button)findViewById(R.id.button);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) btn.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
btn.setLayoutParams(lp);
keep in mind that RelativeLayout.LayoutParams.removeRule is only supported for API >= 17
Am quite new to Android programming and hope someone can help me out in this:
1. I am coding from one of the demos here:
http://developer.android.com/training/animation/screen-slide.html
I have a main activity_screen_slide.xml file:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
In another xml file, main.xml, I have a few buttons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonStart"
android:text="test"/>
</LinearLayout>
Within the main java activity file, the context is set as following:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
When I tried to set a listener for testButton, am unable to do so. This means that when the button is generated subsequently (through an inflator in another java file), there is no response when I click the button.
The java file to generate the inflator is simply:
ViewGroup rootView;
rootView = (ViewGroup) inflater.inflate(R.layout.main, container, false);
Thank you and appreciate your help
Cheers
D
I assume your button is defined in your xml layout activity_screen_slide.xml similar to this:
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
In your Activity you can add a ClickListener like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("", "I've been clicked");
}
});
}
Try this:
public class Settings extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState) {
//...ALL THE OTHER CODE..\\
Button button = (Button) findViewById(R.id.YOUR_BUTTON_ID);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.YOUR_BUTTON_ID: {
ALPHA.YourFunction(parameters);
}
}
}
}
I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});
It is possible using Dialog class but I do not want it that way. Instead I want it to be done by using PopupWindow class which gets popped up on startup and display some message on the popup. I am helpless, just can not getting this after spending many days behind it. Hope I get it here. Please and Thanks. Also look at below snippet if you didn't get what I want..
public class PopupActivity extends Activity implements OnClickListener {
LinearLayout ll = new LinearLayout(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(ll);
}
protected void onStart() {
super.onStart();
final PopupWindow pw;
Button button = new Button(this);
button.setText("Hello");
pw = new PopupWindow(button, 245, 284, true);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
pw.dismiss();
}
});
pw.showAsDropDown(ll, 10, -50);
}
}
Above code gives me FORCE CLOSE :/ Help guys..
this is how you may show a popup window.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="#+id/layoutTemp">
</LinearLayout>
popup_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up" />
</LinearLayout>
main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Handler().postDelayed(new Runnable() {
public void run() {
showPopup();
}
}, 100);
}
public void showPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false), 100, 100, true);
pw.showAtLocation(findViewById(R.id.layoutTemp), Gravity.CENTER, 0, 0);
}
The idea is that your popup will be displayed once your activity is loaded, otherwise it will produce exception Unable to add window -- token null is not valid; is your activity running?. Unless you show popup on a button click, that is the reason I'm showing it after a delay of 100 milliseconds (which is almost unnoticeable).