How to Set Scanning Area ZXing Scanner - java

Hello i create Reader QR Code using ZXING Scanner in Android Studio
i want increase height and width scanning area green border see this image.
Can you have solution to set height and width scanning area (green border) ?
This is my code MainActivity.java
package com.example.gajelo.barcodereader;
import android.widget.RelativeLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mScannerView = new ZXingScannerView(this);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative_scan);
rl.addView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
CaptureFragment.java
package com.example.gajelo.barcodereader;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class CaptureFragment extends Fragment {
private ZXingScannerView mScannerView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
View view = inflater.inflate(R.layout.capture, container, false);
return view;
}
}
activity_main.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:id="#+id/activity_main"
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.gajelo.barcodereader.MainActivity">
<TextView
android:text="This Is Main Activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:textSize="30sp"
android:textStyle="normal|bold"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp" />
<TextView
android:text="Scan QR CODE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="30sp"
android:textStyle="normal|bold"
android:textColor="#android:color/black"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:layout_marginBottom="54dp" />
<RelativeLayout
android:id="#+id/relative_scan"
android:layout_width="300dip"
android:layout_height="300dip"
android:layout_centerInParent="true"
android:layout_gravity="center">
</RelativeLayout>
</RelativeLayout>
capture.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:id="#+id/activity_main"
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">
</RelativeLayout>

You can use
app:zxing_framing_rect_width="200dp"
and app:zxing_framing_rect_height="200dp" to increase height
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="#+id/zxing_barcode_scanner"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
app:zxing_framing_rect_width="200dp"
app:zxing_framing_rect_height="200dp"
app:zxing_preview_scaling_strategy="fitXY"
app:zxing_use_texture_view="false"
/>

Related

Fragment Does Not Appear In Android

I recently learned about Fragments in Android and was building a notepad application to practice them.
Idea: The idea behind the app is simple. I press the button to add a new note. A CardView Viewgroup turns visible. The fragment will be housed within this CardView.
Problem: When I press the button to add a new note, the fragment does not pop up.
MainActivity.java
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Objects;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CardView fragmentCardView;
private Button newNoteButton;
private FragmentManager fragmentManager = getSupportFragmentManager();
private FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBar();
newNoteButton = findViewById(R.id.new_note_button);
fragmentCardView = findViewById(R.id.fragment_cardView);
}
void setActionBar(){
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.layout_custom_action_bar);
}
void showCreateNoteFragment(){
CreateNoteFragment createNoteFragment = CreateNoteFragment.newInstance();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_cardView, createNoteFragment);
fragmentTransaction.commit();
fragmentCardView.setVisibility(View.VISIBLE);
}
#Override
public void onClick(View v) {
if (v == newNoteButton)
showCreateNoteFragment();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="#F2F2F2"
tools:context=".MainActivity">
<ListView
android:id="#+id/notes_listView"
android:layout_width="match_parent"
android:layout_height="667dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="#+id/new_note_button"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="27dp"
android:fontFamily="#font/nunito_bold"
android:text="#string/new_note_button_string"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/notes_listView"/>
<androidx.cardview.widget.CardView
android:id="#+id/fragment_cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:visibility="invisible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
CreateNoteFragment.java
package com.example.notepadapplication;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class CreateNoteFragment extends Fragment implements View.OnClickListener {
private EditText inputEditText;
public CreateNoteFragment() {
// Required empty public constructor
}
static CreateNoteFragment newInstance(){
return new CreateNoteFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_create_note, container, false);
inputEditText = rootView.findViewById(R.id.input_editText);
Button saveButton = rootView.findViewById(R.id.save_button);
saveButton.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
String saveButtonText = inputEditText.getText().toString();
}
}
fragment_create_note.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:background="#F2F2F2"
tools:context=".CreateNoteFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/display_fragment_header_string"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="20sp"
android:fontFamily="#font/nunito_bold"/>
<View
android:id="#+id/divider_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_below="#id/header_textView"
android:background="#000000" />
<EditText
android:id="#+id/input_editText"
android:layout_width="match_parent"
android:layout_height="475dp"
android:layout_below="#id/divider_view"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:padding="10dp"
android:inputType="textMultiLine"
android:gravity="top"
android:background="#FFFFFF"
tools:ignore="Autofill,LabelFor,TextFields" />
<Button
android:id="#+id/save_button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="#string/save_button_string"
android:fontFamily="#font/nunito_bold"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Could anyone check why the fragment isn't popping up? Thanks for the help, guys.
P.S.: I think that the application might become a little bloated because of all the ViewGroups that I'm using. If you guys have a better way to design this, by all means, I would love any input.
When I press the button to add a new note, the fragment does not pop up.
This is expected since your button doesn't know what to do when pressed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBar();
newNoteButton = findViewById(R.id.new_note_button);
// Button will call activity's onClick whenever it's clicked
newNoteButton.setOnClickListener(this);
fragmentCardView = findViewById(R.id.fragment_cardView);
}

It doesn't send the data from Activity to Fragment

I have lots of layouts and activites and fragments. There are tabs in one activity called Hastane1 and it has four tabs in it. They don't have any problem but one of them has. I wanted to send a data from one Activity to a Fragment. They are seperate. Fragment is in Hastane1 activity but Haberlesme1 activity is in another place. It supposed to send the numbers which i entered in the activity called Haberlesme1 to the textView in the fragment called Hastane1Tab3Frag which is a tab of Hastane1.
The Android Studio itself doesn't have any errors but when i run the app on my phone, it stops when i pressed the button called "Gönder" (#id/button8).
Haberlesme1 (Activity):
package com.example.projev021.İllerPackage.KocaeliPackage.GebzePackage.GebzeFragments.Hastane1Fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.projev021.HaberlesmePackage.Haberlesme1;
import com.example.projev021.R;
public class Hastane1Tab3Frag extends Fragment {
Button goster;
TextView sonucText;
int sayi=0;
Haberlesme1.Ogrenci ogr;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hastane1_tab3,container,false);
goster=(Button)v.findViewById(R.id.button13);
sonucText = (TextView)v.findViewById(R.id.textView3);
goster.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sonucText.setText(sayi);
}
});
return v;
}
public void sendData(int birinciSayi) {
this.sayi=birinciSayi;
}
public void sendOgrenci(Haberlesme1.Ogrenci ogrenci) {
this.ogr=ogrenci;
}
}
haberlesme1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:fontFamily="#font/lineto_circular_black"
android:text="#string/kackisivar"
android:textSize="50sp"/>
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:inputType="number"
android:text="" />
<Button
android:id="#+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="#drawable/buttonshape"
android:fontFamily="#font/lineto_circular_black"
android:text="#string/gonder"
android:textColor="#FFC107"
android:textSize="30sp"
android:onClick="calistir"/>
</LinearLayout>
Hastane1Tab3Frag (Fragment):
package com.example.projev021.İllerPackage.KocaeliPackage.GebzePackage.GebzeFragments.Hastane1Fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.projev021.HaberlesmePackage.Haberlesme1;
import com.example.projev021.R;
public class Hastane1Tab3Frag extends Fragment {
Button goster;
TextView sonucText;
int sayi=0;
Haberlesme1.Ogrenci ogr;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hastane1_tab3,container,false);
goster=(Button)v.findViewById(R.id.button13);
sonucText = (TextView)v.findViewById(R.id.textView3);
goster.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sonucText.setText(sayi);
}
});
return v;
}
public void sendData(int birinciSayi) {
this.sayi=birinciSayi;
}
public void sendOgrenci(Haberlesme1.Ogrenci ogrenci) {
this.ogr=ogrenci;
}
}
hastane1tab3.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:fontFamily="#font/lineto_circular_black"
android:text="#string/kisisayisi_"
android:textSize="50sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:fontFamily="#font/lineto_circular_black"
android:text=""
android:textSize="100sp"
android:textColor="#000000" />
<Button
android:id="#+id/button13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="#drawable/buttonshape"
android:fontFamily="#font/lineto_circular_black"
android:text="#string/goster"
android:textColor="#FFC107"
android:textSize="30sp"/>
</LinearLayout>
</FrameLayout>
And the screenshots:

Image is visible but text is not visible in ListView

I am trying to create a custom layout for list view in android studio. I was able to successfully get image in the in ListView on runtime but wasn't able to print text in the list view . When i run my program in emulator it runs without any error but it doesn't show any text it only shows image from drawables.
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private final static String[] names={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
"A","A","A","A","A","A","A","A","A","A","A","A","A","A",};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView ls = findViewById((R.id.customlistview));
ls.setAdapter(new CustomListAdapter(getApplicationContext(),names,R.drawable.za));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ListView
android:id="#+id/customlistview"
android:layout_width="409dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
CustomListAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
private final Context context;
private final String[] names;
private final int image;
private final LayoutInflater layoutInflater;
public CustomListAdapter(Context context,String[] names,int image) {
this.context=context;
this.image=image;
this.names=names;
layoutInflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return names.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rootview;
rootview = layoutInflater.inflate(R.layout.list_item_layout, null);
TextView tv= rootview.findViewById(R.id.textView);
ImageView iv=rootview.findViewById(R.id.imageView);
tv.setText(names[position]);
iv.setImageResource(image);
return rootview;
}
}
list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="70dp"
android:gravity="center"
android:text="TextView"
android:textSize="18sp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="2"
app:srcCompat="#mipmap/ic_launcher" />
</LinearLayout>
The issue is in your list_item_layout file. There is an issue with match_parent in your layout try using below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="70dp"
android:gravity="center"
android:text="TextView"
android:textSize="18sp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="2"
app:srcCompat="#mipmap/ic_launcher" />
The problem was caused by the text colour , changing the colour of textview text in list_item_layout resolved the issue.

Fragment transaction from activity not working

So i have a main activity that commits the transaction of a fragment that inflates the activity with an image view just like a main cover, and with the button "Sign up" that takes you to the signUp activity. However, all of that does work like it shoulds, the transaction works and the inflater does too, but the problem comes when:
I am trying to call a fragment (signUpForm) from the activity (signUp). The root of the problem is that the fragment transaction to signUpForm is not working because the toast programmed on it is not showing, so it does not even get to the inflater.
signUp class:
package com.example.joshumberto.workcon;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class signUp extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
getSupportFragmentManager().beginTransaction()
.add(R.id.sign_up_container, new signUpForm(), "signUpForm").commit();
}
}
activity_sign_up.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/sign_up_container"
tools:context=".signUp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="look, i should have inflated companions"
/>
</LinearLayout>
signUpForm class (only pasting the oncreate method, other things aren't needed):
package com.example.joshumberto.workcon;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class signUpForm extends Fragment implements View.OnClickListener {
EditText correo;
EditText telefono;
Button continuar;
public signUpForm(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setHasOptionsMenu(true);
}
public View OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Toast.makeText(
getActivity(),
"i am supposued to be created!!",
Toast.LENGTH_LONG).show();
View v = inflater.inflate(R.layout.fragment_sign_up_form, container, false);
correo = (EditText) v.findViewById(R.id.txt_usuario_correo);
telefono = (EditText) v.findViewById(R.id.txt_usuario_telefono);
continuar = (Button) v.findViewById(R.id.btn_registro_continuar);
continuar.setOnClickListener(this);
return v;
}
fragment_sign_up_form.xml (just button,textview and edit texts to inflate activity)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/colorSignUpBackground"
tools:context=".signUp"
android:id="#+id/sign_up_form" >
<!--<include android:id="#+id/toolbar"
layout="#layout/toolbar" />-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registro"
android:fontFamily="sans-serif-smallcaps"
android:textSize="#dimen/titulo_txt"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/margin_top_standar" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_usuario_correo"
android:layout_marginTop="#dimen/margin_top_login_btn"
android:hint="Correo"
android:gravity="center"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_usuario_contrasena"
android:layout_marginTop="#dimen/margin_top_login_btn"
android:hint="Contrasena"
android:gravity="center"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_usuario_telefono"
android:layout_marginTop="#dimen/margin_top_login_btn"
android:hint="Telefono"
android:gravity="center"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continuar"
android:id="#+id/btn_registro_continuar"
android:layout_gravity="center_horizontal" />
<!--<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/miLista"
></ListView>-->
Ideas? Thanks.
You should try replace() method instead of add(). like this.
getSupportFragmentManager().beginTransaction().replace(R.id.sign_up_container, new signUpForm(), "signUpForm").commit();

Unable to add a button in a seperate layout to the current view

Please have a look at the following code
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: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=".MainActivity" >
<TableLayout
android:id="#+id/gameBoard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></TableLayout>
</RelativeLayout>
game_buttons.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/buttonLayout" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="18dp"
android:text="Button" />
</RelativeLayout>
MainActivity.java
package com.ace.ticktacktoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends Activity {
private LayoutInflater inflater;
private TableLayout gameBoard;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createUI();
//Create the user interface
}
#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;
}
private void createUI()
{
gameBoard = (TableLayout)findViewById(R.id.gameBoard);
inflater = getLayoutInflater();
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.game_buttons, null);
//Add the buttons
TableRow tableRow = new TableRow(this);
Button btn = (Button)layout.findViewById(R.id.button1);
((RelativeLayout)btn.getParent()).removeView(btn);
tableRow.addView(btn);
gameBoard.addView(tableRow);
}
}
When I run this code, the added button should be displayed. But, my display is totally blank. What am I doing wrong here?
((RelativeLayout)btn.getParent()).removeView(btn);
tableRow.addView(btn);
change to
tableRow.addView(layout);
or change game_buttons.xml to contain ONLY the button without the layout and use the inflater to create buttons without wrapping layout.
Try moving you Button to just 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: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=".MainActivity" >
<TableLayout
android:id="#+id/gameBoard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></TableLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="18dp"
android:text="Button" />
</RelativeLayout>

Categories