When I programmatically generate textviews, static ones don't appear - java

I have this layout :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:orientation="vertical"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_goster" tools:context="com.example.GosterActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_line1"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_line2"
android:layout_centerHorizontal="true"
android:layout_below="#+id/text_line1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rl_root"
android:layout_below="#+id/text_line2">
</LinearLayout>
</LinearLayout>
</ScrollView>
In Java, I set texts of this layout like this :
tvline1.setText("asdf1");
tvline1.setText("asdf2");
And I programmatically generate TextViews in rl_root like this :
LinearLayout relativeLayout2 = (LinearLayout) findViewById(R.id.rl_root);
for(int i = 0; i < 5; i++)
{
TextView textvw = new TextView(this);
textvw.setText(Integer.toString(i));
relativeLayout2.addView(textvw);
for(int j = 0; j < 10; j++)
{
TextView textvw2 = new TextView(this);
textvw2 .setText(Integer.toString(j+5));
relativeLayout2.addView(textvw2);
}
}
With this configuration, I can only see the content of generated textviews, not the first two ones that I wrote by hand. Can you tell me how I can show those two TextViews as well? Thanks.

I am able to get the both kind of textviews - generated ones and handwritten. Code seems to be fine. I just removed these lines
tools:showIn="#layout/activity_goster"
tools:context="com.example.GosterActivity"

Related

Adding multiple textviews to horizontal linear layout

Hi i am running a loop 10 times and each time i am creating a new TextView and I am adding that textview to my Linear layout which has orientation set to 'HORIZONTAL'. I want to show these texts in the form of paragraph where another sentence starts form where one sentence ends.
This is my layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ui.QuestionActivity"
android:padding="16dp"
android:orientation="vertical"
tools:showIn="#layout/activity_question">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title"
android:layout_gravity="center"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#android:color/black"
android:textStyle="bold|italic"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:id="#+id/base_layout"/>
</LinearLayout>
</ScrollView>
This is my java code:
for(int i=0; i<10; i++){
TextView textView = Utility.getTextView(this, i);
if(i == 5) {
textView.setText(sentences.get(i) + "\n" + "\n");
}
else {
textView.setText(sentences.get(i) + " ");
}
mLayout.addView(textView);
}
But the issue is only first textview is shown when orientation is set to 'horizontal' and if i set my orientation to 'vertical' i am able to see all textviews below one another.
Please help me on this.
You should need to wrap your base_layout LinearLayout under HorizontalScrollView.
Try below, should solve your issue. I have verified myself.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal">
<LinearLayout
android:id="#+id/base_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal" />
</HorizontalScrollView>

How to get the Chid Layout's children?

I try to get all the WebView elements in current activity at the time loading and i get the output.
It's working perfectly.
Code:
<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: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.dmp_webview.MainActivity"
android:orientation="vertical">
<WebView
android:id="#+id/simpleWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:scrollbars="none" />
<WebView
android:id="#+id/simpleWebView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:scrollbars="none" />
</LinearLayout>
private void forWebviews() {
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
int elementSize = getWenViews(viewGroup).size();
for(int j =0; j< elementSize;j++)
{
WebView getAllWebView = formIsValid(viewGroup).get(j);
String SplitTypeWebView = String.valueOf(getAllWebView);
String getWebValues = getAllWebView.getUrl().toString();
SplitWebViewID(SplitTypeWebView);
}
}
private ArrayList<WebView> getWenViews(ViewGroup viewGroup) {
ArrayList<WebView> webview = new ArrayList<WebView>();
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View v = viewGroup.getChildAt(i);
if (v instanceof WebView) {
webview.add((WebView) v);
}
}
return webview;
}
BUT when i one more LinearLayout(Not only linearlayout other layouts also) for webview parent means like this,
<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: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.dmp_webview.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="16dp"
android:id="#+id/RootLayout_1">
<WebView
android:id="#+id/simpleWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:scrollbars="none" />
<WebView
android:id="#+id/simpleWebView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:scrollbars="none" />
</LinearLayout>
</LinearLayout>
Its not working properly. why? It will return no child view counts. I need to clarifying one things.
is it possible to get the all the child layout's child.
(e.g )
ROOT LAYOUT -> LinearLayout(some other layout)->LinearLayout(some other layout)-Views
plz guide me.Actually i get root layout and iterate over.
How to get all child views at run time? It does not matter How many root layouts or child layouts or views. i need to get all.
need to get all views(does not matter viewgroup/webview if possible means need both of them also)
You can use a recursive method for that.
Here is an example:
private static void getViews(list, ViewGroup viewGroup){
for(int i = 0; i < viewGroup.getChildCount(); i++){
View view = viewGroup.getChildAt(i);
list.add(view);
if(view instanceof ViewGroup){
getViews((ViewGroup) view);
}
}
}
And here is how you call it.
List<View> list = new ArrayList<View>();
getViews(list, (ViewGroup) this.findViewById(android.R.id.content));
And if you want to filter the WebViews you can do something like this:
List<WebView> webViews = new ArrayList<WebView>();
for(View view : list){
if(view instanceof WebView){
webViews.add((WebView) view);
}
}

Limited recreation of a button through a forloop

So I'm building this app where each time the floatActionButton is clicked, a button is created. That aspect of the app works fine. But my problem is that the buttons are being created on top of one another and are constantly created. I need to create only 4 buttons click after click, one below another.
This is my for-loop. I appreciate any help.
addingSemester.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i <= 4; i++){
Button semesterButton = new Button(MainActivity.this);
semesterButton.setId(i);
semesterButton.setText("Semester " + i);
semesterButton.setLayoutParams(lp);
semesterLayout.addView(semesterButton);
i++;
}
}
});
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="myapp.onur.journeygpacalculator.MainActivity">
<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:orientation="horizontal"-->
<!--android:padding="6dp"-->
<!-->-->
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Semester 1"-->
<!--android:layout_weight="4"-->
<!--android:textColor="#FFFFFF"-->
<!--android:background="#3F51B5"-->
<!--/>-->
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="DELETE"-->
<!--android:layout_weight="0.6"-->
<!--android:clickable="true"-->
<!--android:textAlignment="center"-->
<!--/>-->
<!--</LinearLayout>-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/addActionButton"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/ic_add_black_24dp"
app:borderWidth="0dp"/>
</LinearLayout>

Make RelativeLayout scrollable

I want to make a scrollable RelativeLayout where I can create Buttons with a x and y positions and custom width and height.
Here is the code i got so far XML:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/buttonDel"
android:fillViewport="true">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/RL">
</RelativeLayout>
</LinearLayout>
</ScrollView>
Java:
RelativeLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
linearLayout = (RelativeLayout) findViewById(R.id.RL);
addButton();
}
private void addButton() {
for(int i = 0; i < 20; i++)
for(int j = 0; j < 4; j++)
{
Button but = new Button(this);
but.setX(j * 200);
but.setY(i * 200);
but.setText("B" + i);
linearLayout.addView(but, 200, 200);
}
}
It result in something that i want except the scrolling part. I don't know why the ScrollView isn't working.
Screen so far.
After I changed ScrollView layout_height="match_parent" to "warp_content"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelativeLayout>
</ScrollView>
However the right way to build the requisite structure is to design a gridview or listview .
Use single child for scroll view to behave correctly.
Change the ScrollView height from "match_parent" to "wrap_content" this should fix the problem:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonDel"
android:fillViewport="true"
>
...

When duplicating view how to get reference to each view duplicated?

I have a hidden layout.
add_event_price.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/add_event_price_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText android:id="#+id/add_event_wording"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="#string/event_wording_hint" />
<EditText android:id="#+id/add_event_price"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="#string/event_price_hint" />
<ImageButton android:id="#+id/add_event_cross"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/cross"
android:background="#null"
android:layout_gravity="center_vertical"
android:contentDescription="#string/delete" />
</LinearLayout>
When clicking on a button in the main layout, the hidden layout appears.
<ScrollView 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: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="#color/app_background_color">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/add_event_dynamic"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<Button android:id="#+id/add_event_add_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:text="#string/add_event_add_field" />
</LinearLayout>
</ScrollView>
add_event_add_field.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout priceLayout = (LinearLayout) rootView.findViewById(R.id.add_event_price_layout);
LinearLayout dynamicLayout = (LinearLayout) rootView.findViewById(R.id.add_event_dynamic);
View hidden = inflater.inflate(R.layout.add_event_price, priceLayout, false);
dynamicLayout.addView(hidden);
}
});
Adding the hidden layout dynamically works but I don't know how to retrieve the values of the EditText in the hidden layout, and delete a view when clicking on the corresponding add_event_cross ImageButton.
First, you can call findViewById on dynamicLayout:
EditText et = (EditText) dynamicLayout.findViewById(R.id.add_event_wording);
Second, I suggest not to show/hide layout by adding view. Just put the dynamicLayout inside your main layout and use something like:
dynamicLayout.setVisibility(LinearLayout.GONE); //makes the layout hidden
I have not tested the code but you can build upon it.

Categories