This is the code comming from the Activity file. I would like to have the layout looks the same but i want that code in XML format. Because it's so damn disturbing to have some code in dynamically and some in XML files. So please can someone make this from dynamically to xml layout?
protected void initLayout() {
// root view - GRN
LinearLayout rootView = new LinearLayout(this.getApplicationContext());
rootView.setOrientation(LinearLayout.VERTICAL);
this.mText = new TextView(this.getApplicationContext());
this.mText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rootView.addView(this.mText);
this.eventLayout = new LinearLayout(this.getApplicationContext());
this.eventLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView sv_obj = new ScrollView(this.getApplicationContext());
sv_obj.addView(this.eventLayout);
rootView.addView(sv_obj);
this.setContentView(rootView);
}
<LinearLayout android:orientation="vertical>
<TextView android:width="wrap_content android:height="wrap_content/>
<ScrollView>
<LinerLayout android:orientation="vertical>
</LinearLayout>
</ScrollView>
</LinearLayout>
Related
I'm trying to add a bunch of ImageView on my UI using a loop, the problem is that I have no Idea if the ImageView is being added or not because when I run the app it just gives me a blank white screen.
for(int i = 0; i < jsonArray.length(); i++) {
Log.d("test", "ok"); //the loop works btw
poster.setId(i);
JSONObject jsonObject = jsonArray.getJSONObject(i);
ImageView poster = new ImageView(getApplicationContext());
poster.setBackgroundResource(R.drawable.myPoster);
RelativeLayout.LayoutParams posterParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
posterParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
posterParams.addRule(RelativeLayout.CENTER_VERTICAL);
posterParams.width = 160; //is this DP?
posterParams.height = 220;
relativeLayout.addView(poster, posterParams);
}
Any suggestion is welcome.
EDIT
I added another piece of code just to test if a widget will be added without using a loop:
//test
LinearLayout layout = new LinearLayout(this);
Button btn = new Button(this);
btn.setText("this is a button");
btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
layout.addView(btn);
And I still get the same result, just blank.
First of all, you are giving exactly the same parameter to each ImageView this causes all your ImageView lays on Like STACK only the last one will be visible to you. You may use ScrollView to see if the ImageViews actually added to your root layout
Secondly, set layout parameters to your dynamic ImageViews not your root layout.
Update
How to use ScrollView,
First of all, your XML should contain ScollView and a child (LinearLayout in our case but you can choose any layout depending on your use case) for placing your ImageView
<ScrollView>
<LinearLayout
android:id = "imageViewPlaceHolder">
</LinearLayout>
</ScrollView>
Secondly, in your Java code, you should find an inner layout to add views as follows
LinearLayout placeHolder = findViewById(R.id.imageViewPlaceHolder);
Then you are ready to add ImageViews into placeHolder since your placeHolder wrapped with ScrollView it will create a scroll dynamically if the content height goes beyond the dedicated height.
placeHolder.addView(yourImageView);
Adding everything from Java
HorizontalScrollView hsv = new HorizontalScrollView(context);
hsv.setLayoutParams(yourLayoutParams);
LinearLayout myPlaceHolder = new LinearLayout(context);
myPlaceHolder.setLayoutParams(yourLayoutParamsForLinearLayout);
myPlaceHolder.addView(yourDesiredImageView);
hsv.addView(myPlaceHolder);
yourRootLayout.addView(hsv);
Hope it makes sense, feel free to ask any clarification
try this:
listView = (ListView) findViewById(R.id.lv_info);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, text);
listView.addHeaderView(creatHeader(getString(R.string.abuot_email),getResources().getString(R.string.email_info)));
listView.addHeaderView(creatHeader(getString(R.string.about_phone),getResources().getString(R.string.admin_phone_num)));
listView.addHeaderView(creatHeader(getString(R.string.about_copyright), getResources().getString(R.string.copyright_info)));
listView.addHeaderView(creatHeader(getString(R.string.about_version),getResources().getString(R.string.version_info)));
listView.setAdapter(adapter);
Method creatHeader:
public View creatHeader(String title, String text) {
View v = getLayoutInflater().inflate(R.layout.lv_item_header_info, null);
((TextView) v.findViewById(R.id.tvTitle)).setText(title);
((TextView) v.findViewById(R.id.tvText)).setText(text);
return v;
}
Layout file for items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ssss"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sssssssss"
android:gravity="center_vertical"/>
</LinearLayout>
I am quiet new to android developing sorry!
I want to add doctor values as Strings (first name, last name, and some others later) as one entry (I was trying to do so with Objects) to my Scroll View via a button. I know that I need a Layout and a Text View to do so but my Virtual Machine crushes.
As far as I understand I have to put my Text View (with the Strings) in the Layout and the Layout in the Scroll View (am I wrong?).
I was trying several things from different websites which all provide similar solutions but nothing worked so far. I am using Android 6.0 with android studio and designed my UI with the Design View and adapted some code in the XML files.
public void addDoctor(View view) {
setContentView(R.layout.activity_main);
// standard java class Doctor
Doctor doc = new Doctor("Foo", "Boo");
// this is the ScrollView I generated with the design
ScrollView sv = (ScrollView) this.findViewById(R.id.ScrollViewDoctor);
TextView tv = new TextView(this);
// trying to fix the parent problem here
if(tv.getParent() != null) {
((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}
//this should be "Foo"
tv.setText(doc.getDocName());
// this is the layout I generated with the design "linlay"
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linlay);
sv.addView(ll);
//only one child object! -> in the error log
ll.addView(tv);
setContentView(view);
}
I expected the Strings of the object to appear in the Scroll View but the error log says that "ScrollView can host only one direct child" which I was trying to fix with the if statement but it does not seem to affect my code.
Can you please help me with this. Do I miss something?
Thank you!
As far as I understand I have to put my Text View (with the Strings) in the Layout and the Layout in the Scroll View (am I wrong?).
You've right.
<ScrollView>
<LinearLayout>
<TextView></TextView>
</LinearLayout>
</ScrollView>
How to Add a doctor in the right way
error log says that "ScrollView can host only one direct child"
You're getting the error because you added already the linear layout inside the <ScrollView>, so you don't have to call, sv.addView(ll); because it's already inside (And you cannot add multiple layout in standard ScrollView)
So you're reference to ScrollView it's useless.
if you're xml is like this:
<ScrollView>
<LinearLayout>
</LinearLayout>
</ScrollView>
You can achieve your result with this:
public class MyActivityDoctors extends Activity {
ScrollView sv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv = (ScrollView) this.findViewById(R.id.ScrollViewDoctor);
}
public void addDoctor(Doctor doctor)
{
//Linear Layout inside your ScrollView
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linlay);
//Create a new TextView with doctor data
TextView tv = new TextView(this);
tv.setText(doctor.getDocName());
//Adding textView to LinearLayout
ll.addView(tv);
}
}
remove setContentView(view); on the end, you have already set this layout in first line
setContentView in most cases should be called once, inside onCreate method. if you are calling setContentView multiple times you are basically overrriding previous one
also this
if(tv.getParent() != null) {
((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}
is unnecessary, you are creating new TextView line above, it wont have parent for shure
Why don't you use a RecyclerView? And then you don't need a ScrollView. And don't call setContentView(view); multiples times. You have to do only 1 time.
you need to create Multiline TextView
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:fillViewport="true">
<TextView
android:id="#+id/txtquestion"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/abs__dialog_full_holo_light"
android:lines="20"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:textSize="15sp" />
</ScrollView>
I want to create multiple dynamic TextView but I need them to look the same way as one of the TextView that already style in xml. How can I do that?
For example I want all the dynamic created TextView to have same attribute as the main one:
<TextView
android:id="#+id/mainTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimary"
android:gravity="center_vertical"
android:padding="14dp"/>
You can implement this by using this:
Take a parent Linearlayout with the orientation vertical
LinearLayout parentView= findViewById(R.id.parentView);
code to add the views:
for(int i = 0; i < /*No of views*/; i++) {
View itemView = LayoutInflater.from(this).inflate(R.layout.catagoryitem, parentView, false);
parentView.addView(itemView);
}
Here catagoryitem is the TextView with style xml file.
i want to Call Android Layout(XML) Programmatically in a function.
Layout is already Created in XML. i am asking this question because i don't want to use this in Android Native app, actually i will call these layouts in Unity3D which is a Gaming Engine. So Let say that i have a layout. For Example look at below code:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/useful_nums_item_name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/useful_nums_item_value"/>
</LinearLayout>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/call"
android:id="#+id/call_btn"
android:onClick="callNumber"/>
</LinearLayout>
Now i want to create a function which can call this layout. But But i don't want to use below code as i am not going to use in Android Native App.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
i will directly use the Unity3D class as below:
public class MainActivity extends UnityPlayerActivity {
}
So i need to call my layout in that class but in form of Some Function. For Example:
public class MainActivity extends UnityPlayerActivity {
public void ShowLayout(){
enter code here
}
}
So i need you people help to solve this problem.
Any help will be appreciated.
Take your layout with the use of inflator and then bring it on front.
LayoutInflater inflater = LayoutInflater.from(context);
View yourView = inflater.inflate(R.layout.popup_layout, null, false);
// then bring it to front
yourView.bringToFront();
Define your view / layout without XML instead. In your case, you could define it in the ShowLayout() method. This code should point you in the right direction:
LinearLayout layout = new LinearLayout(getContext());
layout.setWeightSum(1);
LinearLayout childLayout = new LinearLayout(getContext());
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.8f);
childLayout.setLayoutParams(childParams);
LinearLayout.LayoutParams fieldParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView name = new TextView(getContext());
name.setLayoutParams(fieldParams);
TextView value = new TextView(getContext());
name.setLayoutParams(fieldParams);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.2f);
ImageButton call = new ImageButton(getContext());
call.setLayoutParams(buttonParams);
call.setImageResource(getResources().getDrawable(R.id.call));
I am creating my controls programmatically, based on JSON received from a server. One of the controls I need to create is a WebView that is horizontally centred on the screen. This is simple in xml layouts as shown below using the layout_gravity option. But how do you do this in code, the WebView unlike TextView does not have a setGravity(Gravity.HORIZONTAL_GRAVITY_MASK) method.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
I would use a RelativeLayout, then you can use LayoutParams when adding the view:
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
relativeLayout.addView(yourWebView, layoutParams);
i use LinearLayout to show webview and setGravity.
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
linearLayout.setGravity(Gravity.CENTER);
WebView view = new WebView(this);
linearLayout.addView(view);
setContentView(linearLayout);
it help you.