Dynamic layout similar to people app - android

I am attempting to achieve a result similar to:
Where contacts might or might not have email addresses or other profile information. Is it possible to achieve the majority of it through XML or does it all need to be done through Java?
Here is the pseudocode that I imagine would be a solution:
if (contact.hasPhoneNumber)
foreach number in contact
inflate r.layout.contact_number
settext phonenumber textview
add view to layout
Is there a smarter approach?

This was solved by having the following:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/contactContent"
android:layout_below="#+id/profile_background">
<LinearLayout android:orientation="vertical" android:id="#+id/c_detail_content_main" android:paddingBottom="50.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:visibility="gone" android:id="#+id/c_detail_birthday_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20.0dip">
</LinearLayout
</ScrollView>
And the following code:
if (this.contact.hasPhoneNumber())
{
LinearLayout phoneNumberContent = (LinearLayout) view.findViewById(R.id.c_detail_phone_layout);
phoneNumberContent.setVisibility(View.VISIBLE);
for (PhoneNumber number : this.contact.getPhoneNumbers())
{
LinearLayout tempNumberContent = new LinearLayout(this.getActivity().getApplicationContext());
TextView type = new TextView(this.getActivity().getApplicationContext());
TextView phoneNo = new TextView(this.getActivity().getApplicationContext());
type.setWidth(LayoutTools.getDPMeasurement(this.getActivity(), 85));
phoneNo.setText(number.getNumber());
tempNumberContent.addView(type);
tempNumberContent.addView(phoneNo);
phoneNumberContent.addView(tempNumberContent);
}
}

Related

How to create a LinearLayout programmatically using a for loop

I am trying to create a LinearLayout programmatically from information contained in an Array.
Let me explain :
I create a professional application that will not be on PlayStore.
I have the following Array, manually populated, (a kind of global variable):
static final String[][] reseaux = new String[][] {
{"Net1","192.168.0.1"},
{"Net2","192.168.10.1"},
{"Net3","192.168.20.1"},
{"Net4","192.168.30.1"}
};
For some customers, it contains only one entry :
static final String[][] reseaux = new String[][] {
{"Net1","192.168.0.1"}
};
According to the Array containing only one entry, I created the following layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/welcome"
android:layout_gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="Overdraw">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/linearLayoutTest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
And the code :
LinearLayout l = findViewById(R.id.linearLayoutTest);
for (int i = 0; i < reseaux.length; ++i) {
final int id = i;
ImageButton resBtn = new ImageButton(this);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv.setText(reseaux[i][0]);
resBtn.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.button_reseaux));
resBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
l.addView(resBtn);
l.addView(tv);
resBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Check network
if (CheckNetworkStatus.isNetworkAvailable(getApplicationContext())) {
Intent i = new Intent(getApplicationContext(),
HomeReseauActivity.class);
startActivity(i);
BASE_URL = null;
nomReseau = null;
BASE_URL = reseaux[id][1];
nomReseau = reseaux[id][0];
} else {
//Error message if no network connexion
if (toastMessage != null) toastMessage.cancel();
toastMessage = Toast.makeText(HomeActivity.this, getString(R.string.networkError), Toast.LENGTH_LONG);
toastMessage.show();
}
}
});
Give me the following result that suits me perfectly :
Now, when I have multiple entries in my Array, here's what I get :
This does not suit me. I will like horizontal lines of three icons. When a line is filled, it creates a new line of three icons, etc.
Here is a quick edit with paint to demonstrate what I want:
I think the solution must be simple .. but I can not get this result.
Be indulgent with my code, I learn everything by myself and there is surely a better way to write :-)
Hoping someone can help me!
As stated in the comments of my question, the solution was to use RecyclerView and GridLayoutManager.
For future readers, this link has helped me a lot!
Thanks to everyone who answered my question.

LinearLayout using half screen

Hi I have a ScrollView and a vertical LinearLayout inside that. Inside each LinearLayout slot I have a horizonal LinearLayout that holds 2 things a word and a number. The problem is some of the words are hidden?? and it takes up half the screen. Thanks for any help.
Layout bounds
for (int i = 0; i < words.size(); i++) {
LinearLayout horizontal = new LinearLayout(context);
horizontal.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
horizontal.setLayoutParams(LLParams);
btnWord.add(i, new Button(context));
btnWord.get(i).setHeight(60);
btnWord.get(i).setWidth(120);
btnWord.get(i).setTypeface(montFont);
btnWord.get(i).setBackgroundColor(Color.WHITE);
btnWord.get(i).setTag(i);
btnWord.get(i).setGravity(Gravity.CENTER);
btnWord.get(i).setText(" " + words.get(i));
btnWord.get(i).setOnClickListener(btnClicked);
horizontal.addView(btnWord.get(i));
wordWeight.add(i, new Button(context));
wordWeight.get(i).setHeight(60);
wordWeight.get(i).setWidth(40);
wordWeight.get(i).setTypeface(montFont);
wordWeight.get(i).setBackgroundColor(Color.WHITE);
wordWeight.get(i).setTag(i);
wordWeight.get(i).setGravity(Gravity.CENTER);
wordWeight.get(i).setText(" " + wordWeights.get(i));
wordWeight.get(i).setOnClickListener(btnClicked);
horizontal.addView(wordWeight.get(i));
linearLayout.addView(horizontal);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<ScrollView
android:layout_width="match_parent"
style="#android:style/Widget.ScrollView"
android:layout_marginTop="106dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout" />
</ScrollView>
</RelativeLayout>
You could try to set the weight of each "item" to 1 so they will equally divide the space on the screen. Something like the snippet below:
LinearLayout ll;
LinearLayout.LayoutParams lp;
lp.weight = 1;
ll.setLayoutParams(lp);
You could also ditch that logic and use a ListView with a custom adapter like I did on this answer, or setup a RecyclerView as you can see on this blog post. It's way easier and more efficient to do either one of those.
More on ListView vs. RecyclerView here.

No scrolling effect with ScrollView in PopupWindow

I want to implement a popup menu with complex effects, one of them is to support scrolling. It seems PopupMenu and AlertDialog can not meet the requirement what I need. So I tried PopupWindow with ScrollView.
Firstly, I prepared a layout which has a simple strcture just like what ApiDemo shows:
ScrollView
LinearLayout with Vertical attribute
TextView
TextView
TextView
...
Secondarily, I new a PopupWindow with this layout and 200width/300height, show it at somewhere with showAtLocation().
I can make scrollbar displayed and has scroll effect, but TextViews in LinearLayout do NOT scroll(they are in fixed position)!
Something is wrong but I have no sense on it.
(android3.0)
Thanks for any warm-heart man who can give me some tips.
-_-!!
I also faced similar issue. Some how wrapping relative layout in scrollview is not working for popupwindow.
I tried wrapping individual views under scrollview and it worked for me. PLease have a look below. Hope this helps
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#eebd9c"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView2" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:ems="15"
android:gravity="fill_horizontal"
android:minLines="6"
android:scrollbars="vertical"
android:singleLine="false"
android:text="Multiline text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView></RelativeLayout>
I think your requirement can be fullfilled by Dialog try the code i have given
protected void showInputDialog() {
final Dialog splRerDialog = new Dialog(getContext());
splRerDialog.setTitle("Special request.");
ScrollView scroll = new ScrollView(getContext());
LinearLayout lin = new LinearLayout(getContext());
lin.setLayoutParams( new LayoutParams(350,200));
lin.setGravity(Gravity.CENTER);
lin.setOrientation(LinearLayout.VERTICAL);
final EditText req = new EditText(getContext());
req.setLayoutParams( new LayoutParams(300,300));
lin.addView(req);
Button btn = new Button(getContext());
btn.setText("Ok");
btn.setLayoutParams( new LayoutParams(200,LayoutParams.WRAP_CONTENT));
btn.setGravity(Gravity.CENTER);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
splRerDialog.dismiss();
}
});
lin.addView(btn);
scroll.addView(lin);
splRerDialog.addContentView(scroll, new LayoutParams(LayoutParams.WRAP_CONTENT,200));
splRerDialog.show();
}

Links from Spanned text don't work in TexView

I have a TextView embedded in a RelativeLayout, embedded in a ScrollView, and I load a Spanned text object in it created from some HTML code.
I have set the android:autoLink attribute of the TextView to true, and I have tested in another part of the application that I can click on links in the text and they will take me to the target website.
The only difference is that this time the TextView is a child of a ScrollView. Does that screw up the android:autoLink process or am I really missing something here?
XML Code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:padding="10dp" android:background="#FFF">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/RLNewsItem">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:id="#+id/TVText" android:textColor="#000"
android:layout_below="#+id/TVTitle" android:linksClickable="true"
android:autoLink="web|email|map"></TextView>
</RelativeLayout>
</ScrollView>
Java code:
Spanned newsText = Html.fromHtml(htmlsource, this, null);
text.setText(newsText);
Try this
Code in String.xml :
<string-array name="description">
<item> <Data> <![CDATA[ Check this <u>Redirect to Next Activity</u> ]]></Data> </item>
Code in Java class:
ArrayList<String> title_list = new ArrayList<String>();
String[] description_Array = getResources().getStringArray(R.array.description);
String categoryAndDesc = null;
for(String cad : description_Array) {
categoryAndDesc = cad;
title_list.add(categoryAndDesc);
}
CharSequence sequence = Html.fromHtml(categoryAndDesc);
seperator_view.setText(strBuilder);
seperator_view.setMovementMethod(LinkMovementMethod.getInstance());

Accessing child views inside a ListItem defined as LinearLayout

I'm trying to retrieve the getText() value of a TextView defined in my ListItem Layout, inside the setOnItemClickListener method.
Basically, I want to log data (Id, LookupKey, DisplayName, or PhoneNumber) allowing me to retrieve the contact selected regardless of the listview/adapter used.
I display a simple TextView and an Imageview which visibility is set according to the data.
here is the layout : contact_entry.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:baselineAligned="false"
android:layout_width="fill_parent" android:padding="5px">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="16px"
android:text="#+id/contactEntryText" android:id="#+id/contactEntryText">
</TextView>
<TableRow android:paddingLeft="5px" android:visibility="visible"
android:paddingRight="5px" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/indicator_logo_table"
android:gravity="right">
<ImageView android:layout_width="wrap_content"
android:id="#+id/imageView1"
android:layout_height="fill_parent"
android:src="#drawable/indicator">
</ImageView>
</TableRow>
</LinearLayout>
the adapter i use is defined below :
SimpleAdapterWithImageView adapter = new SimpleAdapterWithImageView(
this, R.layout.contact_entry, cursor, fields,
new int[] { R.id.contactEntryText });
mContactList.setAdapter(adapter);
( You could have guessed it's for displaying the contact's Display_Name from the ContactsContract ContentProvider. )
and here is finally the mContactList.setOnItemClickListener ...
mContactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Log.i("done","linear layout"); // it's not done at all ! findViewById returns null;
//this part is currently useless
TextView displayed_name = (TextView) linear.findViewById(R.id.contactEntryText);
Log.i("done","Displayed name = linear.findViewById contactentrytext");
Toast.makeText(getApplicationContext(),
// displayed_name.getText(), //
//view.getContentDescription(), //
Toast.LENGTH_SHORT).show();
}
As you can see i'm only trying to display a toast for the moment, in the end the name will be put as an extra for an intent.
So : the best result i got these last 36 hours (sigh) is displaying the name of the first contact displayed in the toast, even when clicking on the second item...
TextView displayed_name = (TextView) findViewById(R.id.contactEntryText);
Toast.makeText(getApplicationContext(),displayed_name.getText(),
Toast.LENGTH_SHORT).show();
}
I've been trying stuff like the tutorial google provided here, which (am I wrong? I'm new to Android so don't hesitate to slap me in the face ...) actually casts the whole View in a TextView ...
plus other tricks like view.getChildAt() and so on... for hours.
I'm convinced it's not that hard, yet i'm running in circles !
I don't think it is because of my adapter, but if anyone thinks it could be the cause, i could provide the code in another post.
Thanks in advance !
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Here is a mistake - you shan't use R.layout in findViewById. Usually, you need to use R.id there, or replace findViewById by inflating a layout. But in this case you just need the view from arguments.
Replace the quted code with this one:
LinearLayout linear = (LinearLayout) view;

Categories

Resources