How to add buttons in ZXing Scanner View? - java

I already found this How to add buttons in Zxing Scanner Camera View
But it's doesn't work ...
I use a camera in a pop up window like this
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width =dm.widthPixels;
int height =dm.heightPixels;
getWindow().setLayout((int)(width*.9), (int)(height*.7));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.gravity = Gravity.TOP;
params.x =0;
params.y = -20;
getWindow().setAttributes(params);
mScannerView = (ZXingScannerView) findViewById(R.id.zxscan);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
And this is xml file
<RelativeLayout
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="#CFD8DC"
android:orientation="vertical"
tools:context="com.example.adisi.turzilnic.S1">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/zxscan"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/FlashOFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/flashoff"
android:layout_marginLeft="150dp"
android:layout_marginStart="150dp"
android:layout_marginTop="150dp"/>
<ImageButton
android:id="#+id/FlashON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/flashon"
android:layout_gravity="center"
android:layout_marginTop="50dp"/>
</me.dm7.barcodescanner.zxing.ZXingScannerView>
</RelativeLayout>
What is wrong here? I don't understand.. My buttons aren't shown on camera.. Should be from that pop up? I set the buttons with margintop and marginleft on the middle because I wanted to see if the positions it's the problem.. but the same problem ..

The size of your buttons is wrap_content and instead of setting the image source on the button you are setting the image on the background. The view relative sizes don't keep track of your view's background so you will most probably have a view with width/height set to 0.
Try to set the images using "src" instead.

Related

Android - view does not keep height

I'm trying to add a custom view to a linearlayout, but the view does not keep it's height. The height is 48dp so it's fixed but after I do addView() it change to wrap_content :
LinearLayout cmsList = (LinearLayout) mView.findViewById(R.id.ll_cms_sections);
View group = getActivity().getLayoutInflater().inflate(R.layout.item_cms_group, null);
cmsList.addView(group);
The group :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="48dp"
android:background="#color/clear_orange">
<com.dekibae.android.popinthecity.presentation.ui.widgets.FontText
android:id="#+id/tv_cms"
style="#style/TitleOrangexNormal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/margin_normal"
android:layout_marginStart="#dimen/margin_normal"
android:layout_toLeftOf="#+id/indicator"
android:gravity="center"
android:paddingTop="4dp"
android:text="TEST SECTION"
app:font="#string/plaak" />
<ImageView
android:id="#+id/indicator"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/margin_normal"
android:scaleType="centerInside"
android:src="#drawable/dropdown_arrow" />
</RelativeLayout>
The cmsList
<LinearLayout
android:id="#+id/ll_cms_sections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
I even tried to force it :
group.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 48));
Picking up on #Abtin Gramian's comment the constructor used takes as a dimension pixels, and not dp (density independent pixels)
From the android documentation
android.view.ViewGroup.LayoutParams public LayoutParams(int width,
int height)
Creates a new set of layout parameters with the specified width and height.
Parameters:
width - either WRAP_CONTENT, FILL_PARENT or a fixed size in pixels
height - either
WRAP_CONTENT, FILL_PARENT or a fixed size in pixels
So through the use of
public static float applyDimension (int unit, float value, DisplayMetrics metrics)
we can make a conversion from a dimension in one type of metrics like TypedValue.COMPLEX_UNIT_DIP to another, in this case one adequate for the current display.
group.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, context.getResources().getDisplayMetrics())));

Android dialog wrap_content not working

I have a custom dialog set up and I have the layout's width set to wrap_content but it is still taking the entire screen width. I even tried setting the width to 100dp and it still uses the whole screen Why does this happen?
I have set up the dialog like this:
public class DialogGoToLine extends Dialog
{
//The root view of the dialog
private static View rootView;
public DialogGoToLine(Context context, int themeResId){
super(context, themeResId);
}
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Allow the dialog to be canceled by tapping out of its bounds
setCancelable(true);
//Inflate the custom layout
LayoutInflater layoutInflater = LayoutInflater.from(HotspotHelper.getAppContext());
rootView = layoutInflater.inflate(R.layout.dialog_go_to_line, null);
//Set up the layout
//setTitle(HotspotHelper.getAppContext().getString(R.string.clientDetails));
//setTitle("Go to line");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(rootView);
final EditText editText = (EditText) rootView.findViewById(R.id.editTextGoToLine);
Button buttonGoToLine = (Button) rootView.findViewById(R.id.buttonGoToLine);
buttonGoToLine.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View p1)
{
int line = Integer.valueOf(editText.getText().toString()) - 1;
((LinearLayoutManager)LogActivity.recyclerViewLog.getLayoutManager()).scrollToPositionWithOffset(line, (int)HotspotHelper.dpToPx(30));
dismiss();
}
});
}
}
And the layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewGoToLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:labelFor="#+id/editTextGoToLine"
android:text="Go to line: "
android:background="#color/accent"
android:textColor="#color/primaryText"
android:textSize="18sp"/>
<EditText
android:id="#id/editTextGoToLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_margin="4dp"
android:layout_centerVertical="true"
android:inputType="number"
android:gravity="center"
android:textSize="18sp"/>
<Button
android:id="#+id/buttonGoToLine"
android:layout_width="wrap_content"
android:text="Go"
android:layout_margin="4dp"
android:textSize="18sp"
android:layout_height="wrap_content"/>
Edit 1
If it makes a difference, this dialog has a style in which I declared the background to this drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/colorPrimary"/>
<corners
android:radius="2dp" />
</shape>
Use have to use this method setContentView(View view, ViewGroup.LayoutParams params) instead of setContentView(rootView);
ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
// 300,
// 300);
//for coustom size
params.height = thumb_size;
params.width = thumb_size;
// set padding ,margins ,gravity as your desired
Use this setContentView(rootView, params);
I faced same issue, and i came across a solution. If you give fixed width or wrap_content to your root of your layout, it doesn't consider that. I don't know the reason behind this. Put one more parent layout inside your root and give required width and height to the inner parent, put all views inside this parent. I hope it helps.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
//put all your views inside this linear layout
<TextView......./>
<TextView......./>
<TextView......./>
</LinearLayout>
</LinearLayout>
Try this before showing the dialog:
dialog.getWindow().setLayout(context.getResources().getDimensionPixelSize(R.dimen.dialog_width), WindowManager.LayoutParams.WRAP_CONTENT)
dialog_width is 100dp.
For some reason, removing the theme that I applied to the dialog fixed the wrap_content issue. The only thing the custom theme did was set the background of the dialog, but removing it has solved the issue.
Try changing the android:orientation="vertical" in your main Linear Layout because You are using linear layout with horizontal orientation so all the widgets are aligned horizontally and they are acquiring their assigned space horizontally that's why it cover the whole screen.

How to increase the height of a view in Android Layout dynamically?

How to extend the height of the separated vertical line (view) dynamically in the image?
This is the XML I'm using:
<View android:layout_width="1dp"
android:layout_height="100dp"
android:id="#+id/view_verdict"
android:background="#color/line"
android:foregroundGravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
Maybe this example code will be useful (I suppose that your view is a LinearLayout):
final int newHeight = 100; //this is the new height will be set in your view
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, newHeight);//first param is the width, and second the height
myView.setLayoutParams(params);
Or just put:
...
android:layout_height="match_parent"
...
set
android:layout_height="match_parent"
for your view with id view_verdict

Android: alignment after scaling and size changed

I have a view which defines its width and height accordingly to layout dimensions (via ViewTreeObserver). I have another two ImageViews (wrapped in the RelativeLayouts).
I need this two images be aligned to the top and bottom of the first view. They do, but use old view dimensions, not the corrected ones: so there is a gap between the views, when central gets smaller.
Here is an image of what I mean:
And the code:
#Override
public void onGlobalLayout() {
layoutWidth = layout.getWidth(); \\ main layout
layoutHeight = layout.getHeight();
LayoutParams params = arrows.getLayoutParams();
params.width = layoutHeight/3;
params.height = layoutHeight/3;
arrows.setLayoutParams(params);
LayoutParams paramseyes = eyes.getLayoutParams();
paramseyes.width = layoutHeight/4;
eyes.setLayoutParams(paramseyes);
mouth.setLayoutParams(paramseyes);
/*
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(paramseyes);
lp.addRule(RelativeLayout.ABOVE, R.id.arrows);
eyes.setLayoutParams(lp); ... nah, it didnt work
*/
removeOnGlobalLayoutListener(arrows, this);
}
the XML:
<ee.st.running.arrowwidgt
android1:id="#+id/arrows"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_centerInParent="true"
android:dial="#drawable/hourglass1"
android:hand_hour="#drawable/hours"
android:hand_minute="#drawable/minuts"
android:scaleType="fitXY"
android1:src="#drawable/hourglass1" />
<RelativeLayout
android1:id="#+id/eyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android1:layout_above="#id/arrows" >
<ImageView
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_centerInParent="true"
android1:src="#drawable/eyes" />
</RelativeLayout>
<RelativeLayout
android1:id="#+id/mouthset"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android1:layout_below="#id/arrows"
android:layout_centerHorizontal="true"
>
<ImageView
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_centerHorizontal="true"
android1:src="#drawable/mouth"
/>
</RelativeLayout>
This arrows view is big (for multiple screens support I just scale it to certain size later).
I finally realized why.
When I changed layout width, it correspondingly scaled the image. But it didn't change layout height.
I should make
paramseyes.height = ...
(In my situation it is layoutHeight/8;)
So, the problem is solved.

TextView moved down when new line

I'm currently working on a server-client android application, and in one of my views I have a HorizontalScrollView that contains a vertical LinearLayout. This LinearLayout will contain a bunch of TextViews that are customized with a background. It works fine until the TextViews get a new row because of the character length, it looks like this:
http://puu.sh/9L127/bf29a86639.jpg
The idea is that the text is supposed to fit in the background, is there any way that I can keep the TextView position like the other ones, even though it has multiple rows?
I hope I made myself clear enough, this is how the TextViews are set up in the code:
public void setupCard(GameCard card) {
final int CARD_WIDTH = 200;
final int CARD_HEIGHT = 100;
m_card = card;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(CARD_WIDTH, CARD_HEIGHT);
lp.setMargins(0, 0, 5, 0);
super.setText(m_card.getContent());
super.setBackgroundResource(R.drawable.cardbg);
super.setLayoutParams(lp);
super.setGravity(Gravity.CENTER);
}
And this is how the activity is set up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/bng"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/cardLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Thanks in advance!
Thats because the LinearLayout alignes the text baselines of the TextViews.
Try to specify
android:baselineAligned="false"
for the LinearLayout.

Categories