Adding Views dynamically in Linear Layout in Fragment - android

I am trying to add TextView to the nested linearLayout in fragmentin onCreateView, I don't know if this is the correct approach or not, new to android!
Thanks in advance
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
List<LifeBerrysXmlParser.Item> items = (ArrayList<LifeBerrysXmlParser.Item>) getArguments().getSerializable("List");
TextView mainHeading = (TextView) rootView.findViewById(R.id.mainHeading);
ImageView mainImage = (ImageView) rootView.findViewById(R.id.articleMainImage);
LinearLayout articleLayout = (LinearLayout) rootView.findViewById(R.id.articleLayout);
TextView tev = new TextView(getActivity());
tev.setText("Hello..............");
articleLayout.addView(tev);
LifeBerrysXmlParser.Item item = items.get(getArguments().getInt("position"));
String articleMainHeading = item.mainHeading;
String articlemainImage = item.mainImage;
mainHeading.setText(articleMainHeading);
if (!articlemainImage.isEmpty()) {
Picasso.with(getContext()).load(articlemainImage).into(mainImage);
}
return rootView;
}
can anyone please help me what I am doing wrong?
here is my xml for the fragment
<?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:fillViewport="true"
android:id="#+id/content"
android:padding="1dp"
android:layout_weight="1"
android:background="#000000"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:scrollIndicators="right">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/articleMainImage"
android:maxHeight="300dp"/>
<TextView
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightLarge"
android:layout_weight="1"
android:lineSpacingMultiplier="1.2"
android:id="#+id/mainHeading"
android:textColor="#ff6435"
android:clickable="false"
android:layout_gravity="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/articleLayout"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read More"
android:textSize="15dp"
android:textStyle="bold"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:gravity="center"
android:onClick="readMore"
android:padding="1dp"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

You have mistake in your article layout. Your article layout has horizontal orientation, and it's child TextView has match_parent width, so when you add new child, it'll be outside the screen. Change orientation to vertical:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/articleLayout"
android:orientation="vertical"> <---- Change this line
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read More"
android:textSize="15dp"
android:textStyle="bold"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:gravity="center"
android:onClick="readMore"
android:padding="1dp"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>

Related

How to show and hide a view on header without affecting the below ViewPager?

In my requirement, I have an action bar at the top. Below action bar, I have another header. On that header, I have a button. Below that header, the remaining content is view pager. Now when I click the button on the header, some list of textviews should display overlapping the below viewpager. And again when I click on the button those text view should hide. Now my problem is view pager is getting affected when showing and displaying the top header text views.
Below is my image:
Code:
PagerLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorAccent"/>
<LinearLayout
android:id="#+id/mainLinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lin_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:text="Total"
android:textStyle="bold"
android:textColor="#color/white"
android:textSize="18sp" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabBackground="#drawable/tab_selector"
app:tabIndicatorHeight="0dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black"
>
<Button
android:id="#+id/btn1"
android:layout_width="36dp"
android:layout_height="10dp"
android:layout_marginBottom="4dp"
android:background="#color/calculate_grey"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10px"
android:textColor="#color/white"
android:visibility="gone"
android:text="Text 1" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10px"
android:textColor="#color/white"
android:visibility="gone"
android:text="Text 2" />
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10px"
android:textColor="#color/white"
android:visibility="gone"
android:text="Text 3" />
<Button
android:id="#+id/btn2"
android:layout_width="36dp"
android:layout_height="10dp"
android:layout_marginBottom="4dp"
android:background="#color/calculate_grey"
android:layout_gravity="center_horizontal"
android:visibility="gone"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Fragmemt:
public class ViewPagerFragment extends Fragment {
TextView tv1,tv2,tv3;
Button button1, button2;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
View v = inflater.inflate(R.layout.pagerlayout, container, false);
ViewPager pager = (ViewPager)v.findViewById(R.id.viewPager);
pager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
TabLayout tabLayout = (TabLayout) v.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(pager, true);
tv1 = (TextView)v.findViewById(R.id.tv1);
tv2 = (TextView)v.findViewById(R.id.tv2);
tv3 = (TextView)v.findViewById(R.id.tv3);
button1 = (Button) v.findViewById(R.id.btn1);
button2 = (Button) v.findViewById(R.id.btn2);
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// do something
button1.setVisibility(v.GONE);
tv1.setVisibility(v.VISIBLE);
tv2.setVisibility(v.VISIBLE);
tv3.setVisibility(v.VISIBLE);
button2.setVisibility(v.VISIBLE);
}
});
button2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// do something
button1.setVisibility(v.VISIBLE);
tv1.setVisibility(v.GONE);
tv2.setVisibility(v.GONE);
tv3.setVisibility(v.GONE);
button2.setVisibility(v.GONE);
}
});
return v;
}
}
You can use android:visibility = "invisible" instead of gone. And same in the java also instead of setting it gone set it as invisible.

Create a layout in XML and use it several times

I´d like to design a layout in XML and want to duplicate it dynamicly, depence on the size of a list. It has to be done in a fragment. Unfortunally I cannot find how to do it in fragments.
This ist the idea of the XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<FrameLayout
android:id="#+id/complete_map_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ICH bin die MAP!! Ich möchte ein Bild werden"
android:id="#+id/textView7"
android:layout_gravity="center_horizontal" />
<!-- Erstellung einzelner Räume -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="20dp"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:id="#+id/roomRow">
<!-- Bezeichnung Raum -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:orientation="vertical"
android:id="#+id/roomNameOne"
android:background="#ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/roomName"
android:layout_gravity="center_vertical"
android:rotation="-90"
android:text="PLATZHALTER"/>
</LinearLayout>
<!-- Inhalt Raum -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:orientation="horizontal"
android:id="#+id/WeaponNameOne">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/testWeapon"
android:layout_gravity="center_vertical"
android:text="WAFFE"
android:background="#00ff00"/>
</LinearLayout>
<!-- Inhalt Raum2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:orientation="horizontal"
android:id="#+id/weaponNameTwo"
android:background="#0000ff">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/testWeapon2"
android:layout_gravity="center_vertical"
android:text="WAFFE"/>
</LinearLayout>
<!-- Bezeichnung Raum2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.10"
android:rotation="90"
android:id="#+id/roomNameTwo"
android:background="#ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/roomName2"
android:layout_gravity="center_vertical"
android:text="PLATZHALTER"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView8"
android:layout_gravity="right|bottom" />
</FrameLayout>
</ScrollView>
This is the java-code:
public class MapOverview extends Fragment {
Bundle extras;
View fragLayoutV;
Game game;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fragLayoutV = inflater.inflate(R.layout.fragment_map, null);
extras = getActivity().getIntent().getExtras();
game = (Game) extras.get("GAME");
return fragLayoutV;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FrameLayout frame = (FrameLayout)fragLayoutV.findViewById(R.id.complete_map_layout);
//LinearLayout roomInflator = (LinearLayout)frame.findViewById(R.id.roomRow);
Log.e("RAUM", game.getRooms().get(0).getName());
for(Room r:game.getRooms()){
frame.addView((LinearLayout)frame.findViewById(R.id.roomRow));
}
}
}
Of course I get an error by doing this. Can anybody help?
The aim is to have a picture like this:
http://i.stack.imgur.com/3DS73.png
Don´t matter about the colors ^^
You need to re-inflate your layout every time you want to add it. You can not add the same View multiple times.
Try something like this:
LayoutInflater inflater = LayoutInflater.from(this.getContext());
for (Room r : game.getRooms()) {
View view = inflater.inflate(R.layout.your_duplicate_layout, frame, false);
// Do whatever you want with view
frame.addView(view);
}
Where R.layout.your_duplicate_layout correspond to your current layout with id=roomRow.

How to add image view and text view to a linear layout

my xml is like follow I want to add image view and text view programmatically to given xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="25dip" >
<TextView
android:id="#+text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="#string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
but I want output like following programmatically ie a linear layout contain image view and old text view and add that linearlayout below to new textview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="25dip" >
<TextView
android:id="#+name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="#string/app_name"
android:textSize="24.5sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="12dip"
android:src="#drawable/app_icon_big" />
<TextView
android:id="#+text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="#string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/outerLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="#string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
MainActivity.java
public class MyActivity extends Activity {
private LinearLayout outerLinearLayout;
private TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outerLinearLayout = (LinearLayout) findViewById(R.id.outerLinearLayout);
text1 = (TextView) findViewById(R.id.text1);
LinearLayout innerLinearLayout = new LinearLayout(this);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.bottomMargin=12;
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(R.drawable.ic_launcher);
innerLinearLayout.addView(imageView);
TextView textView = new TextView(this);
textView.setTextSize((float) 24.5);
textView.setText("New Text View");
textView.setLayoutParams(params);
outerLinearLayout.removeAllViews();
innerLinearLayout.addView(text1);
outerLinearLayout.addView(textView);
outerLinearLayout.addView(innerLinearLayout);
}
}
change inner layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip" >
By
<LinearLayout
android:id="#+id/internal_image_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip"
android:visibility="gone" >
Then whenEver you want to show just
findViewById(R.id.internal_image_textView).setVisibility(View.VISIBLE)

Add fragment as Subview in android

I have a fragment which has a horizontal scrollview. I want to add another fragment to this scrollview. How can I do that?
Here is my first fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="220dp"
android:background="#drawable/arrow_bg">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/horizontalScrollView"
android:layout_marginTop="15dp" >
<LinearLayout
android:id="#+id/offerLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</HorizontalScrollView>
</LinearLayout>
I am trying to add the below fragment to the scrollview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="174dp"
android:layout_height="214dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="HEALTH"
android:id="#+id/offerTitle"
android:capitalize="characters" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/offer_image_text_bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="135dp"
android:id="#+id/offerIcon"
android:src="#drawable/health_img"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/favLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:numStars="5"
android:rating="4"
android:isIndicator="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
style="#style/ratingBarStyle"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:id="#+id/favIcon"
android:src="#drawable/like_icon"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/favLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/txt_OfferTitle2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/txt_OfferDesc" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
This is the code I am using for the same
private void loadSelectedOffers() {
for(int i=0;i<3;i++)
{
OfferItemFragment offerItem = new OfferItemFragment();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
offerItem.getView().setLayoutParams(params);
offerLayout.addView(offerItem.getView());
}
}
But I am getting a null pointer exception at line
offerItem.getView()
How can I do this properly?
Add a FrameLayout to your first fragment and then add the fragment to that framelayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="220dp"
android:background="#468494">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/horizontalScrollView"
android:background="#BB099D"
android:layout_marginTop="15dp" >
<LinearLayout
android:id="#+id/child"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Then in your code add the fragment to the view using FragmentManager and FragmentTransaction
public class MainActivity extends Activity {
Fragment f;
EditText send;
LinearLayout parent;
LinearLayout child;
HorizontalScrollView scroll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup vg = (LinearLayout) inflater.inflate(R.layout.activity_main, null );
child = (LinearLayout) vg.findViewById(R.id.child);
loadSelectedOffers();
setContentView(vg);
}
private void loadSelectedOffers() {
for(int i=0;i<3;i++)
{
FrameLayout frame = new FrameLayout(this);
child.addView(frame);
frame.setId(i);
frame.setBackgroundColor(getResources().getColor(R.color.black));
frame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
SenderFragment offerItem = new SenderFragment();
transaction.add(i, offerItem, "offer_item_" );
transaction.commit();
}
}
I believe something like this should work now.
You are getting view null because its onCreateView() has not been executed yet, that in turn is because you have merely instantiated the fragment and not attached it to your activity.
Try adding FrameLayouts in your HSV and replacing them by the fragments.

Android: Custom view from xml layout

i would like to create a custom View (subclass of the View class) and use a layout xml resource.
I want something like this:
public class CustomView extends LinearLayout {
public CustomView(Context context) {
super(context);
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.tweet, this, true);
}
This actually creates a View with the right height and width (a ScrollView with a list of those has exactly the expected length and scrollbars) but they are empty (black) even though the xml layout contains a lot of TextViews.
This is my xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tweet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#ffffff">
<TextView
android:text="User"
android:id="#+id/tvusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold">
</TextView>
<View
android:layout_width="5dip"
android:layout_height="1dip">
</View>
<TextView
android:text="userscreenname"
android:id="#+id/tvuserscreenname"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<TextView
android:text="0s"
android:id="#+id/tvtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
<TextView
android:text="Tweet Content"
android:id="#+id/tvcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000">
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#ffffff">
</View>
<TextView
android:text="Retweet by"
android:id="#+id/tvrtby"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
This is my main layout:
<?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:background="#ffffff"
>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/scrollView1"
android:orientation="vertical"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/timeline"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="#+id/tvoutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
This is how I add the custom view to my main view:
Not working (what I try to achieve):
TweetView ctweet = new TweetView(getApplicationContext(),tweet);
timeline.addView(ctweet);
My current solution (works, but uses no custom view):
View vtweet = View.inflate(getApplicationContext(), R.layout.tweet,null);
TextView tvusername = (TextView) vtweet.findViewById(R.id.tvusername);
TextView tvuserscreenname = (TextView) vtweet.findViewById(R.id.tvuserscreenname);
TextView tvcontent = (TextView) vtweet.findViewById(R.id.tvcontent);
TextView tvtime = (TextView) vtweet.findViewById(R.id.tvtime);
tvusername.setText(tweet.getUser().getName());
tvuserscreenname.setText('#' + tweet.getUser().getScreenName());
tvcontent.setText(tweet.getText());
//tvtime.setText(ctweet.tvtime.getText());
timeline.addView(vtweet);
you can use this in your xml just like any other view like <TextView
try <packageName.ClassName ie something like <com.example.CustomView
Probably you have to set the Layout Params of your TweetView object.
LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
ctweet.setLayoutParams(fieldparams);

Categories

Resources