Mobile UAS
Mobile UAS
Mobile UAS
activity_main.xml
Ganti Constraint Layout ke Linear Layout
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:id="@+id/etPassword"/>
Setelah itu package > new > activity > empty activity >
MainActivity2.java > activity_main2.xml
activity_main2.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HellowWorld"
android:id="@+id/tvUsers"></TextView>
</LinearLayout>
MainActivity.java
package com.example.try_sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onClick(View v) {
if(v.getId()==R.id.btnLogin){
Buat arraylist untuk menyimpan data user
ArrayList<User> arrUsers = new ArrayList<>();
Validasi UserDAO
UserDAO userDao = new UserDAO();
userDao.insertUser(this, tempUserForReg);
Toast.makeText(this, "Insert berhasil",
Toast.LENGTH_SHORT).show();
}
String username;
String password;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
}
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
return results;
}
Fungsi Insert
public void insertUser(Context mCtx, User user){
Log.v("insertUser","1");
MySqliteHelper helper = new MySqliteHelper(mCtx);
Log.v("insertUser","2");
SQLiteDatabase db = helper.getWritableDatabase();
Log.v("insertUser","3");
ContentValues cv = new ContentValues();
Log.v("insertUser","4");
cv.put(helper.FIELD_MS_USER_USERNAME, user.username);
Log.v("insertUser","5");
cv.put(helper.FIELD_MS_USER_PASSWORD, user.password);
Log.v("insertUser","6");
db.insertWithOnConflict(helper.TABLE_MS_USER, null,
cv, SQLiteDatabase.CONFLICT_REPLACE);
Log.v("insertUser","7");
db.close();
Log.v("insertUser","8");
}
}
MainActivity2.java
package com.example.try_sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
TextView tvusers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tvusers = findViewById(R.id.tvUsers);
String results="";
for (int i =0;i<users.size();i++){
results = results+" "+users.get(i).username+" "+
users.get(i).password +" ||\n";
}
tvusers.setText(results);
}
}
Fragment
Note :
*Activity tidak bisa berada dalam satu activity lain
*Jadi saat kita menggunakan fragment, saat kita klik pada Tab Home,
maka Fragment dibawahnya akan berubah menjadi Fragment home, saat
kita klik Tab Product maka Fragment dibawahnya akan berubah menjadi
Fragment produk
Java
Main Activity
package com.example.tryfragmentbasic2023;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements
UpperFragment.OnFragmentInteractionListener{
UpperFragment upperFragment;
LowerFragment lowerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.upperContain
er, upperFragment).commit();
}
if (findViewById(R.id.lowerContainer) != null) {
if (savedInstanceState != null) {return;}
lowerFragment = new LowerFragment();
lowerFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.lowerContain
er, lowerFragment).commit();
}
}
@Override
public void onFragmentInteraction(String sentence) {
if(lowerFragment!=null)
lowerFragment.updateSentence(sentence);
}
Upper Fragment
package com.example.tryfragmentbasic2023;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.fragment.app.Fragment;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link UpperFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
*/
public class UpperFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_upper, container,
false);
btnPotato = (Button) rootView.findViewById(R.id.buttonPotato);
btnPotato.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPotato();
}
});
btnTomato = (Button) rootView.findViewById(R.id.buttonTomato);
btnTomato.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onTomato();
}
});
return rootView;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
Lower Fragment
package com.example.tryfragmentbasic2023;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.textservice.SentenceSuggestionsInfo;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
TextView tvLowerFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_lower, container,
false);
tvLowerFragment = (TextView)
rootView.findViewById(R.id.tvLowerFragment);
return rootView;
}
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/upperContainer"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/lowerContainer"/>
</LinearLayout>
UpperFragment 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.tryfragmentbasic2023.UpperFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Potato"
android:id="@+id/buttonPotato"
android:layout_gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Tomato"
android:id="@+id/buttonTomato"
android:layout_gravity="center" />
</LinearLayout>
</FrameLayout>
LowerFragment 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.tryfragmentbasic2023.LowerFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:id="@+id/tvLowerFragment"
android:textColor="@color/white"
android:text="You have choose a potato!"/>
</FrameLayout>