Observing rather strange behavior in relative layout.
This is the initial state:
Defined as:
<EditText
android:id="#+id/bleedCount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/abrMult"
android:layout_below="#+id/textView4"
android:layout_marginRight="10dp"
android:ems="10"
android:inputType="number"
>
<requestFocus />
</EditText>
<TextView
android:id="#+id/abrMult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/abrSubmit"
android:layout_alignBaseline="#+id/abrSubmit"
android:layout_marginRight="10dp"
android:text="x12" />
<Button
android:id="#+id/abrSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_alignParentRight="true"
android:text="Calculate" />
On onItemSelected from the dropdown it is being changed like this:
abrSubmit.setText(pos == 1 ? "Calculate" : "Submit");
abrMult.setVisibility(pos == 1 ? View.VISIBLE : View.GONE);
bleedCount.setHint(pos == 1 ? "# of Meow/month" : "# of Meow/year");
and turns into this:
Notice how EditText bleedCount is way taller on the second picture. The value of bleedCount.getHeight() is changing from 72 to 95, and I can't understand what is causing it.
It's connected with android:ems="10"
When EditText changed it's width, after showing up of view with x12, it must have been splited into two lines.
ems has size of one letter for given font.
I think you don't need ems.
Set EditText as single lined: android:singleLine="true"
The bleedCount EditText resizing is due to your hint text becoming longer than a single line when (pos == 1).
If you comment out the following line in your code, the resizing will stop happening:
// bleedCount.setHint(pos == 1 ? "# of Meow/month" : "# of Meow/year");
Maybe you can make it shorter/smaller to prevent the resizing?
Okay, since I don't have your entire code base I put something simple together to replicate what you are doing. Just FYI, I am using ICS 4.1. I didn't have any of the problems you are having, so perhaps this is an API issue. Perhaps you can look at my code base and see where there are differences between it and your own. Therein may lie the solution.
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_rl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top TextView" />
<Spinner
android:layout_below="#+id/textView4"
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="#+id/spinner1"
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Above EditText" />
<EditText
android:id="#+id/bleedCount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView5"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/abrMult"
android:ems="10"
android:inputType="number" >
</EditText>
<TextView
android:id="#+id/abrMult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_alignBaseline="#+id/abrSubmit"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/abrSubmit"
android:text="x12"
android:visibility="gone" />
<Button
android:id="#+id/abrSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView5"
android:text="Submit" />
</RelativeLayout>
Code:
public class ExampleActivity extends Activity implements OnItemSelectedListener {
private Button submitButton;
private TextView tv;
private Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
submitButton = (Button) findViewById(R.id.abrSubmit);
tv = (TextView) findViewById(R.id.abrMult);
spinner = (Spinner) findViewById(R.id.spinner1);
// create the data array for the spinner
String[] strings = { "This", "That", "The Other" };
// create the spinner adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, strings);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set the adapter on the spinner
spinner.setAdapter(adapter);
// set the event listener for the spinner
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (submitButton.getText().equals("Calculate")) {
submitButton.setText("Submit");
tv.setVisibility(View.GONE);
} else {
submitButton.setText("Calculate");
tv.setVisibility(View.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Hope it helps...
Related
I made this dialog for my app that shows up allows you to delete the items from db. It used to be a textView and a button which called the function, all in a linear layout, which worked good. Now I changed to a Relative Layout so I could add another button on the same line as the other, I also added an editText.
My problem is that since I added those objects I am not able to see the buttons anymore. the editText and textView are visible.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dialog_deletefood"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.paulcosma.app2.SecondActivity">
<TextView
android:text="TextView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lblFoodDetails"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" />
<Button
android:text="Şterge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnDeleteEatenFood"
android:layout_alignBaseline="#+id/btnModifyEatenFood"
android:layout_alignBottom="#+id/btnModifyEatenFood"
android:layout_toStartOf="#+id/lblFoodDetails"
android:visibility="visible" />
<Button
android:layout_marginTop="100dp"
android:text="Modifică"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnModifyEatenFood"
android:layout_below="#+id/lblFoodDetails"
android:layout_toEndOf="#+id/lblFoodDetails"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/txtEditValue"
android:maxLength="6"
style="#style/Widget.AppCompat.AutoCompleteTextView"
android:maxLines="1"
android:textAppearance="#style/TextAppearance.AppCompat"
android:fontFamily="sans-serif"
android:textSize="14sp"
android:textAlignment="center"
android:textColorLink="#android:color/holo_blue_light"
android:inputType="numberDecimal"
android:layout_marginTop="21dp"
android:layout_below="#+id/lblFoodDetails"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This dialog is supposed to show on listView item click. Below is the code I used to show it.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> hmap = (HashMap<String, String>)parent.getItemAtPosition(position);
final String _name = hmap.get("food");
final int _id = foodDB.getFoodId(_name);
AlertDialog.Builder mBuiler = new AlertDialog.Builder(SecondActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_deletefood,null);
final TextView lblFoodDetails = (TextView) mView.findViewById(R.id.lblFoodDetails);
final Button btnDeleteEatenFood = (Button) mView.findViewById(R.id.btnDeleteEatenFood);
lblFoodDetails.setText("Aţi ales produsul " + _name + ". Aici puteţi adăuga sau scădea cantitatea printr-o anumită valoare sau puteţi şterge apariţia produsului.");
mBuiler.setView(mView);
final AlertDialog dialog = mBuiler.create();
btnDeleteEatenFood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
foodDB.deleteEatenFood(_id);
Toast.makeText(SecondActivity.this,_name+" a fost sters",Toast.LENGTH_SHORT).show();
dialog.dismiss();
updateFoodList(foodDB.getAllEatenFood());
}
});
dialog.show();
}
This layout file renders the buttons in the Android layout designer as described (see below)
The problem may reside in code.
The problem was that both buttons where placed based on textView's size. When it changed, buttons got placed out of the screen
Bug:
android:layout_toEndOf="#+id/lblFoodDetails"
android:layout_toEStartOf="#+id/lblFoodDetails"
I have two TextView which I want to put a custom font on.
In the OnCreate of my MainActivity, after the setContentView call, I wrote the following code:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
TextView welcomeTextView = (TextView)findViewById(R.id.welcome_message);
TextView introTextView = (TextView)findViewById(R.id.introduction_message);
welcomeTextView.setTypeface(myTypeface);
introTextView.setTypeface(myTypeface);
But for some reason, the font stays default.
The app runs on Galaxy S6 Edge device (Lollipop 5.0.2).
I read here that there's a problem with custom fonts on Lollipop, and in order to fix it I converted my font with the tool provided in the thread to ttx and vice versa, but even this didn't help.
Any ideas what can I do?
Edit:
Tested on Jellybean (4.3), not working either.
Another edit:
It works on a clean new project! Cant figure out the difference.
Putting the MainActivity and its' XML for help:
public class MainActivity extends FragmentActivity {
private LoginFragment loginFragment;
private TextView welcomeTextView;
private TextView introTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideActionBar();
setContentView(R.layout.activity_main);
welcomeTextView = (TextView)findViewById(R.id.welcome_message);
introTextView = (TextView)findViewById(R.id.introduction_message);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
welcomeTextView.setTypeface(myTypeface);
introTextView.setTypeface(myTypeface);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
loginFragment = new LoginFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, loginFragment, "facebookFragment").commit();
}
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
// get all views on screen
ArrayList<View> views = new ArrayList<View>()
{{
add(findViewById(R.id.welcome_message));
add(findViewById(R.id.introduction_message));
add(findViewById(R.id.below_login));
add(findViewById(R.id.above_login));
add(findViewById(R.id.authButton));
add(findViewById(R.id.logo));
}};
// set animations
for(View view : views)
YoYo.with(Techniques.Tada).duration(700).playOn(view);
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void hideActionBar(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
getActionBar().hide();
}
And the activity's 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:background="#drawable/main_background">
<ImageView
android:id="#+id/logo"
android:layout_width="256dp"
android:layout_height="101dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#drawable/logo" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/welcome_message"
android:textColor="#color/White"
android:textSize="25sp"
style="#style/ShadowStyleText"
android:gravity="center" />
<TextView
android:id="#+id/introduction_message"
android:layout_below="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/introduction_message"
android:textColor="#color/White"
android:textSize="18sp"
android:gravity="center"
style="#style/ShadowStyleText"
android:layout_marginTop="5dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/bottom_login_chunk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp">
<TextView
android:id="#+id/above_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/above_login_button"
android:textColor="#color/White"
android:textSize="18sp"
style="#style/ShadowStyleText"
android:gravity="center" />
<com.facebook.widget.LoginButton
android:id="#+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/above_login"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/below_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/authButton"
android:layout_centerHorizontal="true"
android:text="#string/below_login_button"
android:textColor="#color/White"
style="#style/ShadowStyleText"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>
Ok so I found out recently that the facebook login button caused the problem, since everything worked when it was commented out.
So basically to solve this problem use another method to show the button instead using support fragment manager; sdk docs has lots of examples.
I'm working on my first Android project, which means that I'm not that familiar with Android and Java, and I want to move scroll of ScrollView after a user presses the button for next action (IME_ACTION_SEND). My codes are as follows.
activity_add.xml
It basically consists of TextView, EditText, NumberPicker, Spinner, Button. I want to move the scroll after IME_ACTION_SEND on EditText, so that the NumberPicker is centered on the screen.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/addActivity_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<LinearLayout
android:id="#+id/layout_activity_add"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_addItem"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="40sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout"
android:layout_marginTop="50dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine = "true"
android:maxLength="20" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout3"
android:layout_marginTop="50dp">
<TextView
android:id="#+id/period_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_period"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="#+id/period_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/period_textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</NumberPicker>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout4"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_category"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_toStartOf="#+id/background_spinner"
android:layout_toLeftOf="#+id/background_spinner" />
<Spinner
android:id="#+id/background_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical">
</Spinner>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_submit"
android:layout_gravity="center_horizontal"
android:text="#string/label_submit" />
</LinearLayout>
</ScrollView>
AddItemActivity.java
I've just copied the part which I think is relevant.
public class AddSinceItemActivity extends ActionBarActivity {
EditText title;
Spinner spinner;
NumberPicker numberPicker;
String title_string;
ViewGroup linearLayout;
ScrollView addActivity_scrollview;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
addActivity_scrollview = (ScrollView) findViewById(R.id.addActivity_scrollview);
linearLayout = (ViewGroup) findViewById(R.id.layout_activity_add);
title = (EditText) findViewById(R.id.title_editText);
numberPicker = (NumberPicker) findViewById(R.id.period_number_picker);
spinner = (Spinner) findViewById(R.id.background_spinner);
/* For title */
title.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
// I think here is the most important part.
addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());
}
return false;
}
});
}
}
For addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());,
I've tried many possible codes, such as
addActivity_scrollview.smoothScrollTo(0, spinner.getTop());
addActivity_scrollview.smoothScrollTo(0, 300); (300 is just a random number)
addActivity_scrollview.smoothScrollBy(0, 300);
but the scroll is always stuck (it moves a little but it's always the same position with above codes) and the screen barely shows the selected number of NumberPicker. How can I achieve the goal to set scroll so that the screen shows the entire NumberPicker?
just add this line to your edittext code in xml :android:imeOptions="actionSend" and then just try this into your activityaddActivity_scrollview.smoothScrollTo(0, 480);
I should've added android:imeOptions="actionSend" to <EditText> in order to properly listen on onEditorAction(). I thought it'd be okay with android:singLine="true" because that option enabled "next" action on keyboard.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine="true"
android:maxLength="20"
android:imeOptions="actionSend" />
I am learning Java and Android SDK. I want to create a simple application that is swapping 2 buttons so the left one is moving to the place of right one and v.v. with simple animation. I have tried the ObjectAnimator because I have read that it is moving the views objects permanently. But it's not :-( The objects stays there I mean the left one on the right and v.v. but their getLeft(), getTop() values are the same, and after next animations starts the objects are returning to the start position immediately. I have read that the ObjectAnimator needs some additional function to work properly but in the documentation there is no example :-( I have tried to add the setTranslationX function but it hasn't worked. Could somebody provide me with some simple example of how to do this?
http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator
"The object property that you are animating must have a setter function (in camel case) in the form of set(). Because the ObjectAnimator automatically updates the property during animation, it must be able to access the property with this setter method. For example, if the property name is foo, you need to have a setFoo() method. If this setter method does not exist, you have three options:
Add the setter method to the class if you have the rights to do so.
Use a wrapper class that you have rights to change and have that wrapper receive the value with a valid setter method and forward it to the original object.
Use ValueAnimator instead."
Thanks in advance.
try this code:
public class ActivityStartup extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("hello");
setContentView(R.layout.main);
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.SLIDING_WINDOW);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindOffset(100);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
View view = G.layoutInflater.inflate(R.layout.menu, null);
view.findViewById(R.id.layout_crop).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(G.context, "Crop Clicked", Toast.LENGTH_SHORT).show();
}
});
view.findViewById(R.id.img_logo).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://example.com"));
startActivity(browserIntent);
}
});
menu.setMenu(view);
}
}
public class G extends Application {
public static LayoutInflater layoutInflater;
public static Context context;
#Override
public void onCreate() {
super.onCreate();
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
context = getApplicationContext();
}
}
main.xml is:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
menu.xml is:
<ImageView
android:id="#+id/img_logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#drawable/hlogo" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#00ff00"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/layout_crop"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_crop" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crop"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_day"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_day" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_delete"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_delete" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:textColor="#000000" />
</LinearLayout>
I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.