As they look in the picture: http://es.tinypic.com/r/mug8b8/8
In the month of having a single column, have several with botons
The genre buttons dynamically by a query
The function that generates the buttons:
private void createEmpresas() {
Button b;
JSONObject json = null;
int count = 0;
LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
JSONObject jobj = obj_sqlite.get_descripcion_empresas();
try {
count = Integer.parseInt(jobj.getString("cont"));
json = new JSONObject(jobj.getString("json"));
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
for (int x = 1; x <= count; x++) {
try {
JSONObject json_row = new JSONObject(json.getString("row" + x));
b = new Button(this);
b.setText(json_row.getString("descripcion"));
b.setId(json_row.getInt("empresa"));
b.setTextSize(10);
b.setPadding(8, 3, 8, 3);
b.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(b);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Yipee.." +v.getId(), Toast.LENGTH_SHORT)
.show();
}
});
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
}
}
my xml is:
<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="#color/bg_main"
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.php_mysql_sqlite.MainActivity" >
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button6"
android:layout_alignBottom="#+id/button6"
android:layout_alignParentRight="true"
android:layout_marginRight="17dp"
android:background="#null"
android:onClick="loginOut"
android:text="#string/btLogOut"
android:textColor="#color/Red" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_centerHorizontal="true"
android:onClick="syncSqliteMysql"
android:text="#string/syn" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="borrarTabla"
android:text="#string/delTable" />
<ScrollView
android:layout_width="285dp"
android:layout_height="330dp"
android:layout_marginTop="1dp" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="#+id/button5"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button4"
android:background="#color/White"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Thanks for the help
PS: If you give me negative points, leave a comment of reason, as has happened before and not why
here are some answers that may help
1-
In android how do I add buttons into a TableLayout from an array of buttons programmatically?
2-
How do I programmatically add buttons into layout one by one in several lines?
3-
Add an array of buttons to a GridView in an Android application
Related
I have made an application in which I will be dynamically adding image and text to the application for this i am using
1.relative layout (main layout)
1.(a) linear layout ( a linear layout containing all the 1.(b) values)
1.(b) linear layout ( d layout which contains a image and a text) , each (1.(b)) layout contain one image and one text
now as I am running this app on set top box (with remote) with android, which is connected to smart tv,,,,, so i have to control my app with a remote control provided with the set top box.
so what I want is when the application loads , the focus will be on the first ((1.(b) linear layout)) i.e. on the first pair of image and text.
already done:- I have used requestFocus() but of no use
any help will be greatly appreciated thanks in advance cheers !!!!!!!
the code snippet is as follows:-
activity
LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
75, 98);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.setMargins(0, 25, 0, 0);
RelativeLayout.LayoutParams paramrelative = new RelativeLayout.LayoutParams(
170, 158);
paramrelative.setMargins(2, 0, 0, 0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(180,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(2,0, 0, 0);
//Create four
for(int j= 0;j<=count-1;j++)
{
// Create LinearLayout
ScrollView sv = new ScrollView(this);
final RelativeLayout ll = new RelativeLayout(this);
ll.setBackgroundResource(R.drawable.app_bg);
ll.setLayoutParams(paramrelative);
ll.setFocusableInTouchMode(true);
ll.setClickable(true);
// final String url= separated[j+1];
// Create Button
final Button img = new Button(this);
final TextView tv = new TextView(this);
// Give button an ID
img.setId(j+1);
int s=img.getId();
p.addRule(RelativeLayout.BELOW, s);
tv.setLayoutParams(p);
tv.setGravity(Gravity.CENTER);
//File sdCardRoot = Environment.getExternalStorageDirectory();
// File sdcardPath = new File(Environment.getExternalStorageDirectory()
// .getPath() + "/CATEGORIES");
String filename=finalname[j];
File myDir = new File(sdcardPath,filename);
// File myDir = new File(sdcardPath, "CATEGORIES_hotelinfo.png");
String m1 = myDir.getPath();
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(m1);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
img.setBackgroundDrawable(Drawable.createFromPath(m1.toString()));
//2ndaugust2014 tv.setText(finalname[j]);
int s1= finalname[j].indexOf(".");
final String finalname1=finalname[j].substring(0,s1);
tv.setText(finalname1);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String newPath=newPath1+finalname1;
File sdcardPath = new File(Environment.getExternalStorageDirectory()
.getPath() +newPath);
new CountDownTimer(2000,1000) {
public void onTick(long millisUntilFinished) {
ll.setBackgroundResource(R.drawable.hover_btn_d); //This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
ll.setBackgroundResource(R.drawable.app_bg);
}
}.start();
// if the directory does not exist, create it
if (!sdcardPath.exists())
{
Toast.makeText(getApplicationContext(),
"redirecting to www."+finalname1+".com",
Toast.LENGTH_SHORT).show();
}
else
{ Intent intent = new Intent(Second60grid.this,Second60grid.class);
// startActivity(intent);
// i.setClassName(packageName, activitylast);
Toast.makeText(getApplicationContext(),
"name" + finalname1,
Toast.LENGTH_SHORT).show();
intent.putExtra("nameoffile",newPath+"/");
startActivity(intent);
}
}
});
// set the layoutParams on the button
img.setLayoutParams(params);
//Add button to LinearLayout
// setContentView(sv);
ll.addView(img);
ll.addView(tv);
if(j==0)
{
ll.requestFocus();
}
//Add button to LinearLayout defined in XML
lm.addView(ll);
}
and the xml file is as follows:-----
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="200dip"
android:id="#+id/rl1"
>
<TextView
android:id="#+id/TEXT0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="CATEGORIES" />
<LinearLayout
android:id="#+id/linearMain1"
android:layout_below="#+id/TEXT0"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000">
</LinearLayout>
<LinearLayout
android:id="#+id/linearMain2"
android:layout_below="#+id/linearMain1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000">
</LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain2"
android:orientation="horizontal"
android:id="#+id/linearMain3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain3"
android:orientation="horizontal"
android:id="#+id/linearMain4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
> </LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain4"
android:orientation="horizontal"
android:id="#+id/linearMain5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="1400dp"
android:layout_height="70dp"
android:background="#80ffffff"
android:layout_alignParentBottom="true"
android:layout_below="#+id/rl1"
android:id="#+id/rl2"
>
<ImageView
android:id="#+id/remote_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/remote_btn"
android:layout_alignParentBottom="true" />
<TextView
android:id="#+id/TEXT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/remote_btn2"
android:layout_toRightOf="#+id/remote_btn"
android:text="MOVE" />
<ImageView
android:id="#+id/remote_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/TEXT1"
android:adjustViewBounds="true"
android:src="#drawable/remote_btn2" />
<TextView
android:id="#+id/TEXT2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TEXT1"
android:layout_toRightOf="#+id/remote_btn2"
android:text="SELECT" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl3"
android:layout_width="wrap_content"
android:layout_height="800dip"
android:background="#10ffffff"
android:layout_marginLeft="10dip"
android:layout_marginBottom = "10dp"
android:layout_above="#+id/rl2"
>
<Button
android:id="#+id/buttoniptv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:text="iptv"
android:background="#FF0000"/>
<Button
android:id="#+id/buttontvchannels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttoniptv"
android:background="#FF0000"
android:text="tvchannels" />
<Button
android:id="#+id/buttoninternet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttontvchannels"
android:background="#FF0000"
android:text="internet" />
</RelativeLayout>
</RelativeLayout>
You are currently adding view in ascending order
parentView.addView(child, 0);
Add it in Descending Order:
parentView.addView(child, parentView.getChildCount() - 1);
In my project, im going to make a multiple choice type question. I can select the questions and answer from mysql, however, i cant do the "matching" of the answer between input answer and the data from mysql. I tried some solutions but they doesnt work as well. I need a big help on it. Below are my codes.
the java class that make multiple choice
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normalmode);
new DataAsyncTask().execute();
}
public void onClick(View v){
inputtext = ((Button) v).getText().toString();
tv3 = (TextView)findViewById(R.id.textView3);
tv3.setText(inputtext);
if(inputtext == tv2.getText().toString()){
playerscore +=5;
} else {
playerscore +=1;
}
score.setText("Scores : " + playerscore);
new DataAsyncTask().execute();
}
class DataAsyncTask extends AsyncTask<String, String, String>{
#Override
protected String doInBackground(String... param) {
String result = DBConnector.executeQuery("SELECT * FROM questions ORDER BY RAND( ) LIMIT 1");
return result;
}
#Override
protected void onPostExecute(String result) {
question = (TextView)findViewById(R.id.textView_question);
fchoice = (Button)findViewById(R.id.Btn_choice1);
schoice = (Button)findViewById(R.id.Btn_choice2);
tchoice = (Button)findViewById(R.id.Btn_choice3);
tv2 = (TextView)findViewById(R.id.textView2);
score = (TextView)findViewById(R.id.textView_score);
try {
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonData = jsonArray.getJSONObject(0);
question.setText(jsonData.getString("q_desc"));
fchoice.setText(jsonData.getString("fchoice"));
schoice.setText(jsonData.getString("schoice"));
tchoice.setText(jsonData.getString("tchoice"));
ans = jsonData.getString("q_ans");
tv2.setText(ans);
} catch(Exception e) {
Log.e("log_tag", e.toString());
}
}
}
}
three buttons are sharing same onClick in xml file by using
android:onClick="onClick"
the current problem i faced is that it always return "false" no matter i pressed which button. also, is there any way to store/pass "previous" async task data?
I have tried using internal storage but it doesnt work as well
EDIT:
my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/player_score" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/string_question"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/Btn_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/third_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_submit"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/second_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_choice2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/first_choice"
android:onClick="onClick" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
</RelativeLayout>
</LinearLayout
textview2 and textview3 are used to test my output and display them as text
try this if(inputtext.equals(tv2.getText().toString())) instead of if(inputtext == tv2.getText().toString())
becasue the == operator checks to see if the two strings are exactly the same object.
The .equals() method will check if the two strings have the same value.
I need to Create RadioButton Dynamically in a Radio Group, radio Group is already defined in the XML, after I made my research I found some topics to put the android:drawableRight="#android:drawable/btn_radio" and android:button="#null" in the XML how can I do them programmatically?
Here is my code :
final RadioGroup RG =(RadioGroup) vi.findViewById(R.id.RG);
RG.removeAllViews();
String[] answers= null;
answers=item.Answers.split(";");
final TextView txtTitle = (TextView) vi.findViewById(R.id.QuestionRow);
txtTitle.setText(item.Question);
for (int i=0;i<answers.length;i++){
RadioButton bt=null;
bt = new RadioButton(parent.getContext());
bt.setId(i+1);
bt.setText(answers[i]);
bt.setGravity(Gravity.RIGHT);
RG.addView(bt);
}
And here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Questionlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#color/White" >
<TextView
android:id="#+id/QuestionRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right"
android:textColor="#color/black_overlay"
android:textSize="30sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/RG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below="#+id/QuestionRow"
android:layout_alignRight="#+id/QuestionRow"
></RadioGroup>
<Button
android:id="#+id/Vote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/RG"
android:text="Vote" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/RG"
android:text="See Statistics" />
</RelativeLayout>
protected void onPostExecute(HashMap<String,String> mylist) {
// TODO Auto-generated method stub
super.onPostExecute(mylist);
for(Integer z=0;z<mRG.getChildCount();z++){
View o =mRG.getChildAt(z);
((RadioButton)o).setText(text[z]);
}
for(Integer z =0;z< mRG.getChildCount();z++){
Set set = mylist.entrySet();
Iterator i = set.iterator();
View o = mRG.getChildAt(z);
NumberFormat formatter = new DecimalFormat("#0.00");
if (o instanceof RadioButton){
int counted[] = new int[ mRG.getChildCount()];
int j=0;
while(i.hasNext()) {
enter code here
Map.Entry me = (Map.Entry)i.next();
String Value=String.valueOf(formatter.format(Double.valueOf(me.getValue().toString())*100/count))+"%";
if (Integer.valueOf(me.getKey().toString())==((RadioButton) o).getId())
{
((RadioButton)o).setText(text[z]+" "+ Value);
}
}
}
}
progressDialog.hide();
}
I am using Vuforia AR sdk and want to create a button on the camera preview on the screen.
I cannot figure out where and how to add the button.
I have edit the camera_overlay_udt.xml like this.. In my layout design i have placed back button and listview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/header"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:src="#drawable/back" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipart"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/arcstarButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:src="#drawable/star_button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/favListingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ListView
android:id="#+id/favlist"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="7dp"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/overlay_bottom_bar_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1" >
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/overlay_bottom_bar_separators" />
<ImageButton
android:id="#+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_desc_camera_button"
android:onClick="onCameraClick"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/camera_button_background" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/bottom_bar"
android:background="#color/overlay_bottom_bar_separators" />
</RelativeLayout>
after that please Edit that ImageTargets.java class
private void addOverlayView(boolean initLayout) {
// Inflates the Overlay Layout to be displayed above the Camera View
LayoutInflater inflater = LayoutInflater.from(this);
mUILayouts = (RelativeLayout) inflater.inflate(
R.layout.camera_overlay_udt, null, false);
mUILayouts.setVisibility(View.VISIBLE);
// If this is the first time that the application runs then the
// uiLayout background is set to BLACK color, will be set to
// transparent once the SDK is initialized and camera ready to draw
if (initLayout) {
mUILayouts.setBackgroundColor(Color.TRANSPARENT);
}
// Adds the inflated layout to the view
addContentView(mUILayouts, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Gets a reference to the bottom navigation bar
mBottomBar = mUILayouts.findViewById(R.id.bottom_bar);
// Gets a reference to the Camera button
mCameraButton = mUILayouts.findViewById(R.id.camera_button);
mCameraButton.setVisibility(View.GONE);
favButton = (ImageButton) mUILayouts.findViewById(R.id.arcstarButton);
listview = (ListView) mUILayouts.findViewById(R.id.favlist);
backButton = (ImageButton) mUILayouts.findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
finish();
}
});
listview.setVisibility(View.GONE);
galleryList = SendFile.getFavourites();
if (galleryList != null) {
gridviewAdapter = new GridviewAdapter(ImageTargets.this);
listview.setAdapter(gridviewAdapter);
}
favButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (galleryList != null && galleryList.size() > 0) {
if (listview.getVisibility() == View.GONE) {
listview.setVisibility(View.VISIBLE);
} else {
listview.setVisibility(View.GONE);
}
} else {
Toast.makeText(ImageTargets.this, "Favourites not fond",
Toast.LENGTH_LONG).show();
}
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int positon, long paramLong) {
SendFile.setFavourite(galleryList.get(positon));
Intent intent = new Intent(ImageTargets.this,
LoadingScreen.class);
Bundle bundle = new Bundle();
bundle.putInt("x", x_Axis);
bundle.putInt("y", y_Axis);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
showDialogHandler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Server Response: " + aResponse, Toast.LENGTH_SHORT)
.show();
showAlertDialog(aResponse);
} else {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Not Got Response From Server.", Toast.LENGTH_SHORT)
.show();
}
};
};
loadingDialogHandler.captureButtonContainer = mUILayouts
.findViewById(R.id.camera_button);
mUILayouts.bringToFront();
}
They showing there layouts using handlers
Start you camera preview in a normal way. Place a layout on top of it with transparent background like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<ImageView
android:id="#+id/start_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="#drawable/scan_image"/>
</RelativeLayout>
In java file, you can add this layout like this:
private View mStartupView;
mStartupView = getLayoutInflater().inflate(
R.layout.startup_screen, null);
// Add it to the content view:
addContentView(mStartupView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
This way you will get to see your button on top of camera preview. Hope it helps
You can add buttons in cameraoverlay layout which is in layout folder and you can initialize buttons in initAR function which is in mainactivity.
Step 1: Add the button in the camera_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
style="#android:style/Widget.ProgressBar"
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<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="18dp"
android:layout_marginTop="51dp"
android:text="Button" />
</RelativeLayout>
Step 2: Edit the ImageTargets.java class
private static final String LOGTAG = "ImageTargets";
private Button b1;
Step 3: Modify the initApplicationAR() function of ImageTargets.java class
private void initApplicationAR()
{
// Create OpenGL ES view:
int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();
mGlView = new SampleApplicationGLView(this);
mGlView.init(translucent, depthSize, stencilSize);
mRenderer = new ImageTargetRenderer(this, vuforiaAppSession);
mRenderer.setTextures(mTextures);
mGlView.setRenderer(mRenderer);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setVisibility(View.GONE);
}
});
}
Now lay back and watch your button disappear on a click!
Although it's a long time since the post.. yet I found one article.. wherein you can have the desired thing..
Ref: https://medium.com/nosort/adding-views-on-top-of-unityplayer-in-unityplayeractivity-e76240799c82
Solution:
Step1: Make a custom layout XML file (vuforia_widget_screen.xml). For example, button has been added.
<?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/main_layout">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:text="#string/welcome" />
</FrameLayout>
Step 2: Make following changes in the UnityPlayerActivity.java
Replace "setContentView(mUnityPlayer);" with
setContentView(R.layout.vuforia_widget_screen);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
-> For anyone, who will face the issue in future. :)
help me solve my problem. There is a main layout RelativeLayout it is Linearlayout where I want to programmatically add view. Here is my code.
public class GenBarCodeActivity extends Activity {
BarCodeView Barview = null;
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
RelativeLayout rl;
Button getBarCode;
private static final int ID = 34646456;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
getBarCode = (Button)findViewById(R.id.btnGetBarCode);
getBarCode.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ll = (LinearLayout)findViewById(R.id.linearCodeView);
EANTEXT = edBarText.getText().toString();
ImageView old = (ImageView) ll.findViewById(ID);
if (old != null) {
((LinearLayout) old.getParent()).removeViewInLayout(old);
}
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(Barview, layoutParams);
rl.invalidate();
}
});
fillLinear();
setBarCode();
}
private void fillLinear(){
rl = (RelativeLayout)findViewById(R.id.layout_barcode);
ll = (LinearLayout)findViewById(R.id.linearCodeView);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imBar = new ImageView(this);
Bitmap imbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.barcode);
imBar.setLayoutParams(layoutParams);
imBar.setImageBitmap(imbitmap);
imBar.setId(ID);
ll.setBackgroundColor(Color.TRANSPARENT);
ll.addView(imBar);
}
private void setBarCode(){
Barview = new BarCodeView(GenBarCodeActivity.this);
Barview.setBarCodeNum(0);
}
}
This is my View
public class BarCodeView extends View {
private static int numcode = 0;
private static String codedata = null;
public BarCodeView(Context context) {
super(context);
}
public void setBarCodeText(String text){
this.codedata = text;
}
public String getBarCodeText(){
return this.codedata;
}
public void setBarCodeNum(int num){
this.numcode = num;
}
public int getBarCodeNum(){
return this.numcode;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (numcode) {
// ean 13
case 0:
try{
showEAN13(canvas);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private static void showEAN13(Canvas canvas) throws Exception {
EAN13 barcode = new EAN13();
/*
EAN 13 Valid data char set:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits)
EAN 13 Valid data length: 12 digits only, excluding the last checksum digit
*/
barcode.setData(codedata);
// for EAN13 with supplement data (2 or 5 digits)
/*
barcode.setSupData("12");
// supplement bar height vs bar height ratio
barcode.setSupHeight(0.8f);
// space between barcode and supplement barcode (in pixel)
barcode.setSupSpace(15);
*/
// Unit of Measure, pixel, cm, or inch
barcode.setUom(IBarcode.UOM_PIXEL);
// barcode bar module width (X) in pixel
barcode.setX(2f);
// barcode bar module height (Y) in pixel
barcode.setY(90f);
// barcode image margins
barcode.setLeftMargin(10f);
barcode.setRightMargin(10f);
barcode.setTopMargin(10f);
barcode.setBottomMargin(10f);
// barcode image resolution in dpi
barcode.setResolution(72);
// disply barcode encoding data below the barcode
barcode.setShowText(true);
// barcode encoding data font style
barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 10));
// space between barcode and barcode encoding data
barcode.setTextMargin(6);
barcode.setTextColor(AndroidColor.black);
// barcode bar color and background color in Android device
barcode.setForeColor(AndroidColor.black);
barcode.setBackColor(AndroidColor.white);
/*
specify your barcode drawing area
*/
RectF bounds = new RectF(120, 120, 0, 0);
barcode.drawBarcode(canvas, bounds);
}
}
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_barcode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0e0a2"
android:orientation="vertical" >
<ImageView
android:id="#+id/closeBarCode"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/txtInfo"
android:layout_marginRight="3dp"
android:src="#drawable/delete" />
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="#string/barcode_title"
android:textColor="#000"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/closeBarCode" >
<TextView
android:id="#+id/txtEAN"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_ean"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spEAN"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtInfo"
android:layout_below="#+id/linearLayout1" >
<TextView
android:id="#+id/txtCodeColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_below="#+id/linearLayout2" >
<TextView
android:id="#+id/txtCodeBackColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_back_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeBackColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout3" >
<TextView
android:id="#+id/txtCodePosition"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_position"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodePosition"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearCodeView"
android:layout_width="300dip"
android:layout_height="200dip"
android:layout_below="#+id/edBarCode"
android:layout_marginRight="45dp"
android:layout_marginTop="25dip"
android:layout_toLeftOf="#+id/closeBarCode"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/btnSetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnGetBarCode"
android:layout_alignBottom="#+id/btnGetBarCode"
android:layout_toLeftOf="#+id/closeBarCode"
android:text="#string/set_code" />
<Button
android:id="#+id/btnGetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="#string/get_code" />
<EditText
android:id="#+id/edBarCode"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearCodeView"
android:layout_alignRight="#+id/linearCodeView"
android:layout_below="#+id/txtCodeText"
android:layout_marginTop="23dp"
android:ems="10" />
<TextView
android:id="#+id/txtCodeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/edBarCode"
android:layout_below="#+id/txtInfo"
android:layout_marginRight="86dp"
android:text="#string/code_text"
android:textColor="#000"
android:textSize="20dip" />
</RelativeLayout>
I need to Linearlayout "#+id/linearCodeView" insert my BarCodeView.
View is not visible after adding LinearLayout.
RelativeLayout.LayoutParams paramsBottom = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(ll, paramsBottom); //add linearlayout in relativelayout
Edit:-
LinearLayout.LayoutParams paramsBottom2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(Barview, paramsBottom2);//add Barview in linearlayout
OR
RelativeLayout.LayoutParams paramsBottom2 = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(Barview, paramsBottom2);//add Barview in relativelayout