Generating QR Code on different activity in android - android

i have been trying to create a QR Code generator that generates the QR Code on a separate activity on button click. Below is my code but it crushes on button click. Where did i go wrong?
First class is MainActivity.class:
package com.example.profmox.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;
public class MainActivity extends AppCompatActivity {
TextView tx1;
Button btn1, btn2;
EditText edt1,edt2;
CheckBox ch;
String text2qr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = (EditText)findViewById(R.id.edit1);
edt2 = (EditText)findViewById(R.id.edit2);
btn1 = (Button)findViewById(R.id.login);
btn2 = (Button)findViewById(R.id.signup);
ch = (CheckBox)findViewById(R.id.check);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("EditText", edt1.getText().toString());
Log.v("EditText", edt2.getText().toString());
// text2qr = edt1.getText().toString().trim();
// MultiFormatWriter mfw = new MultiFormatWriter();
// try{
// BitMatrix btm = mfw.encode(text2qr, BarcodeFormat.QR_CODE,250,250);
// BarcodeEncoder ben = new BarcodeEncoder();
// Bitmap bmap = ben.createBitmap(btm);
// UserScreen.qr.setImageBitmap(bmap);
// }catch (WriterException e){
// e.printStackTrace();
// }
startActivity(new Intent(MainActivity.this, UserScreen.class));
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SignUp.class));
}
});
}
}
The layout file for the MainActivity class:
<?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"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/art_background"
tools:context="com.example.profmox.myapplication.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="300dp"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:padding="16dp"
android:layout_marginTop="20dp"
android:src="#mipmap/logo"/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Username"
android:textColorHint="#fff"
android:maxLines="1"
android:drawableLeft="#drawable/girl"
android:layout_marginTop="70dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Password"
android:textColorHint="#fff"
android:maxLines="1"
android:layout_marginTop="4dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#fff"
android:text="Remember Me"
android:padding="9dp"
/>
<Button
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/login_art"
android:text="Login"
android:textColor="#3D405B"
android:textAllCaps="false"
android:padding="16dp"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_marginTop="23dp"
android:textSize="18sp"/>
<Button
android:id="#+id/signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/signuo_art"
android:text="Sign up"
android:textColor="#fff"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:textAllCaps="false"
android:textSize="18sp"
android:layout_marginTop="16dp"
android:clickable="true"
android:padding="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot your password?"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body2"
android:padding="16dp"
android:layout_marginBottom="12dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
The UserScreen.class is where i want to generate the QR Code in:
package com.example.profmox.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class UserScreen extends AppCompatActivity {
static ImageView qr;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_screen);
qr = (ImageView)findViewById(R.id.qrCode);
}
}
The layout file for the UserScreen.class:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/art_background"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.profmox.myapplication.UserScreen">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/qrCode"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

Related

RecyclerView click on item, take some data then go to another activity

I want to take the reference when i click on an item in this recyclerview then go to another activity with this reference there is override functions in listview but recylclerview didn't have the same function
card.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="119dp"
android:layout_gravity="center"
android:elevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="1dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="112dp">
<TextView
android:text="ID Article : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_idArticle1"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#android:color/black" />
<TextView
android:text="Reference : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_reference1"
android:textStyle="normal|bold"
android:layout_above="#+id/txt_des"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"
android:textColor="#android:color/background_dark" />
<TextView
android:text="Prix : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_des1"
android:textStyle="normal|bold"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="-51dp"
android:layout_below="#+id/txt_des"
android:textColor="#android:color/background_dark" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_des"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/txt_prix1"
android:layout_toEndOf="#+id/txt_prix1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_prix"
android:layout_alignBaseline="#+id/txt_des1"
android:layout_alignBottom="#+id/txt_des1"
android:layout_toRightOf="#+id/txt_des1"
android:layout_toEndOf="#+id/txt_des1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_idArticle"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/txt_idArticle1"
android:layout_toEndOf="#+id/txt_idArticle1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_reference"
android:layout_alignBaseline="#+id/txt_reference1"
android:layout_alignBottom="#+id/txt_reference1"
android:layout_toRightOf="#+id/txt_reference1"
android:layout_toEndOf="#+id/txt_reference1" />
<TextView
android:text="Designation : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_prix1"
android:textStyle="normal|bold"
android:textColor="#android:color/black"
android:layout_below="#+id/txt_reference1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView> </LinearLayout>
avticity_recherche_art.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"
xmlns:article_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_recherche_art"
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.bacha.pfe.activity.recherche_art">
<TextView
android:text="Recherche Article :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:textColor="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="rechercher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnrechArt"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/eTextRechArt" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/eTextRechArt"
android:layout_below="#+id/textView"
android:layout_toLeftOf="#+id/btnrechArt"
android:layout_toStartOf="#+id/btnrechArt" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
article_view:cardUseCompatPadding="true"
article_view:cardElevation="5dp"
article_view:cardCornerRadius="5dp"
android:scrollbars="vertical"
android:layout_marginTop="18dp"
android:layout_below="#+id/btnrechArt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
recherche_art.java:
package com.example.bacha.pfe.activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.bacha.pfe.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class recherche_art extends AppCompatActivity {
private RecyclerView recyclerView ;
private GridLayoutManager gridLayoutManager;
private ArticleAdapter adapter ;
private List<Article> data_list ;
private String recherche_article;
private Button btnrechArt ;
private EditText arech;
int d=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recherche_art);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
btnrechArt = (Button) findViewById(R.id.btnrechArt);
btnrechArt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arech= (EditText) findViewById(R.id.eTextRechArt);
data_list = new ArrayList<>();
recherche_article=arech.getText().toString();
load_article_from_server(0);
gridLayoutManager = new GridLayoutManager(recherche_art.this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new ArticleAdapter(recherche_art.this,data_list);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
d=data_list.size();
Log.d("PFE", String.valueOf(d));
load_article_from_server(d);
}
}
});
}
});
}
private void load_article_from_server(final int id) {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() {
#Override
protected Void doInBackground(Integer... Params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.print("End of content");
}
return null ;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}
You have to implement a listener in the holder that you are using in the ArticleAdapter.
Look this: RecyclerView onClick

OnItemClickListener doesn't work , OnItemClick is not called

I'm trying to make a Toast message whenever I click an item from the list, For some reason when I click the item nothing happens,
I hope you can help me, thanks.
ProductList.java
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProductList extends AppCompatActivity {
private List<myProductsView> myProducts_types = new ArrayList<myProductsView>();
ArrayAdapter<myProductsView> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_productlist);
//Button btnDel = (Button) findViewById(R.id.btnDel);
//adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice,list);
populateProductsList();
populateListView();
}
private void populateProductsList() {
myProducts_types.add(new myProductsView("aaa", "aaa", 1111, 12.90, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaaaaa ", 1112, 10.90, R.drawable.cereal, 2));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 30.00, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 20.00, R.drawable.cereal, 3));
}
private void populateListView() {
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.product_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("BLALBALBLALBLABLAL");
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
}
});
}
public void StartCalck(View view){
Intent intent = new Intent(ProductList.this, SplitBuying.class);
startActivity(intent);
}
public class MyListAdapter extends ArrayAdapter<myProductsView>{
public MyListAdapter(){
super(ProductList.this, R.layout.pop_productlist, myProducts_types);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//make sure we have a view to work with(may have been given null
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.product_item_view, parent, false);
}
//we need to populate the list
//find the product to work with
myProductsView currentProduct = myProducts_types.get(position);
//fill the view
CheckBox checkBox = (CheckBox)itemView.findViewById(R.id.product_checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ProductList.this,"BLA",Toast.LENGTH_SHORT);
}
});
TextView productname = (TextView) itemView.findViewById(R.id.product_name);
productname.setText(currentProduct.getProductName());
EditText quantity = (EditText)itemView.findViewById(R.id.edit_text);
quantity.setText(String.valueOf(currentProduct.getQuantity()));
return itemView;
}
}
}
product_item_view.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="7dp"
android:layout_weight="1">
<Button
android:id="#+id/btn_minus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="-" />
<EditText
android:id="#+id/edit_text"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:gravity="center"
android:focusable="false"
android:text="0" />
<Button
android:id="#+id/btn_plus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="+" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Text"
android:textSize="15dp"
android:textIsSelectable="true"
android:gravity="center"
android:id="#+id/product_name"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/product_checkBox"
android:layout_toStartOf="#+id/product_checkBox" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/product_checkBox" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
pop_productlist.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"
android:background="#drawable/ightwall"
android:id="#+id/drawerlayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="הרשימה שלי"
android:textAlignment="center"
android:textSize="30dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/product_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_below="#+id/ChooseStore"
>
</ListView>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/CalckButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calckButton"
android:onClick="StartCalck"
android:layout_gravity="center" />
<Button
android:id="#+id/btnDel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/lblBtnDel"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#D5D4D4"
android:layout_gravity="bottom">
<ImageView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_mylist"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/freeadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_freetext"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/favproductadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_favproduct"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
You have to call .show() on your toast.
change
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
to
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT).show();

How to switch from simple activity to drawer activity?

I am new to android. I want to know how to switch from a simple activity to drawer activity. I read that there are 2 ways to do it.
The first is Intent i =new Intent("com.example.signin");
the second is Intent i =new Intent(MainActivity.this,signin.class);
I tried both. But in the 1st case, my layout which contains toolbar, drawer and the button inside is appearing perfect. But buttons functionality is not working. in 2nd case, buttons functionality is working but layout is disturbed.
Can anyone tell me solution for this problem?I'm using Android Studio.
here is my code:
MainActivity
package com.example.asus1.tlogin;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button signInBtn;
private Button signUpBtn;
private Button submit;
private EditText mUsername, mPassword;
private Layout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signInBtn = (Button) findViewById(R.id.signin);
signUpBtn = (Button) findViewById(R.id.signup);
submit = (Button) findViewById(R.id.submit);
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.password);
//String userName = mUsername.getText().toString();
// String password = mPassword.getText().toString();
signUpBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.asus1.tlogin.SignUpActivity");
startActivity(intent);
}
});
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mUsername.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter username..", Toast.LENGTH_SHORT).show();
mPassword.setText("");
mUsername.setText("");
} else if (mPassword.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter password..", Toast.LENGTH_SHORT).show();
mPassword.setText("");
mUsername.setText("");
} else if (mUsername.getText().toString().equals("gaurav") && mPassword.getText().toString().equals("gaurav")) {
Toast.makeText(getApplicationContext(), "Login Successful..", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,ContentDrawer.class);
startActivity(intent);
setContentView(R.layout.content_drawer);
} else {
Toast.makeText(getApplicationContext(), "Wrong username or password..", Toast.LENGTH_LONG).show();
mPassword.setText("");
mUsername.setText("");
}
}
});
}
}
ContentDrawer.java:
package com.example.asus1.tlogin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
public class ContentDrawer extends AppCompatActivity {
private Button createBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_drawer);
//Toolbar toolbar = (Toolbar)findViewById(R.id.draw);
createBtn = (Button)findViewById(R.id.createBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(getApplicationContext(),CreateGroup.class);
startActivity(intent1);
}
});
}
}
ContentDrawer.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="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.asus1.tlogin.DrawerActivity"
tools:showIn="#layout/app_bar_drawer"
android:orientation="vertical"
android:weightSum="1"
android:background="#ffffff">
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Create your personal profile"
android:background="#d31313"
android:textColor="#ffffff"
android:textAllCaps="false"
android:id="#+id/createBtn"
android:clickable="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal"
android:background="#f29494"
android:clickable="true"
android:id="#+id/linear">
<Button
android:layout_width="180dp"
android:layout_height="match_parent"
android:text="Join group"
android:id="#+id/joinGroup"
android:background="#984848"
android:textAllCaps="false"
android:clickable="true" />
<Button
android:layout_width="195dp"
android:layout_height="match_parent"
android:text="Setup a group"
android:id="#+id/button2"
android:background="#4e7db2"
android:textAllCaps="false"
android:clickable="true" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Setup an eventk"
android:id="#+id/eventBtn"
android:background="#e7ed21"
android:textAllCaps="false"
android:clickable="true" />
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Notifications"
android:id="#+id/button4"
android:textAllCaps="false"
android:background="#8b9ddb" />
</LinearLayout>
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:com.facebook.widget.LoginButtonandroid="http://schemas.android.com/apk/res-auto"
tools:context="com.example.asus1.tlogin.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="#drawable/roomates1"
android:id="#+id/imageView" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:id="#+id/linearLayout">
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/signup"
android:background="#0000"
android:text="SIGN UP"
android:textAlignment="gravity" />
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:id="#+id/signin"
android:layout_alignBottom="#+id/imageView"
android:layout_toRightOf="#+id/signup"
android:layout_toEndOf="#+id/signup"
android:background="#0000"
android:text=" SIGN IN"
android:textAlignment="textStart"
android:textColor="#fefefe"
android:textStyle="bold" />
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<EditText
android:layout_width="272dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/username"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:hint="Username"
android:textAlignment="center" />
<EditText
android:layout_width="274dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/password"
android:layout_gravity="center_horizontal"
android:hint="Password"
android:textAlignment="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIGN IN"
android:id="#+id/submit"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#drawable/roundbutton"/>
</LinearLayout>
</RelativeLayout>
layout is disturbed
I'm assuming what you mean by that is that you are completely replacing the layout of the MainActivity?
That is fixable by just not doing so.
Intent intent = new Intent(MainActivity.this,ContentDrawer.class);
startActivity(intent);
// Remove this line
/* setContentView(R.layout.content_drawer); */
Other than that, it seems you should be able to start the other Activity just fine.

Not-clipped childview of RelativeLayout is not clickable

I have the following activity. It is an simplification of a draggable map. The problem is that mytext2 is not clickable, even if it is visible. Can anybody tell me how to make it clickable? And for some reason the inner-RelativeLayout gets not bigger than screen size, even when i set this high dp.
<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:background="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="400dp"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>
class
package com.example.relativatest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.mytext1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!1");
}
});
findViewById(R.id.mytext2).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!2");
}
});
}
}
Use this code.
<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:background="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:focusable="true"
android:padding="20dp"
android:translationX="400dp"
android:clickable="true"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:clickable="true"
android:padding="20dp"
android:focusable="true"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>

Query on a Text view

I want to display the following output:
Name : Sangeetha
Usn : 4AL09IS025
Address : Anderi West, Mubai.
Name and usn are displaying properly. But my Address is not displaying. What's the problem. Here's my code. Can anyone tell me what's the problem?MainActivity.java is the my main activity class.
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button btn;
EditText etext1;
EditText etext2;
EditText etext3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext1 = (EditText) findViewById(R.id.edit_name);
etext2 = (EditText) findViewById(R.id.edit_usn);
etext3 = (EditText) findViewById(R.id.edit_add);
btn = (Button) findViewById(R.id.submit);
/** Called when the user clicks the Submit button */
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, Details.class);
intent.putExtra("name", etext1.getText().toString());
intent.putExtra("usn", etext2.getText().toString());
intent.putExtra("address", etext3.getText().toString());
startActivity(intent);
}
}
Display.java is my Display activity to receive the intent sent by Main Activity.java
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class Details extends ActionBarActivity {
TextView text1;
TextView text2;
TextView text3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
text1=(TextView)findViewById(R.id.new_name);
text2=(TextView)findViewById(R.id.new_usn);
text3=(TextView)findViewById(R.id.new_add);
Intent intent=getIntent();
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("add");
text1.setText(Name);
text2.setText(Usn);
text3.setText(Address);
}
}
Two layout files. First one is for the first activity(input) and 2nd one is for 2nd activity(output).main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="20.3dp"
android:text="#string/name" />
<EditText
android:id="#+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_name" />
<TextView
android:id="#+id/usn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="30dp"
android:paddingLeft="32dp"
android:text="#string/usn" />
<EditText
android:id="#+id/edit_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_name"
android:layout_marginLeft="85dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_usn" />
<TextView
android:id="#+id/add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="40dp"
android:text="#string/add" />
<EditText
android:id="#+id/edit_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_usn"
android:layout_marginLeft="85dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_add" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_centerHorizontal="true"
android:text="#string/submit"/>
</RelativeLayout>
activity_details.java's layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="20.3dp"
android:text="#string/name"
android:textStyle="bold" />
<TextView
android:id="#+id/new_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/usn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="20dp"
android:paddingLeft="32dp"
android:text="#string/usn"
android:textStyle="bold" />
<TextView
android:id="#+id/new_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/new_name"
android:layout_marginLeft="85dp"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="30dp"
android:text="#string/add"
android:textStyle="bold" />
<TextView
android:id="#+id/new_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/new_usn"
android:layout_marginLeft="85dp"
android:layout_marginTop="30dp"
android:singleLine="false" />
</RelativeLayout>
try that ;
Define same string for address
Intent intent=getIntent();
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("address");
text1.setText(Name);
text2.setText(Usn);
text3.setText(Address);
See lines intent.putExtra("address", etext3.getText().toString()); and String Address=intent.getStringExtra("add"); You need to change the add to address to make it work

Categories

Resources