I m noob to android, I have a simple Question.
i have TextView in a RelativeLayout, i want to add a button dynamically just below that TextView which is present in a RelativeLayout,
how can i do it ?
thanks !
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#mipmap/background"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
<android.support.v4.view.ViewPager
android:id="#+id/screen3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</android.support.v4.view.ViewPager>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:paddingTop="30dp"
android:id="#+id/act3_txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:textAlignment="center"
android:textColor="#FFFFFF"
/>
<ImageView
android:paddingTop="30dp"
android:id="#+id/cam_images"
android:layout_height="250sp"
android:layout_width="wrap_content"
android:layout_below="#+id/act3_txt1" />
<TextView
android:paddingTop="30dp"
android:id="#+id/act3_txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cam_images"
android:textSize="35sp"
android:textAlignment="center"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:paddingBottom="20dp"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
1) create a button like this inside onCreate().
Button myButton = new Button(this);
myButton.setText("Press Me");
2) then add it into your layout. before it you have to add id for your layout in which you want to show the button.
RelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutID);
layout.addView(myButton);
Inflate the relative layout and the existing textview first
relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout);
textView= (TextView) findViewById(R.id.tittle);
Prepare layout params and addRule method to put the button below the textview
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.tittle);
Button button=new Button(this);
button.setLayoutParams(params);
button.setText("Click Me!");
relativeLayout.addView(button,1);
Related
I'm working on a gallery type app. The issue that I have at the moment is trying to fetch the typed text within an EditText. The error that I am getting is a casting exception because the code fetches the EditText block from the layout as a TextView block. This only occurs after I swipe, which causes the some of the LinearLayouts imbeded in the second GridLayout to be removed.
The LinearLayouts are put in at runtime into the Gridlayout (with the id product_details), the amount Layouts is calculated according to which picture is pressed on.
This part of the code fetches the current LinearLayouts that are imbeded in the GridLayout:
for (int i = 0; i<codes.size(); i++) {
//Sets the different elements to te correct texts
ViewGroup linearLayout = (ViewGroup) gridLayout.getChildAt(i);
TextView textCode = (TextView) linearLayout.getChildAt(0);
TextView textPrice = (TextView) linearLayout.getChildAt(1);
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
commentsAddList.add(editTextC.getText().toString());
quantityAddList.add(editTextQ.getText().toString());
priceAddList.add(textPrice.getText().toString().substring(1));
codeAddList.add(textCode.getText().toString());
productModelAddList.add(new ProductModel());
productModelAddList.get(i).setCode(codeAddList.get(i));
if(!quantityAddList.get(i).equals("")) {
productModelAddList.get(i).setQuantity(Integer.parseInt(quantityAddList.get(i)));
}else{
productModelAddList.get(i).setQuantity(0);
}
productModelAddList.get(i).setComment(commentsAddList.get(i));
if(!priceAddList.get(i).equals("")) {
productModelAddList.get(i).setPrice(Double.parseDouble(priceAddList.get(i)));
}else{
productModelAddList.get(i).setPrice(0);
}
mRequestPermissions();
adaptToText.addToList(productModelAddList.get(i));
}
The main picture xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageLayout"
android:columnCount="2"
android:verticalSpacing="0dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:fitsSystemWindows="true">
<ImageView
android:layout_columnSpan="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"/>
<GridLayout
android:id="#+id/product_details"
android:columnCount="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</GridLayout>
</GridLayout>
The LinearLayout that is inserted into the GridLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_columnSpan="1"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:id="#+id/orderSideLayout">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productCode"
android:text=""
android:padding="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productPrice"
android:text=""
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editText1"
android:hint="Quantity goes here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="number"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/comments"
android:text="Comments: "
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editTextComments"
android:hint="Comments here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>
I'm not sure where the casting exception is caused or why exactly it happened. If anyone is able to help, I would appreciate it a lot! Thanks in advance!
Instead of getting the view by position getting it by id would be easier.
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
Try this instead:
EditText editTextQ = (EditText) linearLayout.findViewById(R.id.editText1);
EditText editTextC = (EditText) linearLayout.findViewById(R.id.editTextComments);
This will be better because if change the xml, it could change view positions, and find views by id you wouldn't need to change the code.
I am trying to create a TextView programmatically which will be shown in another layout that doesn't belong to the same activity but the TextView isn't shown. Following is the code:
Activity:
public class LogActivity extends AppCompatActivity {
LinearLayout textLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
LayoutInflater layoutInflater=getLayoutInflater();
View textEntryLayout=layoutInflater.inflate(R.layout.layout_two, null);
textLayout=(LinearLayout) textEntryLayout.findViewById(R.id.layout);
textView=new TextView(this);
textView.setText("TextView");
textView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout.addView(textView);
}
Layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#color/editTextBG">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
That's because your container layout, has other children whose heights are set to match_parent. You won't be able to see TextView you add in Java code since the other children added in XML fill all the screen.
You need to add textEntryLayout to your layout. You are modifying that inflated view without adding it to (e.g.) a layout in your layout_one.
Try:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_id_in_layout1);
layout.addView(textEntryLayout);
I have the following code:
public class MainActivity extends ActionBarActivity {
EditText userfield;
EditText passfield;
Button logbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userfield = (EditText)findViewById(R.id.editText);
passfield = (EditText)findViewById(R.id.editText2);
logbtn = (Button)findViewById(R.id.button);
GifWebView view = new GifWebView(this, "file:///android_asset/watg.gif");
view.getSettings().setLoadWithOverviewMode(true);
view.getSettings().setUseWideViewPort(true);
setContentView(view);
userfield.bringToFront();
userfield.invalidate();
passfield.bringToFront();
passfield.invalidate();
logbtn.bringToFront();
logbtn.invalidate();
}
As you can see the GifWebView (extends WebView) is created programatically after the onCreate()
The button and EditTexts are made in the Activity.xml but are brought to front programatically in the Java file.
Why doesnt the above code work?
EDIT - Frame Layout Problem
My XML Code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.dencallanan.giftestapp.GifWebView android:id="#+id/GWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<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">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_marginBottom="153dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="USERNAME" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_marginTop="58dp"
android:layout_alignTop="#+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:hint="PASSWORD" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="#+id/button"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
</FrameLayout>
The Error:
You are calling setContentView(R.layout.activity_main) and again later setContentView(view). setContentView will overwrite the layout, this will in effect, replace it with a new layout. I suggest using a FrameLayout in XML file... Webview will be your bottom layer. On top of that Put a Linear/RelativeLayout with the Edittexts and Button... Simple and clean.
You can add your webview in XML like:
<com.package.GifWebView android:id="#+id/GWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
In code, access this as:
GifWebView gWView = (GifWebView) findViewById(R.id.GWebView);
gWView = new GifWebView(this, "file:///android_asset/watg.gif");
Dialog Java Code:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Title...");
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
XML Code Here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:scrollbarAlwaysDrawVerticalTrack="true">
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
I know that if i have only text on alert dialog it's automatically scrolling, but with an imageview automatically scrolling doesn't work.
How can i fix this problem?
Put your layout inside a ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" />
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" />
</LinearLayout>
<ScrollView>
I see that your LinearLayout has a horizontal orientation. So if this the case and you want to scroll horizontaly, you should replace ScrollView by HorizontalScrollView and set the orientation to horizontal.
Let me know if this works.
I try to make a ScrollView to act like a ListView. So every row will be a TextView added dynamically. but the program crashes. I have a ScrollView inside a LinearLayout. What i do wrong? Thanks.
Here is my code
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="#+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
A scrollView Can only have one child. If you try to add more than 1 child to the scroll view then it will crash. You should have the LinearLayout inside of the scrollview and then dynamically add textViews to the linear layout.
You are trying to add LinearLayout1 as its own child's child (being your own grandfather, is a confusing notion).
Change you XML around like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
You cannot add views to a scrollview. You can only add them to a viewgroup, like linear or relativelayout.
It is not exactly clear what you are trying to achieve, but what is wrong with using a listview for this? You could set a max height on the listview. Or you could try wdziemia's suggestion