I am trying to make a reusable header. Here is my XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg" >
<ImageButton
android:id="#+id/imagebuttonforheader"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_alignParentLeft="true"
android:background="#drawable/back_button"
/>
<ImageButton
android:id="#+id/imagebuttonforheader"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_alignParentRight="true"
android:background="#drawable/forward_button"
/>
</RelativeLayout>
I made a class for it:
public class Header extends RelativeLayout {
Context context;
Activity activity;
public Header(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.header, null, false);
}
}
And then added this layout into my main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<my.testy.view.Header
android:id="#+id/headerOnMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</my.testy.view.Header>
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/headerOnMain"
android:text="#string/hello" />
</RelativeLayout>
But it's not showing up on the application.
What am I doing wrong here?
You have to attached your inflated layout to your custom class:
li.inflate(R.layout.header, this, true);
Related
I am trying to create a FrameLayout that contains other FrameLayouts. I have created an EditorView class that extends FrameLayout, and I want it to display FileView, another class that extends FrameLayout. But in Android Studio preview I get an blank view. What am I doing wrong.
This is the ediorview_activity.xml:
<?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">
<com.itsvaske.webio.Views.LineView
android:id="#+id/html1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html1" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html2" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html3" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html4" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html5" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html6" />
</RelativeLayout>
This is lineview_layout.xml:
<?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">
<TextView
android:id="#+id/lineNumber"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_weight="0"
android:gravity="center"
android:text="1"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="#+id/line"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_weight="3"
android:background="#null"
android:ems="10"
android:hint="#string/typeyourline"
android:inputType="textPersonName"
android:minHeight="32dp"
android:textColorHint="#546E7A" />
</LinearLayout>
This is EditorView class:
public class EditorView extends FrameLayout {
private Context mContext;
private AttributeSet mAttributeSet;
public EditorView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
this.mAttributeSet = attrs;
initView();
}
private void initView() {
View view = View.inflate(mContext, R.layout.editor_start_html_layout, this);
LineView line1 = view.findViewById(R.id.html1);
LineView line2 = view.findViewById(R.id.html2);
LineView line3 = view.findViewById(R.id.html3);
LineView line4 = view.findViewById(R.id.html4);
LineView line5 = view.findViewById(R.id.html5);
LineView line6 = view.findViewById(R.id.html6);
LineView line7 = view.findViewById(R.id.html7);
line1.setLineNumber(1);
line1.setLineContents("<html>");
line2.setLineNumber(2);
line2.setLineContents("<head>");
line3.setLineNumber(3);
line3.setLineContents(" <title></title>");
line4.setLineNumber(4);
line4.setLineContents("</head>");
line5.setLineNumber(5);
line5.setLineContents("<body>");
line6.setLineNumber(6);
line6.setLineContents("</body>");
line7.setLineNumber(7);
line7.setLineContents("</html>");
}
}
And In preview I get this:
so , i have a Listview activity , that is populated by values from a SQLITE database.
how can i put a button fixed at top or bottom ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/coligada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_alignBottom="#+id/editar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"/>
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantidadesxsx"
android:onClick="editar"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/coligada"
android:layout_toEndOf="#+id/coligada" />
</RelativeLayout>
Here is my ProductAdapter that have custom layout where have the items and a apdater that populate my listview with values from Sqlite database.
package br.exemplosqlite;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
public class ProdutosAdapter extends BaseAdapter {
private Context context;
private List<Produtos> list;
public ProdutosAdapter(Context context, List<Produtos> list){
this.context = context;
this.list = list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0).getId();
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
final int auxPosition = position;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.produtos, null);
TextView tv = (TextView) layout.findViewById(R.id.coligada);
tv.setText(list.get(position).getItem());
Button editarBt = (Button) layout.findViewById(R.id.editar);
editarBt.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NewUserActivity.class);
intent.putExtra("item", list.get(auxPosition).getItem());
intent.putExtra("quantidaderm",list.get(auxPosition).getQuantidaderm());
intent.putExtra("id", list.get(auxPosition).getId());
context.startActivity(intent);
}
});
// *********** BOTÃO DE EXCLUIR ***************** //
//Button deletarBt = (Button) layout.findViewById(R.id.deletar);
//deletarBt.setOnClickListener(new Button.OnClickListener(){
// #Override
// public void onClick(View arg0) {
// BD bd = new BD(context);
// bd.deletar(list.get(auxPosition));
//
// layout.setVisibility(View.GONE);
// }
//}
///);
return layout;
}
}
Try the following xml at the bottom of ListView and centerHorizontal
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Your Button"
/>
</RelativeLayout>
Hope this helps.
Do it like this:
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your text here"/>
</LinearLayout>
Try this:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:orientation="horizontal" >
<TextView
android:id="#+id/coligada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:text="Large Text" />
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/coligada"
android:layout_toRightOf="#+id/coligada"
android:onClick="editar"
android:text="Quantidadesxsx" />
</LinearLayout>
</LinearLayout>
Try this.
<Button
android:id="#+id/btn"
layout_width="wrap_content"
layout_height="wrap_content"
android:alignParentTop="true"
/>
<ListView
layout_width="wrap_content"
layout_height="wrap_content"
android:layout_below="#+id/btn"
/>
Current layout is for listview items.
Remove Button from this layout file and paste it in layout file where you have defined ListView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<ListView
.....
</ListView>
<Button>
</Button>
</RelativeLayout>
in one layout and
<TextView>
</TextView>
in another layout
In button add this line of code
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantidadesxsx"
android:onClick="editar"
android:layout_below="#+id/coligada"/>
You can use android:layout_weight="1" with LinearLayout.
Example.
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantidadesxsx"
android:onClick="editar"/>
</LinearLayout>
I want to place the two text horizontally at the center. It is very easy to put it directly in a layout.
But what i am trying to achieve is :
Testapplayout.xml: This is set as content view of the activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<com.example.testapp.Customlayout
android:id="#+id/custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp">
</com.example.testapp.Customlayout>
</LinearLayout>
class Customlayout
public class Customlayout extends LinearLayout {
public Customlayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Customlayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Customlayout(Context context) {
this(context, null, 0);
}
}
Now on the Testapplayout, customlayout, I am trying to inflate the layout to show the texts at the center:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|bottom"
android:orientation="horizontal" >
<TextView
android:id="#+id/status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/status_text"
/>
</LinearLayout>
This still comes at the
try
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.testapp.Customlayout
android:id="#+id/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
</com.example.testapp.Customlayout>
</RelativeLayout>
I am creating tabs inside the fragment class. Right now i am showing two tabs in the fragment class. Everything works fine and tabs are shown properly. Only cache is that, the tabs shown only take half of the screen width, It doesn't take the full screen width.
So anyone tell me what i need to change in my code to achieve that
My Code
tabsinfo_fragment.xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFEF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#+id/tab_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/tab_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
tab.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:background="#drawable/tab_selector">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#drawable/tab_text_selector"
android:textIsSelectable="false" />
</LinearLayout>
code inside fragment class
public class TabsInfoFragment extends Fragment implements OnTabChangeListener
{
private View m_Root;
private TabHost m_TabHost;
private int m_CurrentTab;
public String m_VisitorTabText;
public String m_FeedTabText;
public TabsInfoFragment()
{
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
m_VisitorTabText = Integer.toString(R.string.tab_visitor_text);
m_FeedTabText = Integer.toString(R.string.tab_feed_text);
m_Root = inflater.inflate(R.layout.tabsinfo_fragment, null);
m_TabHost = (TabHost) m_Root.findViewById(android.R.id.tabhost);
setupTabs();
return m_Root;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
m_TabHost.setOnTabChangedListener(this);
m_TabHost.setCurrentTab(m_CurrentTab);
// Manually start loading stuff in the first tab
updateTab(m_VisitorTabText, R.id.tab_1, new ProfileInfoFragment());
}
private void setupTabs()
{
m_TabHost.setup();
m_TabHost.addTab(newTab(m_VisitorTabText, R.string.tab_visitor_text, R.id.tab_1));
m_TabHost.addTab(newTab(m_FeedTabText, R.string.tab_feed_text, R.id.tab_2));
}
private TabSpec newTab(String tag, int labelId, int tabContentId)
{
View indicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab, (ViewGroup) m_Root.findViewById(android.R.id.tabs), false);
((TextView) indicator.findViewById(R.id.text)).setText(labelId);
TabSpec tabSpec = m_TabHost.newTabSpec(tag);
tabSpec.setIndicator(indicator);
tabSpec.setContent(tabContentId);
return tabSpec;
}
#Override
public void onTabChanged(String tabId)
{
if (m_VisitorTabText.equals(tabId))
{
updateTab(tabId, R.id.tab_1, new ProfileInfoFragment());
m_CurrentTab = 0;
return;
}
if (m_FeedTabText.equals(tabId))
{
updateTab(tabId, R.id.tab_2, new ProfileInfoFragment());
m_CurrentTab = 1;
return;
}
}
private void updateTab(String tabId, int placeholder, Fragment fragmentClass)
{
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null)
{
fm.beginTransaction().replace(placeholder, fragmentClass, tabId).commit();
}
}
}
ScreenShot
The TabWidget class is a subclass of LinearLayout, so in the tab.xml file for the root xml element, in your case a LinearLayout, set the width to 0dp and the weight to 1, then all the tabs will be equal width. Like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:gravity="center">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="This is a tab" />
</LinearLayout>
Try changing your TabWidget to this instead;
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
try this
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+android:id/tab_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:id="#+android:id/tab_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
Try removing HorizontalScrollView and adding android:layout_width="fill_parent" to TabWidget
Need to add one single line
tabs.setDistributeEvenly(true);
I want to do a custom view coming from an XML.
Here's my XML :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/movieTitle"
android:text="#string/movietitle"
android:textSize="35dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<com.galite.headliner.views.LoaderImageView
android:id="#+id/moviePoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/movieTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:contentDescription="#string/poster"
/>
<TextView
android:id="#+id/movieDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/description"
android:layout_below="#id/moviePoster"
android:gravity="center"
/>
</RelativeLayout>
And i want to inflate to a custom view because i need to use methods like onClick and everything.
Here's my constructor :
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
initializeView();
}
public void initializeView(){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.movie, null);
}
How can i make MyView equal to v ?
You'll have to attach the inflated view v to MyView like this:
v = inflater.inflate(R.layout.movie, this, true);
If you want that exact layout then make MyView to extend RelativeLayout and modify the xml layout like this:
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/movieTitle"
android:text="#string/movietitle"
android:textSize="35dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<com.galite.headliner.views.LoaderImageView
android:id="#+id/moviePoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/movieTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:contentDescription="#string/poster"
/>
<TextView
android:id="#+id/movieDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/description"
android:layout_below="#id/moviePoster"
android:gravity="center"
/>
</merge>