Input of symbols in EditText - java

There's an EditText in fragment. User should input a number here (format of input is numeric). But there's a problem: nothing is shown in editText. User can input, but doesn't see the number. What's the matter?
Xml of fragment:
<?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"
tools:context=".CalculatorFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calculator_text"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="#+id/weight"
android:layout_below="#+id/text"
android:layout_marginTop="20dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/weight"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="#+id/height"
android:layout_below="#+id/weight"
android:layout_marginTop="20dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/height"
android:inputType="number"/>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/height"
android:layout_marginTop="30dp"
android:text="#string/count"
android:onClick="onClick"/>
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/count"
android:layout_marginTop="20dp"/>
</RelativeLayout>
Code of fragment:
package asus.example.com.fitnessapp;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class CalculatorFragment extends Fragment implements View.OnClickListener {
TextView textView;
EditText eWeight, eHeight;
int nWeight, nHeight;
public CalculatorFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_calculator, container, false);
Button button = v.findViewById(R.id.count);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
eWeight = v.findViewById(R.id.weight);
eHeight = v.findViewById(R.id.height);
nWeight = Integer.parseInt(eWeight.getText().toString());
nHeight = Integer.parseInt(eHeight.getText().toString());
if (nWeight-nHeight<100) {
textView.setText("Normal weight");
}
}
});
return v;
}
}
UPDATE
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Actually I think that emulator can cause this problem, because when I input numbers, I can change the position of cursor, that's why there's something here. But it's not shown. But at the same time when I try to parse to int information from this editTexts, it cause an error as there's nothing here.

I have tested the codes you've provided, there was a problem with the layout. Just add this line to your EditText xml block: android:layout_height="wrap_content" instead of hard fixing it to 20dp, which is not considered as a good dimension of height for EditText field.
As per Google Android developer suggestions, the minimum height for an EditText should be 50dp. So you may also consider changing the height of EditText to 50dp also instead of "wrap_content"
Thus the modified xml looks like 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"
tools:context=".CalculatorFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calculator_text"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/weight"
android:layout_below="#+id/text"
android:layout_marginTop="20dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/weight"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/height"
android:layout_below="#+id/weight"
android:layout_marginTop="20dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/height"
android:inputType="number"/>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/height"
android:layout_marginTop="30dp"
android:text="#string/count"
android:onClick="onClick"/>
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/count"
android:layout_marginTop="20dp"/>
</RelativeLayout>

Related

Java TextView Onclick() returning expression error

I am really new to java and I came across the following 2 errors error: illegal start of expression public void registerClick(View v) and error: class, interface, or enum expected }
I don't understand what I am doing wrong. I have watched extensive tutorials on Youtube and gone to the Android Studio docs but I can not find out what is wrong with my program. I am trying to go from a signup screen to a register screen if a certain text view is clicked. My code is down below.
Main Activity.java:
package com.example.gooddeed;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
public void registerClick(View v)
{
TextView tv= (TextView) findViewById(R.id.register);
}
}
}
Activity Main.Xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="235dp"
android:background="#drawable/blue_bg" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="36.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/email_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/username_icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/username_icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/email" />
</RelativeLayout>
<!-- Spacer Button -->
<!-- Spacer Button -->
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="14dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="12.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/password" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="18.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_fill__rounded_color"
android:fontFamily="#font/poppins_medium"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/forgotpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="FORGOT PASSWORD?"
android:textColor="#1566e0"
android:textSize="10.5sp" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="Don't have an account? Make one for free!"
android:textColor="#1566e0"
android:textSize="13sp"
android:clickable="true"
android:onClick="registerClick"
/>
</LinearLayout>
<LinearLayout
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="wrap_content"
android:layout_marginBottom="13.7dp"
android:gravity="center"
android:orientation="horizontal"/>
</LinearLayout>
</RelativeLayout>
Thank you to everyone that helps!
You should have registerClick as a separate method inside the activity because Java does not support directly nested methods
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
}
public void registerClick(View v) {
TextView tv= (TextView) findViewById(R.id.register);
}
}

Scrollview is not scrolling in AbsoluteLayout

I'm trying to make an Activity which allows the user to press a button and when he presses it, it generates an EditText and a spinner. The problem is that the scroll view does not scroll. The page stays as is.
XML:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_order_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.nir.nestleapp.OrderItemsActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_y="58dp"
android:inputType="phone"
android:ems="10"
android:id="#+id/Quantity"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="quantity"
android:layout_x="11dp" />
<Spinner
android:layout_width="174dp"
android:layout_height="48dp"
android:layout_x="148dp"
android:layout_y="58dp"
android:id="#+id/ItemSpinner" />
</AbsoluteLayout>
</ScrollView>
<Button
android:text="add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="133dp"
android:layout_y="431dp"
android:id="#+id/Generate" />
</AbsoluteLayout>
Java:
package com.example.nir.nestleapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class OrderItemsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_items);
final AbsoluteLayout layout = (AbsoluteLayout) findViewById(R.id.activity_order_items);
final Spinner Items=(Spinner)findViewById(R.id.ItemSpinner);
final Button Generate=(Button)findViewById(R.id.Generate);
final EditText Quantity=(EditText)findViewById(R.id.Quantity);
final float[] yPlacement = {280};
Generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Spinner GeneratedSpinner=new Spinner(OrderItemsActivity.this);
EditText GeneratedQuantity=new EditText(OrderItemsActivity.this);
layout.addView(GeneratedSpinner);
GeneratedSpinner.setX(Items.getX());
yPlacement[0] =yPlacement[0]+200;
GeneratedSpinner.setY(yPlacement[0]);
layout.addView(GeneratedQuantity);
GeneratedQuantity.setX(Quantity.getX());
GeneratedQuantity.setY(yPlacement[0]);
GeneratedQuantity.setHint("quantity");
GeneratedQuantity.setWidth(300);
}
});
}
You can change your Absolute layout, use as child LinearLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_y="58dp"
android:inputType="phone"
android:ems="10"
android:id="#+id/Quantity"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="quantity"
android:layout_x="11dp" />
<Spinner
android:layout_width="174dp"
android:layout_height="48dp"
android:layout_x="148dp"
android:layout_y="58dp"
android:id="#+id/ItemSpinner" />
</LinearLayout>
</ScrollView>
Changing AbsoluteLayout into a LinearLayout is the best way to get rid from this issue.

How to open fragments containing webViews in another activity on button click from fragments of another activity?

I have image-buttons in one of the fragments of an activity and want to open corresponding fragments containing webViews on button clicks in another activity.
I am a beginner so please do give appropriate codes for this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.administrator.hiha.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar">
</include>
<fragment
android:id="#+id/upperFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.administrator.hiha.Upper_main_Fragment"
android:layout_below="#id/toolbar"
>
</fragment>
<fragment
android:layout_below="#id/upperFragment"
android:id="#+id/lowerFragment"
android:layout_width="match_parent"
android:layout_height="140dp"
android:name="com.example.administrator.hiha.Lower_main_Fragment"
>
</fragment>
<fragment
android:layout_below="#id/lowerFragment"
android:id="#+id/Bottom_Most_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.administrator.hiha.BottomMostFragment"
tools:layout="#layout/fragment_bottom_most">
</fragment>
<View
android:id="#+id/hLastRow"
android:layout_centerHorizontal="true"
android:layout_below="#id/Bottom_Most_Fragment"
android:layout_width="300dp"
android:layout_height="2dp"
android:background="#EEEEEE"
/>
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hLastRow"
android:layout_centerHorizontal="true"
android:text="Designed \u0026 Developed by me © 2016."/>
</RelativeLayout>
</ScrollView>
MainActivity.java
package com.example.administrator.hiha;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
//Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar=(Toolbar)findViewById(R.id.toolbar);
}
}
fragment_lower_main.xml
<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.example.administrator.hiha.Lower_main_Fragment">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/about_board"
android:id="#+id/about_img_btn"
android:onClick="onAboutBoardClick"
/>
<TextView
android:id="#+id/text_about_img_btn"
android:text="About Board"
android:layout_below="#id/about_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/sarasvati"
android:id="#+id/sarasvati_img_btn"
android:layout_toRightOf="#id/about_img_btn"
android:onClick="onSarasvatiClick"
/>
<TextView
android:layout_toRightOf="#id/text_about_img_btn"
android:id="#+id/text_sarasvati_img_btn"
android:text="Sarasvati"
android:layout_below="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/scientific_evidences"
android:id="#+id/scientific_evidences_img_btn"
android:onClick="onScientificEvidencesClick"
/>
<TextView
android:layout_toRightOf="#id/text_sarasvati_img_btn"
android:id="#+id/text_scientific_evidences_img_btn"
android:text="Scientific EVidences"
android:layout_below="#id/scientific_evidences_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/digital_library_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/affltd_organization"
android:id="#+id/affltd_oragnization_img_btn"
android:onClick="onAffltdOrganizationClick"
/>
<TextView
android:layout_toRightOf="#id/text_digital_library_img_btn"
android:id="#+id/text_affltd_oragnization_img_btn"
android:text="Affiliated Organizations"
android:layout_below="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/tender"
android:id="#+id/tender_img_btn"
android:onClick="onTenderClick"
/>
<TextView
android:layout_toRightOf="#id/text_affltd_oragnization_img_btn"
android:id="#+id/text_tender_img_btn"
android:text="Tenders"
android:layout_below="#id/tender_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
Lower_Main_Fragment.java
package com.example.administrator.hiha;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Lower_main_Fragment extends Fragment {
ImageButton about_img_btn;
ImageButton sarasvati_img_btn;
ImageButton scientific_evidences_img_btn;
ImageButton digital_library_img_btn;
ImageButton affltd_oragnization_img_btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.fragment_lower_main_,container,false);
//cast image buttons
about_img_btn=(ImageButton)getView().findViewById(R.id.about_img_btn);
sarasvati_img_btn=(ImageButton)getView().findViewById(R.id.sarasvati_img_btn);
scientific_evidences_img_btn=(ImageButton)getView().findViewById(R.id.scientific_evidences_img_btn);
digital_library_img_btn=(ImageButton)getView().findViewById(R.id.digital_library_img_btn);
affltd_oragnization_img_btn=(ImageButton)getView().findViewById(R.id.affltd_oragnization_img_btn);
return rootView;
}
//onclick methods of image buttons
public void onAboutBoardClick(View view){
}
}
These are the files in which i want that if i click on about_img_btn then it should open a fragment containing a webView in another activity. The same should be repeated for all the buttons.
Please define the further code that i have to use and mention the files where i have to use it. Thanks!
try this:
in your fragment_lower_main xml:
Give an id to the Framelayout:
<?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"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<FrameLayout
android:id="+#id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<--Rest of the code -->
/>
</LinearLayout>
Now in code open new Fragment during button click using:
about_img_btn=(ImageButton)rootView.findViewById(R.id.about_img_btn);
about_img_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AboutUS fragment = new AboutUS();
Replace_fragment(fragment); //pass the fragment u want to replace
}
});
public void ReplaceFragment(Fragment r_fragment) {
FragmentManager fragmentManager = getActivity.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, r_fragment).commit();
}

I wanted the TextView to appear over the Button instead of Under it. How can I do that?

I want the text to appear on the button and not under it.And How can I give more weightage to the TextView rather than the Button background Colour.I was building an App TicTacToe and I came across this Issue.I'm not able to solve it.Can someone please guide me?
Activity(MainActivity.java):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends
AppCompatActivity implements View.OnClickListener {
TextView textone;
Button button,buttonPanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textone = (TextView)findViewById(R.id.textone);
button = (Button)findViewById(R.id.button);
buttonPanel = (Button)findViewById(R.id.buttonPanel);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button){
textone.setText("This is not Fair");
}
}
}
Layout(activity_main.xml):
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="#+id/buttonPanel"
android:text="#string/button"
android:textSize="40sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f4f18a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/textone"
android:id="#+id/textone"
android:textSize="45sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button2"
android:id="#+id/button"
android:layout_marginBottom="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
If you have <LinearLayout ... as a parent of your layout, You need to add your <TextView .. and <Button .. like as below
<LinearLayout layout_height=".." layout_width=".." orientation="vertical">
<TextView layout_height=".." layout_width=".." id="#+id/textone"/>
<Button layout_height=".." layout_width=".." id="#+id/button" />
<!-- your other layout view -->
</LinearLayout>
2.If you have <RelativeLayout ... as a parent of your layout, You need to add your <TextView .. and <Button .. as below
<RelativeLayout layout_height=".." layout_width=".." orientation="vertical">
<TextView layout_height=".." layout_width=".." id="#+id/textone"/>
<Button layout_height=".." layout_width=".." id="#+id/button" android:layout_below="#id/textone" />
<!-- your other layout view -->
</RelativeLayout>

Button and image disappear Android

Im taking my first steps in developing for android. Following a tutorial i have created a button, textview,edit text, and am now trying to implimetn a list view.
i have followed the tutorial Exactly, and for some reason, when i implement the Tableview, my button and image disappear.
can anybody find out why?
.java:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
TextView mainTextView;
Button mainButton;
EditText mainEditText;
ListView mainListView;
ArrayAdapter mArrayAdapter;
ArrayList mNameList = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//access the textview defined in Layout XML & set its value
mainTextView = (TextView) findViewById(R.id.main_textview);
mainTextView.setText("set In Java!");
//access the button defined in layout XML
mainButton = (Button) findViewById(R.id.main_button);
mainButton.setOnClickListener(this);
mainEditText = (EditText) findViewById(R.id.main_edittext);
mainListView = (ListView)findViewById(R.id.main_listview);
mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,mNameList);
mainListView.setAdapter(mArrayAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
mainTextView.setText(mainEditText.getText().toString() + " is learning Android Development");
mNameList.add(mainEditText.getText().toString());
mArrayAdapter.notifyDataSetChanged();
}
}
activity.XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/main_textview"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textview"/>
<EditText
android:id="#+id/main_edittext"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/hint"/>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- set OnClickListener to trigger results when pressed -->
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="#string/button"/>
<!--shows an image from your drawable rescources-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/main_textview"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textview"/>
<EditText
android:id="#+id/main_edittext"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/hint"/>
</LinearLayout>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="#string/button"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
Try this..
<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="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/main_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="textview" />
<EditText
android:id="#+id/main_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:hint="hint" />
</LinearLayout>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- set OnClickListener to trigger results when pressed -->
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="button" />
<!-- shows an image from your drawable rescources -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
LinearLayout is horizontal by default.
Your ListView is layout_width="match_parent" and root LinearLayout is horizontal.
so ListView Expands wide, ImageView and etc have pushed out and become invisible.
I recommend you to always specify android:orientation ("vertical" or "horizontal") in LinearLayout.

Categories