I've got an app based on Sliding Tabs layout. I want to call the addMedicine method in the PageFragment class from an XML layout with a Button. The issue is that i cannot assign the method to the Button onClick event.
Here's the PageFragment class:
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
// TEMP VARIABLES //
ListView ItemsLst;
String[] Items = {"Medicine 1", "Medicine 2", "Medicine 3"};
TextView output;
EditText nameFld;
EditText formatFld;
EditText amountFld;
EditText exp_dateFld;
EditText timeFld;
DBManager dbAdapt = new DBManager(getActivity());
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
switch(mPage)
{
case 1:
view = inflater.inflate(R.layout.layout_sqltest, container, false);
nameFld = (EditText) view.findViewById(R.id.nameFld);
formatFld = (EditText) view.findViewById(R.id.formatFld);
amountFld = (EditText) view.findViewById(R.id.amountFld);
exp_dateFld = (EditText) view.findViewById(R.id.exp_dateFld);
timeFld = (EditText) view.findViewById(R.id.timeFld);
return view;
case 2:
view = inflater.inflate(R.layout.layout_settings, container, false);
return view;
case 3:
view = inflater.inflate(R.layout.layout_info, container, false);
return view;
}
return view;
}
public void addMedicine() // The method i want to call
{
String name = nameFld.getText().toString();
String format = formatFld.getText().toString();
int amount = Integer.parseInt(amountFld.getText().toString());
String exp_date = exp_dateFld.getText().toString();
String time = timeFld.getText().toString();
long id = dbAdapt.addMedicine(name, format, amount, exp_date, time);
}
}
And here is the layout_sqltest XML file:
<?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="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/nameLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nameFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Format"
android:id="#+id/formatLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/formatFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Amount"
android:id="#+id/amountLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/amountFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Expiration Date"
android:id="#+id/exp_dateLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/exp_dateFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time"
android:id="#+id/timeLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/timeFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Medicine"
android:id="#+id/addMedicineBtn"
android:layout_gravity="center_horizontal" />
</LinearLayout>
The addMedicineBtn is the button i want to call the method with.
Someone can help me? I really need that!
Thank you in advance!
EDIT: I tried giving to addMedicine method the View param, but it still doesn't appear in the available methods for the addMedicineBtn in the onClick properties (I'm using Android Studio).
Try to use onClickListener
view.findViewById(R.id.addMedicineBtn).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
addMedicine();
}
});
You must add tools:context="PageFragment" line to the container (LinearLayout) of PagerFragment's layout file (layout_sqltest). Then onClick() method will find methods in PagerFragment class.
Related
Hey i have a simple question. i have a mobile_navigation that contain code to call a fragment class, and i have the fragment class but i want to call / pass to another java class. for example i have Inputdata.xml that when tap will go to fragment class nah i want when i tap the input xml it will instantly go to another java class not stuck in fragment class
here is the example code for mobile_navigation
<fragment
android:id="#+id/nav_pantuan"
android:name="com.joshua.r0th.crud2.ui.gallery.GalleryFragment"
android:label="#string/pantauan"
tools:layout="#layout/fragment_pantauan" />
and here is the GalleryFragment code
public class GalleryFragment extends Fragment {
private GalleryViewModel GalleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
GalleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_pantauan, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
GalleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
and this is the another java class that i willing to reach
public class pantauan extends AppCompatActivity {
database1 myDb;
EditText editNomorRumah,editJentikDalam,editJentikLuar;
Button btnAddData;
Button btnViewAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pantauan);
myDb = new database1(this);
editNomorRumah = (EditText)findViewById(R.id.nomorrmh);
editJentikDalam = (EditText)findViewById(R.id.jentikdirumah);
editJentikLuar = (EditText)findViewById(R.id.jentikdiluarrumah);
btnAddData = (Button)findViewById(R.id.tambahdata);
btnViewAll = (Button)findViewById(R.id.lihatdata);
AddData();
viewAll();
}
//fungsi tambah
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editNomorRumah.getText().toString(),
editJentikDalam.getText().toString(),
editJentikLuar.getText().toString() );
if(isInserted == true)
Toast.makeText(pantauan.this,"Data Iserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(pantauan.this,"Data Not Iserted",Toast.LENGTH_LONG).show();
}
}
);
}
//fungsi menampilkan data
public void viewAll() {
btnViewAll.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Noting Found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext() ) {
buffer.append("NomorRumah :"+ res.getString(0)+"\n");
buffer.append("JentikDalam :"+ res.getString(1)+"\n");
buffer.append("JentikLuar :"+ res.getString(2)+"\n");
}
// show all data
showMessage("Data",buffer.toString());
}
}
);
}
//membuat alert dialog
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
and here is the Input.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/gradient"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:background="#drawable/gradient"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nomor Rumah" />
<EditText
android:id="#+id/nomorrmh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Dalam Rumah" />
<EditText
android:id="#+id/jentikdirumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Luar Rumah" />
<EditText
android:id="#+id/jentikdiluarrumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/tambahdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="Tambah Data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
<Button
android:id="#+id/lihatdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="contoh lihat data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:id="#+id/textviewadd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:gravity="center_horizontal"
android:text="Input Data"
android:textColor="#fff"
android:textSize="26sp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/text_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
My goal is to pass the fragment into the java class that i want, because i dont know how to put that input class to the fragment always get cannot be cast to androidx.fragment.app.Fragment when i replace the GalleryFragment with pantauan.java code. thank you
It's quite simple. I have a button that has an onClickListener which takes a text from EditText, hides it and sets it to TextView which it shows in the same place.
Now, i want to do that to 2 more different edit texts and text views with the same button. Code below.
XML with my buttons:
<?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 = ".profil"
android:orientation = "vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:background="#color/pozadina"
android:id="#+id/rltv">
<ImageView
android:id="#+id/slikaProfil"
android:layout_width="130dp"
android:layout_height="160dp"
android:src="#drawable/profilna"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/textProfil1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/userProfil"
android:layout_below="#+id/slikaProfil"
android:layout_centerInParent="true"
android:textSize="20sp"
android:textStyle="bold"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/rltv">
<TextView
android:id="#+id/imeProfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account info"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ime i prezime"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/profil"
android:drawablePadding="5dp"
android:id="#+id/imeProfil1"
android:textSize="16sp"
android:layout_below="#+id/imeProfil"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite ime i prezime"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/imeProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/imeProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telefon"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/telefon"
android:drawablePadding="5dp"
android:id="#+id/telefonProfil1"
android:textSize="16sp"
android:layout_below="#+id/imeProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite broj telefona"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/telefonProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/telefonProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/mail"
android:drawablePadding="5dp"
android:id="#+id/mailProfil1"
android:textSize="16sp"
android:layout_below="#+id/telefonProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite e-mail"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/mailProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/mailProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adresa"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/adresa"
android:drawablePadding="5dp"
android:id="#+id/adresaProfil1"
android:textSize="16sp"
android:layout_below="#+id/mailProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite adresu"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/adresaProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/adresaProfil1"
/>
<Button
android:id="#+id/prihvati"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_below="#+id/adresaProfil2"
android:layout_marginLeft="70dp"
android:text="#string/submit"
android:onClick="zamijeni"/>
</RelativeLayout>
</RelativeLayout>
My java file:
public class profil extends Fragment {
private Button dugme4;
private TextView text1;
private EditText text2;
public profil(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_profil, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
text1 = view.findViewById(R.id.imeProfil1);
text2 = view.findViewById(R.id.imeProfil2);
dugme4 = view.findViewById(R.id.prihvati);
text2.setVisibility(View.INVISIBLE);
dugme4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String tekst = text2.getText().toString();
if (text1.getVisibility()== View.VISIBLE){
text1.setVisibility(View.INVISIBLE);
text2.setVisibility(View.VISIBLE);
}else if (text2.getVisibility() == View.VISIBLE){
text2.setVisibility(View.INVISIBLE);
text1.setVisibility(View.VISIBLE);
text1.setText(tekst);
}
}
});
}
}
So as you see i have an if function that on click gets the text, makes the edit text invisible and text visible and at the end sets the text to the textView.
Should i do a few more if functions or should it be done differently.
You should implement the same logic to access the text from EditText and display it in the TextView, you can make a function which should be called on the click of this button.
You can achive this more efficiently by use only a edittext.. no need to use text view.. I am giving a sample demo below
public class profil extends Fragment {
private Button dugme4;
//private TextView text1;
private EditText text2;
public profil(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_profil, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// text1 = view.findViewById(R.id.imeProfil1);
text2 = view.findViewById(R.id.imeProfil2);
dugme4 = view.findViewById(R.id.prihvati);
// text2.setVisibility(View.INVISIBLE);
dugme4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (text2.isEnabled()){
//make it not editable and non focusable
text2.setEnable(false);
text2.setCursorVisible(false);
//for clearing underline of editText so that it looks like textview
text2.setBackgroundResource(android.R.color.transparent);
}else{
text2.setEnabled(true);
text2.setCursonVisible(true);
// set any color what you want as underline
text2.setBackgroundResource(android.R.color.black);
}
}
});
}
}
Try it.hope it will solve your problem by reducing extra element and complexity.
I'm trying to create validations in some edittext and I am using an example I found on the internet but when I implemented the code my findViewById is in error (in view can not be applied to android.view.view.
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_cadastro_contas_bancarias, container, false);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = null;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.btn_cadastrar_conta:
registerAttemptWithRetrofit(1,
"3948",
"0",
"01089842",
"3",
1);
fragment = new ContasBancarias();
default:
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
View view1 = inflater.inflate(R.layout.fragment_cadastro_contas_bancarias, container, false);
EditText agencia = (EditText) getView().findViewById(R.id.txt_agencia);
if ("".equals(agencia.getText().toString().trim())) {
Toast.makeText(getActivity(), "Campo Obrigatório", Toast.LENGTH_SHORT).show();
return;
//
}
}
};
return view; }
<?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">
<TextView
android:text="Dados Bancários"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_marginTop="16dp"
android:textSize="18sp"
android:textColor="#android:color/black"
android:textColorHighlight="#color/Black"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="130dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txt_agencia"
android:hint="Agência"
android:layout_alignBaseline="#+id/txt_digito_agencia"
android:layout_alignBottom="#+id/txt_digito_agencia"
android:layout_toEndOf="#+id/spn_tipo_de_conta"/>
<EditText
android:layout_width="95dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txt_digito_conta"
android:layout_alignBottom="#+id/txt_conta"
android:layout_toEndOf="#+id/txt_conta"
android:layout_alignTop="#+id/txt_conta"
android:hint="Digito"
android:textStyle="bold" />
<Spinner
android:layout_width="150dp"
android:layout_height="40dp"
android:id="#+id/spn_tipo_de_conta"
android:layout_below="#+id/textView3"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<Button
android:text="Cancelar"
android:layout_width="90dp"
android:layout_height="40dp"
android:id="#+id/btn_cancelar"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="21dp"
android:background="#color/DefaultBlue"
android:textColor="#android:color/white"
android:textStyle="normal|bold"
android:onClick="cancelar (CadastroContasBancarias)" />
<Button
android:text="Cadastrar"
android:layout_width="90dp"
android:layout_height="40dp"
android:id="#+id/btn_cadastrar_conta"
android:background="#color/DefaultBlue"
android:textColor="#android:color/white"
android:textStyle="normal|bold"
android:layout_alignBaseline="#+id/btn_cancelar"
android:layout_alignBottom="#+id/btn_cancelar"
android:layout`enter code here`_alignParentEnd="true" />
</RelativeLayout>
You can try this you get the solutions....
View view1 = inflater.inflate(R.layout.fragment_cadastro_contas_bancarias, container, false);
EditText agencia = (EditText) view1 ().findViewById(R.id.txt_agencia);
My TextView has layout_centerInParent="true" and looks good in the layout preview in Android Studio, but when I compile and run the "Let's Get Started" text ends up near the bottom as shown below. Why is this happening?
Fragment XML:
<?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"
android:id="#+id/intro_background"
android:background="#color/primary">
<TextView
android:id="#+id/tv_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxWidth="240sp"
android:gravity="center"
android:textSize="45sp"
android:textStyle="bold"
android:textColor="#color/white"
android:text="Let\'s Get Started!"
/>
<Button
android:id="#+id/btn_add_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_not_now"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:background="#drawable/btn_primary_with_white"
android:paddingBottom="10dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="10dp"
android:text="Add My First Course"
android:textColor="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/tv_not_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:maxWidth="280sp"
android:layout_gravity="center_horizontal"
android:gravity="left"
android:textStyle="italic"
android:textSize="16sp"
android:textColor="#color/white"
android:text="#string/intro_not_now"
/>
</RelativeLayout>
Activity 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">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/nav_header1" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="3dp"
android:layout_marginTop="-25dp"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
Activity:
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new Adapter_Intro(getSupportFragmentManager()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}
}
Fragment:
public class IntroFragment extends Fragment {
private static final String BACKGROUND_COLOR = "backgroundColor";
private static final String PAGE = "page";
private int mBackgroundColor, mPage;
public static IntroFragment newInstance(int backgroundColor, int page) {
IntroFragment frag = new IntroFragment();
Bundle b = new Bundle();
b.putInt(BACKGROUND_COLOR, backgroundColor);
b.putInt(PAGE, page);
frag.setArguments(b);
return frag;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!getArguments().containsKey(BACKGROUND_COLOR))
throw new RuntimeException("Fragment must contain a \"" + BACKGROUND_COLOR + "\" argument!");
mBackgroundColor = getArguments().getInt(BACKGROUND_COLOR);
if (!getArguments().containsKey(PAGE))
throw new RuntimeException("Fragment must contain a \"" + PAGE + "\" argument!");
mPage = getArguments().getInt(PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Select a layout based on the current page
int layoutResId;
switch (mPage) {
case 0:
layoutResId = R.layout.fragment_intro_welcome;
break;
default:
layoutResId = R.layout.fragment_intro_start;
}
// Inflate the layout resource file
View view = getActivity().getLayoutInflater().inflate(layoutResId, container, false);
// Set the current page index as the View's tag (useful in the PageTransformer)
view.setTag(mPage);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Set the background color of the root view to the color specified in newInstance()
View background = view.findViewById(R.id.intro_background);
background.setBackgroundColor(mBackgroundColor);
}
}
It seems that Android Studio is not accurate while rendering your layout.
I "measured" your screenshot using Gimp - as you can see, TextView is positioned exactly to the center of it parent, between top picture and bottom:
Your TextView is well centered inside the fragment, but not inside the activity.
Because in your activity, there is the ImageView that takes some height in the top.
I have a FragmentActivity that should hold 2 Fragments. Each fragment has it's own layout with a table in it. I need to access the tables from the fragments' layouts from within the FragmentActivity to put some data that I read from an XML file. My problem is that when I use
tableOrders = (TableLayout)findViewById(R.id.tabel1);
it returns null. Now, I know that items from layouts can only be accessed before they have been inflated, and I've done that in the FragmentActivity before trying to call findViewById(). Here is the code for what I've tried:
The onCreate of the FragmentHolder.java that is derived from FragmentActivity:
private TableLayout tableOrders = null;
private TableLayout tableExecutions = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_holder);
initialisePaging();
//View orderFragment = (View)findViewById(R.layout.fragment_orders);
tableOrders = (TableLayout)/*orderFragment.*/findViewById(R.id.tabel1);
//View execFragment = (View)findViewById(R.layout.executions_fragment);
tableExecutions = (TableLayout)/*execFragment.*/findViewById(R.id.tabel2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null)
logedUser = (User) bundle.getSerializable("user");
AssetManager am = getApplicationContext().getAssets();
String xmlContentOrders = XMLfunctions.getXml(am, "ordersData.xml");
populateOrdersTable(xmlContentOrders);
String xmlContentExecutions = XMLfunctions.getXml(am, "executionsData.xml");
populateExecutionsTable(xmlContentExecutions);
}
The fragment class (don't know if it helps):
public class OrdersFragment extends Fragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
return view;
}
}
The fragment class (don't know if it helps):
public class OrdersFragment extends Fragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
return view;
}
}
And the .xml file for the fragment:
<?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" >
<TableLayout
android:id="#+id/tabel1header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/tablebgcolor" >
<TableRow >
<TextView
android:id="#+id/ordersHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="#string/orders"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/ordersTable"
style="#style/HeaderRow" >
<TextView
android:id="#+id/textSymbol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/symbol"
style="#style/HeaderText" />
<TextView
android:id="#+id/textSide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/side"
style="#style/HeaderText" />
<TextView
android:id="#+id/textPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/price"
style="#style/HeaderText" />
<TextView
android:id="#+id/textQuantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/quantity"
style="#style/HeaderText"/>
<TextView
android:id="#+id/textRemaining"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/remanent"
style="#style/HeaderText" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableLayout
android:id="#+id/tabel1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/tablebgcolor">
</TableLayout>
</ScrollView>
</LinearLayout>
With that said hope someone has a solution for this (probably it's something easy that I missed).
Edit: The code I use to put the Fragments on the FragmentActivity:
private void initialisePaging() {
OrdersFragment fragmentOne = new OrdersFragment();
ExecutionsFragment fragmentTwo= new ExecutionsFragment();
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(fragmentOne);
pagerAdapter.addFragment(fragmentTwo);
ViewPager viewPager = (ViewPager) super.findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(2);
viewPager.setCurrentItem(0);
}