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>
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:
Im trying to get my menu accessible across my app with out having to add the XML code to all my activities as well as the code in all the classes.
I already have a "BaseController" class that handle some aspects of the menu like the TabHost and text etc but I am unable to get the menu extrapolated so it can be used across the app without having to retype all my code over and over again?
HomeActivity.class
public class HomeActivity extends BaseController {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
Log.i(TAG, "onCreate has been called");
menu();
mDrawerLayout =(CustomDrawerLayout)findViewById(R.id.home_activity);
left = (RelativeLayout) findViewById(R.id.left_menu);
menu_button = (ImageView) findViewById(R.id.menuImageButton)
}
private void openMenu() {
if (hasLoggedInBefore() == false) {
goToLogin();
} else {
mDrawerLayout.openDrawer(left);
}
}
activity_home.xml
<com.myapp.app.ui.CustomDrawerLayout
android:id="#+id/home_activity"
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:background="#drawable/bg"
tools:context=".MainActivity">
<include
layout="#layout/home_screen_items"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="#+id/left_menu"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/bob_orange6">
<TabHost
android:id="#+id/menuTabs"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/menu_tab1_tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1 Tab Text"/>
</LinearLayout>
<LinearLayout
android:id="#+id/menu_tab2_tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2 Tab Text"/>
</LinearLayout>
<LinearLayout
android:id="#+id/menu_tab3_tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab3 Tab Text"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
as you can see above my menu is in the HomeActivity XML and if i try add it using the tag, the menu overlays the content and does not collapse.
The only way it works is if i do it the way i have above.
my BaseController.java
public class BaseController extends Activity {
CustomDrawerLayout mDrawerLayout;
RelativeLayout left;
public void menu(){
final TabHost menuTab = (TabHost)findViewById(R.id.menuTabs);
menuTab.setup();
TabHost.TabSpec tab1_tab_menu = menuTab.newTabSpec("Tab1");
tab1_tab_menu.setContent(R.id.menu_tab1_tab);
tab1_tab_menu.setIndicator("Tab1");
menuTab.addTab(tab1_tab_menu);
TabHost.TabSpec tab2_tab_menu = menuTab.newTabSpec("Tab2");
tab2_tab_menu.setContent(R.id.menu_tab2_tab);
tab2_tab_menu.setIndicator("Tab2");
menuTab.addTab(tab2_tab_menu);
TabHost.TabSpec tab3_tab_menu = menuTab.newTabSpec("Tab3");
tab3_tab_menu.setContent(R.id.menu_tab3_tab);
tab3_tab_menu.setIndicator("Tab3");
menuTab.addTab(tab3_tab_menu);
}
}
incase its needed i have include the CustomDrawerLayout code
CustomDrawerLayout.java
public class CustomDrawerLayout extends DrawerLayout{
public CustomDrawerLayout(Context context) {
super(context);
}
public CustomDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
The idea is to have it in the baseController class and pretty much call a method in the onCreate in a specific activity that i want the menu in as well as say in the xml for that Activity and the menu is basically there with the same content etc?
Been battling with this for a couple days now. Help will be greatly appreciated.
Thanks
Implemention for UrMenu should be done in BaseControllerActivity, and all other activities in our application must inhert this BaseControllerActivity. Then you can use that menu, and you can override if want to customize onClick of menu's.
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);
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>
I have a listview with checkboxes:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/nomeAPP" style="?listItem" />
</LinearLayout>
But the problem is: checkboxes is over text (text is to much for the left)
How can I correct that?
How about putting android:paddingLeft="xdp" where is x is the number of dp that you will need to put in order to you text?
Putting padding will only shift text but not your "Tick-Button".
You may consider android:gravity property too if putting a hard-coded dp doesn't suit your need.
If your text is in the same layout of the checkbox, do it like this:
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/nomeAPP"
style="?listItem"
android:layout_marginLeft="10dp" />
But if its not, do it like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp">
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/nomeAPP"
style="?listItem" />
</LinearLayout>
Obs: I've put margin and padding with left only for the example.
Let me know if you have more doubts. Thanks.
Here’s what I used for items in a list, each of which has text and a radio button. Note the layout_weight and layout_gravity settings:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/select_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:layout_weight="100"
/>
<RadioButton
android:id="#+id/file_item_checked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:layout_gravity="right"
/>
</LinearLayout>
There are two ways to resolve this issue_
1 Building your own custom CheckBox Class by extending android.widget.CheckBox class. The Complete Custom code is as below_
public class CustomCheckBox extends CheckBox {
public CustomCheckBox(Context context) {
super(context);
}
public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public int getCompoundPaddingLeft() {
final float scale = this.getResources().getDisplayMetrics().density;
/*add padding you needed so the text is not on top of the
* CheckBox.
*/
return (super.getCompoundPaddingLeft() + (int) (10.0f * scale + 0.5f));
}
}
2 Set Padding in Your Activity_
final float scale =this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
checkBox.getPaddingTop(),
checkBox.getPaddingRight(),
checkBox.getPaddingBottom());
I hope this will help all who have this issue... :)