additonal line space in textview - android

I have a viewpager and inside of it a textview.
For filling pages of viewpager I should measure textview line height with a textpaint.
When viewpager adapter is set, my problem is that in 2 difference page line number is equal but page textview height is different and I want textview fit to page,It is happen in android 4.4.2 and Below, how can I solve this problem.
Notice that I can't use maxLine ellipsize or other solution like them because textview lines number is uncertain.
activity_main.xml
<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:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pages"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="1dp"></LinearLayout>
<SeekBar
android:id="#+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0"/>
<SeekBar
android:id="#+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0"/>
<SeekBar
android:id="#+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0"/>
</LinearLayout>
page.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="left" >
</TextView>
PageFragment :
public class PageFragment extends Fragment {
private final String PAGE_TEXT = "PAGE_TEXT";
private final String TEXT_SIZE = "TEXT_SIZE";
private final String LINE_SPACE = "LINE_SPACE";
public PageFragment newInstance(CharSequence pageText, float textSize, float lineSpace) {
PageFragment frag = new PageFragment();
Bundle args = new Bundle();
args.putCharSequence(PAGE_TEXT, pageText);
args.putFloat(TEXT_SIZE, textSize);
args.putFloat(LINE_SPACE, lineSpace);
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CharSequence text = getArguments().getCharSequence(PAGE_TEXT);
float textSize = getArguments().getFloat(TEXT_SIZE);
float lineSpace = getArguments().getFloat(LINE_SPACE);
TextView pageView = (TextView) inflater.inflate(R.layout.page, container, false);
pageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, UnitConverter.spToPixels(getContext(), textSize));
pageView.setText(text);
Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "Roboto_Medium.ttf");
pageView.setTypeface(plain);
pageView.setLineSpacing(UnitConverter.spToPixels(getActivity(),lineSpace), 1f);
pageView.setMovementMethod(LinkMovementMethod.getInstance());
return pageView;
}
}

Definitely, Its device specific issue.
For better output in all devices you should use ScrollView as a container of your TextView.
In addition, If you want to add some extra space between text lines then you can use textView.setLineSpacing() programmatically or from XML you can use attribute android:lineSpacingMultiplier="1.3" to TextView for extra spacing.
Hope this will help~

you have change in your base layout LinearLayout to RelativeLayout try this:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:layout_above="#+id/obttom_layout"
android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="#+id/obttom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"></LinearLayout>
<SeekBar
android:id="#+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0" />
<SeekBar
android:id="#+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0" />
<SeekBar
android:id="#+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0" />
</LinearLayout>
</RelativeLayout>
Page Xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pages_txt"
android:ellipsize="end"
android:gravity="left" >
</TextView>
</LinearLayout>
</ScrollView>

Related

Web Fragment not Full Fill

How can I load a web view full Fill into a fragment?
FragmentActivity.java
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
View rootView = inflater.inflate(R.layout.web, group, false);
wb = ((WebView)rootView.findViewById(R.id.webViewApp));
wb.getSettings().setJavaScriptEnabled(true);
pg = ((ProgressBar)rootView.findViewById(R.id.progressBar2));
wb.setWebViewClient(new WebViewClient());
wb.getSettings().setDomStorageEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setSupportMultipleWindows(true);
wb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wb.setHorizontalScrollBarEnabled(false);
wb.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
wb.getSettings().setAllowFileAccessFromFileURLs(true);
wb.getSettings().setAllowUniversalAccessFromFileURLs(true);
wb.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView paramAnonymousWebView, int paramAnonymousInt)
{
web.this.pg.setVisibility(0);
web.this.pg.setProgress(paramAnonymousInt);
if (paramAnonymousInt == 100) {
web.this.pg.setVisibility(8);
}
}
});
this.pg.setVisibility(0);
this.wb.loadUrl("http://stackoverflow.com/");
return rootView;
}
Web.xml
<LinearLayout
android:orientation="vertical"
android:background="#ffffe0b2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
android:id="#id/progressBar2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
<WebView
android:id="#id/webViewApp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="#id/progressBar2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
Screenshot
Thanks :))
I think, you have to simplify your layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#id/webViewApp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/progressBar2"/>
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal" />
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
And make sure, that in your Activities' layout Fragment's Container is below Toolbar, something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="#dimen/action_bar_height"/>
<FrameLayout
android:layout_below="#+id/toolbar"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

Two ViewPagers inside ScrollView - second not visible

I'm trying to create layout with two ViewPager inside ScrollView. First one has set height to desired size and second should match to rest of layout. Problem is that second ViewPager is not visible. What am I doing wrong? This is my layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="200dp" />
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
</ScrollView>
</FrameLayout>
I'm using second ViewPager to show Fragments (using FragmentPagerAdapter), this is my FragmentPagerAdapter code:
public class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
default:
return new FirstFragment();
}
}
#Override
public int getCount() {
return 2;
}
}
Code of FirstFragment:
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_step, container, false);
}
}
first_step.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#android:color/holo_blue_light" />
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#android:color/holo_green_light" />
</LinearLayout>
if you are using layout weight. you have to set width or height layout 0dp accordingly to your use
this gives equal space to both pagers
give weight in ratio i.e 2:1 or 2:1 etc
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
you have to define
android:layout_height="200dp"
remove
android:layout_weight="1"
after both viewpager is visible not to set android:layout_height="wrap_content"
Change
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
to
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" />
and also
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
to
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Set fillViewport=true in ScollView.
I think ScollView is useless, if the second ViewPager have not fixed height over screen.
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
case ViewPager is a puzzle view,you must set the height for it,then it can
appear in you UI Screen,gallery dont have this problem;
why use ScrollView in this layout, i dont understand.
but,if you use linearlayout
so you can do like this,background is just for test,and you should get destiny in your project to match screen, not just 200 *2:
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/content1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#android:color/darker_gray"
android:orientation="vertical" />
<android.support.v4.view.ViewPager
android:id="#+id/content2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ff0000"
android:orientation="vertical" />
</LinearLayout>
activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpage01);
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
ViewPager content = (ViewPager) findViewById(R.id.content2);
content.getLayoutParams().height = screenHeight - 200 * 2;
content.requestLayout();
}

How can I display the layout containing the buttons down on the screen

With the layout below the buttons are being shown on top of the page. How can I make them appear below, after the container, down on the page?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/map"
android:onClick="ClickClients"
android:text= "#string/Clients"
>
</Button>
</LinearLayout>
</LinearLayout>
The MainActivity looks as follows and the HomePage.xml is also below
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetButtonClicked(5);
setContentView(R.layout.activity_main);
}
public void GetButtonClicked(int position)
{
// update the main content by replacing fragments
Fragment fragment = null;
switch (position){
case 0:
fragment= new FirstPageFragment();
break;
default:
fragment = new FirstPageFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void ClickHomePage(View view){
GetButtonClicked(0);
}
public void ClickClients(View view){
GetButtonClicked();
}
}
<FrameLayout 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" tools:context="com.test.FirstPageFragment" android:background="#drawable/theme">
</FrameLayout>
This is the FirstPage
public class FirstPageFragment extends Fragment {
public FirstPageFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_page, container, false);
}
}
Use ralative layout insted of linear.
Make a different layout for bottom buttons, and then at the last of your container layout use this:
<include android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/bottom_buttons"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="#string/Home" />
<Button
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="#string/Clients" >
</Button>
</LinearLayout>
Try this
Use RelativeLayout with android:layout_alignParentBottom="true" step by step guide is available here.
Your skeleton for layout should look like this
<RelativeLayout
<LinearLayout android:layout_alignParentBottom="true">
<Button
<Button
</LinearLayout>
</RelativeLayout>
Final Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="Home" />
<Button
android:layout_marginLeft="30dp"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="Clients" >
</Button>
</LinearLayout>
The issue was that I did not include the container in another layout...
<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"
tools:context="com.test.MainPageActivityActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/container">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentTop="#+id/container">
and insert buttons layout here....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>

Can not open fragment in FrameLayout

Helo every one, i try to open fragment in FrameLayout, but the screen is white. My code is:
public class ProfileActivity extends FragmentActivity {
Database db;
TextView tvName, tvCity;
ImageView imageView;
private FragmentTabHost mTabHost;
FrameLayout frameLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
initialize();
}
private void initialize() {
tvName = (TextView) findViewById(R.id.tv_name);
tvCity = (TextView) findViewById(R.id.tv_city);
db = Database.getInstance(this);
imageView = (ImageView) findViewById(R.id.iv_profile);
Fragment fragment = new StatisticFragment();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
activity_profile.xml
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="1">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/iv_profile" android:src="#drawable/background_profile_first_part"
android:scaleType="fitXY"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="1.4"
android:background="#drawable/background_profile_second_part">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tv_name" android:layout_gravity="right|center_vertical"
android:textColor="#ffffff"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tv_city" android:layout_gravity="center"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:visibility="invisible"
android:layout_width="0dp"
android:layout_height="0dp"
/>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
StaticFragment.class
public class StatisticFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_statistic, null);
return v;
}
}
fragment_statistic.xml
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Statistic"
android:id="#+id/textView" android:layout_gravity="left|center_vertical" android:textColor="#fff555"/>
</LinearLayout>
Apply a background to the linear layout in the statistic fragment xml
I think you have set visibility to invisible inside frame layout so please edit your layout file
from
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:visibility="invisible"
android:layout_width="0dp"
android:layout_height="0dp"
/>
</android.support.v4.app.FragmentTabHost>
remove this line and try again.
android:visibility="invisible"

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